几何尺寸与公差论坛

 找回密码
 注册
查看: 1384|回复: 0

【转帖】using a macro to insert parts into an assembly

[复制链接]
发表于 2009-4-13 15:09:26 | 显示全部楼层 |阅读模式
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
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-22 13:28 , Processed in 0.035489 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表