jinjunbai 发表于 2019-6-8 14:12:22

solidworks自己录制的VBA代码有问题

本帖最后由 jinjunbai 于 2019-6-8 14:17 编辑

今天尝试用VBA代码完成一个图形的绘制,发现程序自己录制的VBA执行都有问题,比如基准面,绘图的时候设置好,VBA中执行出来就没有了,请高手帮忙解决一下

代码如下:
' ******************************************************************************
' C:\Users\admin\AppData\Local\Temp\swx11724\Macro1.swb - macro recorded on 06/08/19 by admin
' ******************************************************************************
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


' New Document
Dim swSheetWidth As Double
swSheetWidth = 0
Dim swSheetHeight As Double
swSheetHeight = 0
Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2018\templates\gb_part.prtdot", 0, swSheetWidth, swSheetHeight)
Dim swPart As PartDoc
Set swPart = Part
swApp.ActivateDoc2 "零件1", False, longstatus
Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
boolstatus = Part.Extension.SelectByID2("注解", "DCABINET", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("前视基准面", "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(-4.03305583756345E-02, 3.97460575296108E-02, 0, 6.89710998307952E-02, -0.03010179357022, 0)

' Named View
Part.ShowNamedView2 "*上下二等角轴测", 8
Part.ViewZoomtofit2
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.01, 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
boolstatus = Part.Extension.SelectByRay(-1.52826298517539E-02, 1.47929888240128E-02, 9.99999999999091E-03, -0.400036026779312, -0.515038074910024, -0.758094294050284, 5.70826886238244E-04, 2, False, 0, 0)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByRay(-1.52826298517539E-02, 1.47929888240128E-02, 9.99999999999091E-03, -0.400036026779312, -0.515038074910024, -0.758094294050284, 5.70826886238244E-04, 2, False, 0, 0)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
Dim myRefPlane As Object
Set myRefPlane = Part.FeatureManager.InsertRefPlane(8, 0.01, 0, 0, 0, 0)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.ClearSelection2 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)
vSkLines = Part.SketchManager.CreateCornerRectangle(-1.26249913529932E-02, 1.98473013094258E-02, 0, 4.43244050501335E-02, -1.64793375533918E-02, 0)
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.01, 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
End Sub


gddx110 发表于 2019-6-8 16:00:55

SW录制的部分动作不会记录,需要对二次开发的语句有一定了解才能修改,建议看一下API帮助文档入门后再提问。

魍者归来 发表于 2019-6-8 16:29:15

先说清楚自己想实现什么动作

jinjunbai 发表于 2019-6-8 16:49:25

问题已经搞定

远祥 发表于 2019-6-8 20:20:08

这样都是C语言吗

未来第一站 发表于 2019-6-8 22:28:52

进阶功能^_^
页: [1]
查看完整版本: solidworks自己录制的VBA代码有问题