Ilogic code for changing drawing properties

Ilogic code for changing drawing properties

jaconoorland45
Contributor Contributor
393 Views
5 Replies
Message 1 of 6

Ilogic code for changing drawing properties

jaconoorland45
Contributor
Contributor

Dear users,

 

I want to make a piece of ilogic code which instantly changes all lines from a (sub)assembly within a inventor drawing to a new line weight and line Color. So far everything i have tried failed. You can do something with ilogic code such as "select as edges" and then go line by line and change them to the desired properties. Only this doesn't work because it is about 10.000 lines and it takes 30 minutes or longer. The manual Inventor window option which is shown in the screenshots below only takes 1 second. Does anyone know how to acces this part of the inventor API with ilogic? Your help is much appreciated!

jaconoorland45_0-1791195908075.pngjaconoorland45_1-1791195932803.png

 

 

0 Likes
394 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Hi @jaconoorland45.  Unfortunately, although what you want to do is 'possible' through Inventor's API, there is no 'simple & easy' way to do it through Inventor's API.  There are multiple ways to accomplish it, but some code paths to get it done will process faster than others though.  The closest 'built-in' method for doing something similar to that is the relatively new DrawingView.SetComponentLineColor (new in 2025 version), but that is only for 'Color', not any other aspects of the view geometry like line style, like weight, or Layer.  The next best (fastest processing) scenario that comes to mind is one that involves creating & customizing a new Layer with the color & line weight you want, then using the Sheet.ChangeLayer method, which instantly changes all drawing curves within the input ObjectCollection to the input Layer.  The slowest path involves changing those specific properties of each individual drawing curve as you iterate through them.  In each code path though, you must first navigate through the document that the drawing view directly references, then step down into its components and there geometry, and have a good enough understanding of how the 'proxy' system works in Inventor's assemblies to be able to retrieve the proxy model references required to use as input into the API methods involved.  In most cases you will usually need to use the DrawingView.DrawingCurves property.  That property can work like a Function when you input a model object for it to retrieve the drawing curves of.  Doing that is much faster than simply iterating through all view curves, then trying to figure out which ones are associated with the specific lower level component geometry you are looking for.  There are many similar forum posts about wanting to change specific portions of drawing view geometry in some way, so there likely some pretty good example codes already available to start from, which could then be modified to suit your specific needs.  Changing minor details about some of the geometry within a DrawingView is a very custom task which usually requires being very familiar with all the models involved and how the drawing was made, while having all those files locally available for code-based trial & error testing as the code is developed.

Edit:  You can review the code in the following forum Links as just a couple similar examples:

https://forums.autodesk.com/t5/inventor-programming-forum/possible-to-assign-a-specific-line-type-ba... 

https://forums.autodesk.com/t5/inventor-programming-forum/change-component-property-in-drawing/m-p/1... 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

jaconoorland45
Contributor
Contributor

Thank you for your quick reply! Something that I don't understand is the following: Why can it be done so easily in the UI but is impossible in iLogic? (Also I am bound to use inventor 2023 because of the company i work for)

0 Likes
Message 4 of 6

WCrihfield
Mentor
Mentor

