solidworks二次開發—06—在裝配體中添加配合
折騰了三天終于完成了計劃中的功能模塊。在一個裝配體中自動判斷插入合適的零件,并添加配合。
在前面幾篇文章中我已經基本上說明了如何得到零部件的數據信息、如何插入零部件、如何得到已經選擇的特征等。
下面只介紹怎樣進行配合
在做配合時,需要經常選擇到零件的面、線等,這是一個問題,還有就是介紹一下addmate2函數的使用:
一般進行配合我們按照下面的次序來進行:
1-modeldoc.clearselection2 ‘取消所有選擇
2-選擇需要配合的實體(entity)
3-使用addmate2函數進行配合
4-再次使用 modeldoc.clearselection2 ‘取消所有選擇
主要的問題在于如何選擇合適的面:
由于面的命名沒有什么規律,很多時候是程序自動來命名的,這樣,不方便使用selectbyid來選擇,我也不想使用坐標值來選擇一個面,那樣做更加糟糕。
在得到一個組件(component)或者一個特征(feature)時,我們有getfaces、getfirstface、getnextface等方法,我們可以使用這些方法遍歷一個組件或特征等的各個面,來達到選擇面的目的,看下面程序:
private function selectface(dcom as sldworks.component2, tp as integer) as boolean
set swdowelbody = dcom.getbody()
if swdowelbody is nothing then '錯誤處理
msgbox "選擇零件失敗"
selectface = false
exit function
end if
set swdcface = swdowelbody.getfirstface ‘得到第一個面
do while not swdcface is nothing ‘遍歷各個面
set swdsurface = swdcface.getsurface ‘得到表面對象
if swdsurface.iscylinder then ‘如果是圓柱面
if tp = 0 then 'means cylinder
set swdent = swdcface
swdent.select4 true, selddata
selectface = true
exit function
end if
else ‘如果是其它,當然實際中我們可能需要使用select來定義好多分支
if tp = 1 then 'means plane
set swdent = swdcface
swdent.select4 true, selddata
selectface = true
exit function
end if
end if
set swdcface = swdcface.getnextface
loop
end function
此函數接受兩個參數,第一個是一個component對象,第二個用來標識選擇類型:0表示圓柱面,1表示平面。此函數運行完成后將選擇指定組件的指定類型的一個面。需要注意的是我們需要在判斷面類型時候需要轉換到surface對象。而且選擇需要定義一個entity對象,用來select4,達到選擇的目的。可能這個過程有些復雜,大家按照這個順序多測試幾次,就明白了它的工作原理。
上面的函數寫的并不好,是我從我的工程中截取的一段。
下面介紹一下addmate2函數:
syntax (ole automation) ole語法:
pmateobjout = assemblydoc.addmate2 ( matetypefromenum, alignfromenum, flip, distance, distabsupperlimit, distabslowerlimit, gearrationumerator, gearratiodenominator, angle, angleabsupperlimit, angleabslowerlimit, errorstatus )
參數:
input:
(long) matetypefromenum
type of mate as defined in swmatetype_e
配合類型
input:
(long) alignfromenum
type of alignment as defined in swmatealign_e
對齊選項
input:
(variant_bool) flip
true to flip the component, false otherwise
是否翻轉
input:
(double) distance
distance value to use with distance or limit mates
距離
input:
(double) distabsupperlimit
absolute maximum distance value (see remarks)
距離限制max
input:
(double) distabslowerlimit
absolute minimum distance value (see remarks)
距離限制min
input:
(double) gearrationumerator
gear ratio numerator value for gear mates
齒輪配合分子值
input:
(double) gearratiodenominator
gear ratio denominator value for gear mates
齒輪配合分母值
input:
(double) angle
angle value to use with angle mates
角度
input:
(double) angleabsupperlimit
absolute maximum angle value
角度限制max
input:
(double) angleabslowerlimit
absolute minimum angle value
角度限制min
output:
(long) errorstatus
success or error as defined by swaddmateerror_e
錯誤報告
return:
(lpmate2) pmateobjout
pointer to the mate2 object
返回指向配合的指針
remarks
to specify a distance mate without limits, set the distabsupperlimit and distabslowerlimit arguments equal to the distance argument's value.
指定一個沒有限制的距離,設定距離限制的最大、最小值和距離值相等
if matetypefromenum is swmatedistance or swmateangle when the mate is applied to the closest position that meets the mate condition specified by distance or angle, then setting flip to true moves the assembly to the other possible mate position.
如果是距離或角度配合,配合將從符合條件的最近端進行配合,我們可以設定flip為true,改變配合至另一個合適的位置
use:使用配合的步驟
modeldoc2::clearselection2(variant_true) before selecting entities to mate.
modeldocextension::selectbyid2 with mark = 1 to select entities to mate.
modeldoc2::clearselection2(variant_true) after the mate is created.
if matetypefromenum is swmatecamfollower, then use a selection mark of 8 for the cam-follower face.
如果配合類型為 凸輪,在表面標示8. 注:這個我也不太明白哈哈
if nothing is preselected, then errorstatus is swaddmateerror_incorrectseletions and pmateobjout is null/nothing.
如果實現沒有限定實體來配合,將會抱錯swaddmateerror_incorrectseletions,函數返回null或者nothing
上面就是api幫助所說的話,下面給出一段示例程序,假設之前我們已經選擇了兩個半徑一樣的圓柱面,那么我們來定義一個同心配合:
set swmatefeat = swassy.addmate2(1, 0, false, 0, 0, 0, 0, 0, 0, 0, 0, nerrors)
其中的 dim swassy as sldworks.assemblydoc
dim swmatefeat as object
注:在編程中有時候不能實現確定一個對象的類型,我們可以聲明一個object對象,讓vb自己去匹配。但這樣做是影響了效率。
要完成一個距離或者角度要麻煩一些,就像上面的remark中說明的:
set swmatefeat = swassy.addmate2(5, 1, true, 0.001, 0.001, 0.001, 0, 0, 0, 0, 0, nerrors)
在這里我們需要將min和max都設置成與距離值相等,要不然配合會認為我們設定了高級配合中的限制條件,會報錯。并且第三個參數和第二個參數需要按實際情況來確定。
最后我們列出addmate2的類型:
swmatetype_e
‘specifies values for assembly-mate information.
swmatecoincident 0 重合
swmateconcentric 1 同心
swmateperpendicular 2 垂直
swmateparallel 3 平行
swmatetangent 4 相切
swmatedistance 5 距離
swmateangle 6 角度
swmateunknown 7 未知
swmatesymmetric 8 對稱
swmatecamfollower 9 凸輪
swmategear 10 齒輪