SolidWorks二次开发异步模式,使用COM

用VC创建控制台工程,支持ATL和MFC

把SolidWorks安装路径下的sldworks.tlb、swconst.tlb、swpublished.tlb拷贝到工程文件夹中

在代码中添加相应代码,如下:

#import "sldworks.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
#import "swpublished.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids 
#import "swconst.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		::CoInitialize(NULL);
		{
			CComPtr swApp;

			CLSID clsid;
			//"SldWorks.Application.18"代表SolidWorks2010,依次类推
			::CLSIDFromProgID(L"SldWorks.Application.18", &clsid);
			CComPtr pUnk = NULL; 
			GetActiveObject(clsid, NULL, &pUnk);
			if (pUnk != NULL)
			{
				CComPtr swAppTemp = NULL;
				pUnk->QueryInterface(IID_ISldWorks, (void**)&swAppTemp);
				swApp = swAppTemp;
			}//SolidWorks已经启动
			else
			{
				::CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, 
					__uuidof(ISldWorks), (void**)&swApp);		
			}

			if (swApp != NULL)
			{
				CString strFilePath = _T("");
				CFileDialog dlg(TRUE);
				dlg.m_ofn.lpstrFilter = _T("Text Files (*.sldprt)\0\0");
				dlg.m_ofn.lpstrDefExt = _T("sldprt");
				dlg.m_ofn.lpstrInitialDir = _T("d:\\");
				if (dlg.DoModal() == IDOK)
				{
					strFilePath = dlg.GetPathName();
				}
				
				CComPtr pModel;
				long lErr, lWarning;
				swApp->OpenDoc6(_bstr_t(strFilePath), 
								swDocPART, 
								swOpenDocOptions_Silent,
								_bstr_t(_T("")), 
								&lErr,
								&lWarning,
								&pModel);
				if (pModel != NULL)
				{
					CString strMsg = _T("");
					CString strTemp = _T("");
					CComPtr pFeat;
					pModel->IFirstFeature(&pFeat);
					while (pFeat != NULL)
					{
						CComPtr pNextFeat;
						pFeat->IGetNextFeature(&pNextFeat);

						CComBSTR bstrFeatName;
						CComBSTR bstrFeatTypeName;
						pFeat->get_Name(&bstrFeatName);
						strTemp = bstrFeatName;
						strMsg = strMsg + strTemp + _T("\n");
						pFeat->GetTypeName2(&bstrFeatTypeName);
						strTemp = bstrFeatTypeName;
						strMsg = strMsg + strTemp + _T("\n");

						pFeat = NULL;
						pFeat = pNextFeat;
					}

					if (!strMsg.IsEmpty())
					{
						swApp->SendMsgToUser(_bstr_t(strMsg));
					}
				}
				swApp->CloseDoc(_bstr_t(strFilePath));	//关闭文档
				//swApp->ExitApp();						//关闭SolidWorks
			}
		}
		::CoUninitialize();
	}
	return nRetCode;
}


启动SW,还可以:

::CoInitialize(NULL);
	{
		CComPtr swApp;
		HRESULT hres = swApp.CoCreateInstance(__uuidof(SldWorks), 
			NULL, CLSCTX_LOCAL_SERVER); 

		if (swApp != NULL)
		{	
			//……自己的代码写在这里……

			swApp->ExitApp();//关闭SolidWorks
		}
		swApp = NULL;
	}
	::CoUninitialize();


你可能感兴趣的:(SolidWorks二次开发异步模式,使用COM)