This is part 1 of the two part tutorial where you learn:
- How to determine the type of a document currently open in Solid Edge using C++ with MFC i.e. pure C++.
- Difference between Solid Edge Document and Environment types.
- How to determine Solid Edge Document and Environment types.
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.
Delete the default Static Text control with text TO DO: Add … that the wizard adds to the dialog.
Click Build > Build Solution and then press F5 to check everything is working fine.
The OK button should work without any code.
In design mode, double click the Document & Env Type 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.
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 the following line to the class as a public variable:
public:
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
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);
this->oApp.GetActiveObject("SolidEdge.Application");
}
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: If you click the LocalServer32 just above ProgID folder in the left panel, it displays the installed path for Solid Edge. |
Build > Build Solution to check if everything is working fine.
Double click the Document & Env Type button and add the following line of code for the button:
If (this->oApp->Documents->Count > 0)
switch (this->oApp->ActiveDocumentType)
To determine the type of the currently active document, first check if there is at least one document open using the if statement as above.
Set up a switch construct to check the document type using the ActiveDocumentType property of the Solid Edge Application object.
The ActiveDocumentType is a SolidEdgeFramework.DocumentTypeConstants enum
Summary of Part 1:
You learned how to set up a MFC project and add Button and Static controls and how to access Solid Edge.
Complete the switch statement as below:
This two part tutorial post continues in Part 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.
No comments:
Post a Comment