Monday 30 December 2013

Cust 08: Document and Environment Type: Managed C++

In this tutorial you learn:

  • How to invoke or determine the type of  Solid Edge document using Managed C++ i.e. C++/CLI
  • What is the difference between document type and environment type and how to determine it.


Start Visual Studio and add a Windows Forms Application project under Visual C++/CLR.


Visual Studio 2010 with .Net framework 4 should work well with Solid Edge 20.

Specify a suitable name for the project and a location and click OK.

In the Solution Explorer, right click on the project name and select References or from the menu, select Project > References…

In the Property Pages dialog, click select
Add New Reference... button.

In the dialog that appears take the COM tab.
Select Solid Edge Framework Type Library from the list.

Back to the Property Pages dialog, Solid Edge Framework gets added under References.
In the Solution Explorer, expand the folder Header Files and select Form.h then click the View Designer button as shown in image besides.

Add several buttons and check boxes to the form as shown.

View the code for the form and at the end of the existing
using statements, add the following:

using namespace System;
namespace System::Collections.Generic;



using namespace SolidEdgeFramework;
using namespace System::Runtime::InteropServices;

Inside the Form1 class, add a variable oApp for Solid Edge.

public ref class Form1 : public System::Windows::Forms::Form
        public: Form1(void)

        SolidEdgeFramework::Application^ oApp;

Note: Intellisense may not be available for C++/CLI in VS2010.



In the form’s load event add the following code:

oApp = (SolidEdgeFramework::Application^)Marshal::GetActiveObject("SolidEdge.Application");

The GetActiveObject method of the Marshal class helps to connect to a running instance of Solid Edge, which in turn is made available by virtue of the

using namespace System::Collections::Generic; statement added earlier.

Here, SolidEdge.Application is the Program ID for Solid Edge which does not change from version to version.

To check this, start the Registry Editor by pressing Windows+R button to invoke the Run utility. Type regedit and press ENTER.

In the registry editor application which looks like the Windows Explorer, select Edit > Find or press F3 and type SolidEdge.Application. After a while it searches for SolidEdge.Application and displays the search as below:


If you click the LocalServer32 just above ProgID folder in the left panel, it displays the installed path for Solid Edge.


Build the project and check if everything is fine.

Back to Visual Studio, double click the Check Document Type button and add the following line of code for the button:

if(oApp->Documents->Count > 0)
    switch (oApp->ActiveDocumentType)

To determine the type of the currently active document, first check if there is at least one document open using the if statement as above.

  Set up a switch construct to check the document type using the ActiveDocumentType property of the Solid Edge Application object.

  The ActiveDocumentType is a SolidEdgeFramework.DocumentTypeConstants enum

Complete the switch statement as below:

if(oApp->Documents->Count > 0)
  switch (oApp->ActiveDocumentType)
    case SolidEdgeFramework::DocumentTypeConstants::igPartDocument:
  Label1->Text = "Part Document";

    case SolidEdgeFramework::DocumentTypeConstants::igDraftDocument:
  Label1->Text = "Draft Document";

    case SolidEdgeFramework::DocumentTypeConstants::igAssemblyDocument:
  Label1->Text = "Assembly Document";

    case SolidEdgeFramework::DocumentTypeConstants::igSheetMetalDocument:
  Label1->Text = "Sheetmetal Document";

    case SolidEdgeFramework::DocumentTypeConstants::igWeldmentDocument:
  Label1->Text = "Weldment Document";

    case SolidEdgeFramework::DocumentTypeConstants::igWeldmentAssemblyDocument:
  Label1->Text = "Weldment Assembly Document";

    case SolidEdgeFramework::DocumentTypeConstants::igUnknownDocument:
  Label1->Text = "Unknown Document Type";
  }// switch

  Label2->Text = oApp.ActiveEnvironment.ToString();
} // if
Label1->Text = "No Document Open";

Each time check the document type property against the document type constants and accordingly display the document type in the first label.

The second label displays the ActiveEnvironment property directly by converting to string.

The subtle difference between document type and environment type can be understood by running the program.

First start Solid Edge and close any open documents. Run the program and click the Check Document Type button. The program readily connects to Solid Edge as soon as the form loads.

The first label displays No Document Open from the else part of the if statement.

The second label is blank.


Next, without closing the program, open a Part document and click the button again.

Now the document type is displayed as Part Document and the environment type is displayed, as Part:


Keep the program running and in Solid Edge, start sketching on a plane and switch back to the program to click the button. Now the second label displays the environment type as LayoutInPart.


Open or create a Drawing document in Solid Edge with the program still running in the background. Click the button:


Place a view in the drawing and right click on the view. Select Draw in View from the context menu.


Click the button in the program and note the change in the environment type.


Similarly check the labels in an assembly document.

 clip_image002 Post a comment below if you need the Visual studio code files and be aware of cMayoCAD where you create your own, brand new, fully functional CAD system with scripting capabilities using a geometric modeling kernel.

Download the detailed course contents for cMayoCAD here.



No comments:

Post a Comment