SolidWorks secondary development asynchronous mode, using COM

Source: Internet
Author: User

Create a console project with VC, supporting ATL and MFC

Copy sldworks. tlb, swconst. tlb, and swpublished. tlb under the SolidWorks installation path to the project folder.

Add the following code to the Code:

# 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, callback, no_namespace, named_guidsCWinApp theApp; using namespace std; int _ tmain (int argc, TCHAR * argv [], TCHAR * envp []) {int nRetCode = 0; // initialize MFC and print and er Ror on failureif (! 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 <ISldWorks> swApp; CLSID clsid; // "SldWorks. application.18 "stands for SolidWorks2010, and so on: CLSIDFromProgID (L" SldWorks. application.18 ", & clsid); CComPt R <IUnknown> pUnk = NULL; GetActiveObject (clsid, NULL, & pUnk); if (pUnk! = NULL) {CComPtr <ISldWorks> swAppTemp = NULL; pUnk-> QueryInterface (IID_ISldWorks, (void **) & swAppTemp); swApp = swAppTemp ;} // SolidWorks has started 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 <IModelDoc2> pModel; long lErr, lWarning; swApp-> OpenDoc6 (_ bstr_t (strFilePath), swDocPART, swOpenDocOptions_Silent, _ bstr_t (_ T (""), & lErr, & lWarni Ng, & pModel); if (pModel! = NULL) {CString strMsg = _ T (""); CString strTemp = _ T (""); CComPtr <IFeature> pFeat; pModel-> IFirstFeature (& pFeat ); while (pFeat! = NULL) {CComPtr <IFeature> pNextFeat; pFeat-> temperature (& temperature); CComBSTR bstrFeatName; CComBSTR temperature; 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 )); // close the document // swApp-> ExitApp (); // close SolidWorks }:: CoUninitialize ();} return nRetCode ;}

Start SW. You can also:

: CoInitialize (NULL); {CComPtr <ISldWorks> swApp; HRESULT hres = swApp. CoCreateInstance (_ uuidof (SldWorks), NULL, CLSCTX_LOCAL_SERVER); if (swApp! = NULL ){//...... Write your own code here ...... SwApp-> ExitApp (); // disable SolidWorks} swApp = NULL; }:: CoUninitialize ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.