用c++创建一个独立的Solidworks应用程序

Creating a standalone Solidworks application in c++

本文关键字:独立 一个 Solidworks 应用程序 c++ 创建      更新时间:2023-10-16

我正在尝试创建一个独立的Solidworks应用程序(我希望我的c++程序在Solidworks中创建新的几何图形,在后台运行)。我用的是msvc++ express 2010

我已经尝试实现以下代码建议在这里

//Import the SolidWorks type library
#import "sldworks.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
//Import the SolidWorks constant type library    
#import "swconst.tlb"  raw_interfaces_only, raw_native_types, no_namespace, named_guids  
int _tmain(int argc, _TCHAR* argv[])  
{
//Initialize COM
CoInitialize(NULL);
//Use ATL smart pointers       
CComPtr<ISldWorks> swApp;
//Create an instance of SolidWorks
HRESULT hres = swApp.CoCreateInstance(__uuidof(SldWorks), NULL, CLSCTX_LOCAL_SERVER);
//My Code here
//Shut down SolidWorks    
swApp->ExitApp();
// Release COM reference
swApp = NULL;
//Uninitialize COM
CoUninitialize();
return 0;
}

它不会抱怨库的import语句,但由于以下错误而无法构建:

1>main.cpp(19): error C2065: 'CComPtr' : undeclared identifier
1>main.cpp(19): error C2275: 'ISldWorks' : illegal use of this type as an expression
1>          c:usersnolandocumentsc++solidworks_testsolidworks_testdebugsldworks.tlh(7515) : see declaration of 'ISldWorks'
1>main.cpp(19): error C2065: 'swApp' : undeclared identifier
1>main.cpp(22): error C2065: 'swApp' : undeclared identifier
1>main.cpp(22): error C2228: left of '.CoCreateInstance' must have class/struct/union
1>          type is ''unknown-type''
1>main.cpp(26): error C2065: 'swApp' : undeclared identifier
1>main.cpp(26): error C2227: left of '->ExitApp' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>main.cpp(29): error C2065: 'swApp' : undeclared identifier

显然我错过了什么,但我不知道那是什么。我觉得这和ATL有关,但我不确定……请帮助。

感谢编辑:

好的,我已经下载了windows开发工具包8.0,所有的文件都在那里。我在属性页中静态链接到ATL,我还尝试链接目录中的库文件:C:Program FilesWindows Kits8.0LibAtl

但是这些头文件无处可寻…请帮助。

好的,我找到了一个解决方案。这可能不是最优雅的,但它是有效的。

不幸的是,WDK中的ATL对象文件库没有帮助,因为头文件无处可寻。

因此,经过更多的挖掘,我发现Visual Studio的完整版本(不是Express)允许您使用ATL库。我很幸运,因为微软向学生提供了visual studio的完整版本(参见Dreamspark网页),而我恰好是一名学生。:)

所以在下载、安装和安装任何服务包(我只有一个)之后,我只需要再做一步就可以让它工作了:

我导航到属性页-> C/c++ ->通用我包含了可以找到.tlb文件的目录(在我的情况下是C:Program FilesSolidWorks CorpSolidWorks)

然后运行以下代码:

//main.cpp
#include <afxwin.h>
#include <iostream>
#import "sldworks.tlb"
void main()  
{
//Initialize COM
CoInitialize(NULL);
//Use ATL smart pointers       
CComPtr<SldWorks::ISldWorks> swApp;
//Create an instance of SolidWorks
HRESULT hres = swApp.CoCreateInstance(__uuidof(SldWorks), NULL, CLSCTX_LOCAL_SERVER);
//Make the instance visible to the user
swApp->put_Visible(VARIANT_TRUE);
std::cin.get();
//Shut down SolidWorks    
swApp->ExitApp();
// Release COM reference
swApp = NULL;
//Uninitialize COM
CoUninitialize();
}

就是这样。Solidworks在程序运行时打开(附加的put_Visible功能允许用户看到窗口),当用户在控制台窗口中按enter键时关闭而没有抱怨。

看起来编译器很难找到某些东西的定义,特别是CComPtr,这是ATL的一部分(如您所提到的)。我没有使用ATL,但它会工作去你的项目属性在Visual Studio,并确保你有"使用ATL"设置为静态或动态适当?

正如Jerry所提到的,您还需要包含相关的标题。