I am trying to do a complex geometry using SolidWorks and a VBA macro that I launch it with a batch script.
Firstly I did a simple macro to check if it is possible to run solidworks in background, creating a new part, doing a simple cube and then save it & close it.
Two things happens that I don't want to: the solidworks GUI actually is shown in my screen and the solidworks don't automatically exit after saving the document. It just closes the current part but not the software. The document is perfectly saved and the geometry is correct. It is a problem because my main batch macro cannot continue running unless I close the software manually.
Here is the code:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = _
Application.SldWorks
Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2015\templates\Pieza.prtdot", 0, 0, 0)
' "Pieza" means part
swApp.ActivateDoc2 "Pieza1", False, longstatus
Set Part = swApp.ActiveDoc
Dim myModelView As Object
' Use SolidWorks in background mode
swApp.Visible = False
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
' Select lateral view and insert a sketch. Then extrude it
boolstatus = Part.Extension.SelectByID2("Vista lateral", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch True
Part.ClearSelection2 True
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstToRectEntity, swUserPreferenceOption_e.swDetailingNoOptionSpecified, False)
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType, swUserPreferenceOption_e.swDetailingNoOptionSpecified, True)
Dim vSkLines As Variant
vSkLines = Part.SketchManager.CreateCornerRectangle(0, 0, 0, -0.144655684475683, 8.79061467198382E-02, 0)
Part.ClearSelection2 True
Part.SketchManager.InsertSketch True
Part.ShowNamedView2 "*Trimétrica", 8
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Croquis1", "SKETCH", 0, 0, 0, False, 4, Nothing, 0)
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.2, 0.01, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)
Part.SelectionManager.EnableContourSelection = False
longstatus = Part.SaveAs3("C:\tmp_SW\part.SLDPRT", 0, 2)
' I tried to use a variable as a string, however using "swApp.QuitDoc Part.GetTitle" gives the same result
Dim myTitle As String
myTitle = Part.GetTitle
swApp.QuitDoc myTitle
End Sub
I hope the spanish words won't be a problem for understanding the text.
Thanks in advance.
Well, few days of research and I have found this beautiful example:
And the key to my answer was:
Sub Macro3()
Dim swApp As Object
Dim Part As Object
Set swApp = CreateObject("SldWorks.Application")
' Whether this value was true or false I got the same. GUI is shown on screen...
swApp.Visible = True
' First put swApp and then set it to nothing, to close the document.
swApp.ExitApp
Set swApp = Nothing
End Sub
I hope this will help you all. Best regards.