Complete steps to create an SDK example on MicroStation CONNECT Edition with managed Pick List APIs

Introduction

This article will show the complete steps to create an SDK example on Microstation CONNECT Edition, and some basic managed Pick List APIs are also used. By these steps, you can create other SDK examples which are using managed code.

Steps

  1. Install products.
  2. Create a project.
  3. Add references.
  4. Input the output path.
  5. Rename file.
  6. Modify class PickListAddin.
  7. Create Keyins.cs.
  8. Create commands.xml.
  9. Modify PickListExamples.csproj.
  10. Rebuild the project.
  11. Start Microstation.
  12. Run “mdl load PickListExamples”.
  13. Run “PICKLISTDEMO CREATE”.
  14. Close Microstation.
  15. Add Pick List code in method CreatePickListLibraryAndPickLists.
  16. Rebuild the project and start Microstation again.
  17. Run “mdl load PickListExamples” and “PICKLISTDEMO CREATE” in key-in window.
  18. Check the new created Pick Lists.

1. Install MicroStation CONNECT and MicroStation CONNECT SDK.

2. Start VS2015, create new project, select Visual C# -> Class Library, choose a location and input the project name which is PickListExamples in this guide, also make sure the version of .NET Framework is 4.6.1. Click OK button to finish the creating.

3. Add basic references Bentley.General.1.0.dll, Bentley.Microstation.Interfaces.1.0.dll, and ustation.dll.

4. Right click on project PickListExamples in Solution Explorer, go to Build page and input the output path. In my computer the output path is “D:\Program Files\Bentley\MicroStation-10.11.00.025\MicroStation\Mdlapps”. If you want to use another path, you need to add the path into configuration variable "MS_ADDINPATH".

5. Right-click Class1.cs in Solution Explorer and then select Rename from the pop-up menu. Change the Class1.cs to PickListAddin.cs. When you face the below prompt, please click the Yes button to continue. This step is optional, and you can rename it as you like.

6. Modify source code of PickListAddin.cs as below:

using System.Windows;

namespace PickListExamples
    {
    public class PickListAddin : Bentley.MstnPlatformNET.AddIn
        {
        private static PickListAddin s_instance;
        internal static PickListAddin Instance
            {
            get
                {
                return s_instance;
                }
            }
        private PickListAddin (System.IntPtr mdlDesc)  : base(mdlDesc)
            {
            }
        protected override int Run (string[] commandLine)
            {
            s_instance = this;
            return 0;
            }
        public void CreatePickListLibraryAndPickLists (string unparsed)
            {
            MessageBox.Show ("Creating PickListLibrary and PickLists");
            }
        }
    }

Now this project can be built correctly. Next steps will show how to create keyins.

7. Create a C# class named Keyins.cs. And modify it source code to below:

namespace PickListExamples
    {
    public class Keyins
        {
        public static void CreatePickListLibraryAndPickLists (string unparsed)
            {
            PickListAddin.Instance.CreatePickListLibraryAndPickLists (unparsed);
            }
        }
}

Note: the class and the keyin methods are public, otherwise Microstation will not call the methods.

8.Create a xml file named commands.xml.

Replace the contents in commands.xml with below 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 SubtableRef="Commands" CommandClass="MacroCommand" CommandWord="PICKLISTDEMO" >
            <Options Required ="true" TryParse="true"/>
        </Keyword>
    </RootKeyinTable>

    <SubKeyinTables>
        <KeyinTable ID="Commands">
            <Keyword CommandWord="CREATE" />
        </KeyinTable>
    </SubKeyinTables>

    <KeyinHandlers>
        <KeyinHandler Keyin="PICKLISTDEMO CREATE" Function="PickListExamples.Keyins.CreatePickListLibraryAndPickLists"/>
    </KeyinHandlers>
</KeyinTree>

Right click on the commands.xml in solution explorer and select “Properties”, then from Properties pane change the Build Action to Embedded Resource.

