solidworks二次开发-01-录制一个宏
第一步:
我们需要自己录制一个宏,然后看看程序产生了什么代码。当初学习excel时候就是这么干的。只是,solidworks要复杂一些,直接录制的宏不能使用,需要做一些调整。在没有经验的时候我们最好按照下面的建议来做。
edit or debug solidworks macro
edit or debug solidworks macros using microsoft vba. 使用microsoft vba编辑或调试宏
to edit or debug a solidworks macro:
click edit macro on the macro toolbar, or click tools, macro, edit.
notes: 注意:
to automatically edit a macro after recording it, click tools, options, systems options. on the general tab, select automatically edit macro after recording and click ok. this setting is persistent across solidworks sessions.
此选项automatically edit macro after recording 顾名思义是在记录宏完毕后自动打开编辑界面。
if you recently edited the macro, you can select it from the menu when you click tools, macro. this menu lists the last nine macros that you edited.
已经编辑了宏,菜单中会有最近的9个宏程序列表供选择。
in the dialog box, select a macro file (.swp) and click open. 选择一个宏swp文件
note: you can also edit .swb files, which are older-style solidworks macro files. when you run or edit a .swb file, it is automatically converted to a .swp file. 旧的宏文件后缀为swb,你也可以打开swb,那么会自动保存为swp。
edit or debug the macro. if it is a new macro, be sure to:如果是新的宏
delete extra lines of code: 删除一些多余的代码:
the following variables are declared automatically in a solidworks macro. delete any variables not used in the macro. 这些对象的声明是自动产生的,可以将没用的删除 dim swapp as object
dim part as object
dim boolstatus as boolean
dim longstatus as long, longwarnings as long
dim featuredata as object
dim feature as object
dim component as object
delete all lines of code that change the view. 删除切换试图的代码
译者注:像这样的 part.activeview().rotateaboutcenter 0.0662574, 0.0346621 无情的删掉吧
delete all modeldocextension::selectbyid2 calls appearing immediately before modeldoc2::clearselection2 calls. however, do not delete modeldocextension::selectbyid2 calls appearing immediately after modeldoc2::clearselection2 calls.
delete all modeldoc2::clearselection2 calls appearing immediately before modeldocextension::selectbyid2.