occurrence name in drawing

occurrence name in drawing

clamuro-IPEC
Enthusiast Enthusiast
2,862 Views
8 Replies
Message 1 of 9

occurrence name in drawing

clamuro-IPEC
Enthusiast
Enthusiast

Let me start out by saying that I have zero experience with Inventor API or code in general.

I am trying to assign the same .ipt files unique occurrence names in an assembly and get them to display either balloons or custom symbols (larger oval balloons).  I have found some code through google that will add occurrence names to the balloon (ITEM + OCCURRENCE) if there is a qty of (2+)

 

link to code:

https://vasampelas.wordpress.com/2015/10/25/autodesk-inventor-api-component-occurrence-name-on-ballo... 

 

Is there a way to adapt this to displaying only the occurrence name for ALL components in the drawing?

 

 

 

0 Likes
Accepted solutions (1)
2,863 Views
8 Replies
Replies (8)
Message 2 of 9

MechMachineMan
Advisor
Advisor

I'll give you a couple of pretty big HINTS.

 

Public Sub GetComponentReferencedByBalloon()
    ' Set a reference to the active drawing document
    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument
    
	' Set Reference to Active Sheet on Drawing
    Dim oSheet As Sheet
    Set oSheet = oDoc.ActiveSheet
    
    ' Iterate through all the balloon in Sheet
    Dim oBalloon As Balloon
    For Each oBalloon In oSheet.Balloons
    
		' Set Refrence to the Leader attached to the balloon
        Dim leader As leader
        Set leader = oBalloon.leader
        
        'assuming the leader is a single line segment
        Dim leaderNode As leaderNode
        Set leaderNode = leader.AllNodes(2)
    
		' Set the intent of the leader
        Dim intent As GeometryIntent
        Set intent = leaderNode.AttachedEntity
		
		' Set Reference to the curve that the leader points to
        Dim curve As DrawingCurve
        Set curve = intent.Geometry

		' Set Reference to the geoetry that the curve belongs to
        Dim oModelGeom As Object
        Set oModelGeom = curve.ModelGeometry

		' Get component occurance that the model geometry belongs to
        Dim occurrence As ComponentOccurrence
        Set occurrence = oModelGeom.ContainingOccurrence
    
        ' MsgBox "The part number is: " & occurrence.Name
        
		' Iterate through all the balloon sets 
        Dim oBalloonValueSet As BalloonValueSet
        For Each oBalloonValueSet In oBalloon.BalloonValueSets
            'Dim strDisplay As String
            ' strDisplay = "Balloon Item Number: "
            
            ' Set Reference to the Quantity value of the component from the BOM
            Dim oCount As Integer
            oCount = oBalloonValueSet.ReferencedRow.BOMRow.ItemQuantity
            
            ' Set Reference to the Item Number on the BOM
            Dim oBalloonNumber As Integer
            oBalloonNumber = oBalloonValueSet.ItemNumber
            
            ' MsgBox "Balloon Number: " & oBalloonNumber & " - " & oCount
            
            ' If More than One Components, update Balloon with Item Number & Occurance Name
            If oCount > 1 Then
                oBalloonValueSet.OverrideValue = oBalloonValueSet.Value & " - " & occurrence.Name
            Else
                ' Do Nothing
            End If
            
            Dim oDrawingBOMRow As DrawingBOMRow
            Set oDrawingBOMRow = oBalloonValueSet.ReferencedRow
            
            If oDrawingBOMRow.Custom Then
                ' The referenced item is a custom parts list row.
                ' strDisplay = strDisplay & vbNewLine & "Referenced Component(s):"
                ' strDisplay = strDisplay & vbNewLine & "     Custom PartsList Row"
            Else
                Dim oBOMRow As BOMRow
                Set oBOMRow = oDrawingBOMRow.BOMRow
                
                ' Add the Item Number from the model BOM.
                ' strDisplay = strDisplay & vbNewLine & "BOM Item Number: " & oBOMRow.ItemNumber
                ' strDisplay = strDisplay & vbNewLine & "Referenced Component(s):"
                
                Dim oCompDefs As ComponentDefinitionsEnumerator
                Set oCompDefs = oBOMRow.ComponentDefinitions
                   
                If oDrawingBOMRow.Virtual Then
                    ' The referenced item is a virtual component.
                    ' strDisplay = strDisplay & vbNewLine & "     Virtual: " & oCompDefs.Item(1).DisplayName
                Else
                  
                    ' Add the document name of the referenced component.
                    ' There could be multiple if the balloon references
                    ' a merged BOM row in the model.
                    Dim oCompDef As ComponentDefinition
    
                    ' strDisplay = vbNewLine & strDisplay & vbNewLine & "     " & oPartDef.Document.FullDocumentName
                End If
            End If
          '  MsgBox strDisplay
        Next
    Next
End Sub

If you read the bolded stuff above, you should be able to get a pretty good idea of how to add occurrence name to a balloon 

