runswmacro
Description
The runswmacro command executes a SOLIDWORKS macro against files selected from a search or from a CSV list. This is useful for automating repetitive SOLIDWORKS tasks or applying custom logic to many files.
Syntax
runswmacro -filePath macro_path (-search search_query | -list csv_path) [-recursive] [-skip number] [-count number] [-timeout seconds] [-version year] [-csv report_path]
Parameters
| Parameter | Alias | Required | Description |
|---|---|---|---|
-filePath |
Yes | Path to the SOLIDWORKS macro file. Supports .swp, .swb, and DLL macros implementing IPDMShellSOLIDWORKSMacro. |
|
-search |
No | Search query, or a <Path.csv> search-query CSV, used to find files to run the macro on. Use exactly one of -search or -list. |
|
-list |
No | CSV file with a header and target file paths in the first column. Use exactly one of -search or -list. |
|
-recursive |
No | Includes subfolders for search-based selection. | |
-skip |
No | Skips the specified number of items. Only valid with -list. |
|
-count |
No | Only processes the specified number of items. Only valid with -list. |
|
-timeout |
No | Positive number of seconds for each file attempt. Default is 30. |
|
-version |
No | SOLIDWORKS year version. Example 2023. Default is latest. | |
-csv |
No | CSV report path. Reports successful, failed, and interrupted attempts. |
Selection input
Specify -filePath for the macro and exactly one selection method: -search or -list.
Both selection methods support .swp, .swb, and DLL macros implementing IPDMShellSOLIDWORKSMacro.
Vault-based macro and list inputs are retrieved locally before execution. Checked-out working copies are preserved; unlocked writable files are not silently overwritten.
Search input
Use -search for one search query or for a search-query CSV:
runswmacro -filePath "C:\Macros\Process.swp" -search "<C:\Temp\queries.csv>" -recursive -timeout 120 -csv "C:\Temp\macro-results.csv"
Search-query CSVs use a header row. Each value in the first column is treated as a search query.
Search
Name=%.sldprt
StateName=Released;Name=%.sldasm
-recursive works with search-based selection.
List input
Use -list for a CSV that contains target file paths. The CSV must have a header, and file paths are read from the first column. Quoted fields are supported. Duplicate rows remain separate attempts.
File Path
C:\TestVault\part1.sldprt
C:\TestVault\assembly2.sldasm
C:\TestVault\drawing3.slddrw
C:\TestVault\bracket4.sldprt
-skip and -count apply only to -list.
Warning
If you only have a list of filenames, use the SOLIDWORKS VBA complete-path helper macro to create full file paths from filenames.
Execution and timeout
Standalone, PDM task, and MCP execution use pdmcli.macroworker.exe for isolated macro execution.
-timeoutis a positive number of seconds. The default is30.- The per-file deadline includes worker and SOLIDWORKS startup, target-file caching, macro execution, and cleanup.
- Failure or timeout discards the worker. The failed file is not automatically retried.
- Subsequent files can use a fresh worker. If termination cannot be verified, the batch stops.
- The worker's vault-view login must match the parent command's PDM user.
- Unknown dialogs are not automatically confirmed.
- Macros must explicitly save intended changes. Unsaved documents in an owned worker cause failure.
This improves isolation and recovery, but it does not guarantee crash-free macro execution.
Existing SOLIDWORKS session
When runswmacro is called inside the user's existing SOLIDWORKS session, PDMShell reuses that application. It does not terminate or restart SOLIDWORKS, and it does not close the user's open documents.
In this mode, there is no forced execution timeout. Cancellation is checked between files, and a macro failure stops the batch for inspection.
Reports and recovery
The -csv report uses this structure:
Path,Result
Reports include successful, failed, and interrupted attempts. An incremental recovery CSV is also retained, and its location is written to the command log.
Existing vault reports must be checked out on the current machine. Reports are not automatically added to the vault or checked in. Output cannot overwrite input files.
What version of SOLIDWORKS will PDMShell use?
PDMShell will use the latest SOLIDWORKS version installed on your system by checking the Windows Registry at:
HKEY_LOCAL_MACHINE\SOFTWARE\SolidWorks
Note
If you have multiple SOLIDWORKS versions installed, PDMShell will automatically select the most recent version found in the registry.
SWP Macro
Warning
Please read the remarks below to properly call your macro.
- The macro procedure name must be called
main. - The macro module name must be called the file name of the macro appended by
1. Example: If the macro calledprint.swpthe module name must be calledprint1.
Important
runswmacro does not check out files automatically. It opens the file in SOLIDWORKS and runs the selected macro.
If your macro needs to modify and save files inside SOLIDWORKS PDM, the macro itself must handle the check-out and check-in process.
The example below shows a generic pattern that:
- Gets the active SOLIDWORKS document.
- Logs in to the PDM vault.
- Checks out the active file.
- Runs your custom macro logic.
- Saves the file.
- Closes the document.
- Checks the file back into the vault.
Before using this macro, enable the SOLIDWORKS PDM reference from:
Tools > References > PDMWorks Enterprise Type Library
Then update the vaultName constant with your vault name.
Make sure there are no missing references.
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim pdmVault As EdmVault5
Dim handle As Long
Const vaultName As String = "Your vault name"
Sub main()
On Error GoTo CleanExit
Dim swFrame As SldWorks.Frame
Dim swModel As SldWorks.ModelDoc2
Dim docType As Integer
Dim filePath As String
Dim docTitle As String
Set swApp = Application.SldWorks
Set swFrame = swApp.Frame
handle = swFrame.GetHWnd
Set pdmVault = New EdmVault5
pdmVault.LoginAuto vaultName, handle
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then GoTo CleanExit
docType = swModel.GetType
filePath = swModel.GetPathName
If Len(filePath) = 0 Then GoTo CleanExit
checkOutFile pdmVault, docType
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then GoTo CleanExit
RunCustomLogic swModel
Dim errors As Long
Dim warnings As Long
swModel.Save3 swSaveAsOptions_Silent, errors, warnings
docTitle = swModel.GetTitle
swApp.CloseDoc docTitle
checkInClosedFile pdmVault, filePath
CleanExit:
End Sub
Private Sub checkOutFile(ByVal pdmVault As EdmVault5, ByVal docType As Integer)
On Error GoTo Done
Dim swModel As SldWorks.ModelDoc2
Dim pdmFile As IEdmFile5
Dim pdmFolder As IEdmFolder5
Dim ret As Integer
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then GoTo Done
Set pdmFile = pdmVault.GetFileFromPath(swModel.GetPathName, pdmFolder)
If pdmFile Is Nothing Then GoTo Done
If pdmFolder Is Nothing Then GoTo Done
If pdmFile.IsLocked Then GoTo Done
If swModel.ForceReleaseLocks Then
pdmFile.LockFile pdmFolder.ID, handle
End If
If docType = swDocDRAWING Then
ret = swApp.CloseAndReopen(swModel, swCloseReopenOption_DiscardChanges, swModel)
Else
ret = swModel.ReloadOrReplace(False, swModel.GetPathName, True)
End If
Done:
End Sub
Private Sub checkInClosedFile(ByVal pdmVault As EdmVault5, ByVal filePath As String)
On Error GoTo Done
Dim pdmFile As IEdmFile5
Dim pdmFolder As IEdmFolder5
Set pdmFile = pdmVault.GetFileFromPath(filePath, pdmFolder)
If pdmFile Is Nothing Then GoTo Done
If pdmFolder Is Nothing Then GoTo Done
If Not pdmFile.IsLocked Then GoTo Done
pdmFile.UnlockFile handle, "Updated by SOLIDWORKS macro", EdmUnlock_OverwriteLatestVersion
Done:
End Sub
Private Sub RunCustomLogic(ByVal swModel As SldWorks.ModelDoc2)
On Error GoTo Done
If swModel Is Nothing Then GoTo Done
' Add your custom SOLIDWORKS macro logic here.
' Examples:
' - Update custom properties
' - Rebuild the model
' - Update drawing views
' - Export files
' - Change configurations
' - Run geometry checks
swModel.ForceRebuild3 False
swModel.ViewZoomtofit2
Done:
End Sub
## DLL Macro
>[!WARNING]
> Please read the requirements below to properly create your DLL macro.
### Requirements
- The macro class must implement the `IPDMShellSOLIDWORKSMacro` interface
- The class must be decorated with the `PDMShellMacro` attribute
- The interface can be found in the NuGet package `BlueByte.PDMShell.SOLIDWORKSMacro` on nuget.org
### Required NuGet Package Versions
>[!IMPORTANT]
> Your project must use these specific versions of the NuGet packages:
```xml
<PackageReference Include="BlueByte.PDMShell.SOLIDWORKSMacro" Version="1.0.0" />
<PackageReference Include="BlueByte.SOLIDWORKS.Interops" Version="2019.0.0" />
<PackageReference Include="BlueByte.SOLIDWORKS.PDMProfessional.Interops" Version="2024.5.50" />
Install using Package Manager Console:
Install-Package BlueByte.PDMShell.SOLIDWORKSMacro -Version 1.0.0
Install-Package BlueByte.SOLIDWORKS.Interops -Version 2019.0.0
Install-Package BlueByte.SOLIDWORKS.PDMProfessional.Interops -Version 2024.5.50
Note
Using different versions may result in compatibility issues or runtime errors. These specific versions are tested and guaranteed to work with PDMShell.
Example Implementation
using PDMShellSOLIDWORKSMacro;
using SolidWorks.Interop.sldworks;
using EPDM.Interop.epdm;
[PDMShellMacro]
public class MyMacro : IPDMShellSOLIDWORKSMacro
{
public bool Execute(
SldWorks swApp,
IEdmFile5 pdmFileObject,
IEdmFolder5 pdmFolderObject,
int handle,
string progress,
IPDMCmdLineCallback callback,
out string error)
{
error = string.Empty;
// Update progress using the callback
callback.AppendMessage("Starting macro execution...");
// Your macro implementation here
callback.AppendMessage($"Processing file: {pdmFileObject.Name}");
// Report completion
callback.AppendMessage("Macro execution completed successfully");
return true;
}
}
Note
Use the callback.AppendMessage() method to provide progress updates and status messages during macro execution. These messages will be displayed in the PDMShell output.
Running the Macro
# runswmacro
runswmacro -filePath "Macros/MyMacro.dll" -search "%.sldprt" -timeout 12000
Note
The DLL must be built against .NET Framework 4.7.2 or higher
Examples
# runswmacro
runswmacro -filePath "Macros/BatchExport.swp" -search "%.sldprt"
Important
You can use the token edit in the search to force a check out and a check in of the search results. The check out uses the default checkout settings specified in the reference dialog of your PDM user settings (ie: this might check out the file itself and its references too). The check in will only check in the document itself (not the references).
# runswmacro
runswmacro -filePath "Macros/BatchExport.swp" -search "Name=%.sldprt;Edit"