2
我正在嘗試使用SolidWorks和VBA宏執行一個複雜的幾何體,並使用批處理腳本啓動它。在後臺執行SolidWorks並使用VBA宏退出
首先,我做了一個簡單的宏來檢查是否可以在後臺運行solidworks,創建一個新零件,做一個簡單的多維數據集,然後保存它關閉它&。
發生了兩件事情,我不想要:solidworks GUI實際顯示在我的屏幕中,solidworks在保存文檔後不會自動退出。它只是關閉當前的部分,而不是軟件。該文件完美保存,幾何圖形正確。這是一個問題,因爲我的主批宏不能繼續運行,除非我手動關閉軟件。
下面是代碼:
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
我希望西班牙的話會不會是理解課文的問題。
在此先感謝。
你能告訴我你如何運行這個腳本?我有一個宏代碼,我希望在不打開Solidworks應用程序的情況下從Windows命令行運行 –