Schmalls » Blog » 2008 » May 2008 » Solidworks Batch Runner

Solidworks Batch Runner

In my everyday work, I spend a lot of time waiting on SolidWorks to finish a task. The SolidWorks task scheduler is able to remove some of these burdens by allowing them to be run overnight. The great thing about the task scheduler is that it allows me to select folders or groups of files and runs the task in a separate SolidWorks instance for each file it finds. It also has a timeout for each file to complete its task. This means it can keep churning through all of the files and recover from a problem with one of the files (SolidWorks 2008 is notoriously unstable).

The problem is that the number of tasks available is limited. There is the Custom Task which allows you to run a custom program or macro, but for some reason removes the file selection capability. It is for this reason that I set out to recreate the functionality of the task scheduler, but with the ability to run any macro (or multiple macros) in separate instances of SolidWorks for entire groups of files. I call it SolidWorksBatchRunner.

Let me start with the usage statement for SolidWorksBatchRunner and continue with some examples of its functionality.

Usage: SolidWorksBatchRunner /m:MacroFile[|MacroModule[|MacroProcedure]] ..
       [/l:LogFile] [/n] [/r] [/s [/q]] [/t:Timeout] FileSearch ..
Runs the specified macros on the files found from the specified file
searches.
       /m:MacroFile|MacroModule|MacroProcedure
              The macro(s) to run on the specified files. MacroModule
              defaults to MacroFile - extension + 1. MacroProcedure
              defaults to main
       /l:LogFile
              The log file to use. Defaults to the current date/time:
              yyy-MM-dd HH-mm-ss.log.
       /n
              Specifies that each macro should be ran in a new SolidWorks
              instance.
       /q
              Specifies that the files should be rebuilt after all macros
              have run. Cannot be used without /s switch.
       /r
              Searches will also include subdirectories.
       /s
              Specifies that the files should be saved after all macros
              have run.
       /t
              The time, in seconds, each macro is given to complete its
              task. The default is 300 seconds.
       /?
       /h
       /help
              Prints this usage statement and exits.

I have included several options that I have found useful to a task scheduler application. So lets say you want to take a group of drawings and run your “DXFExport” macro on all the files. We don't need to save the SolidWorks files afterward because we didn't change anything. A command to do this could look like:

SolidWorksBatchRunner /m:"C:\My SW Macro Folder\DXFExport.swp" "C:\My Drawings Folder\*.SLDDRW"

A couple of assumptions have been made.

  1. The DXFExport module's name is DXFExport1.
  2. The DXFExport1 module method's name is main.
  3. *.SLDDRW will find all the SolidWorks drawings in “C:\My Drawings Folder”.
  4. A file log with the current time and date is created to store log messages.

The previous example shows how to run one macro on one file search, but it is possible to do multiple of each. We can also specify the name of a log file to create.

SolidWorksBatchRunner ^
  /m:"C:\My SW Macro Folder\Macro1.swp|Macro11|main" ^
  /m:"C:\My SW Macro Folder\Macro1.swp|Macro11|alternate" ^
  /m:"C:\My SW Macro Folder\Macro2.swp" ^
  /s /q /r ^
  /l:"My Log File Name.log" ^
  "C:\My Drawings Folder\Folder 1\*.SLDDRW" ^
  "C:\My Drawings Folder\Folder 2\*.SLDDRW" ^
  "C:\My Drawings Folder\Folder 3\Start*End.SLDDRW"

With this example:

  • On each file Macro1 is called twice, once with the main method followed by the alternate method.
  • On each file Macro2 is then called. All the macros run within one SolidWorks instance per file which can be changed by adding the /n switch.
  • /s saves each file after the macros have run (like Ctrl-S).
  • /q rebuilds each file before saving (like Ctrl-Q).
  • /r causes the file searches to recursively search subdirectories on all of the file searches.
  • The first file search finds all drawings in Folder 1 and its subdirectories (because of the /r switch).
  • The second file search finds all drawings in Folder 2 and its subdirectories (because of the /r switch).
  • The third file search finds only the drawings whose filename begins with “Start” and ends with “End” in Folder 3 and its subdirectories (because of the /r switch)
  • Status information is written to the command line as well as to the “My Log File Name.log” file.

I have uploaded a zipped up Visual Studio Express project file used to make this program as well as a zipped up file of the executable and the SolidWorks libraries that it uses. The code is released under the ISC License. I have only tested it with SolidWorks 2008 SP3.1 and .NET framework 3.5 installed. If anyone runs into any problems or has a suggestion, let me know by posting a comment to this post.

Discussion

Enter your comment
PNUVL
 
blog/2008/05/solidworks-batch-runner.txt · Last modified: 2009-09-21 08:49 by Josh Thompson