• Blog posts


    by Clint Brown

    Have you ever exported your Inventor Drawing files to AutoCAD, and wish that you could have each part individually on it’s own layer, instead of Inventor’s pre-defined layering system? Well now you can, through the magic of iLogic.

    I ran across some iLogic code on an Autodesk forum over a year ago, and have been meaning to use it in a blog. Basically what the code below does, is it gets the material of each part in the Assembly and it puts it onto a unique layer, by material. The catch here, is that if you had several Stainless Steel parts, their geometry would be shown on the same layer.

    If you wanted each individual part on it’s own layer, you would need to define a different material to each part (at least temporarily) this would then allow the drawing to create a layer for each material. A nice easy way (depending on size of the overall assembly) to do this, is to change the material of components from the BOM in the Assembly, this makes it much easier, as you do not have to open each individual part.

    To use this code, create a new iLogic rule in your Drawing file, and run it, this will copy the materials over to layers as described above. There are some limitations to this, you would need to run the rule every time you added a new drawing view, or add new parts to the assembly, but the result is very good

    Sub Main
        drawDoc = TryCast(ThisDoc.Document, DrawingDocument)
        If (drawDoc Is Nothing) Then
           MessageBox.Show("This rule can only be run in a drawing,", "iLogic")
        End If
    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")
            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)
        Catch ex As Exception
            Throw ex
        End Try
    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
                    ' Change the layer of all of the segments. 
                    Call drawView.Parent.ChangeLayer(objColl, newLayer)
                End If
                On Error Goto 0
                ' It's an assembly so process its contents. 
                Call ProcessOccurrences(drawView, occ.SubOccurrences)
            End If
    End Sub

    The original posting of this code can be found here: