几何尺寸与公差论坛

 找回密码
 注册
查看: 1209|回复: 0

【转帖】macro to delete parts from assembliessub-assemblies

[复制链接]
发表于 2009-4-12 18:55:03 | 显示全部楼层 |阅读模式
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
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-22 19:32 , Processed in 0.037115 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表