Thursday, 23 January 2014

Cust 34: Drawing Sheets: C++ with MFC

In this Solid Edge programming tutorial you learn using C++ with MFC how to:

• Create new drawing sheets.
• Count the number of sheets in a drawing.
• Determine common sheet properties like size, type, etc.
• 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.
CSharp version is here.

A list of all Solid Edge tutorials is here.

Most tutorials use Visual Studio 2010.

image

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

Start Visual Studio and add a project under Visual C++ | MFC  | MFC Application.

  1. Assign a proper name to the project and click OK.
  2. In the MFC Wizard Welcome step, click Next.
  3. In the Application Type step, Select Dialog based and uncheck Use Unicode libraries.
  4. In the User Interface Features step, uncheck just about all check boxes like Thick Frame, System menu, etc. and set a Dialog title.
  5. In the Advanced Features step, again uncheck all check boxes that you can.
  6. In the Generated Classes step, click Finish.

Select View > Other Windows > Resource View or in the Solution Explorer, expand Resource Files Folder and double-click the .rc file to open the Resource View panel.

In the Resource View panel, expand Dialog and double-click IDD_PROJNAME_DIALOG to display the dialog created by the MFC Wizard.

Add two Buttons image_thumb3_thumb a Check Box image and a List Box image_thumb5_thumb to the form as shown above:

In the Solution Explorer, expand the folder Header Files and open the ProjNameDlg.h file.

In this header file, just below the #pragma once once line, add the following:

#pragma once

#import "D:\Program Files\Solid Edge V20\Program\framewrk.tlb" \
rename("GetOpenFileName", "SeGetOpenFileName")

#import "D:\Program Files\Solid Edge V20\Program\fwksupp.tlb"
#import "D:\Program Files\Solid Edge V20\Program\draft.tlb"
#import "D:\Program Files\Solid Edge V20\Program\constant.tlb"

To know what the above statements all mean, read this original tutorial post.

In the header file, scroll down inside the class definition

class CCust01Dlg : public CDialogEx,

and set public a variables as below:

SolidEdgeFramework::ApplicationPtr oApp;
SolidEdgeDraft::DraftDocumentPtr oDoc;
SolidEdgeDraft::SheetsPtr oSheets;
SolidEdgeDraft::SheetPtr oSheet;

These are pointer variables for Solid Edge, the Drawing Document and the Sheets, etc.

Note that the document is declared explicitly as a DraftDocumentPtr. This is unlike earlier tutorials where the document is declared as SolidEdgeFramework::DocumentPtr.

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

From the Solution Explorer, under the folder for Source Files double-click the file ProjNameDlg.cpp and add the lines #include <iostream> and using namespace std; at the end of the existing lines as below:

#include "stdafx.h"
.

.

#include <iostream>
using namespace std;

Next, scroll down to find the definition for:

BOOL CEnvTypeDlg::OnInitDialog()

Under this function, locate the comment

// TODO: Add extra initialization here

and add the following below this comment line:

try
  {
  CoInitialize(NULL);
  oApp.GetActiveObject("SolidEdge.Application");
  this->oDoc = oApp->ActiveDocument;
  this->oSheets = oDoc->Sheets;
  this->oSheet = oDoc->ActiveSheet;
  }
catch(_com_error &error)
  {
  MessageBox("COM Error", "Error", 0);
  CoUninitialize();
  }

The GetActiveObject method defined in comip.h helps to connect to a running instance of Solid Edge.

Build the solution again but don’t start debugging yet.

A draft document should be active 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 drawing document.

Double click the button for Add New Sheet:

oSheet = oSheets->AddSheet("Newsheet001", SolidEdgeDraft::SheetSectionTypeConstants::igWorkingSection);

AddSheet adds a new sheet to the sheets collection, the first argument being the sheet name.
The second argument is the sheet type for which Solid Edge provides an enumerated list and the last two optional arguments specify the sheets between which the newly added sheet will be inserted.
 
The Click event of the List all Sheets button looks like this:

lstSheets.ResetContent();
    for (long i = 1; i <= this->oSheets->Count; i++)
    {
        this->oSheet = oSheets->Item(i);
        _bstr_t fullName(oSheet->Name);
        string FullName((char*)fullName);
        lstSheets.AddString(FullName.c_str());
    }










First the listbox is cleared using ResetContent().


Then in a for 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:


long lCnt = oSheets->Count;


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




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


oSheet = oDoc->ActiveSheet;


    int sheetSize(oSheet->SheetSetup->SheetSizeOption);
    switch (sheetSize)
    {
    case 28:
        MessageBox("igIsoA4Tall", "Sheet Size", 0);
        break;


    case 29:
        MessageBox("igIsoA4Wide", "Sheet Size", 0);
        break;
   
    case 30:
        MessageBox("igIsoA3Tall", "Sheet Size", 0);
        break;
   
    case 31:
        MessageBox("igIsoA3Wide", "Sheet Size", 0);
        break;
    }




There are more sheet size option enums than illustrated here and ranging from -2 to 47.


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]_thumb


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]_thumb


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.


cMayoCAD24


No comments:

Post a Comment