Saturday 14 December 2013

Cust 01: Start Show Stop Solid Edge: CSharp

In this tutorial you learn:

  • How to invoke or access Solid Edge using CSharp.
  • 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.

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.

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 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;
    Type typ;



image

Double click the button Start Solid Edge add the following code:

Type type = Type.GetTypeFromProgID("SolidEdge.Application");
oApp = (
SolidEdgeFramework.Application)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

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

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:

this.Cursor = Cursors.WaitCursor;

Type type = Type.GetTypeFromProgID("SolidEdge.Application");
oApp = (SolidEdgeFramework.Application)Activator.CreateInstance(type);

this.Cursor = Cursors.Default;

Run the program again and click the Start Solid Edge button and after 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.

Add the code label1.Text = oApp.Name + " - " + oApp.Version; for the Display Version button, Clicking the button should display the current version of Solid Edge in the label below the button:

image

For the Append to Status Bar button, type

oApp.StatusBar += txtStatus.Text;

image

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

image

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 bool 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 this->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 = (SolidEdgeFramework.Application)Marshal.GetActiveObject("SolidEdge.Application");

 clip_image002Post 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]

No comments:

Post a Comment