Friday 17 January 2014

Cust 21: Solid Edge Documents: C++ with MFC: Part 1

In this tutorial you learn using C++ with MFC i.e. pure C++, 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

    Note:

Also note:

  • This tutorial does not intend to teach MFC, classes or their hierarchy, objects, Document-View architecture or any of the MFC concepts or features. Use Google for that.
  • This tutorials does show you step-by-step how to get things done in a cookbook style and explains all concepts where Solid Edge interfaces with MFC.
  • VS 2010 and Solid Edge 20 are used.
  • For Solid Edge 20, VS 2008 was also found to be working.

Start Visual Studio and under Visual C++, select MFC Application.

image_thumb3_thumb

Name the project suitably and click OK.

In the MFC Wizard, click Next on the first screen.

image_thumb33_thumb

On the next screen, change the Application type to Dialog based.

image_thumb5_thumb

Un-check Use Unicode Libraries, so the settings appear similar to as below:

image_thumb60_thumb

 
In the next screen, un-check most options as shown in the image on the right and specify a suitable Dialog title.

Next, on the Advanced features screen, un-check just about everything possible as shown below.

image_thumb11_thumb           image_thumb13_thumb



image_thumb9_thumb
The final screen shows generated classes.
Click Finish here.


Observe the Solution Explorer and note 3 folders:
Header Files
Resource Files
Source Files


Lets assume you have named the project as Cust01




image_thumb15_thumb
In VS 2008, the default dialog box created may not be visible initially.

Expand the Resource Files folder and double-click
Cust01.rc

This will display the Resource View dialog.

Expand the Dialog folder and double click on IDD_CUST01_DIALOG to show the dialog created by the MFC wizard.


image_thumb29_thumb1

Click anywhere on the dialog and observe the Properties pane.

The Dialog title specified in the Wizard is available for modification as the Caption property.

Note that a Static Text control with Caption TODO: Place dialog controls here. is created by default.

Also note that two buttons OK and Cancel are created.




image_thumb3
Click View > Toolbox.


Create several Buttons and a list box
on the dialog and change their Captions as shown in the image besides or at the top of this tutorial.


image
Note that you may not be able to change the default text in the Edit Control at design time.

Delete the default Static Text control with text TO DO: Add … that the wizard adds to the dialog.

Also resize and move the default OK Cancel pair at the bottom of the dialog.


Click Build > Build Solution and then press F5 to check everything is working fine.

The OK and Cancel buttons should work without any code.

In design mode, double click the Create New Document button which opens Cust01Dlg.cpp

The cursor is inside the function stub for the button.

void CCust01Dlg::OnBnClickedButton1()
{
    // TODO: Add your control notification handler code here

Press Ctrl+Home to go to the top of the code window. Add the lines #include <iostream> and using namespace std; at the end of the existing lines as below:

#include "stdafx.h"
#include "Cust01.h"
#include "Cust01Dlg.h"

.

.

#include <iostream>

using namespace std;

Next, right click anywhere inside line #include "Cust01Dlg.h" and select Open Document.

image_thumb37_thumb

At the top of the cust01Dlg.h file which opens, add an #import statement below the #pragma line:

// Cust01Dlg.h : header file
#pragma once

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

Here the framewrk.tlb is a Type Library. Specify the complete path for this file from where Solid Edge is installed on your computer.

The rename attribute is used when a name in the type library (Solid Edge in present case) coincides with a macro definition in the system header files (stdafx.h etc.)

If this situation is not resolved, various syntax errors will be generated, such as Compiler Error C2059 and Compiler Error C2061… – MSDN.

Build > Build Solution at each step to ensure its on track. Do not debug the project yet.

Scroll down and inside the class definition

class CCust01Dlg : public CDialogEx,

add these variables to the class definition as a public:

public:
   
SolidEdgeFramework::ApplicationPtr oApp;
    SolidEdgeFramework::DocumentsPtr oDocs;
    SolidEdgeFramework::SolidEdgeDocumentPtr oDoc;

These are the pointer variable for Solid Edge, the documents collection and a single Solid Edge document respectively.

Return to the file Cust01Dlg.cpp and 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);
  }
catch(_com_error &error)
  {
  MessageBox("COM Error", "Error", 0);
  CoUninitialize();
  }

CoInitialize starts the COM library.

Build > Build Solution to verify this is working.

Below the CoInitialize statement, add the line:

try
  {
  CoInitialize(NULL);
  oApp.GetActiveObject("SolidEdge.Application");
  this->oDocs = oApp->Documents;

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

Here, SolidEdge.Application is the Program ID for Solid Edge which does not change from version to version.

To check this, start the Registry Editor by pressing Windows+R button to invoke the Run utility. Type regedit and press ENTER.

In the registry editor application which looks like the Windows Explorer, select Edit > Find or press F3 and type SolidEdge.Application. After a while it searches for SolidEdge.Application and displays the search as below:

image_thumb7_thumb

If you click the LocalServer32 just above ProgID folder in the left panel, it displays the installed path for Solid Edge.

image_thumb133_thumb

The oDocs variable stores the documents collection of Solid Edge. At the time of the form loading, if no documents are open, the documents collection still exits and has count of 0.

Similarly the oDoc variable is capable of storing any Solid Edge document type viz. Part, Draft, Assembly, etc.

Build the project and check if everything is fine.

Back to Visual Studio, double click the Create New Document button and add the following line of code for the button:

this->oDoc = this->oDocs->Add("SolidEdge.PartDocument");

This adds a new Part document to the documents collection and is immediately displayed to the screen giving the impression of creating a new document.

Similarly other type of documents can be created by changing the ProGID as below:

this->oDoc = this->oDocs->Add("SolidEdge.SheetMetalDocument");

this->oDoc = this->oDocs->Add("SolidEdge.AssemblyDocument");

this->oDoc = this->oDocs->Add("SolidEdge.DraftDocument");

For the Save Document button, simply add:

oDoc->Save();

This invokes the Solid Edge Save File dialog where you can specify a location and file name.

To directly specify a location and file name for saving the document, double click the Save Document As button and enter:

_bstr_t fileName("D:\\Temp\\Sample123.par");
oDoc->SaveAs(fileName);

The SaveAs function takes a BSTR string for the file name.

The directly specified path or a hard-coded path is not a recommended practice though. You will see in a subsequent blog post how to invoke a standard Windows file dialog or Solid Edge file dialog that allows user to specify a location and filename for saving a file.

For the Open Document button:

_bstr_t fileName("D:\\Temp\\Sample123.par");
this->oDocs->Open(fileName);

This opens a document and adds it to the documents collections and also makes it visible in Solid Edge.

Continued in Part 2…

clip_image002_thumb1_thumb_thumb_thu[2] Drop a comment below if you need the Visual studio code files and also if you liked the depth of discussion here, 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.

cMayoCAD242242

No comments:

Post a Comment