Thursday, 23 January 2014

Cust 33: Drawing Sheets: VB.Net

In this Solid Edge programming tutorial you learn using VB.Net how to:

• Create new drawing sheets.
• Count the number of sheets in a drawing.
• Determine common sheet properties like size, type
• Activate a sheet by its name.
• Remove or delete a sheet from a drawing.
• Export all sheets to other formats.

CSharp version is here.
C++ with MFC version is here.

A list of all Solid Edge tutorials is here.

Most tutorials use Visual Studio 2010.

image_thumb[8]

This tutorial should be read progressively beginning with the first post in the programming series.

Start Visual Studio and add a Visual Basic Windows Forms Application project.

Add several Buttons, a check box and a listbox to the form as shown.

Declare variables for Solid Edge, the drawing document and the sheets, etc. as below:

Imports System.Runtime.InteropServices

Public Class Form1
    Dim oApp As SolidEdgeFramework.Application
    Dim oDoc As SolidEdgeDraft.DraftDocument
    Dim oSheets As SolidEdgeDraft.Sheets
    Dim oSheet As SolidEdgeDraft.Sheet

Note that the document is declared explicitly as a DraftDocument. This is unlike earlier tutorials where the document is declared as SolidEdgeFramework.Document.

Declaring the document as a DraftDocument enables to access the sheets and more drawing specific properties and methods.

In the Form's Load event, access a running instance of Solid Edge using the GetActiveObject method:

oApp = Marshal.GetActiveObject("SolidEdge.Application")
oDoc = oApp.ActiveDocument
oSheets = oDoc.Sheets
oSheet = oDoc.ActiveSheet

A draft document should be active when running this program since no error checking is implemented. Store this drawing in oDoc using the ActiveDocument property of Solid Edge.

Also store all sheets in oSheets. This is the sheets collection of the drawing. A single sheet can only be accessed through the sheets collection. As an exception, the active sheet can also be accessed directly from the draft document as in last line above.

Double click the button for Add New Sheet:

oSheet = oSheets.AddSheet()
cmdSheets_Click(cmdSheets, New System.EventArgs())

AddSheet adds a new sheet to the sheets collection, the first optional argument being the sheet name.
The second optional argument is the sheet type for which Solid Edge provides an enumerated list like SolidEdgeDraft.SheetSectionTypeConstants.igWorkingSection and the last two optional arguments specify the sheets between which the newly added sheet will be inserted.
 
Since all the arguments are optional, Solid Edge adds a new sheet with an automatically incrementing sheet number.
 
In the second line, the listbox is being updated via the button List all Sheets, whose click event is called from the Click event of the Add Sheet button.
 
The Click event of the List all Sheets button looks like this:
 
lstSheets.Items.Clear()
For Each oSh as SolidEdgeDraft.Sheet In oDoc.Sections.WorkingSection.Sheets
lstSheets.Items.Add(oSh.Name)





First the listbox is cleared and then in a For Each loop, the listbox is added with names of the sheets accessed from the sheets collection of the drawing.


Counting the number of sheets via the Count Sheets button is as easy as:


MessageBox.Show(oDoc.Sections.WorkingSection.Sheets.Count.ToString)


A draft document has two sections: Working and Background. The above statements displays the sheets in the working section.


The size of the sheet is a property under the SheetSetup which can be accessed as below from the Sheet Size button:


oSheet = oSheets.Item(lstSheets.SelectedItem)
MessageBox.Show(oSheet.SheetSetup.SheetSizeOption.ToString)


To activate the sheets via the Activate Sheets button:


oSheets.Item(lstSheets.SelectedItem).Activate()


In the above line of code, oSheets.Item(lstSheets.SelectedItem) represents a single sheet from the sheets collection.


Finally, a sheet can be removed via the Remove Sheet button as below:


oSheets.Item(lstSheets.SelectedItem).Delete()
cmdSheets_Click(cmdSheets, New System.EventArgs())


The technique for removing a sheet is same as activating. Here additionally the listbox needs to be updated by first clearing all the items and then flooding the list with the sheet names from the drawing again.


A single sheet can be exported to various formats like dwg, dxf,igs or dgn. The draft document's SaveAs method does this with the option set to Active Sheet Only.


To set this option, select File > Save As and in the Save As dialog, select DXF from the Save as type list. Then click the Options button.


image_thumb[5]


In the Solid Edge to AutoCAD Translation Wizard that appears, click Next >.


In the Sheet Options panel, select the Active Sheet Only option.


image_thumb[7]


Finish the wizard and run the program. Since the option is now set as active sheet in the ini file, the automation program takes this current setting and saves the current document to DXF with only the contents of the currently active sheet.


A single drawing sheet in Solid Edge has many more properties and methods like dimensions, drawing views, text boxes, sketches, layers, etc. Bookmark this blog to read more about these in subsequent posts.


Once again, A list of all Solid Edge on this blog tutorials is here.


Meanwhile, drop a comment below if you need any other drawing sheet technique illustrated.


clip_image002_thumb1_thumb_thumb_thu_thumbAlso, drop a comment if you need the Visual Studio project files and if you liked the depth of this discussion, similar in-depth techniques are discussed in 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]