几何尺寸与公差论坛

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

【转帖】api to delete all entities on a particular layer

[复制链接]
发表于 2009-4-13 09:00:20 | 显示全部楼层 |阅读模式
api to delete all entities on a particular layer
i am writing a macro to remove proprietary information from our drawings before we send them on to our customers.
my steps are as follows:
1. turn all layers off, except the ones to delete.
2. select all entites remaining through this code.
dim swentity as sldworks.entity
dim swent as sldworks.entity
dim selmgr as sldworks.selection manager
do while not swentity is nothing
swent = swentity.select4(true,selmgr)
swentity.delete
swentity=swentity.getnext
loop
i cannot get the "select4" to work.
3. turn remaining layers back on.
i have steps one and three down, but i am having problems with step 2.
thanks for the help
bama
what errors are you gettings? is it coming up with a "with or variable not defined", is it returning "nothing", etc...?
also, you need to change your select4 statement to:
swent = swentity.select4(false,selmgr)
else you will be adding to your selection constantly
the error is "value of type 'boolean' cannot be converted to 'solidworks..interop.sldworks.entity'. here is my complete code for reference.
imports solidworks
imports solidworks.interop
module module1
dim swapp as sldworks.sldworks
dim swmodel as sldworks.modeldoc2
dim swdrawing as sldworks.drawingdoc
dim swdimension as sldworks.dimension
dim swentity as sldworks.entity
dim retval as string
dim swcustominfonumber as string
dim swdraw as sldworks.drawingdoc
dim swlayermgr as sldworks.layermgr
dim swlayer as sldworks.layer
dim vlayerarr as object
dim vlayer as object
dim swview as sldworks.view
dim swactiveview as sldworks.view
dim viewvisible as boolean
dim isoviewcounter as integer
dim viewname as string
dim bret as boolean
dim swent as sldworks.entity 'swent is solidworks entity
dim swselmgr as sldworks.selectionmgr
dim swnote as sldworks.note
dim swann as sldworks.annotation
sub main()
swapp = getobject(, "sldworks.application")
'swapp = createobject("sldworks.application")
swmodel = swapp.activedoc
swdrawing = swapp.activedoc
form1.close()
swlayermgr = swmodel.getlayermanager
vlayerarr = swlayermgr.getlayerlist
for each vlayer in vlayerarr
swlayer = swlayermgr.getlayer(vlayer)
if swlayer.name = "250" then
swlayer.visible = false
elseif swlayer.name = "revisions" then
swlayer.visible = false
elseif swlayer.name = "tight tolerance" then
swlayer.visible = false
elseif swlayer.name = "256-blue" then
if swlayer.visible = false then
swlayer.visible = true
'else
'swlayer.visible = true
end if
elseif swlayer.name = "256" then
if swlayer.visible = false then
swlayer.visible = true
else
swlayer.visible = true
end if
elseif swlayer.name = "256-red" then
if swlayer.visible = false then
swlayer.visible = true
else
swlayer.visible = true
end if
else
swlayer.visible = false
end if
next
swmodel.clearselection()
do while not swentity is nothing
swent = swentity.select4(false, swselmgr)
swentity.delete()
swentity = swentity.getnext
loop
end sub
end module
firstly your main problem is that you never get the first entity of the drawing to start with, instead you get solidworks itself.
secondly, you have no need to select4 the entity once you have it.
thirdly, the selection returns true or false, not the selected object so you cannot set the entity by selecting it, and you have already set swent at the top, so remove that line completely.
and finally, there is no call for entity.getnext; an entity is the base class for nearly all sw objects so it isn't that simple; an assembly is an entity, a drawing is, and a part, so by trying to delete all entities you would fail to delete the drawing itself. also, entity.delete does not exist either.
effectively your entire loop is invalid. you are much better off being more specific and looping features not entities, or finding out exactly what it is you need to delete, because you need to be able to check the visibility of the objects also.
ok, now i have worked out the process for deleting annotations and dimensions. the code to do this is as follows. my next issue is to delete the sketch segments. what is the most efficient method to traverse the drawing and select these segments?
option explicit on
imports solidworks
imports solidworks.interop
module module1
dim swapp as sldworks.sldworks
dim swmodel as sldworks.modeldoc2
dim swdrawing as sldworks.drawingdoc
dim swview as sldworks.view
dim swdisplaydim as sldworks.displaydimension
dim swdimension as dimension
dim retval as string
dim swcustominfonumber as string
dim swlayermgr as sldworks.layermgr
dim swlayer as sldworks.layer
dim vlayerarr as object
dim vlayer as object
dim swactiveview as sldworks.view
dim viewvisible as boolean
dim isoviewcounter as integer
dim viewname as string
dim bret as boolean
dim swselmgr as sldworks.selectionmgr
dim swann as sldworks.annotation
sub main()
swapp = getobject(, "sldworks.application")
'swapp = createobject("sldworks.application")
swmodel = swapp.activedoc
swdrawing = swmodel
form1.close()
swmodel.clearselection2(true)
swview = swdrawing.getfirstview
' this section selects annotations on processing layers for deletion
while not swview is nothing
swann = swview.getfirstannotation2
while not swann is nothing
'if swann.gettype = 6 then
if swann.layer = "256" then
bret = swann.select2(true, 0)
end if
if swann.layer = "256-blue" then
bret = swann.select2(true, 0)
end if
if swann.layer = "256-red" then
bret = swann.select2(true, 0)
end if
if swann.layer = "256-black" then
bret = swann.select2(true, 0)
end if
'end if
swann = swann.getnext2
end while
' this section selects dimensions on processing layers for deletion
swdisplaydim = swview.getfirstdisplaydimension
while not swdisplaydim is nothing
swann = swdisplaydim.getannotation
if swann.layer = "256" then
bret = swann.select2(true, 0)
end if
if swann.layer = "256-blue" then
bret = swann.select2(true, 0)
end if
if swann.layer = "256-red" then
bret = swann.select2(true, 0)
end if
if swann.layer = "256-black" then
bret = swann.select2(true, 0)
end if
swdisplaydim = swdisplaydim.getnext5
end while
bret = swmodel.deleteselection(false)
swview = swview.getnextview
end while
end sub
end module
your best bet for drawings is to traverse the feature tree and delete based on type:
swtncentermark = "centermark"
swtndrsheet = "drsheet"
swtnabsoluteview = "absoluteview"
swtndetailview = "detailview"
swtnrelativeview = "relativeview"
swtnsectionpartview = "sectionpartview"
swtnsectionassemview = "sectionassemview"
swtnunfoldedview = "unfoldedview"
swtnauxiliaryview = "auxiliaryview"
swtndetailcircle = "detailcircle"
swtndrsectionline = "drsectionline"
swtnbomtablefeature = "bomfeat"
swtnholetablefeature = "holetablefeat"
swtnrevisiontablefeature = "revisiontablefeat"
swtnweldmenttablefeature = "weldmenttablefeat"
swtngeneraltableanchor = "generaltableanchor"
swtnholetableanchor = "holetableanchor"
swtnweldmenttableanchor = "weldmenttableanchor"
swtnrevisiontableanchor = "revisiontableanchor"
swtnbomtableanchor = "bomtemplate"
for example checking only for centermark items...:
set swfeat = swpart.firstfeature
do while not swfeat is nothing
if swfeat.gettypename2 = "centermark" then
swfeat.select false, -1
swpart.editdelete
end if
set swfeat = swfeat.getnextfeature
loop
obviously i haven't put any error checking in there.
luke, these are great, but they don't handle items that are part of the drawing such as lines and arcs that are sketched on the drawing and blocks
unfortunately not, but they cover some for you. to do what you are after you need to combine many loops, for features, for annotations, and for view items etc...
i remember seeing code somewhere that traversed a layer and deleted everything except images. i don't think it was this long.
matt lorono
solidworks 2007 sp3.1
cad engineer/ecn analyst
co-moderator of
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-12-24 08:53 , Processed in 0.039243 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

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