C# .NET Template with IPrimitiveCommandEvents Class


Thought this might benefit someone learning C# within Microstation.

The code below is the contents of a "bare bones" CS file that can be used as a template when creating a program using the IPrimitiveCommandEvents object. Thatis, start with this code if you want the user to select an element for manipulation or data point for element creation. (Note: ILocateCommandEvents is more suitable for element selection, and the code layout to implement it is similar to IPrimitiveCommandEvents.)

Typical instructions for use:

 

******************************************************************************************************************

CS (C# .NET) CODE:

******************************************************************************************************************

/*--------------------------------------------------------------------------------------+
* Microstation C# dotNET Template
*
* Taken from SDK Examples and modified.
+--------------------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using BMW=Bentley.MicroStation.WinForms;
using BMI=Bentley.MicroStation.InteropServices;
using BCOM=Bentley.Interop.MicroStationDGN;

namespace MS_dotNET_Template
{

/// <summary>When loading an AddIn MicroStation looks for a class
/// derived from AddIn.</summary>
[Bentley.MicroStation.AddInAttribute (KeyinTree="MS_dotNET_Template.Commands.xml", MdlTaskID="MS_dotNET_Template")]
public sealed class MS_dotNET_Template : Bentley.MicroStation.AddIn
{
static MS_dotNET_Template s_App = null;

/// <summary>Private constructor required for all AddIn classes derived from
/// Bentley.MicroStation.AddIn.</summary>
private MS_dotNET_Template ( System.IntPtr mdlDesc ) : base(mdlDesc)
{
s_App = this;
}

internal static MS_dotNET_Template Instance
{
get { return s_App; }
}

protected override int Run ( System.String[] commandLine )
{
return 0;
}
} // End of MS_dotNET_Template class


/// <summary>Class used for running key-ins. The key-ins
/// XML file provides the name of the class and the methods.
/// </summary>
public class KeyinCommands
{
/*********************************************************************************
* Template_Key-In
*
* Command: dotNETTemplate
*
* Description: Template Key-In. Runs the IPrimitiveCommandEvents class
*
*********************************************************************************/

public static void Template_KeyIn(System.String uparsed)
{
BMI.Utilities.ComApp.CommandState.StartPrimitive(new Template_Primitive_Event_Class());
}
} // End of KeyinCommands


/// <summary>
/// Template IPrimitiveCommandEvents implemented class.
///
/// Additional code is added into this class as an example. It
/// follows the "Line Element Creation Command" Example provided
/// in the Microstation VBA Helpfile.
/// </summary>
public class Template_Primitive_Event_Class : BCOM.IPrimitiveCommandEvents
{
// Application variable
private BCOM.Application app = BMI.Utilities.ComApp;

// Variables used in Example Code
private BCOM.Point3d[] m_atPoints = new BCOM.Point3d[2];
private int m_nPoints;

/// <summary>
/// Required Methods
/// </summary>
public void Cleanup() { }
public void DataPoint(ref BCOM.Point3d pt, BCOM.View view)
{
// Example Code
if (m_nPoints == 0)
{
app.CommandState.StartDynamics();
m_atPoints[0] = pt;
m_nPoints = 1;
app.ShowPrompt("Place end point: ");
}
else if (m_nPoints == 1)
{
m_atPoints[1] = pt;
BCOM.LineElement oEl = app.CreateLineElement1(null, m_atPoints);
app.ActiveModelReference.AddElement(oEl);
oEl.Redraw();
m_atPoints[0] = m_atPoints[1];
}
}
public void Dynamics(ref BCOM.Point3d pt, BCOM.View view, BCOM.MsdDrawingMode DrawMode)
{
// Example Code
if (m_nPoints == 1)
{
m_atPoints[1] = pt;
BCOM.LineElement oEl = app.CreateLineElement1(null, m_atPoints);
oEl.Redraw(DrawMode);
}
}
public void Keyin(string Keyin) { }
public void Reset() { app.CommandState.StartDefaultCommand(); }
public void Start()
{
app.ShowCommand("MS_dotNET_Template: ");
app.ShowPrompt("Select start of line: ");
}
}
} // End of the namespace



******************************************************************************************************************

XML CODE:

******************************************************************************************************************

<?xml version="1.0" encoding="utf-8" ?>
<KeyinTree xmlns="http://www.bentley.com/schemas/1.0/MicroStation/AddIn/KeyinTree.xsd">
<RootKeyinTable ID="root">
<Keyword CommandClass="MacroCommand" CommandWord="dotNETTemplate"></Keyword>
</RootKeyinTable>

<SubKeyinTables>
</SubKeyinTables>

<KeyinHandlers>
<KeyinHandler Keyin="dotNETTemplate"
Function="MS_dotNET_Template.KeyinCommands.Template_KeyIn"/>
</KeyinHandlers>
</KeyinTree>