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 ();