Tuesday, 14 January 2014

Cust 18: Solid Edge Documents: C++/CLI: Part 1

In this tutorial you learn using C++/CLI i.e. managed C++, how to:

  1. Create new Solid Edge document.
  2. Save document.
  3. Save As a document with different name.
  4. Open a Solid Edge document.
  5. Count open documents in Solid Edge.
  6. Access the active document.
  7. Export a document to other CAD formats.
  8. Iterate through all open documents.
  9. Activate a desired document from among open documents.
  10. Close a specific document.
  11. Close all open documents at once
image_thumb4

    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 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_thumb
In the Solution Explorer, expand the folder Header Files and select Form.h then click the View Designer button as shown in image below.

image

Add several buttons and a list box to the form as shown.


image_thumb6

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

.

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

Inside the Form1 class, add a variable oApp for Solid Edge, oDocs for the documents collection and oDoc for a Solid Edge Document.

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

        SolidEdgeFramework::Application^ oApp;

        SolidEdgeFramework::SolidEdgeDocument^ oDoc;
        SolidEdgeFramework::Documents^ oDocs;

 

In the form’s load event add the following code:

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

oDocs = oApp->Documents;
oApp->DisplayAlerts = false;

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

The oDocs variable stores the documents collection of Solid Edge. At the time of the form loading, if no documents are open, the documents collection still exits and has count of 0.

Similarly the oDoc variable is capable of storing any Solid Edge document type viz. Part, Draft, Assembly, etc.

The line oApp->DisplayAlerts = false; suppresses the display of any alert messages or prompts from Solid Edge that may appear during the execution of the program.

Build the project and check if everything is fine.

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

oDoc = (SolidEdgeFramework::SolidEdgeDocument^)oApp->Documents->Add("SolidEdge.PartDocument", nullptr);

This adds a new Part document to the documents collection and is immediately displayed to the screen giving the impression of creating a new document.

Similarly other type of documents can be created by changing the ProGID as below:

oDoc = (SolidEdgeFramework::SolidEdgeDocument^)oApp->Documents->Add("SolidEdge.SheetMetalDocument", nullptr);

oDoc = (SolidEdgeFramework::SolidEdgeDocument^)oApp->Documents->Add("SolidEdge.AssemblyDocument", nullptr);

oDoc = (SolidEdgeFramework::SolidEdgeDocument^)oApp->Documents->Add("SolidEdge.DraftDocument", nullptr);

Or simply use

oDoc = (SolidEdgeFramework::SolidEdgeDocument^)oDocs->Add("SolidEdge.PartDocument", nullptr);

Since oDocs is same as oApp->Documents as assigned in the form’s load event.

For the Save Document button, simply add:

oDoc->Save();

This invokes the Solid Edge Save File dialog where you can specify a location and file name.

To directly specify a location and file name for saving the document, double click the Save Document As button and enter:

oDoc->SaveAs("D:\\Temp\\Sample123.par", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);

The SaveAs function takes a string for the file name and several other arguments that can be set to null.

The directly specified path or a hard-coded path is not a recommended practice though. You will see in a subsequent blog post how to invoke a standard Windows file dialog or Solid Edge file dialog that allows user to specify a location and filename for saving a file.

For the Open Document button:

Object^ m = Type::Missing;
(SolidEdgeFramework::SolidEdgeDocument^)oDocs->Open("D:\\Temp\\Sample123.par", m, m, m, m, m);

This opens a document and adds it to the documents collections and also makes it visible in Solid Edge.

Continued in Part 2…

 image_thumb10 Post a comment below if you need the Visual studio code 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.

cMayoCAD2

 cmayocad142

No comments:

Post a Comment