![]() |
【转帖】fixed par
fixed part
i'm trying to insert the first part into an assy, but the first part always comes in fixed which is understandable. but with the code i'm currently working on that after it inserts, floats the part so that it will rotate the part to a preselected plane using mate techniques. i know about fixcomponent but is there a float? here's my code, it's pretty cool. to use it create an assy file, save it somewhere then select a plane, like top or right, then run code. it will create a fake plane part at the zero origin of that plane using boundrybox elements, but at the moment doesn't rotate the fakeface part to it. doing this for a post point selection in null space (since no elements in 3d space will exist) to orient the first part once the program has started, also to orient a part in an existing assy using planes not faces. when completed i will code to remove the fakeface part from the assy. just need it for a selection point. 'option explicit dim swapp as sldworks.sldworks dim swmodel as sldworks.modeldoc2 dim swassy as sldworks.assemblydoc dim itemstatusb as boolean, itemstatusl as long dim swfeat as sldworks.feature dim swfeatmgr as sldworks.featuremanager dim swpart as sldworks.partdoc dim swrefplane as sldworks.refplane dim swmodeldocext as sldworks.modeldocextension dim swselmgr as sldworks.selectionmgr dim swsketchmgr as sldworks.sketchmanager dim swcomp as sldworks.component2 dim bndboxpnts as variant dim bndboxary as variant dim itemplane as variant dim itemplanename as string dim parttemplate as string dim i as integer dim swerrors as long, swwarnings as long, mateerror as long sub main() set swapp = application.sldworks set swmodel = swapp.activedoc assyfilename = swmodel.gettitle ' get assy file name assyname = (left$(assyfilename, len(assyfilename) - 7)) filepathnopartname = left(swmodel.getpathname, instrrev(swmodel.getpathname, "\")) ' get the assembly path only set swmodeldocext = swmodel.extension set swselmgr = swmodel.selectionmanager set swsketchmgr = swmodel.sketchmanager parttemplate = swapp.getuserpreferencestringvalue(swdefaulttemplatepart) set itemplane = swselmgr.getselectedobject6(1, -1) ' create temp plane set swfeat = swmodel.createplaneatoffset3(0, false, true) swfeat.name = "tempplane" itemplanename = swfeat.name itemstatusb = swmodeldocext.selectbyid2(itemplanename, "plane", 0, 0, 0, false, 0, nothing, 0) set swfeat = swselmgr.getselectedobject6(1, -1) set swrefplane = swfeat.getspecificfeature2 swmodel.clearselection2 (true) ' get bounding box off temp plane bndboxpnts = swrefplane.boundingbox '2 mathpoint objects are always returned ' assign highest bounding box elements to array for i = 0 to ubound(bndboxpnts) bndboxary = bndboxpnts(1).arraydata next i ' capture greatest values for plane creation if bndboxary(0) = 0 then if bndboxary(1) > bndboxary(2) then bndboxary(0) = bndboxary(1) else bndboxary(0) = bndboxary(2) end if elseif bndboxary(1) = 0 then if bndboxary(0) > bndboxary(2) then bndboxary(1) = bndboxary(0) else bndboxary(1) = bndboxary(2) end if elseif bndboxary(2) = 0 then if bndboxary(0) > bndboxary(1) then bndboxary(2) = bndboxary(0) else bndboxary(2) = bndboxary(1) end if end if ' create fake face set swpart = swapp.newdocument(parttemplate, 0, 0, 0) ' create new part file ' set fakeface file name set swmodel = swapp.activedoc swmodel.settitle2 ("fakeface") fakepartname = swmodel.gettitle fakepartfilename = fakepartname & ".sldprt" ' get and set plane for fakeface set swmodeldocext = swmodel.extension itemstatusb = swmodeldocext.selectbyid2("front plane", "plane", 0, 0, 0, false, 0, nothing, 0) set swselmgr = swmodel.selectionmanager set fakefaceplane = swselmgr.getselectedobject6(1, -1) ' create sketch for fakeface set swsketchmgr = swmodel.sketchmanager swsketchmgr.insertsketch (true) ' insert and extrude retang using assy bounding box array elements swsketchmgr.createcenterrectangle 0#, 0#, 0#, bndboxary(0), bndboxary(1), bndboxary(2) set swfeatmgr = swmodel.featuremanager swfeatmgr.featureextrusion2 true, true, true, 0, 0, 0.0001, 0, false, false, false, false, 0, 0, false, false, false, false, true, true, true, 0, 0, false swmodeldocext.saveas filepathnopartname & fakepartfilename, swsaveascurrentversion, swsaveasoptions_silent, nothing, swerrors, swwarnings ' go back to assy set swmodel = swapp.activatedoc2(assyfilename, true, swerrors) set swmodeldocext = swmodel.extension set swassy = swmodel ' insert new part into assembly set swcomp = swassy.addcomponent4(fakepartfilename, "", 0#, 0#, 0#) compname = swcomp.name2() swmodel.clearselection2 (true) firstselection = compname & "@" + assyname itemstatusb = swmodeldocext.selectbyid2(firstselection, "component", 0, 0, 0, false, 0, nothing, 0) swassy.fixcomponent ' <---- need to float comp for it to mate ' stop ' close fakeface part file swapp.closedoc (fakepartfilename) matename = "tempmate" firstselection = "front plane" + "@" + compname & "@" + assyname secondselection = itemplanename + "@" + assyname swmodel.clearselection2 (true) ' select the planes for the mate itemstatusb = swmodeldocext.selectbyid2(firstselection, "plane", 0, 0, 0, false, 0, nothing, 0) itemstatusb = swmodeldocext.selectbyid2(secondselection, "plane", 0, 0, 0, true, 0, nothing, 0) ' add the mate set swfeat = swassy.addmate3(swmatecoincident, swmatealignaligned, false, 0, 0, 0, 0, 0, 0, 0, 0, true, mateerror) swmodel.viewzoomtofit2 ' remove temp objects swmodel.clearselection2 (true) itemstatusb = swmodeldocext.selectbyid2(itemplanename, "plane", 0, 0, 0, false, 0, nothing, 0) itemstatusl = swmodeldocext.deleteselection2(0) end sub 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 answer assembly::unfixcomponent works for me. solidworks 2006,2007,2008,2009 (office premium.) core 2 duo e6850 @ 3.00 mhz window xp pro sp3 32 bit ati firegl v7350 ben, thank you very much!! here's the adjusted code for those wanting to do this. also made fakeface transparent. now just to stuff this into my primary code. option explicit dim swapp as sldworks.sldworks dim swmodel as sldworks.modeldoc2 dim swassy as sldworks.assemblydoc dim itemstatusb as boolean, itemstatusl as long dim swfeat as sldworks.feature dim swfeatmgr as sldworks.featuremanager dim swpart as sldworks.partdoc dim swrefplane as sldworks.refplane dim swmodeldocext as sldworks.modeldocextension dim swselmgr as sldworks.selectionmgr dim swsketchmgr as sldworks.sketchmanager dim swcomp as sldworks.component2 dim bndboxpnts as variant, bndboxary as variant dim itemplane as variant, itemplanename as string dim assyname as string, assyfilename as string dim parttemplate as string dim filepathnopartname as string dim fakepartname as string, fakepartfilename as string dim fakefaceplane as variant dim compname as string dim firstselection as string, secondselection as string dim i as integer dim swerrors as long, swwarnings as long, mateerror as long sub main() set swapp = application.sldworks set swmodel = swapp.activedoc assyfilename = swmodel.gettitle ' get assy file name assyname = (left$(assyfilename, len(assyfilename) - 7)) filepathnopartname = left(swmodel.getpathname, instrrev(swmodel.getpathname, "\")) ' get the assembly path only set swmodeldocext = swmodel.extension set swselmgr = swmodel.selectionmanager set swsketchmgr = swmodel.sketchmanager parttemplate = swapp.getuserpreferencestringvalue(swdefaulttemplatepart) set itemplane = swselmgr.getselectedobject6(1, -1) ' create temp plane set swfeat = swmodel.createplaneatoffset3(0, false, true) swfeat.name = "tempplane" itemplanename = swfeat.name itemstatusb = swmodeldocext.selectbyid2(itemplanename, "plane", 0, 0, 0, false, 0, nothing, 0) set swfeat = swselmgr.getselectedobject6(1, -1) set swrefplane = swfeat.getspecificfeature2 swmodel.clearselection2 (true) ' get bounding box off temp plane bndboxpnts = swrefplane.boundingbox '2 mathpoint objects are always returned ' assign highest bounding box elements to array for i = 0 to ubound(bndboxpnts) bndboxary = bndboxpnts(1).arraydata next i ' capture greatest values for plane creation if bndboxary(0) = 0 then if bndboxary(1) > bndboxary(2) then bndboxary(0) = bndboxary(1) else bndboxary(0) = bndboxary(2) end if elseif bndboxary(1) = 0 then if bndboxary(0) > bndboxary(2) then bndboxary(1) = bndboxary(0) else bndboxary(1) = bndboxary(2) end if elseif bndboxary(2) = 0 then if bndboxary(0) > bndboxary(1) then bndboxary(2) = bndboxary(0) else bndboxary(2) = bndboxary(1) end if end if ' create fake face set swpart = swapp.newdocument(parttemplate, 0, 0, 0) ' create new part file ' set fakeface file name set swmodel = swapp.activedoc swmodel.settitle2 ("fakeface") fakepartname = swmodel.gettitle fakepartfilename = fakepartname & ".sldprt" ' get and set plane for fakeface set swmodeldocext = swmodel.extension itemstatusb = swmodeldocext.selectbyid2("front plane", "plane", 0, 0, 0, false, 0, nothing, 0) set swselmgr = swmodel.selectionmanager set fakefaceplane = swselmgr.getselectedobject6(1, -1) ' create sketch for fakeface set swsketchmgr = swmodel.sketchmanager swsketchmgr.insertsketch (true) ' insert and extrude retang using assy bounding box array elements swsketchmgr.createcenterrectangle 0#, 0#, 0#, bndboxary(0), bndboxary(1), bndboxary(2) set swfeatmgr = swmodel.featuremanager swfeatmgr.featureextrusion2 true, true, true, 0, 0, 0.0001, 0, false, false, false, false, 0, 0, false, false, false, false, true, true, true, 0, 0, false swmodeldocext.saveas filepathnopartname & fakepartfilename, swsaveascurrentversion, swsaveasoptions_silent, nothing, swerrors, swwarnings ' go back to assy set swmodel = swapp.activatedoc2(assyfilename, true, swerrors) set swmodeldocext = swmodel.extension set swassy = swmodel ' insert new part into assembly set swcomp = swassy.addcomponent4(fakepartfilename, "", 0#, 0#, 0#) compname = swcomp.name2() swmodel.clearselection2 (true) firstselection = compname & "@" + assyname itemstatusb = swmodeldocext.selectbyid2(firstselection, "component", 0, 0, 0, false, 0, nothing, 0) itemstatusb = swassy.setcomponenttransparent(true) ' make fakeface transparent swassy.unfixcomponent swmodel.clearselection2 (true) ' close fakeface part file swapp.closedoc (fakepartfilename) firstselection = "front plane" + "@" + compname & "@" + assyname secondselection = itemplanename + "@" + assyname ' select the planes for the mate itemstatusb = swmodeldocext.selectbyid2(firstselection, "plane", 0, 0, 0, false, 0, nothing, 0) itemstatusb = swmodeldocext.selectbyid2(secondselection, "plane", 0, 0, 0, true, 0, nothing, 0) ' add the mate set swfeat = swassy.addmate3(swmatecoincident, swmatealignaligned, false, 0, 0, 0, 0, 0, 0, 0, 0, true, mateerror) swmodel.viewzoomtofit2 ' remove temp objects swmodel.clearselection2 (true) itemstatusb = swmodeldocext.selectbyid2(itemplanename, "plane", 0, 0, 0, false, 0, nothing, 0) itemstatusl = swmodeldocext.deleteselection2(0) end sub 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 |
所有的时间均为北京时间。 现在的时间是 04:31 AM. |