附录中的示例将重点介绍使用SOLIDWORKS API构建生产工具的其他方式。尽管用于构建这些应用程序的概念可能比较超前,但它们可以为读者未来的开发需求提供资源。

学习目标

● 宏特征

● 批量转换1

● 批量转换2

● 遍历装配体

● 自定义模型视图

附录A 宏特征

1.模块:沉孔(CounterBore)

MacroFeature或“COM”特征允许程序员在SOLIDWORKS中创建自己的自定义特征。

本例中,将创建一个允许用户输入直径和深度的沉孔特征,如附图A-1所示。当用户接受输入值时,PropertyManager页面的OnClose处理程序将在FeatureManager设计树中创建一个宏特征。用户随时可以右键单击新的宏特征来更改值,通过接受新的输入值的方式重建特征。

Untitled

实现代码如下所示:

'MacroFeature.
'The MacroFeature or "COM" Feature allows the programmer to create their own custom features in the SolidWorks
'FeatureManager Design Tree. In this example we are creating a CounterBore feature that allows the user to enter a
'Diameter and a Depth of a counter bore. When the user accepts the values, the OnClose handler for the the
'Property Manager Page creates a macro Feature in the Feature Manager. The user at any time can right click on the
'Feature and change the values, Accept them, and the feature will be rebuilt according to the changed input.

Sub main()
  'Entry Point for the Macro.
  'As Soon as the Macro is started, we call the Method that
  'creates and shows our custom PropertyManagerPage
  Dim MacroUI As New CMacroFeaturePropPage
  MacroUI.PropPageMenuCallback
End Sub

2.模块:MacroFeature函数

以下代码模块包含MacroFeature函数。为清楚起见,已将其与PMP实现代码分开。有关此代码的注释,请参阅宏CounterBoreMacroFeature.swp。

'This module contains the MacroFeature Functions

Public Function swmRegenCBore(app As Variant, _
  swPart As Variant, feature As Variant) As Variant
  'This function is called every time you need to regenerate
  'the geometry of this feature
  'On Creation, On regen....
  
  'This function uses the SldWorks.Modeler object to create a
  'cylinder and subtract it from the
  'Body that we are adding the feature to.
  
  Dim swMyFeature As SldWorks.feature
  Dim swMacroFeatureData As SldWorks.MacroFeatureData
  Dim dboxDimArray(7) As Double
  Dim boxDimArray As Variant
  Dim PartBody As Object
  Dim ResultBodies As Variant
  Dim errorCode As Long
  Dim MyParamNames As Variant
  Dim MyParamTypes As Variant
  Dim MyParamValues As Variant
      
  'The feature variable is passed as a parameter into this
  'function from SldWorks
  Set swMyFeature = feature
  'Get the definition ..similar to getting the definition of
  'any other feature
  Set swMacroFeatureData = swMyFeature.GetDefinition
  'Now get the driving parameters of the feature.
  swMacroFeatureData.GetParameters MyParamNames, _
      MyParamTypes, MyParamValues

  'The MacroFeatureData.GetSelections3 method retrieves
  'arrays of all the objects that were selected by the
  'user when creating the Macrofeature
  'in this example we only selected a circular face. Here we
  'are filling the arrays with all the selection info and
  'objects
  'We are only going to use the first index of the selObjects
  'array. This will be the circular planar face that we
  'selected to create the counterbore on.
  
  Dim swCicularFace As SldWorks.face2
  Dim SelObjects As Variant
  Dim SelObjectTypes As Variant
  Dim SelMarks As Variant
  Dim SelDrViews As Variant
  Dim SelXforms As Variant
  'Fill the arrays with the selection objects and
  'selection info
  swMacroFeatureData.GetSelections3 SelObjects, _
    SelObjectTypes, SelMarks, SelDrViews, SelXforms
  
  'Gets a pointer to the selected circular Face
  Set swCircularFace = SelObjects(0)
  Dim swSurface As SldWorks.Surface
  'Get the Surface from the selected Face
  Set swSurface = swCircularFace.GetSurface
      
  'Get the center location of the selected end swFace
  Dim Edges As Variant
  Edges = swCircularFace.GetEdges
  Dim swCurve As SldWorks.Curve
  Set swCurve = Edges(0).GetCurve
  Dim CircleParams As Variant
  CircleParams = swCurve.CircleParams
    
  'Get the radius for the selected end swFace
  Dim FaceODRadius As Double
  FaceODRadius = CircleParams(6)
  
  'Get the normal for the selected end swFace
  Dim FaceNormal As Variant
  FaceNormal = swCircularFace.Normal
  
  Dim TparamValues(9) As Double
  'Center
  TparamValues(0) = CircleParams(0)
  TparamValues(1) = CircleParams(1)
  TparamValues(2) = CircleParams(2)
  'Axis
  'negate the values so that the cylinder dir is opposite
  'the direction of the face normal
  TparamValues(3) = -FaceNormal(0)
  TparamValues(4) = -FaceNormal(1)
  TparamValues(5) = -FaceNormal(2)
  'Radius
  TparamValues(6) = MyParamValues(0) * 0.0254
  'Depth
  TparamValues(7) = MyParamValues(1) * 0.0254
  'empty
  TparamValues(8) = 0
  'Empty
  TparamValues(9) = 0

  'Fill the box array with the bounding box dimensions
  'of the Counterbore
  dboxDimArray(0) = TparamValues(0)
  dboxDimArray(1) = TparamValues(1)
  dboxDimArray(2) = TparamValues(2)
  
  'Axis
  dboxDimArray(3) = TparamValues(3)
  dboxDimArray(4) = TparamValues(4)
  dboxDimArray(5) = TparamValues(5)
  
  dboxDimArray(6) = TparamValues(6) / 2 'Radius
  dboxDimArray(7) = TparamValues(7) 'Height
  
  boxDimArray = dboxDimArray

  'Use the modeler to create a cylinder body
  Dim swModeler As SldWorks.Modeler
  Set swModeler = app.GetModeler
  Dim TempCylOut As SldWorks.body2
  Set TempCylOut = swModeler.CreateBodyFromCyl(boxDimArray)
  
  'Display it to the user if stepping through the code
  TempCylOut.Display3 swPart, RGB(1, 0, 0), _
    swTempBodySelectOptionNone
    
