Files
Add or find attributes on all faces
Code
'This macro demonstrates how to add attributes to all faces.
'Precondition: single body part is open
'Results: Attributes are added to faces if not already present; otherwise attribute parameter _
values are printed to immediate window and owning face is selected.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim swBody As SldWorks.Body2
Dim swFace As SldWorks.Face2
Dim swEnt As SldWorks.Entity
Dim vBodies As Variant
Dim vFaces As Variant
Dim swAttDef As SldWorks.AttributeDef
Dim swAtt As SldWorks.Attribute
Dim swParam As SldWorks.Parameter
Dim i As Integer
Dim blnFound As Boolean
Dim dblArea As Double
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel.GetType <> swDocPART Then
MsgBox "Please open a part."
End
End If
swModel.ClearSelection2 True
'define and register attribute
Set swAttDef = swApp.DefineAttribute("FaceAtt")
swAttDef.AddParameter "FaceArea", swParamTypeDouble, 0, 0
swAttDef.Register
Set swPart = swModel
vBodies = swPart.GetBodies2(swSolidBody, False)
Set swBody = vBodies(0) 'assume one body
vFaces = swBody.GetFaces
For i = 1 To UBound(vFaces) + 1
Set swFace = vFaces(i - 1)
Set swEnt = swFace
Set swAtt = swEnt.FindAttribute(swAttDef, 0)
If swAtt Is Nothing Then
Set swAtt = swAttDef.CreateInstance5(swModel, swFace, "Face" & i, 0, swAllConfiguration)
Set swParam = swAtt.GetParameter("FaceArea")
dblArea = swFace.GetArea
swParam.SetDoubleValue2 dblArea, swAllConfiguration, Empty
Else
'It is good practice to use IAttribute::GetEntityState in conjuction with _
IEntity::FindAttribute. See latter method's API Help article.
If swAtt.GetEntityState(swIsEntityInvalid) = False And _
swAtt.GetEntityState(swIsEntitySuppressed) = False And _
swAtt.GetEntityState(swIsEntityAmbiguous) = False And _
swAtt.GetEntityState(swIsEntityDeleted) = False Then
Set swParam = swAtt.GetParameter("FaceArea")
Debug.Print swAtt.GetName & ": " & Round(swParam.GetDoubleValue, 5)
Set swEnt = swAtt.GetEntity2
swEnt.Select4 True, Nothing
End If
End If
Next i
End Sub
'Notes:
'If, in another macro, you want to do nothing more than find an existing attribute, then you _
need to know the attribute definition name, the attribute instance name, and the _
parameter name. You will need to 1) re-define the attribute definition and 2) re-register _
the attribute definition as well.
'Using IEntity::FindAttribute you can access multiple instances of the same attribute definition _
that might belong to a single entity. For example, a face with 10 instances of the same _
attribute definition. These instances much each have a unique attribute name. However you do _
not need to know the name of the instances when you access them with IEntity::FindAttribute. _
Instead, you specify an integer in the WhichOne argument that corresponds to the instances, _
beginning at 0. Zero is always the most recent instance added, and 9 would be the first _
instance of that attribute definition added. Moreover, the instances never step. For example, _
if instance 0 was deleted, the second-to-last instance would then correspond to 0, and the _
first instance ever added would correspond to 8.