Wednesday 18 December 2013

Cust 03: Start Show Stop Solid Edge: VB.Net

In this tutorial you learn:

  • How to invoke Solid Edge using VB.Net.
  • How to Start-Show-Hide-Stop Solid Edge from the program.
  • How to change several properties of Solid Edge.

  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
Imports SolidEdgeFramework

 

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

 

Public Class Form1
    Dim
oApp As SolidEdgeFramework.Application

image
In the code for the button Start Solid Edge add the following code:

Dim type As Type = type.GetTypeFromProgID("SolidEdge.Application")
oApp = Activator.CreateInstance(type)

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_thumb7

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

image_thumb13

Build the project and start Debugging by pressing F5.

Click the Start Solid Edge button and nothing happens after several minutes of waiting.

Right click the TaskBar and select Task Manager. Take the Processes tab.

Solid Edge will be seen listed in the Image Name column as shown below:

image_thumb9

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

oApp.Visible = True

There is no way however to know when Solid Edge has fully loaded and when to click the Show button. You can add the wait cursor very easily:

Me.Cursor = Cursors.WaitCursor

Dim type As Type = type.GetTypeFromProgID("SolidEdge.Application")
oApp = Activator.CreateInstance(type)

Me.Cursor = Cursors.Default;

Run the program again and click the Start Solid Edge button and when the Wait cursor is gone click the Show button. Solid Edge now should be visible.

For the Hide button, simply add oApp.Visible = False to its Click event.

Click the Show and Hide buttons alternatingly. Solid Edge should appear and disappear.

For the Display Version button, add the code:

label1.Text = oApp.Name & " - " & oApp.Version

Clicking the button should display the current version of Solid Edge in the label below the button:

image_thumb14

For the Append to Status Bar button, type oApp.StatusBar += txtStatus.Text

image_thumb15

Clicking this button multiple times should render the status bar as shown below:

image_thumb16

The button simply appends the contents of the textbox txtStatus to Solid Edge’s status bar.

For the checkbox Display Status Bar add the code:

oApp.StatusBarVisible = chkStatus.Checked

Both sides of the statement are of type Boolean and this will toggle the display of the status bar depending upon the checked state of the check box.

Similarly, for the Display Edge Bar button, add

oApp.EdgeBarVisible = chkEdge.Checked

Finallly, for the Quit Solid Edge button, type oApp.Quit()

This will quit Solid Edge but the CSharp application will continue to run.

you can add Me.Dispose() to the Close button to terminate to application.

Instead of invoking Solid Edge every time, it is possible to simply connect to a running instance of Solid Edge. For this, add a button Connect to Solid Edge to the form and add the code:

oApp = Marshal.GetActiveObject("SolidEdge.Application")

 clip_image002 Post a comment below if you need the Visual Studio project files and also be aware of cMayoCAD a first of its kind training program 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.
cMayoCAD2422

No comments:

Post a Comment