Friday 20 December 2013

Cust 04: Start Show Stop Solid Edge: C++ with MFC Part 1

This is part 1 of the 3 part tutorial where you learn:

  • How to invoke Solid Edge using C++ with MFC i.e. pure C++.
  • How to Start-Show-Hide-Stop Solid Edge from the program.
  • How to manipulate several properties and interface elements of Solid Edge.

    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

Name the project suitably and click OK.

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

image_thumb33

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

image_thumb5

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

image_thumb60

 
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           image_thumb13



image_thumb9
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
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

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
Click View > Toolbox.


Create several Buttons
image_thumb22 Edit Controls image_thumb24 and Check Boxes image_thumb26 on the dialog and change their Captions as shown in the image besides.


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

Place the default TODO Static Text control just below the Start Solid Edge button. We will put it to good use later.

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 button should work without any code.
image

In design mode, double click the Start Solid Edge 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

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. To search this file, consider downloading the Everything utility from voidtools website. It is just 400 kb but very fast hence useful for quickly searching files.

To understand why the rename attribute is specified, read here. In short, 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 this line towards the end of the class definition and may be just below the declaration for the OnBnClickedButton;

public:
    afx_msg void OnBnClickedButton1();
    SolidEdgeFramework::ApplicationPtr oApp;
};

This is the pointer variable for Solid Edge and Intellisense should work for both Solid Edge and ApplicationPtr which is a typedef for _com_ptr_t

Once you are done with the pasting stuff, return the cursor inside the OnClickedButton1 event stub in the file Cust01Dlg.cpp and add:

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);
  this->oApp.CreateInstance("SolidEdge.Application");

  SetDlgItemText(IDC_STATIC, "Solid Edge has started.");

  }

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

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

image_thumb13[3]

In the next line, the SetDlgItemText function changes the display text of any control on a dialog like Button or Check Box but is mostly used with an Edit Control. Here we use it for the Static Text control.

The first argument is the ID of the control which can be found by selecting the control on the form or dialog and looking up the ID property:

image

The second argument to SetDlgItemText is the text to be displayed which we set to “Solid Edge has started”.

Build the project and start Debugging by pressing F5.

Click the Start Solid Edge button and nothing happens after several seconds of waiting. The wait cursor appears for a while and later the Static Text changes from TODO:… to “Solid Edge has started”. Solid Edge does not actually show up.

image

This is because the Visible property of Solid Edge is set to false by default. We will change it to true from the Show button in the next part of this tutorial.

For now, right click the Windows Task Bar and select Task Manager. Take the Processes tab.

Solid Edge will be seen listed in the Image Name column as shown below:

image_thumb9[3]

Right click on the Edge.exe as shown above and select End Process.

Summary of Part 1:

You learned how to set up a MFC project and add Button and Static controls and how to invoke Solid Edge.

The next parts of this tutorial show how to add Edit and Check Box controls and get values from user that can be passed on to Solid Edge and how its various interface elements can be manipulated as desired.

This three part tutorial post continues in Part 2…


clip_image002_thumb1_thumb_thumb_thu Drop a comment below if you need the Visual Studio project 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.

cMayoCAD24224

No comments:

Post a Comment