Changing Line Colors for Individual Parts in Drawing Views With iLogic

dusan.naus.trz
Advisor
Advisor

Changing Line Colors for Individual Parts in Drawing Views With iLogic

dusan.naus.trz
Advisor
Advisor

Hi,
I found the code for VBA on the forum
I wanted to modify this code for iLogic, but I can't do it at all, I'm constantly getting bugs or nothing.

Original Code for VBA is OK

Sub ChangePartColor()
    Dim oDoc As DrawingDocument, partStr As String
    If ThisApplication.ActiveDocumentType <> kDrawingDocumentObject Then
        Call MsgBox("This macro only works on drawing documents.")
        Exit Sub
    End If
    Set oDoc = ThisApplication.ActiveDocument
    partStr = InputBox("Part to color:", "Prompt")
    Dim oTrans As Transaction, i, j, k, c
    Dim ViewCurves As DrawingCurvesEnumerator, refAssyDef As ComponentDefinition, oColor As color
    
    Set oColor = ThisApplication.TransientObjects.CreateColor(255, 0, 0) 'RGB
    
    Set oTrans = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Colorize [PART]")
        For Each i In oDoc.Sheets
        For Each j In i.DrawingViews
            If j.ReferencedDocumentDescriptor.ReferencedDocumentType = kPresentationDocumentObject Then
                Set refAssyDef = j.ReferencedDocumentDescriptor.ReferencedDocument.ReferencedDocuments(1).ComponentDefinition
            ElseIf j.ReferencedFile.DocumentType = kAssemblyDocumentObject Then
                Set refAssyDef = j.ReferencedFile.DocumentDescriptor.ReferencedDocument.ComponentDefinition
            End If

            For Each k In refAssyDef.Occurrences
                If k.name Like partStr & ":*" Then
                    Set ViewCurves = j.DrawingCurves(k)
                    For Each c In ViewCurves
                        c.color = oColor
                    Next
                End If
            Next
        Next
    Next
    oTrans.End
    
End Sub

 

That's how I changed it, but it still doesn't work for me. I think the error is on this line = Dim oTrans As Transaction

iLogic = doesn't work

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

	Dim ViewCurves As DrawingCurvesEnumerator
	Dim refAssyDef As ComponentDefinition
	Dim oColor As Color
    
    oColor = ThisApplication.TransientObjects.CreateColor(255, 0, 0) 'RGB
    
    oTrans = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Colorize [PART]")
        For Each i In oDoc.Sheets
        For Each j In i.DrawingViews
            If j.ReferencedDocumentDescriptor.ReferencedDocumentType = kPresentationDocumentObject Then
                refAssyDef = j.ReferencedDocumentDescriptor.ReferencedDocument.ReferencedDocuments(1).ComponentDefinition
            ElseIf j.ReferencedFile.DocumentType = kAssemblyDocumentObject Then
                refAssyDef = j.ReferencedFile.DocumentDescriptor.ReferencedDocument.ComponentDefinition
            End If

            For Each k In refAssyDef.Occurrences
                If k.Name Like partStr & ":*" Then
                    ViewCurves = j.DrawingCurves(k)
                    For Each c In ViewCurves
                        c.Color = oColor
                    Next
                End If
            Next
        Next
    Next
    oTrans.End
    
End Sub

 

0 Likes
Reply
Accepted solutions (1)
1,248 Views
3 Replies
Replies (3)

JelteDeJong
Mentor
Mentor
Accepted solution

in iLogic exception handling is done different from vba. in vba you have "OnErrorResume next" (could be written a bitt difference. not an expert on VBa) in iLogic you need to use Try/Catch blocks.

 

try this:

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()

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

dusan.naus.trz
Advisor
Advisor

Hi,
It works great. Thank you very much for your help. I am sending the result to others as well. Attached video.

2020-12-20_15h41_15.png

0 Likes

julien.avril
Enthusiast
Enthusiast

Bonjour

 

Avez-vous la variante avec les sous-niveau ? car ca ne fonctionne pas avec les pièces sous un assemblage?

Exemple ci-dessous:  ca fonctionne avec module mais pas avec planche qui ce trouve sous l'assemblage module

 

julienavril_0-1635230233848.png

 

0 Likes