Hi @jaconoorland45.  A lot of us who have done some Inventor automation coding have asked the same question many times (why doing it by code is so much more complicated).  I likely do not have the best answer for that, since I never worked for Autodesk in developing Inventor.  I do know that when we do things manually, using Inventor's user interface, a ton of code resources are monitoring and reacting to our every move, click, keyboard key stroke, and so on, in an attempt to determine or help with what we are currently doing, and predict what we want to do next, and to make doing it as simple & easy as possible.  Part of that involves showing custom dialogs that are not part of Inventor's API, then reacting to our interactions within those dialogs.  This is partly because, when we are doing things by code, we usually want to keep any manual user interactions down to a minimum, which usually does not involve any visual dialogs.  I like to think of the code resources in Inventor's API in two main categories:  kinetic & utility versus UI support & reactive.  The kinetic & utility stuff is the exposed portion of the API's general 'object model' behind the documents, features, sketches, parameters, constraints, & geometry objects involved.  The UI support & reactive stuff is the exposed portion of the API's object model which defines and supports Inventor's user interface, such as its model screen, its ribbons, its model browser tree, its dock-able windows, and all of the visible & invisible tools within all of those.  When we click on a button in Inventor's ribbon, or press a keyboard shortcut to start a built-in 'command', that usually 'executes' something like a ControlDefinition.  When that happens, it runs a block of code defined somewhere within Inventor's installation files that we never see, and often do not have much control over by code after that point.  It can change Inventor's environment, and/or show some sort of command specific dialog which we can only interact with manually, but not by code.  On the other hand, when we utilize the kinetic/utility side API, we are almost completely in control of everything that will happen, without all of that automatic, reactive, support stuff being involved, because there is no way for Inventor to monitor or react to us typing code into a code editing window.  In most cases, the API objects, methods, & properties involved in doing most of the major or popular stuff are relatively easy to use, and make a lot of logical sense.  But there have always been some limitations, where not everything that we can do manually, we can do by code through its API.  As time goes on, the developers at Autodesk either 'expose' more stuff to the publicly accessible API, or add more new stuff to it.  At first, waaaaay back there, almost everything was originally designed for manual user interactions, with no code way to do it.  Then people started asking how they could 'automate' stuff in their software by code, and thus begins the long journey of developing Inventor's API for us to use.   Some tasks are simply still not popular enough for them to spend a lot of time developing something custom for, or the way everything else was developed over time ended up not being a great fit for certain highly custom tasks much later on.

I'm in a similar situation...still stuck using Inventor Pro 2024.4.3 at the moment.  We were supposed to get updated to 2026 a while back, but that still hasn't happened.  Gone are the days when common sense and decent morals ruled the day and we could all fully manage our own work stations without fear of countless AI empowered evil doers attempting to mess it all up for their own profit, pleasure, or hate.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 6

lauri_barnhart
Autodesk
Autodesk

Greetings, @jaconoorland45.

 

Hope you are well. When you have a moment, please review the information provided by WCrihfield to see if it helps resolve your question. If it did, please consider clicking on the 'Accept Solution' button.

 

Thanks,

Let us know how we did on Trustpilot.


Lauri | Community Manager

0 Likes
Message 6 of 6

info28T45
Explorer
Explorer

Hi @jaconoorland45, short version: the Sheet.ChangeLayer route WCrihfield described works, and the rule below does it, but it's about 4 times faster than curve by curve, not instant like the UI. It finds the component by its name (or its path, if it sits deeper) in the assembly, collects every line segment of it in all views of the active sheet, and moves them in one call to a copy of their layer with your color and weight. Each original layer gets its own copy, so hidden lines stay dashed.

forum_layer_by_component.png

On a test sheet about your size (a sub-assembly of 60 plates with 80 holes each, placed twice, plus one other part, 3 views) it moved 10,410 segments in about 30 s, rule start included; curve by curve took about 132 s on the same sheet. Your loop takes 30 minutes, mine 132 s, so something else may be slowing yours down too (machine, sheet, or how the loop is written): I can't promise your seconds, only the ratio. About two thirds of the time goes into reading the segments, and that part the ChangeLayer call doesn't save. The picture is a smaller example, a tank agitator on its frame, same rule (orange there is red in the drawing).

 

About the why: I can't see inside the UI, but my guess is that it stores the color and weight on the component in the view, as one setting, without touching the curves. As far as I can tell, 2023 has no API for that kind of setting, so code has to read the curves first. Inventor 2025.1 added one (DrawingView.SetComponentLineColor, color only): on the same big sheet it took 0.16 s, close to your 1 s in the UI. If you upgrade, that's instant for the color; the weight still needs the layer rule.

' Moves every line of one component (part or sub-assembly, at any level) to its own layer, in every view of the active sheet.
' Sheet.ChangeLayer moves a whole collection in one call, instead of editing the curves one by one.
' Each original layer (visible, hidden...) gets its own copy, so hidden lines stay dashed.
Dim sOccPath As String = "AGITATOR:1"   ' CHANGE: component as shown in the assembly browser; if it sits inside
                                        ' another sub-assembly, the whole path with "/", e.g. "MIXER:1/AGITATOR:1"
