Wednesday, 15 January 2014

Cust 19: Solid Edge Documents: C++/CLI Part 2

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_thumb

    Note:


…continued from Part 1

The Count Documents button simply displays the Count property of the documents collection after converting the number to a string:

MessageBox::Show(Convert::ToString(oDocs->Count));

To determine the currently active document among all open documents, add the following to the Get Active Document button:

oDoc = (SolidEdgeFramework::SolidEdgeDocument^)oApp->ActiveDocument;
MessageBox::Show(oDoc->FullName);

To export a Solid Edge document to other CAD formats, add the following to the button Export to STL…

oDoc = (SolidEdgeFramework::SolidEdgeDocument^)oApp->ActiveDocument;
String^ sExpFile = System::IO::Path::ChangeExtension
(oDoc->FullName, ".STL");
oDoc->SaveAs(sExpFile, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);

Here, first grab the FullName string property of the active document which consists the complete path with filename and extension and using the ChangeExtension method from the System::IO::Path namespace replace just the extension to STL.

Provide this old path with old name and a changed extension to the SaveAS method. The Solid Edge API is smart enough to recognize the extension and actually exports the Solid Edge file to the specified format.

In Solid Edge, check the list for Save as type to all know the formats supported.

image_thumb8_thumb

To access and list all documents open in Solid Edge, add the following code to the List all Open Documents button:

lstFiles->Items->Clear();
for each (SolidEdgeFramework::SolidEdgeDocument^ oDoc in oDocs)
  lstFiles->Items->Add(oDoc->FullName);

First the list is cleared. This is because the user can click the button several times and each time the names of currently open documents will appended to the existing ones.

Next, the for each loop is run for each document oDoc in the documents collection oDocs. The FullName of each document oDoc is added to the list.

This will display the full name of the document with its extension and complete path.

If you want to list display just the name without path or extension, prefix the System::IO::Path::GetFileNameWithoutExtension method to the FullName property before adding it to the list.

Once the document names are displayed in the list, activate a selected document from the Activate Selected button as below:

if(lstFiles->SelectedIndex >= 0)
  {
  oDoc = (SolidEdgeFramework::SolidEdgeDocument^)oDocs->Item(lstFiles->SelectedItem);
  oDoc->Activate();
  }
else
  MessageBox::Show("No file selected !", "Activate");
  }

First check if an item is selected in the list using the SelectedIndex property. The selected index is 0 for the first item in the list, so check if the index is either 0 or greater, else display a message box mentioning no file was selected.

If an item i.e. a file name is selected in the list, get it using the SelectedItem property of the list box. Use this item as an argument to the Item of the oDocs collection. The oDocs collection then checks in its list if the file name with the selected item name exists.

So the oDocs->Item(lstFiles->SelectedItem) stub represents a single document from the documents collection.

The Activate method then applies to this single document which gets activated or comes to front in Solid Edge.

Continued in Part 3…


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

cMayoCAD24[2]

 cmayocad1422