Drawing an Ellipse

This example covers creating and inserting a drawing object into the current drawing.

Key Concepts

CamBamUI is a fundamental class than provides access to most areas of the CamBam user interface, including the Active drawing view and file, as well as menus.

The main CamBamUI object can be accessed using the static class reference '_ui', if this was used to store the CamBamUI object passed to InitPlugin.
Alternatively, you can use CamBamUI.MainUI to access the main user interface object.

The main drawing view is accessed by CamBamUI.MainUI.ActiveView.

The main drawing file is a property of the drawing view and is accessed by CamBamUI.MainUI.ActiveView.CADFile.

Sample C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

using CamBam;
using CamBam.UI;
using CamBam.Util;
using CamBam.CAD;

namespace CBPluginDemo
{
    public class MyPlugin
    {
        protected static CamBamUI _ui;

        // This is the main entry point into the plugin.
        public static void InitPlugin(CamBamUI ui)
        {
            // Store a reference to the CamBamUI object passed to InitPlugin
            _ui = ui;

            // Create a new menu item in the top Plugins menu
            ToolStripMenuItem mi = new ToolStripMenuItem();
            mi.Text = "Draw Ellipse";
            mi.Click += new EventHandler(DrawEllipse_Click);
            ui.Menus.mnuPlugins.DropDownItems.Add(mi);
        }

        static void DrawEllipse_Click(object sender, EventArgs e)
        {
            // Set the width of the elipse
	        double width = 35;

	        // Set the height of the elipse
	        double height = 30;

	        // Set steps to the number of line segments to use to approcximate the elipse
	        // larger number will be smoother but slower.
	        double steps = 100;
	
	        double s, t, x, y;
	        
            // Create a polyline object
	        Polyline poly = new Polyline();
	
	        for (s=0; s < steps; s++)
            {
		        t = 2 * Math.PI * s / steps;
		        x = width * Math.Cos(t);
		        y = height * Math.Sin(t);

                // Add a point to the polyine
		        poly.Add(x,y,0);
            }

            // Close the polyline
	        poly.Closed = true;

            // Then add it to the active drawing.
            _ui.ActiveView.CADFile.Add(poly);

            // Refresh the view to show the ellipse immediately
            _ui.ActiveView.RefreshView();
        }
    }
}