gpt4 book ai didi

vba - 在后台执行SolidWorks并使用VBA宏退出

转载 作者:行者123 更新时间:2023-12-02 01:53:55 29 4
gpt4 key购买 nike

我正在尝试使用 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 Example

我回答的关键是:

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

希望这对大家有帮助。谨致问候。

关于vba - 在后台执行SolidWorks并使用VBA宏退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37488355/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com