solidworks的api帮助还是很全面的,里面有好多见简单而有效的程序,成天在api帮助里泡着,现在做程序的速度是越来越快。好像当年成天在excel中录制宏看代码的感觉。下面的程序是api帮助里的,它用来显示装配体的所有零部件。我给我的同事们用,他们觉得不错 :)。程序使用了一个简单的递归方法遍历了装配体。
make all assembly components visible example (vb)
this example shows how to make all assembly components visible.
—————————————
preconditions: an assembly document is open.
postconditions: any hidden assembly components are made visible.
—————————————
option explicit
public enum swcomponentvisibilitystate_e
}}–> }}–>swcomponenthidden = 0
}}–> }}–>swcomponentvisible = 1
end enum
sub traversecomponent _
( _
}}–> }}–>swcomp as sldworks.component2, _
}}–> }}–>nlevel as long _
)
}}–> }}–>dim vchildcomparr }}–> }}–>as variant
}}–> }}–>dim vchildcomp }}–> }}–>as variant
}}–> }}–>dim swchildcomp }}–> }}–>as sldworks.component2
}}–> }}–>dim swcompconfig }}–> }}–>as sldworks.configuration
}}–> }}–>dim spadstr }}–> }}–>as string
}}–> }}–>dim i }}–> }}–>as long
}}–> }}–>
}}–> }}–>for i = 0 to nlevel – 1
}}–> }}–>spadstr = spadstr + " }}–> }}–>"
}}–> }}–>next i
}}–> }}–>
}}–> }}–>vchildcomparr = swcomp.getchildren
}}–> }}–>for each vchildcomp in vchildcomparr
}}–> }}–>set swchildcomp = vchildcomp
}}–> }}–>
}}–> }}–>debug.print spadstr & swchildcomp.name2 & " <" & swchildcomp.referencedconfiguration & ">"
}}–> }}–>
}}–> }}–>if swcomponenthidden = swchildcomp.visible then
}}–> }}–>swchildcomp.visible = swcomponentvisible
}}–> }}–>end if
}}–> }}–>
}}–> }}–>traversecomponent swchildcomp, nlevel + 1
}}–> }}–>next
end sub
sub main()
}}–> }}–>dim swapp }}–> }}–>as sldworks.sldworks
}}–> }}–>dim swmodel }}–> }}–>as sldworks.modeldoc2
}}–> }}–>dim swassy }}–> }}–>as sldworks.assemblydoc
}}–> }}–>dim swconf }}–> }}–>as sldworks.configuration
}}–> }}–>dim swrootcomp }}–> }}–>as sldworks.component2
}}–> }}–>dim bret }}–> }}–>as boolean
}}–> set swapp = application.sldworks
}}–> }}–>set swmodel = swapp.activedoc
}}–> }}–>set swconf = swmodel.getactiveconfiguration
}}–> }}–>set swrootcomp = swconf.getrootcomponent}}–>}}–>
}}–> }}–>debug.print "file = " & swmodel.getpathname
}}–> traversecomponent swrootcomp, 1
end sub