几何尺寸与公差论坛------致力于产品几何量公差标准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-13, 09:00 AM   #1
yang686526
高级会员
 
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
yang686526 向着好的方向发展
默认 【转帖】api to delete all entities on a particular layer

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
yang686526离线中   回复时引用此帖
GDT自动化论坛(仅游客可见)
回复


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

高级搜索
显示模式

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

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

相似的主题
主题 主题发起者 论坛 回复 最后发表
【转帖】layer macro yang686526 SolidWorks二次开发 0 2009-04-12 09:33 PM
C++风格与技巧(转) huangyhg vc编程 0 2008-10-27 11:03 AM
【转帖】C++深度探索系列:智能指针(Smart Pointer) [一] huangyhg vc编程 1 2007-11-21 02:21 PM
C++风格与技巧 Faq huangyhg vc编程 0 2007-10-29 02:24 PM


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


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