几何尺寸与公差论坛------致力于产品几何量公差标准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, 06:55 PM   #1
yang686526
高级会员
 
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
yang686526 向着好的方向发展
默认 【转帖】macro to delete parts from assembliessub-assemblies

macro to delete parts from assemblies/sub-assemblies
does anyone know how to get the component name (as a string) of an actively selected component in an assembly? i'm trying to write a macro that eliminates certian hardware parts from several assemblies. this macro will be in heavy use for years becuase it is part of a new in house process.
i can currently store a list of the hardware i don't want on an excel document and load that list in to solidworks. i can also open and save off all of the documents that need this edit and delete the unwanted parts, but what i cannot do is delete the hardware from a sub-assembly. with this in mind, if i could traverse through the assembly and find all of the sub assemblies and run them first, then i could complete the macro after a bit more work.
currently i am testing a small macro that origonally fixed all of the components in an assembly. it selects the parts, but i cannot get any code to help me get the names. most of them say that it's the wrong function.
anyway i would greatly appreciate any help on this one.
i could also use some help in determining if a component of an assembly is a sub assembly or not.
sw 2009
pdm workgroup
3d connexion user (space pilot)
bluebeam
mdt & acad user
furniture industry 5 years
hp workstation xw6400
what are you trying to get? the name of the component as displayed in the feature tree? the filename? perhaps if you could post your code we can assist further.
i'll get you eh steve, if it's the last thing i dooooo!
is this what your looking for?
i left out four lines of code. see my message below for the correct code. i copied this code from a program i wrote a while back and the string "filetitle" was retrieved in another part of the code.
dan miel
edited: 01/07/2009 at 07:26 pm by dan miel
hi,
here is a quick way to find the sub assemblies.
dim swapp as sldworks.sldworks
dim part as sldworks.modeldoc2
dim assem as sldworks.assemblydoc
dim allcomps as variant
dim thecomp as component2
sub main()
set swapp = application.sldworks
set part = swapp.activedoc
set assem = part
allcomps = assem.getcomponents(false)
for x = 0 to ubound(allcomps)
set thecomp = allcomps(x)
if thecomp.getpathname like "*.sldasm" then
msgbox thecomp.getpathname
end if
next x
end sub
i hate it when i post the wrong code! i added four lines to the code and marked the lines i added. after running this program the string comppath can be used to select the component as in the next line.
boolstatus = part.extension.selectbyid2(comppath "component", 0.02138278787885, 1.253236, -0.2309433426609, false, 0, nothing, 0)
'written dan miel jan 7, 08
'this will return the component.name to a string that can be used to
'select a part by using "selectbyid2".
'to select a component in the tree use "getselectedobject6"
'to select a component in the graphics window use getselectedobjectscomponent3
dim swapp as sldworks.sldworks
dim part as sldworks.modeldoc2
dim comp as sldworks.component2
dim selmgr as sldworks.selectionmgr
dim comppath as string
sub main()
set swapp = application.sldworks
set part = swapp.activedoc
set selmgr = part.selectionmanager
'set comp = selmgr.getselectedobject6(1, -1)' this will select part in tree
set comp = selmgr.getselectedobjectscomponent3(1, -1) 'select the face or edge of a part in the window
comppath = getcompstring(comp.name)
debug.print comppath
end sub
function getcompstring(byval comppath as string)
dim v as variant
dim ti as integer
dim i as integer
dim inassm as string
dim loops as integer
dim pathstring as string
'added next four lines when i edited
dim filetitle as string
filetitle = part.gettitle
i = instr(1, filetitle, ".") - 1
if i > 0 then
filetitle = left(filetitle, i)
end if
'splits comp name into seperate strings
v = split(comppath, "/")
pathstring = v(0) & "@" & filetitle
'reworks componet name to string needed
for ti = 0 to ubound(v) - 1
inassm = v(ti)
inassm = left(inassm, instrrev(inassm, "-") - 1)
pathstring = pathstring & "/" & v(ti + 1) & "@" & inassm
next ti
getcompstring = pathstring
'returns string in this format
thank you dan, i was pulled off of this project temoprarily, but the code you sent seemed to work fine. i assume it will work with the larger code which opens each assembly and selects the included parts, i will do more testing soon. is it safe to assume that this will only identify sub-assemblies? that's what it seemed to do and what i need, but i wanted to ask
sw 2009
pdm workgroup
3d connexion user (space pilot)
bluebeam
mdt & acad user
furniture industry 5 years
hp workstation xw6400
adam
it should work with any component whether it is an assembly or sub assembly. this code is the same as above but i added "select by id" and changed the debug line to print whether the component is selected. true is selected, false is not selected.
when using the "getselectedobjectscomponent" you can pick a surface of a component in the window and after the code is ran the entire component will change color to show that it is selected. let me know if that is not what you see.
dan miel
'written dan miel jan 7, 08
'this will return the component.name to a string that can be used to
'select a part by using "selectbyid2".
'to select a component in the tree use "getselectedobject6"
'to select a component in the graphics window use getselectedobjectscomponent3
dim swapp as sldworks.sldworks
dim part as sldworks.modeldoc2
dim comp as sldworks.component2
dim selmgr as sldworks.selectionmgr
dim comppath as string
dim bret as boolean
sub main()
set swapp = application.sldworks
set part = swapp.activedoc
set selmgr = part.selectionmanager
'set comp = selmgr.getselectedobject6(1, -1)' this will select part in tree
set comp = selmgr.getselectedobjectscomponent3(1, -1) 'select the face or edge of a part in the window
comppath = getcompstring(comp.name)
'added this to select part and print if part is selected
' this gets the selected component
bret = part.extension.selectbyid2(comppath, "component", 0, 0, 0, false, 0, nothing, 0)
'this prints the component path and whether the component was selected
debug.print comppath & " " & bret
end sub
function getcompstring(byval comppath as string)
dim v as variant
dim ti as integer
dim i as integer
dim inassm as string
dim loops as integer
dim pathstring as string
'added next four lines when i edited
dim filetitle as string
filetitle = part.gettitle
i = instr(1, filetitle, ".") - 1
if i > 0 then
filetitle = left(filetitle, i)
end if
'splits comp name into seperate strings
v = split(comppath, "/")
pathstring = v(0) & "@" & filetitle
'reworks componet name to string needed
for ti = 0 to ubound(v) - 1
inassm = v(ti)
inassm = left(inassm, instrrev(inassm, "-") - 1)
pathstring = pathstring & "/" & v(ti + 1) & "@" & inassm
next ti
getcompstring = pathstring
'returns string in this format
hi dan,
i wish i would have found this earlier, could have really used it . see the thread i created. boy i feel sheepish...lol
cadcam systems analyst
-solidworks office premium 2009 sp3.0
-solidworks simulation premium 2009 sp3.0
-solidworks flow simulation 2009 sp3.0
-2 cpu (fx-62), 2.0 gb of ram
-window xp pro sp2
-nvidia geforce 7950 gx2 (512mb) 6.14.11.6921
quick
yang686526离线中   回复时引用此帖
GDT自动化论坛(仅游客可见)
 


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

高级搜索
显示模式

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

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

相似的主题
主题 主题发起者 论坛 回复 最后发表
【转帖】iterating through sub-assemblies yang686526 SolidWorks二次开发 0 2009-04-12 06:46 PM
C++风格与技巧(转) huangyhg vc编程 0 2008-10-27 11:03 AM
C++风格与技巧 Faq huangyhg vc编程 0 2007-10-29 02:24 PM


所有的时间均为北京时间。 现在的时间是 05:30 AM.


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