<< Click to Display Table of Contents >> Hello World |
|
Every programming effort starts with the ubiquitous "Hello World" example. This example is meant to provide just enough information to show the framework and produce an output.
Please note that in C# comments are preceded by //.
//////////////////////////////////////////////////////////////////////
//WireCAD Plugin
//Contents:
// Basic WireCAD Plugin Framework
//
//Instructions:
// 1.This project assumes that you installed WireCAD in the default
// location. If you did not, you will need to change the reference
// path and the build path to that of your install path
// ..\WireCADx\bin folder. You can do that byclicking:
// Application Menu > Project Options [Reference Paths] Reference path
// Application Menu > Project Options [Compiling Tab] Output Path
// 3.Add your code and build
// 4.Create a wpi file (from within WireCAD click Plugins>Plugin Manager [New]) that points to your assembly and method and
// place it in the ..WireCADx\bin\plugins folder
//
//////////////////////////////////////////////////////////////////////
//Explanation:
//This heloworld example demonstrates a number of different WireCAD SDK
//concepts. This is WireCAD Automatically Discovered (AD) plugin and
//therefore does not require a .wpi file in the /plugins folder. As such,
//it will load silently and can only be executed from the commandline since
//it does not add any other GUI elements.
//
//TESTING:
//First familiarize yourself with the code below and try to understand
//what will happen before testing.
//1. Build the project and ensure that the helloworld.plugin.dll is located
// in the ..WireCADx\bin folder.
//2. if WireCAD is running click Plugins>Plugin Manager[Rescan and Load Plugins]
//3. Type hw into the WireCAD commandline.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VectorDraw.Professional.vdFigures;
using VectorDraw.Professional.vdObjects;
using WireCAD;
using WireCAD.Interfaces;
using WireCAD.Translation;
namespace hello_world
{
public class MyPlugin: IPluginCore
{
#region Fields
//Place your field level variables here
CommandInfo ci = null;
#endregion
#region Properties
//Place your Properties Here
#endregion
#region IPluginCore Members
/// <summary>
/// Called before plugin is loaded to make sure that this plugin
/// has permission to run at this application mode level and
/// for this person(Identity)
/// </summary>
/// <param name="ws">The Singleton Workspace object</param>
/// <param name="id">Current user identity</param>
/// <returns>should return true if the plug can load</returns>
public bool HasPermissionToRun(IWorkspace ws, Identity id)
{
return true;
}
/// <summary>
/// This is called when the plugin is loaded at application start
/// </summary>
/// <param name="ws">Singleton WireCAD Workspace object</param>
public void Load(IWorkspace ws)
{
//We pass an IWorkspace object exposing most if the WireCAD
//object model
//on load we will register a commandInfo object with our
//commands so that our static method can be invoked from
//the command line in WireCAD
ci = new CommandInfo();
//The name of our dll
ci.Assembly = "helloworld.plugin.dll";
//The NameSpace and Class of our function
ci.NameSpaceAndClass = "hello_world.MyPlugin";
//Our function's name
ci.MethodName = "HelloWorld";
//The long descriptive name of our function
ci.CommandLongName = "Hello World Demo";
//An alternative name for our function. You can type this text into the WireCAD command line
//to call the function
ci.CommandAlt = "hello";
//The shortcut
ci.ShortCut = "hw";
//this registers the command so that the commandline knows how to parse the information
//and call our function
ws.Commands.RegisterCommand(ci);
}
/// <summary>
/// Unload code for your plugin
/// </summary>
/// <param name="ws"></param>
public void Unload(IWorkspace ws)
{
//here we place any code to unload our plugin.
//Unregistering the commandInfo prohibits the command from
//being persisted. This is more a development function.
//once you are ready to distribute your plugin you will
//probably want your user to be able to save his own shortcuts
//and therefore not unregister the command.
ws.Commands.UnRegisterCommand(ci);
}
#endregion
#region Static Methods
/// <summary>
/// Static method that can be called from the WireCAD command line
/// It's a good idea to rename this to something meaningful
/// </summary>
/// <param name="ws">WireCAD is expecting to find this parameter</param>
public static void HelloWorld(Workspace ws)
{
//This hello world function demostrates a number of different
//areas of the WireCAD SDK
//this is a winForms messagebox
MessageBox.Show("Hello World");
//now we'll show an instance of form1(defined elsewhere in this project);
Form1 f = new Form1();
f.ShowDialog();
//now let's send a message to the Command Line History
ws.MainForm.CommandLine.AppendHistory("Hello World");
//let's check to make sure that we have an open drawing
if(!Commands.IsActiveDrawing(ws,true))
{
//no active drawing so return
return;
}
//Get some user input on the next step
if(DialogResult.No == MessageBox.Show(
"Would you like us to add some text to the active drawing?",
"WireCAD SDK",MessageBoxButtons.YesNo)) return;
//First we create a vdText object
vdText text = new vdText();
//register it with the active document
text.SetUnRegisterDocument(ws.ActiveDrawing);
//give it the document defaults
text.setDocumentDefaults();
//Set the string
text.TextString = "hello world";
//locate it in the coordinate space
text.InsertionPoint = new VectorDraw.Geometry.gPoint(0,0,0);
//Set the textHeight
text.Height = .25;
//Set the color
vdColor colorRed = new vdColor();
colorRed.SetUnRegisterDocument(ws.ActiveDrawing);
colorRed.Palette = ws.ActiveDrawing.Palette;
colorRed.FromRGB(255,0,0);
text.PenColor = colorRed;
//alternately you could do this
//text.PenColor.ColorIndex = 1;
//our text will be added to the drawing on the ActiveLayer and with
//the ActiveTextStyle if you want to change those you can by setting
//those properties on the text object
ws.ActiveDrawing.ActiveLayOut.Entities.AddItem(text);
//now refresh the drawing
ws.ActiveDrawing.Redraw(true);
//alternately you can just invalidate the text object(less expensive)
//like this:
//text.Invalidate();
}
#endregion
}
}