几何尺寸与公差论坛

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

【转帖】controlling sw with excel

[复制链接]
发表于 2009-4-13 09:52:14 | 显示全部楼层 |阅读模式
controlling sw with excel?
i am fairly new to the solidworks api and have been working alot with excel and the vba editor there to create different workbooks and what not, but now i am trying to do some very basic stuff in solidworks with data created in excel. the basic project i am trying to do is;
1) a series of points in 3d (have the xyz corrdinates of the points) to create a sketch, add annotations to the points. (basically a 3d scatter plot)
2) create 3d line sketches between these points (has point 1 and point 2 , using a sketch between 2 points that i figured out the code from backwards engineering a recorded macro)
3) adding annotations to those lines
4) renaming the sketches that were made to following a naming convention for the sketch as it was created, ie 3dsketch2 -> "1" and so forth through around 300 sketches
i have backwards engineered most of the code for this with recording macros and pulling out the code and then using a mail merge in word to formulate the code. the only problem is that i get vba code that is a few thousand lines long and it will not compile it, so it has to be broken up and entered in peices which takes way to long. i know i can do this with a loop, but i don't know how to access excel from sw or what would be even better is if i could use excel api to access sw and create the model from there. its seems very basic but not sure how to start it. i am going to pick up the 2008 api book that luke malpass wrote as it seems like it would help alot in basic understanding of the api structure. but some input on this matter would be very helpful.
thanks for the help in advance,
adam
answer if i understand you correctly you have more or less mastered the create sketches from points part. that you need help with is how to access sw from excel. here is an example on how to do that:
'this code is inserted in the vba module in excel
dim swapp as sldworks.sldworks
dim part as sldworks.modeldoc2
'this code starts sw from excel
set swapp = createobject("sldworks.application")
swapp.visible = true 'makes sw visible
[insert the rest of your code here]
hope this will help. can also recommend a good book that has helped me a lot. namely "automating solidworks 2006 using macros", by mike spens. isbn 1-58503-263-8
joel andersson
development engineer hägglunds drives
solidworks 2007 sp5.0
edited: 07/15/2008 at 02:21 am by joel andersson
that helps alot actually, thank you very much!
as for that book, does it help with basic stuff like this? i am trying to control some more sw stuff from excel and would like to learn as much as i can of the api since it seems to be one of the best ways to work with sw.
yes, the book give you a help with this kind of basic things.
joel andersson
development engineer hägglunds drives
solidworks 2007 sp5.0
cool, i guess i shall pick that up. got a few more questions though,
i am creating a new sketch each time i run my macro, which yeilds a part1, part2, part3... and so on for the new window before it saves. my code calls this out for the part. command, is there a way to make it so it always selects the new window?
-> code in question
set part = swapp.activatedoc2("part14", false, longstatus)
with the "part14" needed to be varible....
also, is there a way to disable the screen refresh on solidworks. in excel you can change the application.screenupdate = false so it won't display what is going on until the job is finished, and it is alot faster. i am hoping there is a way to do this in solidworks too because watching it make 300 points, add annotations and a bunch of other fun stuff takes way longer then it should since it shows the screen making the point, going to edit the point location, and so on.
thanks for the help
maybe a for-loop could work?
[example]
dim countpart as integer 'rename countpart to a variable that suits you
for countpart = 1 to 300
set part = swapp.activatedoc2("part"&countpart, false, longstatus)
[the rest of the code]
next countpart
on your last question, try to set swapp.visible=false to see if this makes any different
joel andersson
development engineer hägglunds drives
solidworks 2007 sp5.0
for the for loop, i normally close the window when it doesn't work, or done checking something, so counting open parts wouldn't work (same goes for it i have other parts open at the same time). it is the internal sw default new part name that pops up and resets to 1 when you close sw and reopen it.
for the second question, i tried that and it still shows it, which baffles me....
set modeldoc = swapp.activedoc
name = modeldoc.gettitle
that right there gives me the name and everything so i plugged that into the set part command and it works perfect for the new doc created and always updates for the newest part the macro opens.
now to see if i can turn off the screen refresh while making parts...
quick
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-12-24 09:04 , Processed in 0.036579 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

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