Thursday, April 10, 2008

Batch mode SolidWorks

I saw a post on Google Groups where a user had posted a question (2 years ago) asking how one could open SolidWorks silently viz. no GUI and use SolidWorks APIs to open a part but there was no answer that I could find right of.

In the same vein as my previous post titled "Batch mode Pro/Engineer" I tried to create a similar workspace for SolidWorks that executes SolidWorks with no GUI but still uses the SolidWorks API to load and query parts or assemblies. Fortunately SolidWorks API makes it much easier to do this as compared to Pro/Toolkit. After a little experimentation I came up with the following solution. The solution was created in Visual Studio 2005 using Visual C++. Click on the following zip file to download the solution.



http://ossandcad.googlecode.com/files/swbatch.zip






If you prefer to download from the source, you can do so using the following link with your SVN client (e.g. TortoiseSVN (follow the instructions at http://code.google.com/p/ossandcad/source/checkout)):


https://ossandcad.googlecode.com/svn/trunk/swbatch





Setup of Solution
Simply create a new "Win32 Console Application" project in Visual Studio 2005.


Edit the project's properties and add the following directory to the "Additional Include Directories" under "Configuration Properties"->C/C++->General:

"c:\program files\solidworks\samples\appcomm\win32\"


You should have a "cpp" file named swbatch.cpp (or something similar depending on the name you selected for the console application). Open that file and add the following code to it above the main() (or _tmain()) function.
#include "iostream"
#include "objbase.h"
#include "atlbase.h"

//Import the SolidWorks type library
#import "C:\Program Files\SolidWorks\sldworks.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
//Import the SolidWorks constant type library
#import "C:\Program Files\SolidWorks\swconst.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
Then modify your main() (or _tmain()) function to look like the source code included in the zip file. I apologize for not posting the code directly into the body of this post, but Blogger was messing up the formatting.

Make the following changes to the code in main():

  • Make sure you have a SLDPRT file in the location stated above instead of "C:\\swbatch\\Debug\\camtest.sldprt". Change the path to which sFileName points to, to coincide with your true path.
Build the solution and execute it. The console application will startup and silently load SolidWorks. If you have Windows Task Manager open then you will see SLDWORKS.exe running. If the SLDPRT is found in the right location, then the code above reads the SLDPRT and throws a MessageBox with the name of the first feature of the SLDPRT, which in this case is "Annotations". After the program exits, SolidWorks exits too.

Note:
Please keep in mind the following points about the code in main() in the zip file
:
  • I first tried to use OpenDoc6 as follows:


    • swApp->OpenDoc6(L"C:\\cygwin\\home\\ganesh\\downloads\\imagecom\\batch\\camtest.sldprt", swDocPART, swOpenDocOptions_Silent, L"Default", &fileerror, &filewarning, &swModel)
  • By doing that, SolidWorks started up with its GUI and then tried to load the model, which was not what we wanted. Next I tried an old API OpenDocSilent() as follows:


    • swApp->IOpenDocSilent(sFileName, swDocPART, &fileerror, &swModel)
  • This had the required effect of opening SolidWorks silently and loading the model in it.
  • The other big problem I noticed was: IF YOU LOAD A READ-ONLY SLDPRT INTO SOLIDWORKS USING OPENDOCSILENT(), FOR SOME REASON SOLIDWORKS STARTS UP WITH GUI. The only solution I have for this at the present is to not load read-only parts or assemblies.
If you found this article useful, please let me know. It helps me identify which posts, my readers prefer.

UPDATE: THE POST WAS UPDATED ON AUGUST 3RD 2009 TO REPLACE THE DOWNLOAD LINK FOR SWBATCH.ZIP. PREVIOUSLY SWBATCH.ZIP WAS SERVED FROM BOX.NET. IT IS NOT BEING SERVED FROM GOOGLECODE.

5 comments:

  1. once opened in silent mode: how might you interact with the file?

    ReplyDelete
  2. Hello nate, I don't know if you looked through the ZIP file and the code, but I have some sample code in there that opens a SLDPRT file and queries the open model document:

    e.g.:
    ===============================
    IPartDoc *part;

    hres = swModel->QueryInterface(IID_IPartDoc, (LPVOID *)&part);

    swModel->put_Visible(FALSE);
    CComPtr swfeat;
    part->IFirstFeature(&swfeat);
    ===============================

    I hope that helps. Download the http://ossandcad.googlecode.com/files/swbatch.zip file and have a look at the specific lines in swbatch.cpp

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hi Ganeshram,

    How can I run a VB macro while SolidWorks is in running without GUI. I tries implementing this in your code.

    ================================================

    VARIANT_BOOL* n (NULL);
    CComBSTR macroFilePath (L"C:\\Users\\rpt1g12\\Desktop\\Matlab\\createModel.swp");
    CComBSTR moduleName ("createModel1");
    CComBSTR functionName("main");

    swApp->RunMacro(macroFilePath,moduleName,functionName,n);

    ================================================

    Thank You

    ReplyDelete
  5. Such a nice blog you wrote, I am glad to read it. It is so informative and helpful for designer like me. I have also something to share with you about perfect industrial designing and management also.

    Siemens CAD software

    Solid edge CAM software

    Siemens PLM software

    ReplyDelete