' If More than Zero Components, update Balloon with Item Number & Occurance Name  

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 9

clamuro-IPEC
Enthusiast
Enthusiast

Would it be possible to adapt this code to work with balloons that have a multi-segment leader?

 

Currently, it gives the following error when the drawing contains multi-segment leaders

 

Run-time error'91':

Object variable or With block variable not set

 

 

When you enter Debug, it highlights the segment of code that assumes a single segment leader.

 

 

 

 

 

 

Public Sub GetComponentReferencedByBalloon()
    ' Set a reference to the active drawing document
    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    ' Set Reference to Active Sheet on Drawing
    Dim oSheet As Sheet
    Set oSheet = oDoc.ActiveSheet
    
    ' Iterate through all the balloon in Sheet
    Dim oBalloon As Balloon
    For Each oBalloon In oSheet.Balloons
    
        ' Set Refrence to the Leader attached to the balloon
        Dim leader As leader
        Set leader = oBalloon.leader
        
        'assuming the leader is a single line segment
        Dim leaderNode As leaderNode
        Set leaderNode = leader.AllNodes(2)
    
        ' Set the intent of the leader
        Dim intent As GeometryIntent
        Set intent = leaderNode.AttachedEntity
        
        ' Set Reference to the curve that the leader points to
        Dim curve As DrawingCurve
        Set curve = intent.Geometry

        ' Set Reference to the geoetry that the curve belongs to
        Dim oModelGeom As Object
        Set oModelGeom = curve.ModelGeometry

        ' Get component occurance that the model geometry belongs to
        Dim occurrence As ComponentOccurrence
        Set occurrence = oModelGeom.ContainingOccurrence
        
        ' Iterate through all the balloon sets
        Dim oBalloonValueSet As BalloonValueSet
        For Each oBalloonValueSet In oBalloon.BalloonValueSets
            'Dim strDisplay As String
            ' strDisplay = "Balloon Item Number: "
            
            ' Set Reference to the Quantity value of the component from the BOM
            Dim oCount As Integer
            oCount = oBalloonValueSet.ReferencedRow.BOMRow.ItemQuantity
            
            ' Set Reference to the Item Number on the BOM
            Dim oBalloonNumber As Integer
            oBalloonNumber = oBalloonValueSet.ItemNumber
            
            ' If More than One Components, update Balloon with Item Number & Occurance Name
            If oCount > 0 Then
                oBalloonValueSet.OverrideValue = occurrence.Name
            Else
                ' Do Nothing
            End If
            
            Dim oDrawingBOMRow As DrawingBOMRow
            Set oDrawingBOMRow = oBalloonValueSet.ReferencedRow
            
            If oDrawingBOMRow.Custom Then
                ' The referenced item is a custom parts list row.
                ' strDisplay = strDisplay & vbNewLine & "Referenced Component(s):"
                ' strDisplay = strDisplay & vbNewLine & "     Custom PartsList Row"
            Else
                Dim oBOMRow As BOMRow
                Set oBOMRow = oDrawingBOMRow.BOMRow
                
                ' Add the Item Number from the model BOM.
                ' strDisplay = strDisplay & vbNewLine & "BOM Item Number: " & oBOMRow.ItemNumber
                ' strDisplay = strDisplay & vbNewLine & "Referenced Component(s):"
                
                Dim oCompDefs As ComponentDefinitionsEnumerator
                Set oCompDefs = oBOMRow.ComponentDefinitions
                   
                If oDrawingBOMRow.Virtual Then
                    ' The referenced item is a virtual component.
                    ' strDisplay = strDisplay & vbNewLine & "     Virtual: " & oCompDefs.Item(1).DisplayName
                Else
                  
                    ' Add the document name of the referenced component.
                    ' There could be multiple if the balloon references
                    ' a merged BOM row in the model.
                    Dim oCompDef As ComponentDefinition
    
                    ' strDisplay = vbNewLine & strDisplay & vbNewLine & "     " & oPartDef.Document.FullDocumentName
                End If
            End If
          '  MsgBox strDisplay
        Next
    Next
End Sub

 

0 Likes
Message 4 of 9

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @clamuro-IPEC,

 

Can you please provide sample drawing and related files which contains balloons with multiple segment leader?

 

Please make sure that files are non confidential.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 5 of 9

clamuro-IPEC
Enthusiast
Enthusiast

@chandra.shekar.g

 

see attached.  Three instances of the same .ipt with unique occurrence names.  All three are ballooned and the code stops replacing the number with the occurrence name when it gets to the multi-segment leader.

0 Likes
Message 6 of 9

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @clamuro-IPEC,

 

VBA code is generalized to work for both single and multiple line segments of leader. Try the following changed VBA code.

 

