Geometry viewer with WPF

Instead of using the geometry viewer on a Windows Form as shown in this example, we look in this sample at using the geometry viewer for a WPF user interface. Therefore we create a new project "WPF Application" in Visual Studio. On the Main Window we place two controls, a button and a WindowsFormsHost, which we use to put our geometry viewer control into it. At the references we add CPlan.VisualObjects and CPlan.Geometry from the CPlan library. Furthermore we need to add OpenTK.dll and OpenTK.GL.Control.dll to our references. Either download it from the website or use the more comfortable way via NuGet Now, your project should look like this:

Next we add a few lines of code into the MainWindows.xaml.cs. Our geometry viewer will be placed into the WindowsFormsHost. Therefore we add a private variable myCamera:

	private CPlan.VisualObjects.GLCameraControl myCamera;

After calling InitializeComponent() in the MainWindow method, we initialize myCamera in line 37 – 44. Afterwards we add test geometry to the viewer to see if it works.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using CPlan.VisualObjects;
using CPlan.Geometry;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace SynthesisWPF
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window

        private CPlan.VisualObjects.GLCameraControl myCamera;

        public MainWindow()

            myCamera = new GLCameraControl();
            myCamera.BackColor = System.Drawing.Color.Black;
            myCamera.BackgroundColor = System.Drawing.Color.White; 
            myCamera.RasterColor = System.Drawing.Color.LightGray; 
            myCamera.ShowRaster = true;
            myCamera.Dock = DockStyle.Fill;
            myCamera.Resize += GLCameraControl_Resize;
            Host.Child = myCamera;

            // --- test geometry ---
            GRectangle myRect = new GRectangle(1, 1, 200, 100); 
            myRect.Filled = true;
            myRect.FillColor = ConvertColor.CreateColor4(System.Drawing.Color.LightGreen);
            myCamera.AllObjectsList.Add(myRect, true, true);

            GPrism myPrism = new GPrism(myRect.Poly, 70);
            myCamera.AllObjectsList.Add(myPrism, true, true);


        private void GLCameraControl_Resize(object sender, EventArgs e)
            var curCtrl = (GLCameraControl)sender;
            if (!curCtrl.Loaded) return;
            int w = curCtrl.Width;
            int h = curCtrl.Height;
            GL.Viewport(0, 0, w, h);

        private void Button2D3D_Click(object sender, RoutedEventArgs e)
            myCamera.Dock = DockStyle.Fill;
            myCamera.Size = new System.Drawing.Size((int)Host.Width, (int)Host.Height);
            if (myCamera.ActiveCamera == myCamera.Camera_2D)
                myCamera.ActiveCamera = myCamera.Camera_3D;
                myCamera.ActiveCamera = myCamera.Camera_2D;


The Resize handle needs to be added manually, since it was not possible to include it into the viewer component directly:

	private void GLCameraControl_Resize(object sender, EventArgs e)

The Button is used to switch from 2D to 3D and vice versa. That's it! All view controls and basic object manipulators are available as described in the first sample. The final result look like this:

Leave a Reply

Your email address will not be published. Required fields are marked *