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
end sub
最后列出这两个函数的vb语法:
modeldocextension::selectbyid2
description
this method selects the specified entity.
syntax (ole automation)
retval = modeldocextension.selectbyid2 ( name, type, x, y, z, append, mark, callout. selectoption )
input:
(bstr) name
name of object to select or an empty string
input:
(bstr) type
type of object (uppercase) as defined in swselecttype_e or an empty string
input:
(double) x
x selection location or 0
input:
(double) y
y selection location or 0
input:
(double) z
z selection location or 0
input:
(variant_bool) append
if…
and if entity is…
then…
true
not already selected
the entity is appended to the current selection list
already selected
the entity is removed from the current selection list
false
not already selected
the current selection list is cleared, and then the entity is put on the list
already selected
the current selection list remains the same
input:
(long) mark
value that you want to use as a mark; this value is used by other functions that require ordered selection
input:
(lpcallout) callout
pointer to the associated callout
input:
(long) selectoption
selection option as defined in swselectoption_e (see remarks)
output:
(variant_bool) retval
true if item was successfully selected, false if not
selectionmgr::getselectedobject5
description
this method gets the selected object.
syntax (ole automation)
retval = selectionmgr.getselectedobject5 ( atindex )
input:
(long) atindex
index position within the current list of selected items, where atindex ranges from 1 to selectionmgr::getselectedobjectcount
output:
(lpdispatch) retval
pointer to the dispatch object as defined in swseltype_e; null may be returned if type is not supported or if nothing is selected
也可以通过com使用vc来访问。