几何尺寸与公差论坛------致力于产品几何量公差标准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-12, 08:55 PM   #1
yang686526
高级会员
 
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
yang686526 向着好的方向发展
默认 【转帖】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
yang686526离线中   回复时引用此帖
GDT自动化论坛(仅游客可见)
 


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

高级搜索
显示模式

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

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

相似的主题
主题 主题发起者 论坛 回复 最后发表
【转帖】create sweep in new part based on another par yang686526 SolidWorks二次开发 0 2009-04-12 08:34 PM


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


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