Dim iRed As Integer = 255            ' CHANGE: line color (red, green, blue 0-255)
Dim iGreen As Integer = 0
Dim iBlue As Integer = 0
Dim dWeightCm As Double = 0.07       ' CHANGE: line weight in cm (0.07 = 0.7 mm)
Dim sSuffix As String = " - " & sOccPath.Replace("/", " > ")   ' new layers: "<original layer> - <component path>"

Dim oDrawDoc As DrawingDocument = ThisDoc.Document
Dim oSheet As Sheet = oDrawDoc.ActiveSheet
Dim oColor As Inventor.Color = ThisApplication.TransientObjects.CreateColor(iRed, iGreen, iBlue)
Dim oGroups As New Dictionary(Of String, ObjectCollection)
Dim aPath() As String = sOccPath.Split("/"c)
Dim iCount As Integer = 0

For Each oView As DrawingView In oSheet.DrawingViews
    Dim oOcc As ComponentOccurrence = Nothing
    Dim oCurves As DrawingCurvesEnumerator = Nothing
    Try
        Dim oAsmDoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument
        oOcc = oAsmDoc.ComponentDefinition.Occurrences.ItemByName(aPath(0))
        For i As Integer = 1 To aPath.Length - 1   ' walk down the path, one level at a time
            Dim oNext As ComponentOccurrence = Nothing
            For Each oSub As ComponentOccurrence In oOcc.SubOccurrences
                If oSub.Name = aPath(i) Then
                    oNext = oSub
                    Exit For
                End If
            Next
            If oNext Is Nothing Then Throw New Exception(aPath(i) & " not found")
            oOcc = oNext
        Next
        oCurves = oView.DrawingCurves(oOcc)
    Catch
        Continue For   ' view of another model, or the component is not in this view
    End Try
    For Each oCurve As DrawingCurve In oCurves
        For Each oSeg As DrawingCurveSegment In oCurve.Segments
            Dim sLayer As String = oSeg.Layer.Name
            If sLayer.EndsWith(sSuffix) Then Continue For   ' already moved by an earlier run
            If Not oGroups.ContainsKey(sLayer) Then
                oGroups.Add(sLayer, ThisApplication.TransientObjects.CreateObjectCollection)
            End If
            oGroups(sLayer).Add(oSeg)
            iCount += 1
        Next
    Next
Next

For Each sLayer As String In oGroups.Keys
    Dim oNewLayer As Layer = Nothing
    Try
        oNewLayer = oDrawDoc.StylesManager.Layers.Item(sLayer & sSuffix)
    Catch
        oNewLayer = oDrawDoc.StylesManager.Layers.Item(sLayer).Copy(sLayer & sSuffix)
    End Try
    oNewLayer.Color = oColor
    oNewLayer.LineWeight = dWeightCm
    oSheet.ChangeLayer(oGroups(sLayer), oNewLayer)
Next

Logger.Info(iCount & " line segments of " & sOccPath & " moved to " & oGroups.Count & " layer(s)")

Change sOccPath to your sub-assembly as it shows in the browser, plus the color and the weight. If it sits inside another sub-assembly, put the whole path, like MIXER:1/AGITATOR:1. I tried that one level down too (two copies of the same assembly, each with the same sub-assembly inside): only the copy in the path changed, the one with the same name in the other copy kept its lines. Running it a second time on the same sheet moves nothing and adds no layers, and the copy of the same sub-assembly next to it stays as it was.

 

I ran it in Inventor 2025. The API help lists ChangeLayer since 2011, so 2023 should have it, but I haven't tried this rule in 2023.

 

I wrote it up with the measurements here: https://steptocut.com/blog/inventor-drawing-change-line-color-ilogic/

 

If more of your drawing cleanup ends up in rules, feel free to ask.

 

Written with Claude (AI), tested in Inventor 2025 on the test sheet above.

 

0 Likes