几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量  


返回   几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量 » 仿射空间:CAX软件开发(三)二次开发与程序设计 » CAD二次开发 » SolidWorks二次开发
用户名
密码
注册 帮助 会员 日历 银行 搜索 今日新帖 标记论坛为已读


回复
 
主题工具 搜索本主题 显示模式
旧 2009-04-12, 09:39 PM   #1
yang686526
高级会员
 
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
yang686526 向着好的方向发展
默认 【转帖】macro help, finding and reporting distance in a msgbo

macro help, finding and reporting distance in a msgbox
i'm trying (very unsuccessfully) to write a macro to report the minimum distance between two objects in an assembly. one is a part, the other a face. i found the modeldoc2.closestdistance, but can't seem to get the objects to work. how does closestdistance work? what about measuredistance? i know some vb, but am apparently lacking. i don't need it to be real accurate, if it's over 1" i report one msgbox, under another (already figured out if-then-else statements). the other challenge is that this assembly will be driven via driveworks express, so the file names will change, but i think i have a method figured out to read that in.
ben,
did you study/try the examples for modeldoc2.closestdistance? they work fine. if you post your code here it is much easier to help you.
jørn
jorn bjarning
cswp
cad & plm consultant
sw2008 sp5 / sw2009 sp2
1. as jorn suggested, posting your code here may help.
2. what sort of objects are you trying to pass into the function? sounds like you need a face2 object and a component2 object.
3. if you are looking for a perpendicular distance from a planar face to your component you will likely have to mess around with transforms. closestdistance will give you the closest absolute distance. if your component is 1" away perpendicular to the plane of the face and 1" outside the boundary of the face then closestdistance will report 1.4142.... (square root of 2) as the closest distance. of course, if your part is cylindrical and its axis is parallel to the planar face but it lies outside the face boundary then closestdistance will measure to some point somewhere on the cylinder and you really won't be able to calculate the normal distance very well at all.
if you are using sw 2008 then the measure object/tool may work better for you than closestdistance.
i'll get you eh steve, if it's the last thing i dooooo!
i didn't know there were examples, where can i see these examples?
ben,
look up modeldoc2.closestdistance in the api help. on top of the page you will see links called 'see also', 'example' and 'availability'
jorn bjarning
cswp
cad & plm consultant
sw2008 sp5 / sw2009 sp2
thank you very much, will study examples and sample the code i need.
okay, so i'm dumber then i thought... what i need to do is measure the distance between two objects in an assembly generated by driveworks express. the basic principle is that if the distance is less than 1 inch (to account for sag across a 40+ foot span), then the model will collide. the surfaces or components need to be pre-selected not by the operator, but by the macro (my boss wants this nearly idiot proof, 1 button, no possibility for error). then it spits up a msgbox with either a warning, or a clearance. my code is below, basically ripped off the whole code from the examples. it works, except for the fact that the distance reported is always wrong. optimally i'd pass 2 components into the code so that it doesn't matter what surface is closest. i realize that any half intelligent trained monkey can use the "measure" tool, but my boss doesn't see it that way. thank you for your time.
edit : removed enum
sub main()
dim swapp as sldworks.sldworks
dim swmodel as sldworks.modeldoc2
dim swselmgr as sldworks.selectionmgr
dim swselobj1 as object
dim swselobj2 as object
dim vpt1 as variant
dim vpt2 as variant
dim ndist as double
dim swskpoint1 as sldworks.sketchpoint
dim swskpoint2 as sldworks.sketchpoint
dim swskline as sldworks.sketchline
dim bret as boolean
set swapp = application.sldworks
set swmodel = swapp.activedoc
set swselmgr = swmodel.selectionmanager
set swselobj1 = swselmgr.getselectedobject5(1)
set swselobj2 = swselmgr.getselectedobject5(2)
ndist = swmodel.closestdistance(swselobj1, swselobj2, vpt1, vpt2)
debug.assert ndist > 0#
debug.assert not isempty(vpt1)
debug.assert not isempty(vpt2)
debug.print "file = " & swmodel.getpathname
debug.print " seltype1 = " & swselmgr.getselectedobjecttype2(1)
debug.print " seltype2 = " & swselmgr.getselectedobjecttype2(2)
debug.print " pt1 = (" & vpt1(0) * 1000# & ", " & vpt1(1) * 1000# & ", " & vpt1(2) * 1000# & ") in"
debug.print " pt2 = (" & vpt2(0) * 1000# & ", " & vpt2(1) * 1000# & ", " & vpt2(2) * 1000# & ") in"
debug.print " dist = " & ndist * 1000# & " in"
swmodel.setaddtodb true
swmodel.insert3dsketch2 false
dim msg, style, title, help, ctxt, response, mystring
if ndist < 1 then
msg = "warning bottom truss will hit, clearance is "
style = vbokonly + vbcritical + vbdefaultbutton2 ' define buttons.
title = "insufficient clearance" ' define title.
help = "demo.hlp" ' define help file.
ctxt = 1000 ' define topic
' context.
' display message.
else
msg = "bottom truss clears by "
style = vbokonly + vbcritical + vbdefaultbutton2 ' define buttons.
title = "insufficient clearance" ' define title.
help = "demo.hlp" ' define help file.
ctxt = 1000 ' define topic
end if
response = msgbox(msg & ndist, style, title, help, ctxt)
set swskpoint1 = swmodel.createpoint2(vpt1(0), vpt1(1), vpt1(2))
set swskpoint2 = swmodel.createpoint2(vpt2(0), vpt2(1), vpt2(2))
set swskline = swmodel.createline2(vpt1(0), vpt1(1), vpt1(2), vpt2(0), vpt2(1), vpt2(2))
swmodel.setaddtodb false
swmodel.insert3dsketch2 true
end sub
'-----------------
edited: 03/17/2009 at 02:01 pm by ben reilly
how far off is your distance? is it off by something like a factor of 25.4 maybe?
i'll get you eh steve, if it's the last thing i dooooo!
okay, so the last two i ran was off by a factor of 1/25.4 * 1000, so somehow a conversion went horribly wrong. i just stuck a conversion factor into the code at ndist = swmodel.closestdistance(swselobj1, swselobj2, vpt1, vpt2) * 1/25.4*1000. it's a bandaid, but it fixes it.
somewhere there must be a metric conversion... i see a conversion from meter to mm, so i'm guessing that closestdistance automatically calls in a meter distance, hence the conversion problem.
now, anybody know how to stuff objects (solid bodies) into closestdistance instead of surfaces?
i really appreciate the help you guys have been giving such a noob... or should that be boob?
edited: 03/15/2009 at 02:40 pm by ben reilly
i think i figured it out, it takes components the way the code is
one more problem, i can get it to take components and i can get it to take surfaces, but closest.distance doesn't seem to like bodyfeatures? my problem code is below, i'll include the entire macro so you can see the whole thing, but the problem is when i try to measure the distance between body features. (look for 'set ut dist, ttdist, or btdist, all of these do not calculate, but do not throw an error.)
edit: removed enum
sub main()
dim swapp as sldworks.sldworks
dim swmodel as sldworks.modeldoc2
dim swselmgr as sldworks.selectionmgr
dim swselobj1 as object
dim swselobj2 as object
dim vpt1 as variant
dim vpt2 as variant
dim ndist as double
dim swskpoint1 as sldworks.sketchpoint
dim swskpoint2 as sldworks.sketchpoint
dim swskline as sldworks.sketchline
dim bret as boolean
dim bdoorframejmem as string
dim bdoorframeimem as string
dim udoorframeimem as string
dim bdoorframedmem as string
dim udoorframedmem as string
dim utdist as double 'upper frame to truss distance
dim btdist as double 'bottom frame to truss distance
dim ttdist as double 'truss to truss distance
set swapp = application.sldworks
set swmodel = swapp.activedoc
set swselmgr = swmodel.selectionmanager
dim vresarray as variant
dim arraybound as integer
dim titlestr as string, ordernumber as string
titlestr = swmodel.gettitle() 'get title
' msgbox titlestr 'for debugging
vresarray = split(titlestr, "-") 'separate at dash
arraybound = ubound(vresarray)
titlestr = vresarray(arraybound) 'set title to second part
vresarray = split(titlestr, ".") 'separate extension from title
ordernumber = vresarray(0) 'set ordernumber as number
dim bdoorframe as string, bcord as string, udoorframe as string
bdoorframe = "bottom door frame -" + ordernumber + "-1@driveworks bifold master -" + ordernumber
bcord = "bottom cord -" + ordernumber + "-1@driveworks bifold master -" + ordernumber
yang686526离线中   回复时引用此帖
GDT自动化论坛(仅游客可见)
回复


主题工具 搜索本主题
搜索本主题:

高级搜索
显示模式

发帖规则
不可以发表新主题
不可以回复主题
不可以上传附件
不可以编辑您的帖子

vB 代码开启
[IMG]代码开启
HTML代码关闭



所有的时间均为北京时间。 现在的时间是 08:26 PM.


于2004年创办,几何尺寸与公差论坛"致力于产品几何量公差标准GD&T | GPS研究/CAD设计/CAM加工/CMM测量"。免责声明:论坛严禁发布色情反动言论及有关违反国家法律法规内容!情节严重者提供其IP,并配合相关部门进行严厉查处,若內容有涉及侵权,请立即联系我们QQ:44671734。注:此论坛须管理员验证方可发帖。
沪ICP备06057009号-2
更多