Thursday 23 January 2014

Cust 32: Drawing Sheets: CSharp

In this Solid Edge programming tutorial you learn using CSharp 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.

VB.Net version of this tutorial is here.
C++ with MFC version is here.

A list of all Solid Edge tutorials is here.

Most tutorials use Visual Studio 2010.


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

Start Visual Studio and add a CSharp 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:

public partial class Form1 : Form
        public Form1()

        SolidEdgeFramework.Application oApp;
        SolidEdgeDraft.DraftDocument oDoc;
        SolidEdgeDraft.Sheets oSheets;
        SolidEdgeDraft.Sheet oSheet;

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:

private void Form1_Load(object sender, EventArgs e)
  oApp = (SolidEdgeFramework.Application)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:
foreach (SolidEdgeDraft.Sheet oSh in oDoc.Sections.WorkingSection.Sheets)

First the listbox is cleared and then in a foreach 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:


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

To activate the sheets via the Activate Sheets button:


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:

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.


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

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


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_thuAlso, 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.


No comments:

Post a Comment