Category Archives: Samples

Samples for the usage of the CPlan library.

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()
        {
            InitializeComponent();

            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);

            myCamera.ZoomAll(); 
            myCamera.Invalidate();
        }

        private void GLCameraControl_Resize(object sender, EventArgs e)
        {
            var curCtrl = (GLCameraControl)sender;
            if (!curCtrl.Loaded) return;
            int w = curCtrl.Width;
            int h = curCtrl.Height;
            curCtrl.MakeCurrent();
            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.ZoomAll();
                myCamera.Invalidate();
            }
            else
            {
                myCamera.ActiveCamera = myCamera.Camera_2D;
                myCamera.ZoomAll();
                myCamera.Invalidate();
            }
        }

    }
}
	

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:

Use of the 3D view and 3D objects

Based on the sample „Start with a geometry viewer“, we add 3D objects to the viewer and change between 2D and 3D view. To add the additional functions, we place two new buttons on our form and name them “prism” and “view 2D/3D”:

Double click a button to assign a click event to it. For the prism buttons click event we add code to create a prism based on an ellipse with random properties:

 

        private void button2_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();
            // -- The ellipse is the basic shape to be extruded as prism. Try e.g. to exchange the ellipse with the rectangle below.
            GEllipse myEllipse = new GEllipse(new Vector2d(rnd.Next(100), rnd.Next(100)), rnd.Next(50), rnd.Next(50), 0.1f);
            //GRectangle myRect = new GRectangle(rnd.Next(100), rnd.Next(100), rnd.Next(50, 200), rnd.Next(50, 200));
            // -- From the 2D geometry objects one can create a prism by using the Poly property.
            GPrism myPrism = new GPrism(myEllipse.Poly, rnd.Next(100));
            myPrism.Transparency = 1;
            myPrism.BorderWidth = 2;
            myPrism.FillColor = ConvertColor.CreateColor4(Color.White);
            myPrism.Filled = true;
            myPrism.BorderColor = ConvertColor.CreateColor4(Color.Orange);
            glCameraControl1.AllObjectsList.Add(myPrism, true, true);
            //glCameraControl1.ZoomAll();
            glCameraControl1.Invalidate();
        }

 

Try to exchange the ellipse with a rectangle (use the commented code).

To see our prism in 3D we have to change the camera used so far for our view. Therefore we add a click event to the “view 2D/3D” button that switch from 2D to 3D view and back:

 

        private void button3_Click(object sender, EventArgs e)
        {
            // -- Change the used camera --
            if (glCameraControl1.Camera == glCameraControl1.Camera_2D)
            {
                glCameraControl1.Camera = glCameraControl1.Camera_3D;
                glCameraControl1.Invalidate();
            }
            else
            {
                glCameraControl1.Camera = glCameraControl1.Camera_2D;
                glCameraControl1.ZoomAll();
                glCameraControl1.Invalidate();
            }
        }

 

To navigate in the 3D view you use the same keys as for the 2D view. Only the free rotation (right mouse+shift) is different in 3D. Finally your scene may look like the one below. You can also drag&drop objects in the 3D view. To manipulate a prism you need to pick its footprint.

At the moment we only have the prism object in 3D, which serves to extrude the 2D shapes. If you want to create your own geometry objects, be sure to inherit from surfaces for 2D and from volumes for 3D objects.

MyGeometryViewer MyGeometryViewer2 (This sample contains an updated VisualObjects.dll)

Start with a geometry viewer

The first steps into creating your own PlanningSynthesis tool is to create a geometry viewer. Please fist download the CPlan library or the code from the repository. In the CPlan library you find a custom OpenTK control, which we extended to provide you the basic view control functions as well as object picking and some other helpful things.

In the following we use VisualStudio 2012. We start the tutorial by creating a new WindowsFormsApplication:

To add a new component right-click on the ToolBar window and click ChooseItems…:

choose-item-in-tool-bar.jpg

Click on browse and select CPlan.VisualObjects.dll. You can find it in the downloaded releas folder or you use a build of the source code from the repository. In the next dialog slect GLCameraControl to be added to the Toolbox Items:

Click ok.

Now you find the new GLCameraControl in the Toolbox:

Drag & drop the GLCameraControl on your Windows Form. Select it and set Dock = Full in the properties menue.
The component brings some references with it, which should have been added automatically to your project References:


Now you can run your program and you should see your new geometry viewer! The properies of the viewer, like BackgroundColor or Raster Color, can be changed in the Properties dialog of the component.

Use the right mouse button for pan, mouse wheel for zoom (or right mouse+ctrl for zoom), right mouse+shift and right mouse+alt for rotation. There are different rotate modes for the 3D mode that we investigate later.

Unfortunatley it was not possible to add the Resize event directly to the component. Therefore you have to add the Resize event to your GLCameraControl component on the form and add the following code:

using System.Windows.Forms;
using CPlan.VisualObjects;
using OpenTK.Graphics.OpenGL;

namespace MyGeometryViewer
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

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

 

Finally you can add a button and assign the code for creating a geometry object:

private void button1_Click(object sender, EventArgs e)
{
	GRectangle myRect = new GRectangle(1, 1, 200, 100);
	myRect.Filled = true;
	myRect.FillColor = ConvertColor.CreateColor4(Color.LightGray);
	glCameraControl1.AllObjectsList.Add(myRect, true, true);
	glCameraControl1.ZoomAll();
	glCameraControl1.Invalidate();
}

With these few lines of code you add a rectangle to the viewer, which is drawn automatically. Furthermore you have already mouse interaction. Try to drag&drop the rectagle – it works! By the two boolean parameters in the method

 glCameraControl1.AllObjectsList.Add(myRect, true, true); 

you can define with the first boolean, if the object is drawn, and with the second boolean, if mouse interaction is enabled. From here you may explore by your own the capabilities of the component and the VisualObjects that you can use (look into the Object Browser, CPlan.VisualObjects).

Download the sample MyGeometryViewer