logo资料库

solidworks二次开发全教程系列(靠谱).doc

第1页 / 共47页
第2页 / 共47页
第3页 / 共47页
第4页 / 共47页
第5页 / 共47页
第6页 / 共47页
第7页 / 共47页
第8页 / 共47页
资料共47页,剩余部分请下载后查看
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. In the dialog box, select a macro file (.swp) and click Open. 选择一个宏 swp 文件 已经编辑了宏,菜单中会有最近的 9 个宏程序列表供选择。 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 ModelDoc2::ClearSelection2 calls. appearing immediately after 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 feature.Name Else Debug.Print "Error"
End If End Sub 最后列出这两个函数的 VB 语法: ModelDocExtension::SelectByID2 Description This method selects the specified entity. Syntax (OLE Automation) retval = ModelDocExtension.SelectByID2 ( Name, Type, X, Y, Z, Append, Mark, Callout. SelectOption ) Input: (BSTR) Name Name of object to select or an empty string Input: (BSTR) Type Type of object (uppercase) as defined in swSelectType_e or an empty string Input: (double) X X selection location or 0 Input: (double) Y Y selection location or 0 Input: (double) Z Z selection location or 0 Input: (VARIANT_BOOL) Append If... And if entity is... Then...
TRUE Not already selected The entity is appended to the current selection list Already selected The entity is removed from the current selection list FALSE Not already selected The current selection list is cleared, and then the entity is put on the list Already selected The current selection list remains the same Input: (long) Mark Value that you want to use as a mark; this value is used by other functions that require ordered selection Input: (LPCALLOUT) Callout Pointer to the associated callout Input: (long) SelectOption Selection option as defined in swSelectOption_e (see Remarks) Output: (VARIANT_BOOL) retval TRUE if item was successfully selected, FALSE if not SelectionMgr::GetSelectedObject5 Description This method gets the selected object. Syntax (OLE Automation)
retval = SelectionMgr.GetSelectedObject5 ( AtIndex ) Input: (long) AtIndex position within Index current SelectionMgr::GetSelectedObjectCount the list of selected items, where AtIndex ranges from 1 to Output: (LPDISPATCH) retval Pointer to the Dispatch object as defined in swSelType_e; NULL may be returned if type is not supported or if nothing is selected solidworks 二次开发-03-访问特征数据 'coder arden 'filename : getchoosed.swp 'date 'used to get the simple hole infomation dep & dia 'finished lucky !! '------------------------------------------------------------ :2005-03-22 '声明一个简单直孔对象 Option Explicit Dim swApp As SldWorks.SldWorks Dim Model As ModelDoc2 Dim curfeature As feature Dim boolstatus As Boolean Dim featdata As SimpleHoleFeatureData2 Dim component As Component2 Dim dep As Double Dim dia As Double Dim SelMgr As SelectionMgr Dim ncount As Integer Sub getselected() Set swApp = Application.SldWorks Set Model = swApp.ActiveDoc Set SelMgr = Model.SelectionManager Set curfeature = SelMgr.GetSelectedObject5(1) '得到当前选中的第一个特征 MsgBox curfeature.Name
Set featdata = curfeature.GetDefinition '得到特征的定义 boolstatus = featdata.AccessSelections(Model, component) ' 可以对数据进行访问了 ncount = featdata.GetFeatureScopeBodiesCount MsgBox ncount dep = featdata.Depth dia = featdata.Diameter MsgBox dia & "*" & dep '在 solidworks 中可以使用 swAPP.sendmsgtouser2 'MsgBox "error arden" 'featdata.ReleaseSelectionAccess Model.Save Model.EditRebuild End Sub ********************************************** 上面程序运行前,假设你选择了一个简单直孔特征。然后得到这个孔德一些参数。 孔深、直径等。 solidworks 的 API 虽然是 e 文的。介绍的还算详细,并且有很多的 example。大家可以多看看代码。 要访问一个特征,需要经历这样的步骤: 定义一个特征对象: dim....as ... 得到这个特征 :比如使用 GetSelectedObject5 还有 SelectebyID 等... 得到定义:GetDefinition 进行访问:AccessSelections 上面的程序没有 if 选择的容错机制,需要添加上。 solidworks 二次开发-04-修改数据 上次已经可以访问特征的各参数了,今天我们来修改它: 要修改前面的步骤不能少,当我们已经可以读取一些特征时,我们就可以给他设定一些值。当然有时需要 调用特定的参数。solidworks 是 ole 和 com 的,所以要习惯这样。 在修改完特征后需要调用函数 modifydefinition()来实现变化。 我们给一个例子,这个例子比前面的都要全面,它有很好的容错引导机制,可以直接拿来成为一个稳定的 宏程序。 This example doubles the length of the base extrude.这个例子将拉伸凸台的长度增加一倍
Dim swApp As SldWorks.SldWorks Dim Model As ModelDoc2 Dim Component As Component2 Dim CurFeature As feature Dim isGood As Boolean ' Will become an ExtrudeFeatureData Object Dim FeatData As Object Dim Depth As Double Dim SelMgr As SelectionMgr Sub doubleBE() }}--> }}-->Set swApp = CreateObject("sldWorks.application") }}--> }}-->Set Model = swApp.ActiveDoc }}--> }}-->' Make sure that the active document is a part }}--> }}-->If Model.GetType <> swDocPART And Model.GetType <> swDocASSEMBLY Then ‘这里的 swDocPART 、swDocASSEMBLY 我的环境没有通过。我使用 msgbox Model.GetType 的笨办法 得到整数为 1 和 2 }}--> }}--> }}--> }}--> }}--> }}-->Msg = "Only Allowed on Parts or Assemblies" ' Define message }}-->Style = vbOKOnly ' OK Button only }}-->Title = "Error" ' Define title }}-->Call MsgBox(Msg, Style, Title) ' Display error message }}-->Exit Sub ' Exit this program }}--> }}-->End If }}-->
}}--> }}--> }}-->' Get the Selection Manager }}--> }}-->Set SelMgr = Model.SelectionManager }}--> }}--> }}--> }}-->' Get the selected object (first in the group if there are more than one) }}--> }}-->' Note that at this point CurFeature is just a Feature Object }}--> }}-->Set CurFeature = SelMgr.GetSelectedObject3(1) }}--> }}-->If CurFeature Is Nothing Then }}--> }}-->' Tell the user that nothing is selected }}--> }}--> }}-->swApp.SendMsgToUser2 "Please select the Base-Extrude", swMbWarning, swMbOk }}-->Exit Sub }}--> }}-->End If }}--> }}--> }}--> }}-->' Check the feature's type name }}-->' Make sure it is an extrusion }}-->If Not CurFeature.GetTypeName = swTnExtrusion Then ’在这里使用 swTnExtrusion 我的环境没有通过,我改成了 Extrusion 才 ok }}--> }}--> }}-->swApp.SendMsgToUser2 "Please select the Base-Extrude", swMbWarning, swMbOk }}-->Exit Sub
分享到:
收藏