《solidworks二次开发全教程系列优选资料》由会员分享,可在线阅读,更多相关《solidworks二次开发全教程系列优选资料(50页珍藏版)》请在金锄头文库上搜索。
1、solidworks二次开发全教程系列solidworks二次开发-01-录制一个宏 第一步:我们需要自己录制一个宏,然后看看程序产生了什么代码。当初学习excel时候就是这么干的。只是,solidworks要复杂一些,直接录制的宏不能使用,需要做一些调整。在没有经验的时候我们最好按照下面的建议来做。Edit or Debug SolidWorks MacroEdit or debug SolidWorks macros using Microsoft VBA. 使用Microsoft VBA编辑或调试宏To edit or debug a SolidWorks macro:Click Edi
2、t 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. 此选项Aut
3、omatically 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. 选择一个
4、宏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: 删除一些多余
5、的代码:The following variables are declared automatically in a SolidWorks macro. Delete any variables not used in the macro. 这些对象的声明是自动产生的,可以将没用的删除 Dim swApp As ObjectDim Part As ObjectDim boolstatus As BooleanDim longstatus As Long, longwarnings As LongDim FeatureData As ObjectDim Feature As ObjectDim
6、 Component As ObjectDelete 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:S
7、electByID2 calls appearing immediately after ModelDoc2:ClearSelection2 calls.Delete all ModelDoc2:ClearSelection2 calls appearing immediately before ModelDocExtension:SelectByID2. solidworks二次开发-02-用来访问特征的两个API 来学习两个api:SelectByID2和GetSelectedObject5。这两个函数,第一个通过给出对象的name选择对象。第二个通过启用程序前已经选择的索引得到对象。看下
8、面程序:Option ExplicitDim swApp As SldWorks.SldWorksDim Model As ModelDoc2Dim feature As featureDim boolstatus As VariantSub main()Set swApp = Application.SldWorksSet Model = swApp.ActiveDoc 选择叫拉伸1的特征boolstatus = Model.Extension.SelectByID2(拉伸1, BODYFEATURE, 0, 0, 0, False, 0, Nothing, swSelectOptionDe
9、fault)主要就是这一句话,在写Option Explicit后函数的最后一个参数swSelectOptionDefault可以使用0来代替 If the selection was successful, that is, Extrude1 was selected and it is a BODYFEATURE, then get that feature; otherwise, indicate failureIf boolstatus = True Then 如果有“拉伸1”这个特征下面的代码将其选中 Dim SelMgr As SelectionMgr Set SelMgr = M
10、odel.SelectionManager Set feature = SelMgr.GetSelectedObject5(1) 此处使用一个索引来得到特征 Debug.Print feature.NameElse Debug.Print ErrorEnd IfEnd Sub最后列出这两个函数的VB语法:ModelDocExtension:SelectByID2DescriptionThis method selects the specified entity.Syntax (OLE Automation)retval = ModelDocExtension.SelectByID2 ( Na
11、me, Type, X, Y, Z, Append, Mark, Callout. SelectOption )Input:(BSTR) NameName of object to select or an empty stringInput:(BSTR) TypeType of object (uppercase) as defined in swSelectType_e or an empty stringInput:(double) XX selection location or 0Input:(double) YY selection location or 0Input:(doub
12、le) ZZ selection location or 0Input:(VARIANT_BOOL) AppendIf.And if entity is.Then. TRUENot already selectedThe entity is appended to the current selection listAlready selectedThe entity is removed from the current selection listFALSENot already selectedThe current selection list is cleared, and then
13、 the entity is put on the listAlready selectedThe current selection list remains the sameInput:(long) MarkValue that you want to use as a mark; this value is used by other functions that require ordered selectionInput:(LPCALLOUT) CalloutPointer to the associated calloutInput:(long) SelectOptionSelec
14、tion option as defined in swSelectOption_e (see Remarks)Output:(VARIANT_BOOL) retvalTRUE if item was successfully selected, FALSE if notSelectionMgr:GetSelectedObject5DescriptionThis method gets the selected object.Syntax (OLE Automation)retval = SelectionMgr.GetSelectedObject5 ( AtIndex )Input:(long) AtIndexIndex position within the current list of selected items, where AtIndex ranges from 1 to SelectionMgr:GetSelectedObjectCountOutput:(LPDISPATCH) retvalPointer to the Dispatch object as defined in swSelType_e; NULL may be returned if type is no