Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
  • Welcome
  • Products
  • Support
  • About
  • More
  • Cancel
MicroStation
  • Product Communities
  • More
MicroStation
MicroStation Wiki C# .NET Template with IPrimitiveCommandEvents Class
    • Sign in

    • +MicroStation Wiki
    • +Administration Wiki
    • +Annotations Wiki
    • +Bentley View Wiki
    • +MicroStation PowerDraft
    • -Programming Wiki
      • A class to help create and modify text element
      • A Complete Example
      • A MicroStation VBA Example With Bentley ProjectWise
      • AddIn Development Using VB.NET
      • C# .NET Template with IPrimitiveCommandEvents Class
      • Capturing Graphics in Dynamic Views
      • Compiling MDL Applications
      • Database Manipulation
      • Debugging Native Code MDL Applications
      • Developing Code in VBA
      • Developing MicroStation Applications For DWG Files
      • Drag and Drop in MicroStation
      • Error: "Cannot save changes to VBA project 'Support' because it is read-only"
      • Getting And Setting Elements Using Graphic Groups In VBA [CS]
      • Getting Started with Visual Basic
      • How To Write A Recursive Routine In MicroStation VBA [CS]
      • Introducing Segment3D Methods In MicroStation V8 2004 Edition
      • Known issues in MDL and MicroStationAPI
      • Launching VBA Applications As Initapps Or Dgnapps [CS]
      • Learning MicroStation Addins Step by Step
      • MDL - Getting Started With XAttributes In MicroStation V8 XM Edition
      • MDL - Native Code Application Development
      • MDL Or MicroStation BASIC Choosing The Right Tool [TN]
      • MFC Dialog And Native Window Support
      • Microsoft Office VBA Patch Utility
      • MicroStation BASIC FAQ
      • MicroStation BASIC Limits [FAQ]
      • MicroStation Developer Documentation and Example Code
      • MicroStation Programming Advice
      • MicroStation SDK
      • MicroStation V8 Programming Tools Readme
      • MicroStation V8 VBA Programming Resources [CS]
      • MicroStation V8 XM Edition View Code Updates
      • MicroStation VBA Resources Revisited [CS]
      • Migrating Dimension Code To MicroStation V8
      • Migrating MDL Applications To Native Code
      • Mouse Wheel Events And The Visual Basic 6.0 IDE
      • Porting MDL Applications To MicroStation V8
      • Reading Elements From A Microsoft Access Database With VBA [CS]
      • Running MDL Applications
      • Scanning For MicroStation V8 Elements In VBA [CS]
      • Unleash A Workspace Wizard Automating Workspace Creation With MicroStation V8 And VBA [CS]
      • Using VBA To Detect The Current Or Last Used Command
      • Using VBA To Programmatically Export A VBA Project [CS]
      • Using VBA To Programmatically Import A VBA Projects Components And References [CS]
      • VBA -- Creating A Complex Application
      • VBA Interface Error: failed creating the comp manager - 0x80040583
      • VBA interface error: unable to get IDE
      • vba recording
      • Working With Levels In MicroStation VBA [CS]
      • Writing An MDL Application To Output Custom Placemarks To Google Earth
      • [V8i C++] PointCloud IPointCloudChannelDisplayHandler
    • +Visualization Wiki

     
     Questions about this article, topic, or product? Click here. 

    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:

    • There are two bits of code. The first section, add to an empty CS file. The second, add to an empty XML file named Commands.xml (further information about this in later bullet point).
    • Using a program like Microsoft Visual C# 2010 Express, add the CS file to a new solution.
    • Add the following reference files to your new solution (please check these with the dotNET examples in the SDK): Bentley.General.1.0.dll, Bentley.interop.microstationgen, Bentley.microstation.dll, bentley.microstation.interfaces.1.0.dll, Bentley.Windowing.dll, Bentley.General.1.0.dll, ustation.dll (all these are located in your Microstation folder)
    • To have Commands.XML embedded directly inside of the dll (IE so you don't have to copy the XML file with the dll since it will be embedded inside), in your IDE (Example: Visual C# Express) program change the XML's "Build Action" property to "Embedded Resource".
      Note: It's name SHOULD BE in following formats:
      In project: Commands.XML
      In Addin's attributes: KeyinTree="<project's default namespace>.Commands.XML"
    • Then it's a matter of making desired changes and building the solution.

     

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

    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>


     

    • C# CSharp .NET dotNET IPrimitive IPrimitiveCommandEvents Template
    • Share
    • History
    • More
    • Cancel
    • Dan Koval Created by Bentley Colleague Dan Koval
    • When: Thu, Sep 5 2013 4:11 PM
    • Revisions: 1
    • Comments: 0
    Recommended
    Related
    Communities
    • Home
    • Getting Started
    • Community Central
    • Products
    • Support
    • Secure File Upload
    • Feedback
    Support and Services
    • Home
    • Product Support
    • Downloads
    • Subscription Services Portal
    Training and Learning
    • Home
    • About Bentley Institute
    • My Learning History
    • Reference Books
    Social Media
    •    LinkedIn
    •    Facebook
    •    Twitter
    •    YouTube
    •    RSS Feed
    •    Email

    © 2021 Bentley Systems, Incorporated  |  Contact Us  |  Privacy |  Terms of Use  |  Cookies