MACRO LIBRARY

Get belt length and add to belt component's custom properties

Free public access. No account or subscription required.

Files

Get belt length and add to belt component's custom properties

Code

Module: mMain

'Writes the length of each belt assembly feature to the custom properties
'   of the belt part component.
    
'Precondition: Assembly is open.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Private Const CUST_PROP_NAME_POSTFIX As String = " Länge"
    
Option Explicit
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    TraverseModel swModel, 1
    
End Sub
    
Private Sub TraverseModel(model As Object, nLevel As Integer)
    Dim swFeat As Feature
    Dim swComp As Component2
    Dim swCompModel As ModelDoc2
    Dim dblLength As Double
    Dim i As Integer
    
    Set swFeat = model.FirstFeature
    
    Do While Not swFeat Is Nothing
        If swFeat.GetTypeName = "Reference" Then
            Set swComp = swFeat.GetSpecificFeature2
            TraverseModel swComp, nLevel + 1
        ElseIf swFeat.GetTypeName = "BeltProfileFeature" Then
            dblLength = GetBeltLength(swFeat)
            Set swComp = model
            Set swCompModel = swComp.GetModelDoc2
            If dblLength <> -1 Then WriteCustProp swCompModel, swFeat.Name, Round(dblLength * 1000)
        End If
        
        Set swFeat = swFeat.GetNextFeature
    Loop
End Sub
    
Private Function GetBeltLength(swFeat As Feature) As Double
    GetBeltLength = 0
    
    Dim swSketch As Sketch
    Set swSketch = swFeat.GetSpecificFeature2
    
    Dim vSegs As Variant
    vSegs = swSketch.GetSketchSegments
    
    If IsEmpty(vSegs) Then
        GetBeltLength = -1
        Exit Function
    End If
    
    Dim i As Integer
    For i = 0 To UBound(vSegs)
        Dim swSeg As SketchSegment
        Set swSeg = vSegs(i)
        GetBeltLength = GetBeltLength + swSeg.GetLength
    Next i
End Function
    
Private Function WriteCustProp(swModel As ModelDoc2, strBeltName As String, dblBeltLength As Double)
    Dim swCustPropMgr As CustomPropertyManager
    Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty)
    swCustPropMgr.Add3 strBeltName & CUST_PROP_NAME_POSTFIX, _
        swCustomInfoText, CStr(dblBeltLength) & "mm", swCustomPropertyDeleteAndAdd
End Function