![]() |
【转帖】inserting and mating parts in an assembly
inserting and mating parts in an assembly
pre-select the componets in the assembly. then have the macro loop through each componet selected searching for specific hole wizard features then when found insert and mate a pin to each... i got to the part to where i need to get the face or loop thru faces in the hole wizard feature. dim swapp as sldworks.sldworks dim swcompmodel as sldworks.modeldoc2 dim swmodel as sldworks.modeldoc2 dim swselmgr as sldworks.selectionmgr dim swcomp as sldworks.component dim swpart as sldworks.partdoc dim temp as string dim part as object dim thefeature as object dim featcount as long dim featname as string dim chk as string dim count as integer dim swcompname as object dim face1 as object dim face2 as object sub main() set swapp = application.sldworks set swmodel = swapp.activedoc set swcompmodel = swapp.activedoc set swselmgr = swmodel.selectionmanager for j = 1 to swselmgr.getselectedobjectcount count = 0 temp = swselmgr.getselectedobjecttype2(j) select case temp case 20 ' assembly model open body selected set swcomp = swselmgr.getselectedobjectscomponent2(j) set swmodel = swcomp.getmodeldoc featcount = swmodel.getfeaturecount for i = featcount to 1 step -1 set thefeature = swmodel.featurebypositionreverse(featcount - i) featname = thefeature.name chk = left(featname, 5) if chk = "cbore" then part.addcomponent "c:\sw_macrolib\xxxx-a-000_shcs.sldprt", 1, 1, 1 'not sure where to go from here.... count = count + 1 msgbox "found =" & featname end if set thefeature = nothing 'swapp.sendmsgtouser "feature " + str(i) + " is " + featname next i msgbox "number cbore's found =" & count case else msgbox ("incorrect type selected " & temp) goto skip end select skip: temp = 0 'swmodel.clearselection2 (true) next j end sub and now? what do you want mate? option explicit sub main() dim swapp as sldworks.sldworks dim swmodel as sldworks.modeldoc2 dim swcompmodel as sldworks.modeldoc2 dim swassy as sldworks.assemblydoc dim swpin as sldworks.component2 dim swselmgr as sldworks.selectionmgr dim swcomp as sldworks.component dim thefeature as sldworks.feature dim featcount as long dim i as integer dim j as integer set swapp = application.sldworks set swmodel = swapp.activedoc if swmodel.gettype = swdocassembly then set swassy = swmodel else msgbox "is not assembly" exit sub end if set swselmgr = swmodel.selectionmanager for j = 1 to swselmgr.getselectedobjectcount select case swselmgr.getselectedobjecttype2(j) case 20 ' assembly model open body selected set swcomp = swselmgr.getselectedobjectscomponent2(j) set swcompmodel = swcomp.getmodeldoc featcount = swcompmodel.getfeaturecount for i = featcount to 1 step -1 set thefeature = swcompmodel.featurebypositionreverse(featcount - i) select case ucase(thefeature.gettypename2) case "holewzd" swapp.opendocsilent "c:\sw_macrolib\xxxx-a-000_shcs.sldprt", swdocpart, 0 set swpin = swassy.addcomponent4("c:\sw_macrolib\xxxx-a-000_shcs.sldprt", "", 0, 0, 0) swapp.closedoc "c:\sw_macrolib\xxxx-a-000_shcs.sldprt" msgbox "found =" & thefeature.name case else end select next i case else goto skip end select skip: next j end sub thanks for the input, i've added a few of the changes you gave me. then tested the macro and inserted the fasteners just fine if only one componet is selected in the assemby. seems to be loosing the selected componets after the fastners are added to the assembly for the first componet selected. i really appreciate the help.... dim swapp as sldworks.sldworks dim swselmgr as sldworks.selectionmgr dim swmodel as sldworks.modeldoc2 dim swassy as sldworks.assemblydoc dim jmodel as sldworks.modeldoc2 dim jcomp as sldworks.component2 dim jtemp as string dim j as integer dim feat as object dim featname as string dim featchk as string dim featcount as integer dim featfound as integer dim i as integer dim part as object dim strblockpartpath as string dim bolstatus as boolean dim face as object dim assydoc as object dim swcomponet as sldworks.component2 dim strcomponet as string dim firstmate as string dim secondmate as string dim assemblyname as string dim assemblytitle as string dim strings as variant option explicit sub main() set swapp = application.sldworks set swmodel = swapp.activedoc if swmodel.gettype = swdocassembly then set swassy = swmodel else msgbox "is not assembly" exit sub end if set swselmgr = swmodel.selectionmanager assemblytitle = swmodel.gettitle strings = split(assemblytitle, ".") assemblyname = strings(0) strblockpartpath = "c:\sw_macrolib\xxxx-a-000_shcs.sldprt" for j = 1 to swselmgr.getselectedobjectcount jtemp = swselmgr.getselectedobjecttype2(j) select case jtemp case 20 ' assembly model open body selected set jcomp = swselmgr.getselectedobjectscomponent3(j, -1) set jmodel = jcomp.getmodeldoc featfound = 0 featcount = jmodel.getfeaturecount - 1 for i = featcount to 1 step -1 set feat = jmodel.featurebypositionreverse(featcount - i) featname = feat.name featchk = left(featname, 5) if featchk = "cbore" then ' finds only counter bored holes ' get face of componet in model ' end get face of componet in model ' insert screw 'set part = swapp.activedoc 'set swcomponet = part.addcomponent4(strblockpartpath, "shcs 3_8-16x1.25", -1.1, 1.1, 1.1) swapp.opendocsilent strblockpartpath, swdocpart, 0 set swcomponet = swassy.addcomponent4(strblockpartpath, "shcs 3_8-16x1.25", -1.1, 1.1, 1.1) swapp.closedoc strblockpartpath strcomponet = swcomponet.name2() ' end insert screw ' create mate face 'secondmate = "fc1@" + strcomponet & "@" + assemblyname 'name of face for mate - fc1 is name of face for screw ' end mate face featfound = featfound + 1 msgbox "found =" & featname end if set feat = nothing next i msgbox "number cbore's found =" & featfound case else msgbox ("incorrect type selected " & jtemp) ' must select componet in assembly goto skip end select skip: jtemp = 0 'jmodel.clearselection2 (true) next j end sub ok - i had a few successes and setbacks. the macro searches the selected componet in the assembly for cbored features then loops thru each turning the face blue that i want to mate the screw with. then inserts the screw and performs the same action turning the face blue that i want to mate. i can't figure out how to select the faces (which are blue) then mate them. i've tried many different combinations with no sucess. what am i doing wrong? (this online forum is wonderful) - thanks in advance dim swapp as sldworks.sldworks dim swselmgr as sldworks.selectionmgr dim swmodel as modeldoc2 dim swassy as sldworks.assemblydoc dim jmodel as sldworks.modeldoc2 dim jcomp as sldworks.component2 dim jcompre as sldworks.component2 dim jtemp as string dim jname as string dim j as integer dim jfeat as object dim jfeatname as string dim jfeatchk as string dim jfeatcount as integer dim jfeatfound as integer dim i as integer dim jfeatcolor as variant dim jfacearr as variant dim jface as sldworks.face2 dim jent as sldworks.entity dim jfacefeat as sldworks.feature dim jseldata as sldworks.selectdata dim f as integer dim ft as integer dim smodel as sldworks.modeldoc2 dim scomp as sldworks.component2 dim scompre as sldworks.component2 dim stemp as string dim sname as string dim s as integer dim sfeat as object dim sfeatname as string dim sfeatchk as string dim sfeatcount as integer dim sfeatfound as integer dim sfeatcolor as variant dim sfacearr as variant dim sface as sldworks.face2 dim sfaceid as object dim sent as sldworks.entity dim sfacefeat as sldworks.feature dim sseldata as sldworks.selectdata dim g as integer dim gt as integer dim strblockpartpath as string dim bret as boolean dim asmate as sldworks.feature dim errorstatus as long dim firstmate as string dim sfirst as sldworks.feature dim secondmate as string dim assemblyname as string dim assemblytitle as string dim strings as variant dim swdocext as modeldocextension option explicit sub main() set swapp = application.sldworks set swmodel = swapp.activedoc if swmodel.gettype = swdocassembly then set swassy = swmodel else msgbox "is not assembly" exit sub end if set swselmgr = swmodel.selectionmanager assemblytitle = swmodel.gettitle strings = split(assemblytitle, ".") assemblyname = strings(0) strblockpartpath = "c:\sw_macrolib\xxxx-a-000_shcs.sldprt" for j = 1 to swselmgr.getselectedobjectcount jtemp = swselmgr.getselectedobjecttype2(j) select case jtemp case 20 ' assembly model open body selected set jcomp = swselmgr.getselectedobjectscomponent3(j, -1) set jmodel = jcomp.getmodeldoc jname = jcomp.name2 jfeatfound = 0 jfeatcount = jmodel.getfeaturecount - 1 for i = jfeatcount to 1 step -1 set jfeat = jmodel.featurebypositionreverse(jfeatcount - i) jfeatname = jfeat.name jfeatchk = left(jfeatname, 5) if jfeatchk = "cbore" then ' find only counter bored holes ' get face of componet in model jfeatcolor = jmodel.materialpropertyvalues jfeatcolor(0) = 0 'r jfeatcolor(1) = 0 'g jfeatcolor(2) = 1 'b jfacearr = jfeat.getfaces if isempty(jfacearr) then exit sub ft = jfeat.getfacecount - 1 for f = 0 to ft step 3 set jface = jfacearr(f) set jent = jface set jfacefeat = jface.getfeature ' check to see if face is owned by multiple features if jfacefeat is jfeat then firstmate = "face<1>@" + jname & "@" + assemblyname set jseldata = jmodel.selectionmanager.createselectdata jseldata.mark = 1 jface.materialpropertyvalues = jfeatcolor ' insert screw swapp.opendocsilent strblockpartpath, swdocpart, 0 set scomp = swassy.addcomponent4(strblockpartpath, "shcs 3_8-16x1.25", -1.1, 1.1, 1.1) swapp.closedoc strblockpartpath set smodel = scomp.getmodeldoc sname = scomp.name2 sfeatfound = 0 sfeatcount = smodel.getfeaturecount - 1 for s = sfeatcount to 1 step -1 set sfeat = smodel.featurebypositionreverse(sfeatcount - s) sfeatname = sfeat.name if sfeatname = "base-revolve" then ' find only revolved sfeatcolor = smodel.materialpropertyvalues sfeatcolor(0) = 0 'r sfeatcolor(1) = 0 'g sfeatcolor(2) = 1 'b sfacearr = sfeat.getfaces if isempty(sfacearr) then exit sub gt = sfeat.getfacecount - 1 'for g = 0 to gt step 3 g = 5 set sface = sfacearr(g) set sent = sface set sfacefeat = sface.getfeature if sfacefeat is sfeat then set sseldata = smodel.selectionmanager.createselectdata sseldata.mark = 1 sface.materialpropertyvalues = sfeatcolor end if 'next g end if next s 'secondmate = "fc1@" + sname & "@" + assemblyname 'name of face for mate - fc1 is name of face for screw ' create mate face set swdocext = swmodel.extension swmodel.clearselection2 true bret = jface.select(0) bret = sface.select(1) set asmate = swassy.addmate3(swmateconcentric, swmatealignaligned, false, 0, 0, 0, 0, 0, 0, 0, 0, false, errorstatus) ' end mate face ' end insert screw else debug.print " other feature = " & jfacefeat.name + " [" + jfacefeat.gettypename + "]" end if next f ' end get face of componet in model jfeatfound = jfeatfound + 1 'msgbox "found =" & jfeatname end if set jfeat = nothing next i msgbox "number cbore's found =" & jfeatfound case else msgbox ("incorrect type selected " & jtemp) ' must select componet in assembly goto skip end select skip: jtemp = 0 'jmodel.clearselection2 (true) next j swassy.editrebuild end sub quick |
所有的时间均为北京时间。 现在的时间是 10:31 PM. |