Showing posts with label balloon. Show all posts
Showing posts with label balloon. Show all posts

Thursday, 19 February 2015

Component tagging

I have just updated my component tagging code; you can use it as an external rule or straight in the drawing. Would like to thank Chris Benner for taking the time to test this. Check out his blog for more Tube and Pipe stuff https://cadtipstricks.wordpress.com/.
I have merged part tagging in the assembly and retrieval in the drawing intro single code that should work like this:







I would recommend that you create a new balloon style called "Tags" or you need to edit the code and add a comment character ' before the line:
oBalloon.Style = oStyles.BalloonStyles.item("Tags")
This code has been stripped out from the inventor help pages.Unfortunately it's only working with curve segment and when trying to get the middle of it as your balloon attachment reference it's not returning anything for circles and ellipses but for the rest it creates a leather point about 0.5 on axis X and 1 Y .So you can increase these values to get the balloon further away from your reference.

Limitations:
The assemblies need to be checked out, including the runs that contain the parts to tag (valve and all).
Sometimes when you work with different level of details and designs view representations it fails finding the component occurrence so that's why I'm asking you to place a parts list on the drawing sheet (the code tracks and reports if this is the case).
You can’t have same name for different components inside an assembly, so the code will prompt you to try again if you type in an existing value. Ex.: Inside each Route you can have only one valve code V101.
In the tag window dialog if you press cancel it reverts to the original value but make sure you update all balloons at the end when prompted.

You can change the balloon style hexagon linear circular, you can change the display diameter size ; you can change the format of the text but I would much rather change it in the style manager of the drawing create a new style and assign the balloon to it instead of manually entering all these parameters inside the code.
 The second part of the code prompts for update on existing balloons. Here is the code to download.


'this sets a tag to each selected component and creates
'a balloon for it, asks for update of existing balloons
 
' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
 
' Set a reference to the active sheet.
Dim oActiveSheet As Sheet
oActiveSheet = oDrawDoc.ActiveSheet
 
 
Dim oStyles As DrawingStylesManager
oStyles = oDrawDoc.StylesManager
 
