几何尺寸与公差论坛

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

【转帖】multiple macros in one vsta projec

[复制链接]
发表于 2009-4-12 21:51:24 | 显示全部楼层 |阅读模式
multiple macros in one vsta project
i am just starting to fool around with using vsta macros instead vba. with vba i was able to have multiple subs in one file. in solidworks i could then reference whichever sub i wanted to as a separate macro. this allowed me to keep similar macros grouped together into a single easy to maintain file. it also let me use subfunctions in order to avoid repeating code between macros.
when i try to do this with vsta i get an error because solidworksmacro_startup always tries to run main(). is there a way around this, or will i need separate projects for each of my macros, with shared code saved as a separate .vb file?
mahir abrahim, cswp/core. cswp/smtl
mechanical design engineer
sw 2009 sp2.1
proe wf3/4
in solidworksmacro_startup you can change main() in enything you want.
and in solidworks macro button you can choose module and sub which you want.
and if you call macro from another you can also specify which file, module and sub you want.
function runmacro2( _
byval filepathname as string, _
byval modulename as string, _
byval procedurename as string, _
byval options as integer, _
byref error as integer _
) as boolean
thanks, ivana. i already figured that you could change the name of the startup sub, but the problem is that there is still a startup sub. let's say you have 3 macros that do almost the same thing. with vba i could simply write a main routine accompanied by three small subs (see below). i don't see how to do this using vsta without creating multiple project folders.
sub runmacro(i as integer)
'some function relying on the integer i
end sub
sub macro1()
runmacro(1)
end sub
sub macro2()
runmacro(2)
end sub
sub macro3()
runmacro(3)
end sub
mahir abrahim, cswp/core. cswp/smtl
mechanical design engineer
sw 2009 sp2.1
proe wf3/4
i have 2009 only one day. so i don't know if this is right way
imports solidworks.interop.sldworks
imports solidworks.interop.swconst
imports system
partial class solidworksmacro
sub subrunmacro(byval i as integer)
'some function relying on the integer i
msgbox(i)
end sub
public sub macro1()
subrunmacro(1)
end sub
public sub macro2()
subrunmacro(2)
end sub
public sub macro3()
macro4() ' is sub in module1
end sub
'''
''' the sldworks swapp variable is pre-assigned for you.
'''
public swapp as sldworks
end class
edited: 01/25/2009 at 08:50 am by ivana kolin
thanks again, but i tried that already with no success. vsta will try to run whatever your startup routine is (runmacro) and ignore the calling functions you actually want to run first (macro1/2/3). go ahead and give it a try. maybe you can find out something i couldn't.
mahir abrahim, cswp/core. cswp/smtl
mechanical design engineer
sw 2009 sp2.1
proe wf3/4
i've never worked with vsta before, but i created this macro by recording a macro and then adding a subroutine manually:
imports solidworks.interop.sldworks
imports solidworks.interop.swconst
imports system
partial class solidworksmacro
public sub main()
dim swdoc as modeldoc2 = nothing
dim swpart as partdoc = nothing
dim swdrawing as drawingdoc = nothing
dim swassembly as assemblydoc = nothing
dim boolstatus as boolean = false
dim longstatus as integer = 0
dim longwarnings as integer = 0
swdoc = ctype(swapp.opendoc6("m:\7201df.sldprt", 1, 0, "", longstatus, longwarnings),modeldoc2)
swapp.activatedoc2("7201df.sldprt", false, longstatus)
swdoc = ctype(swapp.activedoc,modeldoc2)
dim mymodelview as modelview = nothing
mymodelview = ctype(swdoc.activeview,modelview)
mymodelview.framewidth = 2866
mymodelview.frameheight = 952
end sub
public sub macro2()
msgbox("hello world!")
end sub
'''
''' the sldworks swapp variable is pre-assigned for you.
'''
public swapp as sldworks
end class
then i created a macro button and pointed it to the macro. in the dropdown box labeled "method" i chose "macro2". when i click the button, macro2 runs but main() does not. this is the same functionality as working with vba. how exactly are you trying to run your macros?
i'll get you eh steve, if it's the last thing i dooooo!
edited: 01/26/2009 at 09:00 am by josh brady
john, that's exactly what i'd like to do. i'll give it another go. i've only tried running from within the programming environment, and it wouldn't work. in vba you simply go to the sub you want to run and click f8 to start executing from there. vsta kept running whatever sub was specified in solidworksmacro_startup (main by default). it's possible actually attaching a button to the alternate subs works even though the ide doesn't run the macro as expected.
mahir abrahim, cswp/core. cswp/smtl
mechanical design engineer
sw 2009 sp2.1
proe wf3/4
quick
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-12-23 18:11 , Processed in 0.034940 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

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