几何尺寸与公差论坛------致力于产品几何量公差标准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, 03:09 PM   #1
yang686526
高级会员
 
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
yang686526 向着好的方向发展
默认 【转帖】using a macro to insert parts into an assembly

using a macro to insert parts into an assembly.
i am writing a macro that will eventually (hopefully!) read part numbers from an excel or text file, then search through the config's of my parts library and insert the corresponding components into an assembly, then make the corresponding config'n active.
i broke this problem down into its basic components and have been working to solve each one. i have hit a perplexing problem when it comes to inserting the parts and or assemblies.
i started off by recording a macro of inserting a part. pretty simple. it uses the addcomponent command, which needs a filepath and x-y-z coordinates. this seemed to work ok but it kept glitching up.
apparently, from what i've been able to figure out by fiddling around, this command cannot insert a part or assembly unless that part or assembly has already been used during the current session of solidworks.
for example:
-------------------
option base 1
dim swapp as object
dim part as object
dim selmgr as object
dim boolstatus as boolean
dim longstatus as long, longwarnings as long
dim feature as object
dim filepath as string
dim partnum(4) as string
dim posx as integer
dim posy as integer
dim posz as integer
dim x as integer
sub main()
set swapp = application.sldworks
set part = swapp.activedoc
set selmgr = part.selectionmanager
posx = 0
posy = 0
posz = 0
partnum(1) = "001"
partnum(2) = "002"
partnum(3) = "003"
partnum(4) = "004"
for x = 1 to 4
filepath = "c:\documents and settings\ckennedy\my documents\solid works testing\parts\" + partnum(x) + ".sldprt"
part.addcomponent filepath, posx, posy, posz
filepath = "c:\documents and settings\ckennedy\my documents\solid works testing\assemblies\" + partnum(x) + ".sldasm"
part.addcomponent filepath, posx, posy, posz
posx = posx + 1.5
next
end sub
--------------------
this macro grabs three parts 001.sldprt, 002.sldprt, etc. (a cube, a sphere, and a cylinder) and an assembly (made up of the cube and sphere parts) and inserts them into the active assembly, spacing them out as it does so.
if i open an assembly and run the macro, nothing happens.
if i open an assembly, insert the cube, delete it, then run the macro, it inserts the cube.
if i open an assembly, insert all the parts, delete them, then run the macro, it inserts all the parts (but not the assembly made up of two of them).
if i open an assembly, insert the cube-sphere assembly, delete it, and run the macro, it will insert the cube, the sphere, and the cube-sphere assembly, but not the cylinder.
if the parts have been used in one assembly, and another is opened or made active, then the macro works fine in the new window.
so in conclusion, how do i fix this? is there another command to insert parts that doesn't rely on the "loaded into sw memory" thing that seems to be happening? or a command to do just that and load the parts into sw memory? any help or insight would be appreciated.
~craig
edited: 05/08/2008 at 03:17 pm by craig kennedy
i have created a series of scripts to do this.
my approach was to place named points on each part and select point to point xyz location for insertion.
i found that you must open each part before solidworks will allow you to insert them into a assembly.
as dennis suggested, you must open the part in code before inserting it into the assembly. here's some text from the api help stating the specific function to use.
the specified file must be loaded in memory. a file is loaded into memory when you load the file in your solidworks session (sldworks:pendoc6) or open an assembly that already contains the file.
mahir abrahim, cswp/core. cswp/smtl
mechanical design engineer
sw 2009 sp2.1
proe wf3/4
edited: 05/08/2008 at 04:34 pm by mahir abrahim
thank you both for your help. i've managed to get the macro to open the parts, then place them in the assembly. but that leaves me with a whole bunch of open parts that i don't want there. is there a method of loading the parts to memory without actually opening the part? or should i just open, add the part, close?
you can turn off the visibility for newly opened files.
swapp.documentvisible false, swdocpart
will turn off visibility of part documents. you would need to do the same thing for assembly files.
at the end of your code you will need to turn the visibility back on by using true as the first parameter. otherwise the user will open files manually and he will not see them.
wayne matus
texas engineering systems
thank you so much wayne, that is precisely what i was looking for. problem solved.
quick
yang686526离线中   回复时引用此帖
GDT自动化论坛(仅游客可见)
回复


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

高级搜索
显示模式

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

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

相似的主题
主题 主题发起者 论坛 回复 最后发表
【转帖】macro that loop trough assembly and sub-assembly and parts yang686526 SolidWorks二次开发 0 2009-04-13 12:38 PM
【转帖】execute a sw macro from a macro of another software, is it possible yang686526 SolidWorks二次开发 0 2009-04-13 10:46 AM
【转帖】macro for inserting and mating a new part in an assembly yang686526 SolidWorks二次开发 0 2009-04-12 09:38 PM
【转帖】inserting and mating parts in an assembly yang686526 SolidWorks二次开发 0 2009-04-12 09:28 PM
【转帖】inserting and mating parts in an assembly yang686526 SolidWorks二次开发 0 2009-04-12 06:43 PM


所有的时间均为北京时间。 现在的时间是 09:43 AM.


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