一、引用
二、基础运用
1、新建一个圆柱体,地址注意改成自己的电脑上的模板地址。
Private Sub Command1_Click()
Dim PartTemplate As String
Dim swApp As Object
Dim Part As Object
Set swApp = CreateObject("SldWorks.Application")
swApp.Visible = True
' Set Part = swApp.NewDocument("C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2021\templates\gb_part.prtdot", 0, 0, 0)'设置模板地址
PartTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart) '获取默认地址
Set Part = swApp.NewDocument(PartTemplate, 0, 0, 0)
Part.SketchManager.InsertSketch (True)
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Dim skSegment As Object
Set skSegment = Part.SketchManager.CreateCircle(0, 0, 0, 0, 0.04, 0)
Part.ShowNamedView2 "*上下二等角轴测", 8
boolstatus = Part.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.1, 0.01, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False)
Part.SelectionManager.EnableContourSelection = False
End Sub
2.宏录制
录制完后,工具》宏》编辑,即可查看录制的宏程序
' ******************************************************************************
' C:\Users\Lenovo\AppData\Local\Temp\swx7392\Macro1.swb - macro recorded on 05/23/24 by Lenovo
' ******************************************************************************
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.ActiveDoc
End Sub
这个是solidworks中的宏程序,改为VBA为
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub test()
Set swApp = GetObject(, "SldWorks.Application") '配置程序
Set Part = swApp.ActiveDoc
End Sub
技术交流,软件开发,欢迎微信沟通: