solidworks二次开发全教程系列

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

solidworks二次开发全教程系列

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. solidworks二次开发-02-用来访问特征的两个API

来学习两个api:

SelectByID2和GetSelectedObject5。这两个函数,第一个通过给出对象的name选择对象。第二个通过启用程序前已经选择的索引得到对象。

看下面程序:

Option Explicit

Dim swApp As SldWorks.SldWorks

Dim Model As ModelDoc2

Dim feature As feature

Dim boolstatus As Variant

Sub main()

Set swApp = Application.SldWorks

Set Model = swApp.ActiveDoc

' 选择叫"拉伸1"的特征

boolstatus = Model.Extension.SelectByID2("拉伸1", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, swSelectOptionDefault)

'主要就是这一句话,在写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 failure

If boolstatus = True Then '如果有“拉伸1”这个特征下面的代码将其选中

Dim SelMgr As SelectionMgr

Set SelMgr = Model.SelectionManager

Set feature = SelMgr.GetSelectedObject5(1) '此处使用一个索引来得到特征

Debug.Print

Else

Debug.Print "Error"

相关文档
最新文档