Wednesday, July 21, 2021

Solidworks macro script to save current document as STL

Tools > Macro > New, then copy in this script:

Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim path As String
Dim i As Integer

Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
path = Part.GetPathName
i = InStrRev(path, ".")
If (i = Null Or i = 0) Then i = Len(path)
path = Left(path, i) + "stl"
longstatus = Part.SaveAs3(path, 0, 2)
MsgBox ("Export to STL Done : " + Str(longstatus))
End Sub

Then you can add it to your toolbar via  Right-click on toolbar > Commands > New Macro Button (note the "button", don't confuse with "New Macro"), then choose an icon and the macro file you saved from above.

This will save your current file in the same folder with an "stl" extension, overwriting anything already there. If you find it only exports part of your model, you may need to click to deselect anything in the document or tree to get it to save anything. (I think its defaulting to "selected bodies" rather than "all".

Works for exporting, at least, parts and assemblies.

No comments: