How to Automatically Assign Layers to Angle Components and Retain Hidden Line Styles in Inventor .idw

How to Automatically Assign Layers to Angle Components and Retain Hidden Line Styles in Inventor .idw

william_brown11
Explorer Explorer
260 Views
2 Replies
Message 1 of 3

How to Automatically Assign Layers to Angle Components and Retain Hidden Line Styles in Inventor .idw

william_brown11
Explorer
Explorer

I’m currently working on automating the layer assignment in my .idw drawings. I have a script that already automatically assigns components to their respective layers based on the material (e.g., assigning a specific layer to Angle Steel A36 or Tube Steel A36). This is working well for material-based layer assignment, but I am facing an issue with angle components and hidden lines.

 

Retaining the Hidden Line Style for Hidden Lines:

  • For hidden lines (lines that are not visible in the drawing view but are part of the geometry), I want them to remain as hidden lines (i.e., dashed lines) even after the layer is assigned.
  • The goal is that while the angle components are assigned to the 'Angle' layer, hidden lines should not change from their dashed appearance but still be part of that layer.

What I Have So Far:

  • I already have a script that changes the specified material to its corresponding layer. For example, all angle components are assigned to a Angle Steel A36 layer.
  • However, the hidden lines (that are part of these components) are not retaining their dashed line style after the layer change.

What I’m Looking For:

  • A solution that allows me to automatically assign the 'Angle' layer to angle components, but also ensures that hidden lines (such as the hidden parts of an angle component) remain dashed, as they should be according to the hidden line style.

Below is a picture of the angles showing the visible and hidden lines that I wish to maintain after running the script and the script itself. 

 

If anyone can point me in the right direction or provide a script that can solve this issue, it would be greatly appreciated!

 

Thank you!

 

 

william_brown11_0-1735234633615.png

 

 

Sub Main
    drawDoc = TryCast(ThisDoc.Document, DrawingDocument)
    If (drawDoc Is Nothing) Then
       MessageBox.Show("This rule can only be run in a drawing,", "iLogic")
       Return
    End If
    ChangeLayerOfOccurrenceCurves()
End Sub

Private drawDoc As DrawingDocument

Public Sub ChangeLayerOfOccurrenceCurves()
    ' Process the occurrences, wrapping it in a transaction so the 
    ' entire process can be undone with a single undo operation. 
    Dim trans As Transaction
    trans = ThisApplication.TransactionManager.StartTransaction(drawDoc, "Drawing Layers by Materials")
    Try

        For Each dSheet As Sheet In drawDoc.Sheets
            For Each drawView As DrawingView In dSheet.DrawingViews
                ' Call the recursive function that does all the work. 
                Dim docDesc As DocumentDescriptor
                docDesc = drawView.ReferencedDocumentDescriptor
                If (docDesc Is Nothing) Then Continue For
                If (docDesc.ReferencedDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject) Then Continue For
                Dim asmDef As AssemblyComponentDefinition
                asmDef = docDesc.ReferencedDocument.ComponentDefinition
                Call ProcessOccurrences(drawView, asmDef.Occurrences)
            Next
        Next
    Catch ex As Exception
        trans.Abort()
        Throw ex
    End Try
    trans.End()
End Sub


Private Sub ProcessOccurrences(ByVal drawView As DrawingView, _
   ByVal Occurrences As ComponentOccurrences)
    ' Iterate through the current collection of occurrences. 
    Dim occ As ComponentOccurrence
    For Each occ In Occurrences
        If (occ.Suppressed) Then Continue For
        If (occ.Definition Is Nothing) Then Continue For
        ' Check to see if this occurrence is a part or assembly. 
        If occ.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
            ' ** It's a part so get the material
            Dim subDoc As Document = occ.Definition.Document
            If (subDoc Is Nothing) Then Continue For
            Dim partDoc As PartDocument = subDoc
            Dim materialName = partDoc.ComponentDefinition.Material.Name

            ' Get the TransientsObjects object to use later. 
            Dim transObjs As TransientObjects
            transObjs = ThisApplication.TransientObjects

            ' Verify that a layer exists for this material. 
            Dim layers As LayersEnumerator
            layers = drawDoc.StylesManager.Layers

            On Error Resume Next
            Dim newLayer As Layer
            newLayer = layers.Item(materialName)

            If Err.Number <> 0 Then
                On Error Goto 0

                ' Copy an arbitrary layer giving it the name 
                ' of the material. 
                newLayer = layers.Item(1).Copy(materialName)

                ' Set the attributes of the layer to use the color, 
                ' have a solid line type, and a specific width. 
                '                    newLayer.Color = newColor
                newLayer.LineType = LineTypeEnum.kContinuousLineType
                'newLayer.LineWeight = 0.02
            End If
            On Error Goto 0

            ' Get all of the curves associated with this occurrence. 
            On Error Resume Next
            Dim drawcurves As DrawingCurvesEnumerator
            drawcurves = drawView.DrawingCurves(occ)
            If Err.Number = 0 Then
                On Error Goto 0

                ' Create an empty collection. 
                Dim objColl As ObjectCollection
                objColl = transObjs.CreateObjectCollection()

                ' Add the curve segments to the collection. 
                Dim drawCurve As DrawingCurve
                For Each drawCurve In drawcurves
                    Dim segment As DrawingCurveSegment
                    For Each segment In drawCurve.Segments
                        objColl.Add(segment)
                    Next
                Next

                ' Change the layer of all of the segments. 
                Call drawView.Parent.ChangeLayer(objColl, newLayer)
            End If
            On Error Goto 0
        Else
            ' It's an assembly so process its contents. 
            Call ProcessOccurrences(drawView, occ.SubOccurrences)
        End If
    Next
End Sub

 

 

0 Likes
261 Views
2 Replies
Replies (2)
Message 2 of 3

william_brown11
Explorer
Explorer

If anyone has any tips, suggestions, or a similar script to share, I’d really appreciate it. Thanks again!

0 Likes
Message 3 of 3

SBix26
Consultant
Consultant

I expect you'll get more and quicker answers to coding questions in the Inventor Programming - iLogic, Macros, AddIns & Apprentice forum.


Sam B

Inventor Pro 2025.2 | Windows 11 Home 24H2
autodesk-expert-elite-member-logo-1line-rgb-black.png

0 Likes