![]() |
【转帖】editing blocks per drawing shee
editing blocks per drawing sheet?
i have a question. i have several drawing document sheets that have the same block instance on them. what i have is a macro that will update/replace them but it doesn't do it for just that sheet, it does it for all and if replacing them puts them all on the active sheet you run the macro against. the question, is how do you get the macro to focus and change only the blocks on that sheet and leave the rest unchanged on the following sheets? i've played with the focuslocked without luck. maybe i'm not understanding how it works or need to try something different. i've also tried looping through the features in the document, looking at the drawing sheets only method with no luck either. so any pointers or samples would be much appreciated. here is the array of info i'm gathering from the blocks to use for replacement, but it gathers it from all sheets: -----see message below----- 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 edited: 09/11/2008 at 05:17 pm by william crosby ok, here's what i have for a working copy to detect with, but still have the issue of it picking up all block instances regardless of what sheet the routine runs on. only want the block instances for that sheet. change the const blkitem1 = "block1" to whatever block name you create. '----------code--------------- option explicit dim swapp as sldworks.sldworks ' solidworks application dim swmodel as sldworks.modeldoc2 ' layout object access dim swdraw as sldworks.drawingdoc ' drawing layout object access dim swsketchmgr as sldworks.sketchmanager ' sketch manager object access dim swselmgr as sldworks.selectionmgr ' selection manager object access dim swblockinst as sldworks.sketchblockinstance ' delcare block instance dim swblockdef as sldworks.sketchblockdefinition ' declare block definition dim swfeat as sldworks.feature ' declare feature objects dim swsheet as sldworks.sheet ' sheet object access dim swview as sldworks.view ' view object access const blkitem1 = "block1" ' define name of block dim vblockdefs as variant, vblockinsts as variant dim vblockdefcount as long, vblockinstcount as long dim blocksipnt() as variant ' capture block insertion point dim blocksrot() as variant ' capture block rotation dim blocksscl() as variant ' capture block scale dim blkexist as boolean ' declare true or false for block operations sub checkforblock() on error resume next set swapp = application.sldworks set swmodel = swapp.activedoc set swsketchmgr = swmodel.sketchmanager set swselmgr = swmodel.selectionmanager blkexist = false set swdraw = swmodel set swview = swdraw.getfirstview do while not swview is nothing debug.print "sheet name = " & swview.name vblockdefs = swsketchmgr.getsketchblockdefinitions ' get block definitions debug.print "block definition count = " & swsketchmgr.getsketchblockdefinitioncount if not isempty(vblockdefs) then ' else traverse block definitions for vblockdefcount = lbound(vblockdefs) to ubound(vblockdefs) ' loop block definition references set swblockdef = vblockdefs(vblockdefcount) set swfeat = swblockdef.getfeature if swfeat.name = blkitem1 then ' verify block name debug.print "block definition name = " & swfeat.name vblockinsts = swblockdef.getinstances if false = isempty(vblockinsts) then ' traverse block instances for vblockinstcount = lbound(vblockinsts) to ubound(vblockinsts) ' loop block instance references debug.print "block instance count = " & swblockdef.getinstancecount set swblockinst = vblockinsts(vblockinstcount) blkexist = swblockinst.select(false, nothing) set swblockinst = swselmgr.getselectedobject6(1, -1) debug.print "block instance name = " & swblockinst.name 'assign block insert point, scale & rotation elements to array redim preserve blocksipnt(vblockinstcount) as variant set blocksipnt(vblockinstcount) = swblockinst.instanceposition blocksipnt(vblockinstcount) = blocksipnt(vblockinstcount).arraydata ' block insertion point redim preserve blocksrot(vblockinstcount) as variant blocksrot(vblockinstcount) = swblockinst.angle ' block angle redim preserve blocksscl(vblockinstcount) as variant blocksscl(vblockinstcount) = swblockinst.scale2 ' block scale swmodel.clearselection2 true ' clear selection next vblockinstcount end if end if next vblockdefcount end if set swview = swview.getnextview loop if blkexist = false then ' block still doesn't exist notify user msgbox "warning! there are no block instances in this document tab", vbexclamation, "no block instance found" elseif blkexist = true then ' if block exist then do something to it msgbox "got those values you wanted, now what?" end if end sub '------- did some code refinement ^ 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 edited: 09/11/2008 at 06:00 pm by william crosby answer finally got it. this is it and works flawlessly. if you want to use it you need to declare your objects and vars but you get the logic. '-------the code------------- private sub checkforblock() on error resume next set swapp = application.sldworks set swmodel = swapp.activedoc set swsketchmgr = swmodel.sketchmanager set swselmgr = swmodel.selectionmanager set swdraw = swmodel blkexist = false ' clear selections blockval = swselmgr.deselect2(1, -1) swmodel.clearselection2 true vblockdefs = swsketchmgr.getsketchblockdefinitions ' get block definitions if not isempty(vblockdefs) then ' traverse block definitions for blockdefcount = lbound(vblockdefs) to ubound(vblockdefs) ' loop block definitions set swblockdef = vblockdefs(blockdefcount) set swfeat = swblockdef.getfeature if swfeat.name = blkitem1 then ' verify block name set swsheet = swdraw.getcurrentsheet viewnames = swsheet.getviews set swview = swdraw.getfirstview set swsketch = swview.getsketch vblockinsts = swsketch.getsketchblockinstances if false = isempty(vblockinsts) then ' traverse block instances for blockinstcount = lbound(vblockinsts) to ubound(vblockinsts) ' loop block instances set swblockinst = vblockinsts(blockinstcount) if swblockinst.name like blkitem1 & "*" then ' check the block instance name blockval = swblockinst.select(true, nothing) blkexist = true set swblockinst = swselmgr.getselectedobject6(1, -1) 'assign block insert point, scale & rotation elements to array redim preserve blocksipnt(blockinstcount) as variant set blocksipnt(blockinstcount) = swblockinst.instanceposition blocksipnt(blockinstcount) = blocksipnt(blockinstcount).arraydata ' block insertion point redim preserve blocksrot(blockinstcount) as variant blocksrot(blockinstcount) = swblockinst.angle ' block angle redim preserve blocksscl(blockinstcount) as variant blocksscl(blockinstcount) = swblockinst.scale2 ' block scale swmodel.clearselection2 true ' clear selection end if next blockinstcount end if end if next blockdefcount end if if blkexist = false then ' block still doesn't exist notify user msgbox "warning! there are no block instances in this document tab", vbexclamation, "no block instance found" elseif blkexist = true then ' if block exist then do something to it msgbox "got those values you wanted, now what?" end if end sub '---------- added one more step to verify the instance on the face of the drawing document not just the feature tree 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 edited: 09/16/2008 at 03:08 pm by william crosby quick |
所有的时间均为北京时间。 现在的时间是 01:53 AM. |