Saturday, 25 January 2014

Cust 35: Drawing Views: CSharp

In this Solid Edge programming tutorial you learn using CSharp how to:

• Create 4 standard drawing views: Front, Side, Top and Isometric.
• Specify a part/assembly document to create views.
• Access model links in a draft document.
• Obtain the range or bounding box of a drawing view.
• Align views to each other on the sheet.
• Calculate spacing between views.


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

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

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 two Buttons, a text box to the form as below. Also add a OpenFileDialog control.


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

public partial class Form1 : Form
        public Form1()

        SolidEdgeFramework.Application oApp;
        SolidEdgeFramework.Documents oDocs;

        SolidEdgeDraft.DraftDocument oDocD;
        SolidEdgePart.PartDocument oDocP;
        SolidEdgeDraft.Sheet oSheet;
        SolidEdgeDraft.DrawingView oView;
        SolidEdgeDraft.ModelLink oMlink;

        string sDftFile, sParFile;

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.

Also note the variable oDocP which separately holds the part document for which the views are created.

Apart from these, oSheet holds the sheet on which the drawing sheet on which the views are created. A single variable oView is required for creating the four views and not four variables.

First step is to get the part file name using the Open file dialog named dOpen. Double click the button Select Part Document and add:

dOpen.Filter = "Solid Edge Part Files (*.par)|*.par";
sParFile = dOpen.FileName;
textBox1.Text = sParFile;

The first line sets the filter to Solid Edge Part files. The next line displays the dialog using the ShowDialog() call. The third line stores the filename selected by the user and the fourth line displays the full file name in the text box.

A more detailed synopsis of File dialogs in Solid Edge is here.

Once the part file is specified, its time to kick Solid Edge into action via the button Create Views:

oApp = (SolidEdgeFramework.Application)Marshal.GetActiveObject("SolidEdge.Application");
oDocs = oApp.Documents;
oDocP = oDocs.Open(sParFile);

This accesses a running instance of Solid Edge using the GetActiveObject method and stores the documents collection into oDocs. It does not matter if any document is currently open or not. The code simply opens the specified part file by using the Open function of the Documents collection using the file name as the argument.

A draft document is then added to the documents collection i.e. a new draft document is created as below:

oDocD = oDocs.Add("SolidEdge.DraftDocument");

Once the draft document is in place, access its active sheet as below:

oSheet = oDocD.ActiveSheet;

This sheet is required to place the drawing view on.

Next the link to the part document needs to be established. This is done as below:

oMlink = oDocD.ModelLinks.Add(sParFile);

Using this link, the first drawing view can be created as below:

oView = oSheet.DrawingViews.AddPartView(oMlink, SolidEdgeDraft.ViewOrientationConstants.igFrontView, 1, 0, 0, SolidEdgeDraft.PartDrawingViewTypeConstants.sePartDesignedView);

Views are added to the DrawingViews collection just like documents are added to the documents collection and sheets are added to the sheets collection.

The first argument is the model link established in the previous line. Next argument is the type of view and Solid Edge has a whole list of enums for this:


The following three arguments specified as 1, 0 and 0 are the scale and x- and y-coordinate of the center of the view respectively. this creates the view with its center at the origin of the sheet.


Next step is to create the other three views for which first the extents or bounding box or Range of the initial view is calculated as below:

double xMin, yMin, xMax, yMax;
oView.Range(out xMin, out yMin, out xMax, out yMax);

Next the size of the view is calculated as below:

double xGap, yGap;
xGap = xMax - xMin; yGap = yMax - yMin;

Following this, the remaining views are placed with spacing between them as below:

oView = oSheet.DrawingViews.AddPartView(oMlink, SolidEdgeDraft.ViewOrientationConstants.igLeftView, 1, xGap * 2, 0, SolidEdgeDraft.PartDrawingViewTypeConstants.sePartDesignedView);

oView = oSheet.DrawingViews.AddPartView(oMlink, SolidEdgeDraft.ViewOrientationConstants.igTopView, 1, 0, yGap * 2, SolidEdgeDraft.PartDrawingViewTypeConstants.sePartDesignedView);

oView = oSheet.DrawingViews.AddPartView(oMlink, SolidEdgeDraft.ViewOrientationConstants.igTopFrontLeftView, 1, xGap * 2, yGap * 2, SolidEdgeDraft.PartDrawingViewTypeConstants.sePartDesignedView);

This is an unnamed draft document. A name is created for the Draft document, done by replacing the .PAR extension with .DFT as below:

sDftFile = sParFile.ToUpper().Replace("PAR", "DFT");

This string sDftFile is used to save the draft document.


and finally the part document is closed:


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 view technique illustrated.

clip_image002_thumb1_thumb_thumb_thu[1]Also, drop a comment down below 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.