Saturday 28 December 2013

Cust 07: Document and Environment Type: CSharp

In this tutorial you learn:

  • How to invoke or determine the type of  Solid Edge document using CSharp.
  • 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.

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, right click References and select Add Reference...

In the dialog that appears take the COM tab.

Select Solid Edge Framework Type Library from the list.
         image

Solid Edge gets added under References.
image
Add a button and two labels to the form as shown.

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

using System;
using System.Collections.Generic;
.

.

using SolidEdgeFramework;
using System.Runtime.InteropServices;

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

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SolidEdgeFramework.Application oApp;

image

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 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:

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)
  {
    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

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

image

Complete the switch statement as below:

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

    case SolidEdgeFramework.DocumentTypeConstants.igDraftDocument:
  Label1.Text = "Draft Document";
  break;

    case SolidEdgeFramework.DocumentTypeConstants.igAssemblyDocument:
  Label1.Text = "Assembly Document";
  break;

    case SolidEdgeFramework.DocumentTypeConstants.igSheetMetalDocument:
  Label1.Text = "Sheetmetal Document";
  break;

    case SolidEdgeFramework.DocumentTypeConstants.igWeldmentDocument:
  Label1.Text = "Weldment Document";
  break;

    case SolidEdgeFramework.DocumentTypeConstants.igWeldmentAssemblyDocument:
  Label1.Text = "Weldment Assembly Document";
  break;

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

  Label2.Text = oApp.ActiveEnvironment.ToString();
} // if
else
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.

image

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

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

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

image

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

image

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

image

Similarly check the labels in an assembly document.

 clip_image002 Post a comment below if you need the Visual Studio project 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]

 cmayocad1

No comments:

Post a Comment