Monday, 16 December 2013

Cust 02: Start Show Stop Solid Edge: C++/CLI

In this tutorial you learn:

  • How to invoke Solid Edge using C++/CLI i.e. managed C++.
  • 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 C++/CLR.

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 on the project name and select References or from the menu, select Project > References…


In the Property Pages dialog, click select
Add New Reference... button.


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


Back to the Property Pages dialog, Solid Edge Framework gets added under References.
image
In the Solution Explorer, expand the folder Header Files and select Form.h then click the View Designer button as shown in image besides.

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 namespace System;
using
namespace System::Collections.Generic;

.

.

using namespace SolidEdgeFramework;
using namespace System::Runtime::InteropServices;

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

public ref class Form1 : public... System::Windows::Forms::Form
    {
        public: Form1(void)
        {
            InitializeComponent();
        }

        SolidEdgeFramework::Application^ oApp;

        Type^ type

Note: Intellisense may not be available for C++/CLI in VS2010.

image



image
In the code for 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_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:

System::Windows::Forms::Cursor::Current = System::Windows::Forms::Cursors::WaitCursor;

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

System::Windows::Forms::Cursor::Current = System::Windows::Forms::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.

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 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 check box, add

oApp->EdgeBarVisible = chkEdge->Checked;

Finallly, for the Quit Solid Edge button, type oApp->Quit();

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

you can add System::Environment::Exit(0); to the Close button to terminate to application.

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

oApp = (SolidEdgeFramwwork::Application^)Marshal::GetActiveObject("SolidEdge.Application");

 clip_image002Post 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