' Some modeler methods do not automatically add face ids
' to new geometry.
' Use this code to validate whether or not the IDs were added:

'    Dim IdFaces, IdEdges
'    swMacroFeatureData.GetEntitiesNeedUserId TempCylOut, _
'        IdFaces, IdEdges
'    If Not (IsEmpty(IdFaces) And IsEmpty(IdEdges)) Then
'        swmRegenCBore = "Outer cylinder needs IDs"
'        Exit Function
'    End If
'
' If IDs were not added, use the FaceId property on the
'Face object to assign them.

  Dim vEditBodies As Variant
  vEditBodies = swMacroFeatureData.EditBodies
  Set PartBody = vEditBodies(0)
  Dim ResultBodiesPerm As Variant
  ResultBodiesPerm = PartBody.Operations2(SWBODYCUT, _
    TempCylOut, errorCode)
  
  swmRegenCBore = True
End Function

Public Function swmEditCBore(app As Variant, _
  swPart As Variant, feature As Variant) As Variant
  'We are editing the definition so display the PMP again to
  'modify the values.
  Dim MacroUI As New CMacroFeaturePropPage
  MacroUI.m_IsEditing = True
  Dim i_feature As SldWorks.feature
  Set MacroUI.swMacroFeatureParent = feature
  Set MacroUI.swMacroFeatureData = feature.GetDefinition
  MacroUI.swMacroFeatureData.AccessSelections swPart, Nothing
  
  Dim MyParamNames As Variant
  Dim MyParamTypes As Variant
  Dim MyParamValues As Variant
  'Get the driving parameters of the feature.
  MacroUI.swMacroFeatureData.GetParameters MyParamNames, _
    MyParamTypes, MyParamValues
  'Get the values of the parameters so that we can display
  'them in the Property Manager Page
  MacroUI.m_dDiameter = MyParamValues(0)
  MacroUI.m_dDepth = MyParamValues(1)
  MacroUI.PropPageMenuCallback
End Function

附录B 批量转换1

本例中,将一次性自动修改多个工程图的某一注释,如附图B-1所示。

Untitled