Wednesday, 1 January 2014

Cust 09: Document and Environment Type: VB.Net

In this tutorial you learn:

  • How to invoke or determine the type of  Solid Edge document using VB.Net.
  • What is the difference between document type and environment type and how to determine it.

    Note:

Start Visual Studio and add a Windows Forms Application project under Visual Basic.

image

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, click the Show All Files button and right click References folder in the tree beneath and select Add Reference... from the pop-up menu.

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


Solid Edge gets added under References.
image
Add several buttons and check boxes to the form as shown.

View the code for the form and at the top
 add the following:

Imports System.Runtime.InteropServices

Imports SolidEdgeConstants

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

Public Class Form1
    Dim
oApp As SolidEdgeFramework.Application


image_thumb

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

oApp = 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

Imports System.Runtime.InteropServices

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:

image

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

image

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 Then
                Select Case 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 Select Case construct to check the document type using the ActiveDocumentType property of the Solid Edge Application object.

  The ActiveDocumentType is a SolidEdgeFramework.DocumentTypeConstants enum

  Hover the mouse over the word ActiveDocumentType and note what Intellisense shows:

image

Complete the Select Case statement as below:

If oApp.Documents.Count > 0 Then
  Select Case oApp.ActiveDocumentType
    Case DocumentTypeConstants.igPartDocument
    Label1.Text = "Part Document"

    Case DocumentTypeConstants.igDraftDocument
    Label1.Text = "Draft Document"

    Case SolidEdgeFramework.DocumentTypeConstants.igAssemblyDocument
    Label1.Text = "Assembly Document"

    Case DocumentTypeConstants.igSheetMetalDocument
    Label1.Text = "Sheetmetal Document"
 

    Case SolidEdgeFramework.DocumentTypeConstants.igWeldmentDocument
    Label1.Text = "Weldment Document"

    Case DocumentTypeConstants.igWeldmentAssemblyDocument
    Label1.Text = "Weldment Assembly Document"

    Case DocumentTypeConstants.igUnknownDocument
    Label1.Text = "Unknown Document Type"

End Select 

  Label2.Text = oApp.ActiveEnvironment.ToString()
Else
  Label1.Text = "No Document Open"
End If 

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.

image_thumb5

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:

image_thumb6

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.

image_thumb7

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

image_thumb9

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

image_thumb11

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

image_thumb10

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.

cMayoCAD24[2]

 cmayocad14