Dim oCurve As DrawingCurve
Dim oEdge As EdgeProxy
Dim occurrence As ComponentOccurrence 
Dim oGeometryIntent As GeometryIntent
 
 
While True
    Try
        ' Get a drawing curve segment selection from the user
        Dim oCS As DrawingCurveSegment
        oCS = ThisApplication.CommandManager.Pick( _
                SelectionFilterEnum.kDrawingCurveSegmentFilter, "Pick a drawing curve segment")
                                            
        If oCS Is Nothing Then
            'MessageBox.Show ("Selection was cancelled","ilogic")
            Beep
            Exit While
        End If
                
        oCurve = oCS.Parent
        oEdge = oCurve.ModelGeometry
        occurrence = oEdge.ContainingOccurrence
        
        'MessageBox.Show("Old Occ name: " & occurrence.Name,"ilogic")
        
        Retry = True
        
        'as long as retry is selected by user
        While Retry
            'get tag from user
            oTagOcc = InputBox("Enter Tag No: ", "Tag Prompt", occurrence.Name)
 
            Try
                ' try and set that value
                occurrence.Name = oTagOcc
                'if success exit the retrying loop
                Exit While
            'if tag allready exists
            Catch
                'prompt if user wants to try again
                Retry = InputRadioBox("Allready used, try again", "Yes", "No", Retry, Title := "Retry")
            End Try
        End While
        
        'if user canceled the retry skip the rest of the code and
        'prompt to select parts again
        If Retry = False Then
            Continue While
        End If
 
        'Get the mid point of the selected curve
        ' assuming that the selection curve is linear
        Dim oMidPoint As Point2d
        oMidPoint = oCurve.MidPoint
        
        ' Set a reference to the TransientGeometry object.
        Dim oTG As TransientGeometry
        oTG = ThisApplication.TransientGeometry
        
        Dim oLeaderPoints As ObjectCollection
        oLeaderPoints = ThisApplication.TransientObjects.CreateObjectCollection
        
        Try
            ' Create a couple of leader points.
            Call oLeaderPoints.Add(oTG.CreatePoint2d(oMidPoint.X + 0.5, oMidPoint.Y + 1))
            'Call oLeaderPoints.Add(oTG.CreatePoint2d(oMidPoint.X + 1, oMidPoint.Y + 0.5))
        Catch
            MessageBox.Show("Circular segment found," & _
            vbLf & "Can't get sement center," & _
            vbLf & "Manually move balloon please", _
            "Position error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        
        ' Add the GeometryIntent to the leader points collection.
        ' This is the geometry that the balloon will attach to.
        oGeometryIntent = oActiveSheet.CreateGeometryIntent(oCurve)
        
        Call oLeaderPoints.Add(oGeometryIntent)
        
        Dim oBalloon As Balloon
        Try
            oBalloon = oDrawDoc.ActiveSheet.Balloons.Add(oLeaderPoints)
        Catch 
            MessageBox.Show("Can'te activate BOM." & _
            vbLf & "Temporary add a Parts List on the sheet" & _
            vbLf & "You can remove it later", "ilogic")
        End Try
        
        'set the style of the balloon to "Tags"; if you don't have a style called tags
        'you can remove this to keep it as default or use some of these settings:
        'oBalloon.SetBalloonType (kHexagonBalloonType)
        '---------
        'other options here are kCircularWithOneEntryBalloonType, 
        'kCircularWithTwoEntriesBalloonType, kHexagonBalloonType, 
        'kLinearBalloonType, kNoneBalloonType And kSketchedSymbolBalloonType
        '---------
        oBalloon.Style = oStyles.BalloonStyles.item("Tags")
        
        Dim oBalloonValueSet As BalloonValueSet
            
        ' Iterate over each value set (attached balloons) in a balloon.
        For Each oBalloonValueSet In oBalloon.BalloonValueSets
            ' Set balloon value from browser.
            oBalloonValueSet.OverrideValue = occurrence.Name
        Next
 
    Catch
    'end try
    End Try
End While
 
'----------Update existing balloons on all sheets
 
'Ask the user if he wants to update values of all balloons (if edited some)
booleanParam = InputRadioBox("Update existing balloons?: ", "Yes", "No", True, Title :="Update Existing?")
 
If booleanParam = False Then
    Exit Sub
ElseIf booleanParam = True
    
'process all sheets
For Each oSheets In oDrawDoc.Sheets 
        ' Iterate over each balloon on the sheet.
        For Each oBalloon In oActiveSheet.Balloons
            If oBalloon.Style.Name = "Tags" Then
                Try
                    Dim leader As Leader
                    Leader = oBalloon.Leader
                    'assuming the leader is a single line segment
                    Dim leaderNode As LeaderNode 
                    leaderNode = leader.AllNodes(2)
                    
                    oGeometryIntent = leaderNode.AttachedEntity
                
                    curve = oGeometryIntent.Geometry
                
                    oEdge = curve.ModelGeometry
        
                    occurrence = oEdge.ContainingOccurrence
                    
                    ' Iterate over each value set (attached balloons) in a balloon.
                    For Each oBalloonValueSet In oBalloon.BalloonValueSets
                        ' Set balloon value from browser.
                        oBalloonValueSet.OverrideValue = occurrence.Name
                    Next 'go to next balloon
                Catch'do nothing if error
                End Try
            End If 'end of search for Tags balloons
        Next
    Next
End If
 
'----------End Update existing balloons on all sheets
 


Try this out, and customizing for your needs.  Let me know how that goes for you.
ADS.

Thursday, 14 August 2014

Component Tags in Inventor

As you are aware if you have done any routed systems there is no way of labeling same valve (or components for that matter) with different names / tags / id_numbers and call it on the drawing. You can create leader notes in the drawing but if they need changing you need to manually do it and it’s time consuming if you have large assemblies and multiple sheets and views. I have seen the same problem for people doing assemblies of electric components.
A 2” ball valve can have different identification tags in the P&ID but be a reused library component in Inventor.

I've been searching for a solution to this problem for the last 8 years or so and I couldn't find anything on autodesk forums or other discussion groups.
I instinctively knew that we would need to use the assembly browser name (occurrence name) but didn't see how to get it to show up in the drawing.

I have got together bits of iLogic codes from the internet that can get the assembly occurrence name and link it to the override value of my custom style balloons.
For this I have created a new balloon style called “Tags” and made it of type “Hexagon” with just the “Item” info in it.

The code processes just balloons of style “Tags” for we don’t want to change parts list balloons as well.
Because changing the occurrence name in the assembly involves a lot of steps (find component in browser / edit parent subassembly / change name of component / activate top level again) it is better to do it from the drawing.
You need to use “balloon” command, because “auto-balloon” cannot label same component on different views more than once.

Place balloons as you would normally do using “balloon” command and override the field with your text. You only need to do it once per component then run the code

When running the code it will prompt you if you want to set names from the balloons or from the browser. Choose balloon to send the override value to the model browser (occurrence name).

Continue placing balloons on all views and then run the code again. Choose to set the value from the browser to get the value on all new balloons.

This way I can balloon same component multiple times on same view if wanted.

As you can see the Occurrence name in browser and iproperties changed but the part number, stock number and other properties didn't changed.


Here's the code with explanatory comments:

' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

' Set Reference to Active Sheet
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet

'get the name and path of the first model in the drawing
oAsmName = ThisDrawing.ModelDocument.FullFileName

' Set Reference to the Assembly and Open Silently
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.Documents.Open(oAsmName, False)

' Set a reference to the assembly component definintion.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oAsmDoc.ComponentDefinition

Dim oBalloon As Balloon

'Ask the user if he wants to set the component name from browser
'or if he wants to set the browser name from overwritten balloon value
booleanParam = InputRadioBox("Set name from?: ", "Browser", "Balloon", True, Title :="Read/Write")


'Start a counter for sheets to process
Dim SheetCount As Long
SheetCount = 1
Dim oSheets As Sheet
'step through each drawing sheet
For Each oSheets In oDrawDoc.Sheets

    ' Iterate over each balloon on the sheet.
    For Each oBalloon In oSheet.Balloons
        Dim leader As Leader
        Leader = oBalloon.Leader
        'assuming the leader is a single line segment
        Dim leaderNode As LeaderNode = leader.AllNodes(2)
       
        Dim intent As GeometryIntent
        intent = leaderNode.AttachedEntity
   
        Dim curve As DrawingCurve
        curve = intent.Geometry
   
        Dim edgePx As EdgeProxy
        edgePx= curve.ModelGeometry
   
        Dim occurrencedrg As ComponentOccurrence
        occurrencedrg = edgePx.ContainingOccurrence
        
        Dim oBalloonValueSet As BalloonValueSet
       
        ' Iterate over each value set (attached balloons) in a balloon.
        For Each oBalloonValueSet In oBalloon.BalloonValueSets
            'only process if balloon of style of type "Tags"
            If oBalloon.Style.Name = "Tags" Then
                'if user wants to get name from browser
                If booleanParam = True Then
                    ' Set balloon value from browser.
                    oBalloonValueSet.OverrideValue = occurrencedrg.Name
                'if user wants to set browser name from balloon
                ElseIf booleanParam = False Then
                    ' Set browser name from balloon override value.
                    occurrencedrg.Name = oBalloonValueSet.OverrideValue
                End If
            End If
        Next
    Next
'Increase sheet counter
    SheetCount = SheetCount +1
Next



Best of  luck

ADS.