Sunday, 21 September 2014

Match Properties for Solid Edge - Part 2

...continued from Part 1

From the menu, select View | Solutions Explorer and View | Properties Window if these are not already visible. Drag around and dock the two windows as shown:

09

In the Solution Explorer, select the Form1 node and click View Code:

08

The cursor will be at the beginning of a code snippet that looks like below:

10

Press Ctrl+Home and then ENTER and add the following line of code:

Imports System.Runtime.InteropServices

Next step is to add references to the Solid Edge type libraries. Select Project | References and take the COM tab in the dialog that appears.

Scroll down to Solid Edge and add all Solid Edge items from the list.

Back in the code window, below the Public Class Form1 line, declare variables for the Solid Edge and Draft Document objects:

Public Class Form1
    Dim oApp As SolidEdgeFramework.Application
    Dim oDoc As SolidEdgeDraft.DraftDocument

The document type is declared as Draft since this utility will work in the Draft environment.

Switch to the Form design mode by clicking its file tab, where you added the buttons and check boxes. Double click an empty area in the form to see the Form_Load code snippet:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  On Error Resume Next

  oApp = Marshal.GetActiveObject("SolidEdge.Application")
  If oApp Is Nothing Then
    MessageBox.Show("Solid Edge should be running.", sTitle, MessageBoxButtons.OK, MessageBoxIcon.Information)
    Me.Close()
 End If

This code executes when the Form loads i.e. when the dialog appears for the first time.

The first line On Error Resume Next instructs to resume execution of the program on the next line of code in case an error occurs. This way the program does not show any un-understandable errors to the user.

On the next line, oApp that was declared as the Solid Edge application type stores a reference to the Solid Edge application which is assumed to be running.

The SE object is returned by the GetActiveObject method of the Marshal class.

Marshal is made available by the very first Imports line added the top of the code window: Imports System.Runtime.InteropServices

If oApp is Nothing, it means Solid Edge could not be accessed by GetActiveObject. In such case a message box is displayed informing user that Solid Edge is not running. This is followed by closing the Form i.e. the dialog immediately using Me.Close(). In case you are running this macro from a button on the Ribbon bar, the error about Solid Edge will never occur.

In there is no error, then the active i.e. currently open document is stored in oDoc as below:

oDoc = oApp.ActiveDocument

Continued to Part 3...

Index of all Solid Edge Tutorials, Tips, Videos...