几何尺寸与公差论坛

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

【转帖】sw api practices

[复制链接]
发表于 2009-4-12 22:32:37 | 显示全部楼层 |阅读模式
sw api practices
hello fella's !
i was wondering ...
what's the best, most stable, or quickest way to program the following situation.
a part has 4 configurations.
the user picks a configuration via a form.
a var is set :
scname = "requestedconfig"
the code changes the configuration :
boolstatus = modeldoc2.compconfigproperties4(2, 0, true, true, scname, false)
this changes the configuration on the part, even when the current active config is the same as the requested config by the user right ?
is it more efficient/stable if i got it to check the active current configuration on the part with the one requested, and only if they aren't the same, then configure the part with the requested config ?
else, just leave it like that ?
thing is, i'm switching a config on a part, and on many occasions, it crashes my macro ..
what's your opinion ?
greets,
bert
solidworks professional 2009 sp2.0
hpxw4400 workstation
intel core2 cpu
6600 @ 2.40ghz
yes, i'm aware i have a funky surname ....
bert,
i don't see why it should be a problem activating an already active configuration. i have made a number of configurator solutions working just like that and it has never been a problem. there must be some other unstable elements in your setup.
have you tracked it down to crash exactly when the configuration is changed?
jorn bjarning
cswp
cad & plm consultant
sw2008 sp5 / sw2009 sp2
you are much better getting the component and changing the referenceconfiguration than you are resetting all configuration properties.
jorn,
i didn't have a problem with this, up till now .. for some reason, my macro (wich is getting somewhat elaborate ) crashes each type it has to change a config this way.
luke,
could you possibly be bothered to write me some samplecode on how i can change the config as you have suggested ?
i allways start off with a selected part by using
boolstatus = modeldoc2.etc
i'd be very greatfull !
overall, as my macro is getting better and "heavier" i'm starting to look into what is more efficient, lean and mean when it comes to code ...
this is a disipline on its own i rekon.
greets,
bert
solidworks professional 2009 sp2.0
hpxw4400 workstation
intel core2 cpu
6600 @ 2.40ghz
yes, i'm aware i have a funky surname ....
bert,
all depends how you want to obtain the component, you can select it directly using selectbyid2, or loop all components in an assembly.
once you have the component2 object you literally just call
comp.referencedconfiguration = "configname"
if you provide a bit more info about your layout i can provide a method to get the components.
heya luke ! sorry for my delayed reply ...
in general, my macro looks like this (altough 40x longer and more actions to be taken) but as an example, this is how i select my parts, change my dimensions, suppress parts or features, switch configurations etc ..
dim swapp as object
dim modeldoc2 as object
dim mydimension as object
dim boolstatus as boolean
dim longstatus as long, longwarnings as long
sub main()
set swapp = application.sldworks
set modeldoc2 = swapp.activedoc
'set lenght of cube
boolstatus = modeldoc2 .extension.selectbyid2("sketch1@part1.sldprt", "dimension", 0.04364437810937, 0.0130670150456, -0.05171008214468, false, 0, nothing, 0)
modeldoc2 .parameter("<A href="mailto:d1@sketch1").systemvalue">d1@sketch1").systemvalue = 0.1
'set width of cube
boolstatus = modeldoc2 .extension.selectbyid2("sketch1@part1.sldprt", "dimension", 0.0582534341154, -0.01778784542079, -0.03546929205944, false, 0, nothing, 0)
modeldoc2 .parameter("<A href="mailto:d2@sketch1").systemvalue">d2@sketch1").systemvalue = 0.075
'suppress hole in cube
boolstatus = modeldoc2 .extension.selectbyid2("hole", "bodyfeature", 0, 0, 0, false, 0, nothing, 0)
modeldoc2 .editsuppress2
'set cub height to 50mm
boolstatus = modeldoc2 .extension.selectbyid2("cube@part1.sldprt", "dimension", 0.03768076751945, 0.005412183250505, -0.03809288378403, false, 0, nothing, 0)
modeldoc2 .parameter("<A href="mailto:d1@cube").systemvalue">d1@cube").systemvalue = 0.05
'switch cube configuration to config2
boolstatus = modeldoc2 .extension.selectbyid2("config2@part1.sldprt", "configurations", 0, 0, 0, false, 0, nothing, 0)
boolstatus = modeldoc2 .compconfigproperties4(2, 0, true, true, "config2", false)
'do a final rebuild
boolstatus = modeldoc2 .forcerebuild3(true)
end sub
this has been working well for a long time. still, as my macro grows to be more and more a full-blown configurator, it comes to this point that when switching configurations this way, the macro halts, pauzes and crashes (without giving me an error).
if i comment out the lines of code responsible for switching configs, then there aren't any problems.
could the following better in my case ? :
boolstatus = modeldoc2 .extension.selectbyid2("config2@part1.sldprt", "configurations", 0, 0, 0, false, 0, nothing, 0)
boolstatus = modeldoc2 .showconfiguration2("config2")
but does this work on a part in a subassy aswell ?
many thnaks in advance to all of you, for your opinions or advice !
yours,
bert
*edit* removed typos in code*
solidworks professional 2009 sp2.0
hpxw4400 workstation
intel core2 cpu
6600 @ 2.40ghz
yes, i'm aware i have a funky surname ....
edited: 03/25/2009 at 06:19 am by bert de spiegelaere
change this:
'switch cube configuration to config2
boolstatus = modeldoc2 .extension.selectbyid2("config2@part1.sldprt", "configurations", 0, 0, 0, false, 0, nothing, 0)
to this:
'switch cube configuration to config2
boolstatus = modeldoc2 .extension.selectbyid2("myitem-1@part1", "component", 0, 0, 0, false, 0, nothing, 0)
where "myitem-1@part1" is the string used to select the component from the feature manager tree at the left that you wish to change the configuration of. just record a macro, select it then look at the macro for the correct string.
then do this:
dim selmgr as selectionmgr
set selmgr = modeldoc2.selectionmanager
dim comp as component2
set comp = selmgr.getselectedobject6(1,-1)
comp.referencedconfiguration = "config2"
that seems to work !
altough i'd might want to make a seperate sub ( like : call changeconfig) as repeating that block of code would start to look "heavy" .
odd thing is, when i work via following code :
boolstatus = modeldoc2 .extension.selectbyid2("config2@part1.sldprt", "configurations", 0, 0, 0, false, 0, nothing, 0)
boolstatus = modeldoc2 .showconfiguration2("config2")
it works too, quick and stable.
wich one is better then ?
greets,
bert
solidworks professional 2009 sp2.0
hpxw4400 workstation
intel core2 cpu
6600 @ 2.40ghz
yes, i'm aware i have a funky surname ....
showconfiguration2 is a method, referencedconfiguration is a property, so naturally the property would be quicker, but could not say without benchmarking the 2.
heya luke, i'll have to take back what i said about the referencedconfiguration doing it for me ..
its all good when i switch configurations on sub-assy's but as soon as i want to switch configs on parts within those sub-assy's , it fails.
the other method (showconfiguration2) works fine on both.
what do you suggest i should do ?
regards,
bert
solidworks professional 2009 sp2.0
hpxw4400 workstation
intel core2 cpu
6600 @ 2.40ghz
yes, i'm aware i have a funky surname ....
place the sub-assembly into edit mode first. thats all the showconfiguration method will do
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-12-23 18:15 , Processed in 0.035897 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

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