9. Open file PickListExamples.csproj by Notepat++ or other editors, and find these lines:

  <ItemGroup>
    <Content Include="commands.xml" />
  </ItemGroup>

Replace them with below code:

  <ItemGroup>
    <EmbeddedResource Include="commands.xml">
      <LogicalName>CommandTable.xml</LogicalName>
      <SubType>Designer</SubType>
    </EmbeddedResource>
  </ItemGroup>

Return to Visual Studio, there is a message box:

Click on Reload button to load the updated project.

Note: This step is very important, otherwise Microstation won’t be able to find the keyins.

10. Rebuild the project, it should be built correctly.

11. Start Microstation CONNECT edition and create a new file or open an existing file.

12. Run “mdl load PickListExamples” in key-in window. PickListExamples.dll should be loaded.

13. Run “PICKLISTDEMO CREATE” in key-in window. A message box will be popped.

14. Close Microstation.

15. Add “using Bentley.DgnPlatformNET;” in PickListAddin.cs, add Bentley.DgnPlatformNET to references, and modify method CreatePickListLibraryAndPickLists as below:

        public void CreatePickListLibraryAndPickLists (string unparsed)
            {
            DgnFile dgnFile = BM.Session.Instance.GetActiveDgnFile ();
            System.Diagnostics.Debug.Assert(null != dgnFile);

            PickListLibrary pickListLib = PickListLibrary.Create ();
            System.Diagnostics.Debug.Assert (null != pickListLib);

            PickList pickList1 = pickListLib.AddPickList ("Manufacturer", dgnFile);
            pickList1.AddValue ("Ford");
            pickList1.AddValue ("BMW");
            pickList1.AddValue ("Benz");
            System.Diagnostics.Debug.Assert (pickList1.GetValueCount () == 3);

            PickList pickList2 = pickListLib.AddPickList ("PowerType", dgnFile);
            pickList2.AddValue ("Gasoline");
            pickList2.AddValue ("Electric");
            System.Diagnostics.Debug.Assert (pickList2.GetValueCount () == 2);

            System.Diagnostics.Debug.Assert (pickListLib.GetPickListCount () == 2);

            PickList pickListGotByName = pickListLib.GetPickListByName ("Manufacturer");
            System.Diagnostics.Debug.Assert (null != pickListGotByName);

            System.Diagnostics.Debug.Assert (pickListGotByName.CanFindValueByString ("Ford"));

            int count = 0;
            foreach (PickListValue pickListValue in pickList2)
                {
                if (0 == count)
                    {
                    System.Diagnostics.Debug.Assert (pickListValue.Id == 1);
                    System.Diagnostics.Debug.Assert (pickListValue.Value.CompareTo ("Gasoline") == 0);
                    }
                else if (1 == count)
                    {
                    System.Diagnostics.Debug.Assert (pickListValue.Id == 2);
                    System.Diagnostics.Debug.Assert (pickListValue.Value.CompareTo ("Electric") == 0);
                    }
                else
                    System.Diagnostics.Debug.Assert (false);
                count++;
                }
            
            PickListLibrary.SavePickListLibToDgn(dgnFile, pickListLib, false);
            }

16. Rebuild the project and start Microstation again.

17. Run “mdl load PickListExamples” and “PICKLISTDEMO CREATE” in key-in window. 

18. Open Pick List Manager and check, we will find the new created Pick Lists:

  • I found that this worked:

    using BM = Bentley.MstnPlatformNET;

  • It's good to see a Picklist example! It's even better to see so clearly the steps required to build that example.

    Comments

    15. Add “using Bentley.DgnPlatformNET;”

    DgnFile dgnFile = BM.Session.Instance.GetActiveDgnFile ();
    

    You've introduced namespace BM without defining it.  Either define the namespace or substitute Bentley.MstnPlatformNET.

    The PickListAddIn class definition is missing an attribute. Shouldn't it look like this?

    [Bentley.MstnPlatformNET.AddInAttribute(MdlTaskID = "PickListExamples")] 
    public sealed class PickListAddin : Bentley.MstnPlatformNET.AddIn {}