Drawing iLogic to Automatically change colour by Sub Assembly name

Drawing iLogic to Automatically change colour by Sub Assembly name

brotherkennyh
Advocate Advocate
412 Views
6 Replies
Message 1 of 7

Drawing iLogic to Automatically change colour by Sub Assembly name

brotherkennyh
Advocate
Advocate

Hi,

 

I am after a little help with some iLogic code and don't know where to start.

 

I want an iLogic rule that will change all Occurrences of a part or sub Assembly of a given name to be a certain colour. I want to change the component property for colour and not select the model edges and add them to a coloured layer because I want the other line properties to be unaffected.

 

For Example all Occurrences of "SubAssembly1" on the drawing will turn blue and Occurrences of "SubAssembly2" on the drawing will turn green

 

We use a standard set of colours so these can be hard coded and not detected from the model in any way.

 

Can Anyone help with this?

 

Thanks in advance.

 

Kenny

0 Likes
413 Views
6 Replies
Replies (6)
Message 2 of 7

johnsonshiue
Community Manager
Community Manager

Hi! Here is a related discussion.

 

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/changing-line-colors-for-individual-...

 

I am moving this thread to Inventor iLogic, VB.Net forum.

Many thanks!



Johnson Shiue (johnson.shiue@autodesk.com)
Software Test Engineer
Message 3 of 7

brotherkennyh
Advocate
Advocate

Hi,

 

Thanks for the response. The code I found at the link is not quite working. Example below.

There are 2 problems with this.

 

The first one is that it only looks in the top level of assembly. It doesn't nest through multiple layers of sub-assemblies. How can I achieve that?

 

The next item, I wanted to select the component occurrence property and change that rather than select all the edges and change the edge properties. Is that possible?

 

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
    Call MsgBox("This macro only works on drawing documents.")
    Exit Sub
End If
Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim partStr As String = InputBox("Part to color:", "Prompt")


Dim refAssyDef As ComponentDefinition

Dim oColor As Color = ThisApplication.TransientObjects.CreateColor(255, 0, 0) 'RGB

Dim oTrans As Transaction = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Colorize [PART]")
For Each sheet As Sheet In oDoc.Sheets
    For Each view As DrawingView In Sheet.DrawingViews
        If View.ReferencedDocumentDescriptor.ReferencedDocumentType = DocumentTypeEnum.kPresentationDocumentObject Then
            refAssyDef = View.ReferencedDocumentDescriptor.ReferencedDocument.ReferencedDocuments(1).ComponentDefinition
        ElseIf View.ReferencedFile.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
            refAssyDef = View.ReferencedFile.DocumentDescriptor.ReferencedDocument.ComponentDefinition
        End If

        If (refAssyDef Is Nothing) Then
            Continue For
        End If
        For Each occurrence As ComponentOccurrence In refAssyDef.Occurrences
            If occurrence.Name Like partStr & ":*" Then
                Try
                    Dim ViewCurves As DrawingCurvesEnumerator = View.DrawingCurves(occurrence)
                    For Each c As DrawingCurve In ViewCurves

                        c.Color = oColor

                    Next
                Catch ex As Exception
                End Try
            End If
        Next
    Next
Next
oTrans.End()

 

0 Likes
Message 4 of 7

brotherkennyh
Advocate
Advocate

I also have some code for a similar problem from one of my own past posts. Sadly I have not become more familiar with iLogic in the mean time. Link to previous problem below.

The code here works through every component and searches multiple layers of sub assembly including patterns.

The problem is that it layers the components and colours the layer. Doing this enforces other properties like line type. I want to just change the colour, hence the desire to change the occurrence property in the browser.

 

Solved: update drawing line colour based on part colour - Autodesk Community - Inventor

0 Likes
Message 5 of 7

brotherkennyh
Advocate
Advocate

Hi,

 

I am just posting some code I got working.

 

This selects the component edges and changes the property of those. I would still prefer to change the component property if someone knows how to do this?

 

I am also open to any improvements.

 

sub Main
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
    Call MsgBox("This macro only works on drawing documents.")
    Exit Sub
End If
Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim refAssyDef As ComponentDefinition
Dim oColor As Color


Dim oTrans As Transaction = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Colorize [PART]")
For Each sheet As Sheet In oDoc.Sheets
    For Each view As DrawingView In Sheet.DrawingViews
        If View.ReferencedDocumentDescriptor.ReferencedDocumentType = DocumentTypeEnum.kPresentationDocumentObject Then
            refAssyDef = View.ReferencedDocumentDescriptor.ReferencedDocument.ReferencedDocuments(1).ComponentDefinition
        ElseIf View.ReferencedFile.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
            refAssyDef = View.ReferencedFile.DocumentDescriptor.ReferencedDocument.ComponentDefinition
        End If

        If (refAssyDef Is Nothing) Then
            Continue For
        End If


		For Each occurrence As ComponentOccurrence In refAssyDef.Occurrences
				
			if occurrence.Name.Contains("SubAssembly1")Then
				oColor = ThisApplication.TransientObjects.CreateColor(0, 0, 255) 'RGB Blue
				Call ProcessColour(occurrence, view, oColor)
			end if
			
			if occurrence.Name.Contains("SubAssembly2")Then
				oColor = ThisApplication.TransientObjects.CreateColor(0, 128, 0) 'RGB Green
				Call ProcessColour(occurrence, view, oColor)
			end if
			
			if occurrence.Name.Contains("SubAssembly3") Then
				oColor = ThisApplication.TransientObjects.CreateColor(128, 0, 255) 'RGB Purple
				Call ProcessColour(occurrence, view, oColor)
			end if
					

        Next
		
		
		
    Next
Next

oTrans.End()
end sub

Private Sub ProcessColour(occurrence As ComponentOccurrence, view As DrawingView, oColor As Color )

            
			    Try
                    Dim ViewCurves As DrawingCurvesEnumerator = View.DrawingCurves(occurrence)
					
                    For Each c As DrawingCurve In ViewCurves

                        c.Color = oColor

                    Next
                Catch ex As Exception
					msgbox(ex)
                End Try
            
End Sub

 

0 Likes
Message 6 of 7

A.Acheson
Mentor
Mentor

If your looking to change the occurrence appearance you would do this in the assembly or you can change the part appearance directly in the part file. I would suggest to do this first in the assembly. Here is the VBA sample from the API help file. There are many ilogic equivalent post in this forum so you will likely find one allready for this purpose. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 7 of 7

brotherkennyh
Advocate
Advocate

Hi,

 

Thanks for the response. I am looking to change the assembly occurrence as it appears on the drawing. Menu shown below for clarity.

 

The code I provided does find and colour the desired component by name in all views, but it does so by selecting the edges and changing the edge properties. I would prefer to change the occurrence properties, as below, if this is possible. In fact I don't know how to change any of these properties through iLogic.

capture1.pngcapture2.png

0 Likes