PrusaSlicer export macro from Solidworks
Hi,
I've been using Solidworks as my main design software, but it always bugged me to not have an export button that directly opens in PrusaSlicer (similar to Fusion360), so I made one. It exports the 3MF file in the same directory as where the part is saved, so you can always find it later.
It is a custom VBA macro that is quite easy to install. In Solidworks, go to Tools > Macro > New. Save the file in a directory that will not be removed, and give it a reasonable name (like ExportToPrusaSlicer). Press Save and the VBA edit screen opens. Paste this:
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Dim PrusaSlicerPath As String
PrusaSlicerPath = """C:\Program Files\Prusa3D\PrusaSlicer\prusa-slicer.exe"""
Dim swFeat As SldWorks.Feature
Set swFeat = swModel.FirstFeature
Dim BodyNameTag As String
BodyNameTag = "Description"
Dim BodyName As String
Dim swBodyFolder As SldWorks.BodyFolder
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim swFeat2 As SldWorks.Feature
Dim Bodies As Variant
Dim swBody As SldWorks.Body2
Dim pName As String
Dim errors As Long
Dim warnings As Long
Dim noCLFlag As Boolean 'Catching if there is no cutlist
noCLFlag = True
Dim fName As String
fName = swModel.GetPathName
fName = Left(fName, InStrRev(fName, ".") - 1) 'removing the original extension
fName = Right(fName, Len(fName) - InStrRev(fName, "\")) 'Getting just the file name bit
Do While Not swFeat Is Nothing
If swFeat.GetTypeName2 = "CutListFolder" Then
noCLFlag = False
Set swBodyFolder = swFeat.GetSpecificFeature2
Set swCustPropMgr = swFeat.CustomPropertyManager
Dim DummyVal As String
swCustPropMgr.Get4 BodyNameTag, False, DummyVal, BodyName
If Not BodyName = "" And swBodyFolder.GetBodyCount > 0 Then
Bodies = swBodyFolder.GetBodies
Set swBody = Bodies(0)
'Select and isolate body:
Dim Mark As Long
swBody.Select False, Mark
Set swFeat2 = swModel.FeatureManager.InsertDeleteBody2(True)
'Export File at body name:
pName = swModel.GetPathName
pName = Left(pName, InStrRev(pName, "\")) 'removing the original extension
Dim savefName As String
savefName = pName & BodyName & ".3MF"
swModel.Extension.SaveAs savefName, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, errors, warnings
'Open the exported file in PrusaSlicer
Shell PrusaSlicerPath & " " & """" & savefName & """", vbNormalFocus
swFeat2.Select False
Dim DeleteOption As Long
Dim ModelDocExt As SldWorks.ModelDocExtension
Set ModelDocExt = swModel.Extension
DeleteOption = swDeleteSelectionOptions_e.swDelete_Absorbed
Dim status As Boolean
status = ModelDocExt.DeleteSelection2(DeleteOption)
End If
End If
Set swFeat = swFeat.GetNextFeature
Loop
If noCLFlag Then 'If there is no cutlist then just save with the filename of the part without any doing any body isolation
Dim SaveName As String
pName = swModel.GetPathName
pName = Left(pName, InStrRev(pName, "\")) 'Getting just the pathname
SaveName = pName & fName & ".3MF"
swModel.Extension.SaveAs SaveName, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, errors, warnings
'Open the exported file in PrusaSlicer
Shell PrusaSlicerPath & " " & """" & SaveName & """", vbNormalFocus
MsgBox "Done"
End If
End Sub
My PrusaSlicer is installed in the path given (line 5), edit if needed, keep the triple quotes. To test it, you can run it by pressing F5. Save the file, and you can close the editor.
To add it to the toolbar, we need to create and add the shortcut. In the topbar, press the arrow next to the settings icon -> customize. New screen opens, click tab Commands, search for macro. Drag the most right button (New Macro Button) to the bar you want it. I added it to features. A screen opens, select the macro we just created. For the icon, use this:
. Should be a 16x16 4bit BMP icon. If this is unclear, you can always use this guide: https://help.solidworks.com/2022/english/Solidworks/sldworks/t_assigning_macro_toolbar_button.htm?rid=137228
Now, all should work as intended!
If you have any questions, please let me know.
RE:
I believe it also works with different slicers - haven't tested, but the code should be similar other than the slicer path.
RE: PrusaSlicer export macro from Solidworks
Hi,
First thank you for taking the time to make it really easy for all of us!
Second, do you think it would also be possible to open it up in PrusaSlicer window that i have open?
I usually just add things in the open window and work on multiple parts at the same time to make it time effective.
RE: PrusaSlicer export macro from Solidworks
It Works! This is brilliant. I was searching for how to do exactly this and, low and behold, you did all the work for me! Thanks a ton!
RE: PrusaSlicer export macro from Solidworks
I would also like to thank you for taking the time to write this macro and share it.
The macro works like a champ with your concise instructions and I appreciate it very much.
