Sunday 22 December 2013

Cust 05: Start Show Stop Solid Edge : C++ with MFC Part 2

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

  • How to invoke Solid Edge 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:

…continued from the Part 1

Get back to the code window and for the Show button add the following:

this->oApp->Visible = true;

And for the Hide button:

this->oApp->Visible = false;

Build the project again and check the Show and Hide buttons after starting Solid Edge.

For the Display Version button, add the code:

_bstr_t seVersion(this->oApp->Version);
string SEversion((char*)seVersion);
MessageBox(SEversion.c_str(), "Version", 0);

COM uses the _bstr_t string data type which is the type of the value returned by the Version API of Solid Edge.

Convert it to a std::string which is a typedef for basic_string so that it can be used in the MessageBox function to be displayed when the button is clicked using the .c_str() method.

If you know of better ways of displaying a _bstr_t in a message box, do post a comment.

image

Note: If you do not want to use

this->oApp.CreateInstance("SolidEdge.Application");

every time just to test each new function, use

this->oApp.GetActiveObject("SolidEdge.Application");

which would simply connect to a running instance of Solid Edge. Make sure Solid Edge is started manually before running your MFC application since this function Gets Solid Edge instead of Create.

In that case you may want to simply change the caption property of the button Start Solid Edge to Connect to Solid Edge and that of the Static Text control to something like - "Connected!"

Try this out and note that it is much faster to connect to Solid Edge than starting it.

Next, on to the button Append to Status Bar which when clicked is expected to add the contents of the Edit control above it to the status bar of Solid Edge.

Switch to the tab for file Cust01Dlg.h and just below where you earlier added the oApp variable, add another variable to store the value entered in the edit control:

CString statusBarText;

The data type is CString which is the defacto standard for storing string values in MFC.

Switch to the tab for the dialog in design mode and double-click the Edit control to enter its event stub, OnEnChangeEdit:

GetDlgItemText(IDC_EDIT1, statusBarText);

The GetDlgItemText is similar to the SetDlgItemText function we used earlier except it gets the contents of the Edit control and saves as CString.

The IDC_EDIT1 ID value for the Edit control can be had from the Properties window of the Edit control as discussed earlier for the Static Text control.

When the text in the Edit control is changed it is stored as a CString variable statusBarText which is the second argument.

Double click the button Append to Status Bar and paste the code:

_bstr_t StatusBarText(statusBarText);
oApp->StatusBar += StatusBarText;

The StatusBar property of Solid Edge is of type _bstr_t.

Convert the CString statusBarText to a _bstr_t StatusBarText before assigning to Solid Edge’s StatuBar property.

Type something in the Edit control and click the button below it that says Append to Status Bar. The text in the Edit control should be displayed in the status bar of Solid Edge by virtue of the above code.

image

Note the += assignment used which will append i.e. add to the existing text of the status bar. So if you click the button multiple times, the text in the Edit control will get added repeatedly.


Summary of Part 2:

In this part you learned how to add Edit control and get values from user that can be passed on to Solid Edge and how its Status Bar text can be manipulated as desired.

This three part tutorial post continues in the final Part 3…


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.

cMayoCAD242242

No comments:

Post a Comment