solidworks二次开发-02-用来访问特征的两个API
来学习两个api:
SelectByID2和GetSelectedObject5。这两个函数,第一个通过给出对象的name选择对象。第二个通过启用程序前已经选择的索引得到对象。
看下面程序:
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim Model As ModelDoc2
Dim feature As feature
Dim boolstatus As Variant
Sub main()
Set swApp = Application.SldWorks
Set Model = swApp.ActiveDoc
' 选择叫"拉伸1"的特征
boolstatus = Model.Extension.SelectByID2("拉伸1", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, swSelectOptionDefault)
'主要就是这一句话,在写Option Explicit后函数的最后一个参数swSelectOptionDefault可以使用0来代替
' If the selection was successful, that is, "Extrude1" was
' selected and it is a "BODYFEATURE", then get that feature; otherwise,
' indicate failure
If boolstatus = True Then '如果有“拉伸1”这个特征下面的代码将其选中
Dim SelMgr As SelectionMgr
Set SelMgr = Model.SelectionManager
Set feature = SelMgr.GetSelectedObject5(1) '此处使用一个索引来得到特征
Debug.Print feature.Name
Else
Debug.Print "Error"
End If