Sunday, 12 January 2014

Cust 16: Solid Edge Documents: VB.Net Part 2

In this tutorial you learn using VB.Net, 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:

MessageBox.Show(oDocs.Count.ToString)

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

oDoc = 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 = oApp.ActiveDocument
string sExpFile = System.IO.Path.ChangeExtension(oDoc.FullName, ".STL")oDoc.SaveAs(sExpFile)

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 oDoc As SolidEdgeFramework.SolidEdgeDocument 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 Then
  oDocs.Item(lstFiles.SelectedItem).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

No comments:

Post a Comment