Saturday 18 January 2014

Cust 22: Solid Edge Documents: C++ with MFC: Part 2

In this tutorial you learn using C++ with MFC i.e. pure 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



…continued from Part 1

The Count Documents button simply displays the Count property of the documents collection:

long lCnt = oDocs->Count;

CString sCnt;
sCnt.Format("%d", lCnt);

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

oDoc = oApp->ActiveDocument;
_bstr_t fullName(oDoc->FullName);

string FullName((char*)fullName);
MessageBox(FullName.c_str(), "Active Document", 0);

The ActiveDocument property is of type _bstr_t which needs to be converted to a LPCTSTR so it can be used in a MessageBox

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

oDoc = oApp->ActiveDocument;
_bstr_t fullName(oDoc->FullName);
fullName += ".stl";


string FullName((char*)fullName);
MessageBox(FullName.c_str(), "Export", 0);

Here, first grab the FullName string property of the active document which consists the complete path with filename and extension and append to it the extension STL.

Provide new name with 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.


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

IDispatchPtr pDispatch;
for (long i = 1; i <= this->oDocs->Count; i++)
  pDispatch = oDocs->Item(i);
  HRESULT hr = pDispatch->QueryInterface(&oDoc);

  _bstr_t fullName(oDoc->FullName);
  string FullName((char*)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 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.

The oDoc returned by Item of oDocs is of type IDispatchPtr which is queried to obtain the oDoc whose full name can then be accessed to display in the list box after converting from its original _bstr_t string type to an LPCTSTR.

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

long i = lstFiles.GetCurSel();
this->oDoc = this->oDocs->Item(i+1);

First get the index of the item selected in the list using the GetCurSel method. The index is 0 for the first item in the list, whereas the index of first item in oDocs is 1. Hence the document is obtained using index + 1 as an argument to the Item of the oDocs collection.

So the this->Docs.Item(i + 1) 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_thumb 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.



No comments:

Post a Comment