Public Sub GetComponentReferencedByBalloon()
    ' Set a reference to the active drawing document
    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    ' Set Reference to Active Sheet on Drawing
    Dim oSheet As Sheet
    Set oSheet = oDoc.ActiveSheet
    
    ' Iterate through all the balloon in Sheet
    Dim oBalloon As Balloon
    For Each oBalloon In oSheet.Balloons
    
        ' Set Refrence to the Leader attached to the balloon
        Dim leader As leader
        Set leader = oBalloon.leader
        
        'The leader can be single line segment and multiple line segment.
        Dim leaderNode As leaderNode
        Dim oNode As leaderNode
        For Each oNode In leader.AllNodes
            If Not oNode.AttachedEntity Is Nothing Then
                Set leaderNode = oNode
                Exit For
            End If
        Next
    
        ' Set the intent of the leader
        Dim intent As GeometryIntent
        Set intent = leaderNode.AttachedEntity
        
        ' Set Reference to the curve that the leader points to
        Dim curve As DrawingCurve
        Set curve = intent.Geometry

        ' Set Reference to the geoetry that the curve belongs to
        Dim oModelGeom As Object
        Set oModelGeom = curve.ModelGeometry

        ' Get component occurance that the model geometry belongs to
        Dim occurrence As ComponentOccurrence
        Set occurrence = oModelGeom.ContainingOccurrence
        
        ' Iterate through all the balloon sets
        Dim oBalloonValueSet As BalloonValueSet
        For Each oBalloonValueSet In oBalloon.BalloonValueSets
            'Dim strDisplay As String
            ' strDisplay = "Balloon Item Number: "
            
            ' Set Reference to the Quantity value of the component from the BOM
            Dim oCount As Integer
            oCount = oBalloonValueSet.ReferencedRow.BOMRow.ItemQuantity
            
            ' Set Reference to the Item Number on the BOM
            Dim oBalloonNumber As Integer
            oBalloonNumber = oBalloonValueSet.ItemNumber
            
            ' If More than One Components, update Balloon with Item Number & Occurance Name
            If oCount > 0 Then
                oBalloonValueSet.OverrideValue = occurrence.Name
            Else
                ' Do Nothing
            End If
            
            Dim oDrawingBOMRow As DrawingBOMRow
            Set oDrawingBOMRow = oBalloonValueSet.ReferencedRow
            
            If oDrawingBOMRow.Custom Then
                ' The referenced item is a custom parts list row.
                ' strDisplay = strDisplay & vbNewLine & "Referenced Component(s):"
                ' strDisplay = strDisplay & vbNewLine & "     Custom PartsList Row"
            Else
                Dim oBOMRow As BOMRow
                Set oBOMRow = oDrawingBOMRow.BOMRow
                
                ' Add the Item Number from the model BOM.
                ' strDisplay = strDisplay & vbNewLine & "BOM Item Number: " & oBOMRow.ItemNumber
                ' strDisplay = strDisplay & vbNewLine & "Referenced Component(s):"
                
                Dim oCompDefs As ComponentDefinitionsEnumerator
                Set oCompDefs = oBOMRow.ComponentDefinitions
                   
                If oDrawingBOMRow.Virtual Then
                    ' The referenced item is a virtual component.
                    ' strDisplay = strDisplay & vbNewLine & "     Virtual: " & oCompDefs.Item(1).DisplayName
                Else
                  
                    ' Add the document name of the referenced component.
                    ' There could be multiple if the balloon references
                    ' a merged BOM row in the model.
                    Dim oCompDef As ComponentDefinition
    
                    ' strDisplay = vbNewLine & strDisplay & vbNewLine & "     " & oPartDef.Document.FullDocumentName
                End If
            End If
          '  MsgBox strDisplay
        Next
    Next
End Sub

Please feel free to contact if there is any queries.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 7 of 9

clamuro-IPEC
Enthusiast
Enthusiast

@chandra.shekar.g I seem to get an error when a leader is not attached to a line and also when the object line that it is attached to is not visible (hidden behind a different component).

Capture.JPGCapture2.JPG

 

Is there a way to get the leader that is causing the problem to be highlighted, changed to "XXXXXX", or even if it is just deleted so that the code can continue to run on the rest of the balloons?

0 Likes
Message 8 of 9

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@clamuro-IPEC,

 

To resume the code on error, "On Error Resume Next" line can be used to avoid error message.

 

'The leader can be single line segment and multiple line segment.
        Dim leaderNode As leaderNode
        Dim oNode As leaderNode
        For Each oNode In leader.AllNodes
On Error Resume Next If Not oNode.AttachedEntity Is Nothing Then Set leaderNode = oNode Exit For End If Next

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 9 of 9

Anonymous
Not applicable

Hi,

 this is really helpful.i have some specific requirement is it possible to change the occurrence name in drafting views from file name to custom name format or any other.

 

 when operator in shop floor open the drawing file in Autocad  for cnc cutting the block name syntax is assembly name ans view name it's odd for him to identify the part.this lead to many manufacturing issues.

 

can you help me to fix this in Inventor or in Autocad.

 

I have attached both  image.

 

 

 

0 Likes