刚刚接触Solidworks二次开发,用的是VB。 在VBA里面能执行的语句复制到VS2005中,可以运行,但画不出图形

2024-12-22 21:25:09
推荐回答(2个)
回答1:

SolidWorks二次开发,VBA和VB的命名规则不一样,引用对象的方法也有些不一样。

回答2:

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Private Sub Command1_Click()

Set swApp = CreateObject("SldWorks.Application")
swApp.Visible = True
Set Part = swApp.NewDocument("C:\Documents and Settings\All Users\Application Data\SolidWorks\SolidWorks 2010\templates\零件.prtdot", 0, 0, 0)
swApp.ActivateDoc2 "零件5", False, longstatus
Set Part = swApp.ActiveDoc
'Dim myModelView As Object
'myModelView = Part.ActiveView
'myModelView.FrameState = swWindowState_e.swWindowMaximized
Part.SketchManager.InsertSketch (True)
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
'Part.ClearSelection2(True)
Dim skSegment As Object
Set skSegment = Part.SketchManager.CreateCircle(0, 0, 0, 0, 0.04, 0)
Part.ShowNamedView2 "*上下二等角轴测", 8
'Part.ClearSelection2(True)

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
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

以上代码是我将你的代码拷贝到VB6.0中,修改过的,我试过,在6.0中已经没有问题

你给出的代码,在6.0中运行,有错误:
一、缺参数:
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing)
应为:
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
还有下一个:
boolstatus = Part.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing)
应为:
boolstatus = Part.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)

还有这段代码也缺参数,上面的代码已经改过了,
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)

二、少Set关键字,这个在VB6.0中也报错了,上面的代码已经添加

如果你在VS2005中,还不能运行,那我就不知道是怎么回事了,我没有用过VS2005

另外,在VB6.0中,还要加上必须的库,就是“工程”——“引用”

我一般都是添加四个
※“SldWorks 2010 Type Library”
※“SldWorks 2010 Constant Type Library”
※“SldWorks 2010 exposed Type Libraries for add-in use”
※“SldWorks Extensibility Type Library”