How to attach sketched symbol leader to view in drawing using inventor API?

How to attach sketched symbol leader to view in drawing using inventor API?

TA.Fehr
Advocate Advocate
2,115 Views
6 Replies
Message 1 of 7

How to attach sketched symbol leader to view in drawing using inventor API?

TA.Fehr
Advocate
Advocate

I'm working on a program that needs to attach bubbles to dimensions on the drawing. I want to have the bubble (sketch symbol) attach to one of the "green dots" via the API. I can do this manually, but when I try programatically, the positions are off.  In the screencast I edit out one line of code. This inserts the endpoint of the leader (which is the same location as the start point) but for some reason this changes the symbol position.

 

I'd like to be able to replicate via API what happens in the first part of the screencast. 

 

I've used some of the code found here. But it gets more difficult when dealing with the rangebox function of the dimension.

 

Here is the setup code:

 

 Private Sub btnDim_Click(sender As Object, e As EventArgs) Handles btnDim.Click
        Dim oDoc As DrawingDocument = _invApp.ActiveDocument
        Dim SelectedFeature = _invApp.CommandManager.Pick(SelectionFilterEnum.kAllEntitiesFilter, "Select something")
        Select Case SelectedFeature.type
            Case 117474560
                LinearDim(SelectedFeature)
            Case 117475328
                DiametralDim(SelectedFeature)
            Case 117475072
                RadialDim(SelectedFeature)
            Case 117483008
                FCF(SelectedFeature)
            Case 117471488
                HoleTableTag(SelectedFeature)
            Case 117491712
                HoleTag(SelectedFeature)
            Case 117473024
                Note(SelectedFeature)
            Case 117484032
                Surface(SelectedFeature)
            Case 117488384
                Chamfer(SelectedFeature)
            Case 117469952
                HoleTable(SelectedFeature)
            Case Else
                MsgBox("Unknown")
        End Select
    End Sub
    Private Sub LinearDim(oDim As LinearGeneralDimension)
        dgvDimValues.Rows.Add(dgvDimValues.Rows.Count, oDim.GetHashCode, oDim.Text.Text, "TBD", oDim.Type, oDim.Tolerance.Upper, oDim.Tolerance.Lower)
        InsertSketchedSymboSample(oDim, oDim.Text.RangeBox.MaxPoint, oDim.Text.RangeBox.MinPoint)
    End Sub
    Private Sub DiametralDim(oDim As DiameterGeneralDimension)
        dgvDimValues.Rows.Add(dgvDimValues.Rows.Count, oDim.GetHashCode, oDim.Text.Text, "TBD", oDim.Type, oDim.Tolerance.Upper, oDim.Tolerance.Lower)
        InsertSketchedSymboSample(oDim, oDim.Text.RangeBox.MaxPoint, oDim.Text.RangeBox.MinPoint)
    End Sub
    Private Sub RadialDim(oDim As RadiusGeneralDimension)
        dgvDimValues.Rows.Add(dgvDimValues.Rows.Count, oDim.GetHashCode, oDim.Text.Text, "TBD", oDim.Type, oDim.Tolerance.Upper, oDim.Tolerance.Lower)
        InsertSketchedSymboSample(oDim, oDim.Text.RangeBox.MaxPoint, oDim.Text.RangeBox.MinPoint)
    End Sub
    Private Sub Note(oDim As DrawingNote)
        dgvDimValues.Rows.Add(dgvDimValues.Rows.Count, oDim.GetHashCode, oDim.Text, "TBD", oDim.Type)
        InsertSketchedSymboSample(oDim, oDim.RangeBox.MaxPoint, oDim.RangeBox.MinPoint)
    End Sub
    Private Sub HoleTableTag(oDim As HoleTag)
        dgvDimValues.Rows.Add(dgvDimValues.Rows.Count, oDim.GetHashCode, oDim.Text, "TBD", oDim.Type)
        InsertSketchedSymboSample(oDim, oDim.RangeBox.MaxPoint, oDim.RangeBox.MinPoint)
    End Sub
    Private Sub Surface(oDim As SurfaceTextureSymbol)
        dgvDimValues.Rows.Add(dgvDimValues.Rows.Count, oDim.GetHashCode, "Value", "TBD", oDim.Type, oDim.MaximumRoughness, oDim.MinimumRoughness)
        InsertSketchedSymboSample(oDim, oDim.Position, oDim.Position)
    End Sub
    Private Sub HoleTag(oDim As HoleThreadNote)
        dgvDimValues.Rows.Add(dgvDimValues.Rows.Count, oDim.GetHashCode, oDim.Text, "TBD", oDim.Type, oDim.Tolerance.Upper, oDim.Tolerance.Lower)
        InsertSketchedSymboSample(oDim, oDim.Text.RangeBox.MaxPoint, oDim.Text.RangeBox.MinPoint)
    End Sub
    Private Sub Chamfer(oDim As ChamferNote)
        dgvDimValues.Rows.Add(dgvDimValues.Rows.Count, oDim.GetHashCode, oDim.Text, "TBD", oDim.Type, "NA", "NA")
        InsertSketchedSymboSample(oDim, oDim.RangeBox.MaxPoint, oDim.RangeBox.MinPoint)
    End Sub
    Private Sub HoleTable(oDim As HoleTable)
        Dim Text As String = ""
        For Each Row As HoleTableRow In oDim.HoleTableRows
            For Each Col In oDim.HoleTableColumns
                Debug.Print(Row.Item(Col).Text)
            Next
            'Text = Text & item.HoleTag & " " & item. & " " & item.DatumTwo & " " & item.DatumThree & vbNewLine
            'dgvDimValues.Rows.Add(dgvDimValues.Rows.Count, oDim.GetHashCode, Text, "TBD", oDim.Type, item.Tolerance, item.LowerTolerance)
        Next
        InsertSketchedSymboSample(oDim, oDim.RangeBox.MaxPoint, oDim.RangeBox.MinPoint)
    End Sub
    Private Sub FCF(oDim As FeatureControlFrame)
        Dim Text As String = ""
        For Each item As FeatureControlFrameRow In oDim.FeatureControlFrameRows
            Text = Text & item.GeometricCharacteristic & " " & item.DatumOne & " " & item.DatumTwo & " " & item.DatumThree & vbNewLine
            dgvDimValues.Rows.Add(dgvDimValues.Rows.Count, oDim.GetHashCode, Text, "TBD", oDim.Type, item.Tolerance, item.LowerTolerance)
        Next
        InsertSketchedSymboSample(oDim, oDim.Position, oDim.Position)
    End Sub
    Sub HoleTable()
        Dim oDoc As DrawingDocument
        oDoc = _invApp.ActiveDocument
        Dim oHoleTable As HoleTable
        oHoleTable = oDoc.ActiveSheet.HoleTables.Item(1)
        Dim oRow As HoleTableRow
        For Each oRow In oHoleTable.HoleTableRows
            Dim subItem As HoleTableCell
            For Each subItem In oRow
                Debug.Print(subItem.Text)
            Next
        Next
    End Sub

Here is the insertion code:

    Public Sub CreateSketchedSymbolDefinition()
        ' Set a reference to the drawing document.
        ' This assumes a drawing document is active.
        Dim oDrawDoc As DrawingDocument
        oDrawDoc = _invApp.ActiveDocument
        ' Create the new sketched symbol definition.
        Dim oSketchedSymbolDef As SketchedSymbolDefinition
        oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Add("mySymbol")
        ' Open the sketched symbol definition's sketch for edit. This is done by calling the Edit
        ' method of the SketchedSymbolDefinition to obtain a DrawingSketch. This actually creates
        ' a copy of the sketched symbol definition's and opens it for edit.
        Dim oSketch As DrawingSketch = Nothing
        Call oSketchedSymbolDef.Edit(oSketch)
        Dim oTG As TransientGeometry
        oTG = _invApp.TransientGeometry
        Dim oInsertionPoint As Point2d = (oTG.CreatePoint2d(0, 0))
        Dim oSketchCircle As SketchCircle
        oSketchCircle = oSketch.SketchCircles.AddByCenterRadius(oTG.CreatePoint2d(-0.1, 0.1), 0.1)
        ' Add a prompted text field at the center of the sketch circle.
        Dim sText As String
        sText = "<Prompt>1</Prompt>"
        Dim oTextBox As TextBox
        oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(-0.1, 0.1), sText)
        oTextBox.VerticalJustification = VerticalTextAlignmentEnum.kAlignTextMiddle
        oTextBox.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter
        Call oSketchedSymbolDef.ExitEdit(True)
    End Sub
    Public Sub InsertSketchedSymboSample(oDim As Object, ByRef MaxP As Point2d, ByRef MinP As Point2d) 'ByRef Text As Box2d)
        Dim oDrawDoc As DrawingDocument
        oDrawDoc = _invApp.ActiveDocument
        ' Obtain a sketched symbol definition.
        Dim oSketchedSymbolDef As SketchedSymbolDefinition = Nothing
        For i = 1 To oDrawDoc.SketchedSymbolDefinitions.Count
            If oDrawDoc.SketchedSymbolDefinitions.Item(i).Name = "mySymbol" Then
                oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item(i)
                Exit For
            End If
        Next
        If oSketchedSymbolDef Is Nothing Then
            CreateSketchedSymbolDefinition()
            For i = 1 To oDrawDoc.SketchedSymbolDefinitions.Count
                If oDrawDoc.SketchedSymbolDefinitions.Item(i).Name = "mySymbol" Then
                    oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item(i)
                    Exit For
                End If
            Next
        End If
        Dim oSheet As Sheet
        oSheet = oDrawDoc.ActiveSheet
        Dim sPromptStrings(0) As String
        sPromptStrings(0) = dgvDimValues.RowCount - 1
        ' Create sketched symbol
        Dim oTG As TransientGeometry
        oTG = _invApp.TransientGeometry
        Dim oPoint As Point2d = Nothing
        For x = 0 To 315 Step 45
            Select Case x 'My.Settings.BalloonPos
                Case 0
                    oPoint = oTG.CreatePoint2d(MinP.X + (MaxP.X - MinP.X) / 2, MaxP.Y)
                Case 45
                    oPoint = oTG.CreatePoint2d(MaxP.X, MaxP.Y)
                Case 90
                    oPoint = oTG.CreatePoint2d(MaxP.X, MinP.Y + (MaxP.Y - MinP.Y) / 2)
                Case 135
                    oPoint = oTG.CreatePoint2d(MaxP.X, MinP.Y)
                Case 180
                    oPoint = oTG.CreatePoint2d(MinP.X + (MaxP.X - MinP.X) / 2, MinP.Y)
                Case 225
                    oPoint = oTG.CreatePoint2d(MinP.X, MinP.Y)
                Case 270
                    oPoint = oTG.CreatePoint2d(MinP.X, MinP.Y + (MaxP.Y - MinP.Y) / 2)
                Case 315
                    oPoint = oTG.CreatePoint2d(MinP.X, MaxP.Y)
            End Select
            Dim oSketchedSymbol As SketchedSymbol
            Dim oGI As GeometryIntent
            oGI = oSheet.CreateGeometryIntent(oDim, oPoint)
            Dim oCol As ObjectCollection
            oCol = _invApp.TransientObjects.CreateObjectCollection          oCol.Add(_invApp.TransientGeometry.CreatePoint2d(oGI.Intent.X, oGI.Intent.Y))
            oCol.Add(oGI) ''' This is the line I edited out'''
            oSketchedSymbol = oSheet.SketchedSymbols.AddWithLeader(oSketchedSymbolDef, oCol,,, sPromptStrings)
            oSketchedSymbol.LeaderVisible = True
        Next
    End Sub

 

 

 

 

Screencast will be displayed here after you click Post.

7d2deba2-2ad0-474a-87cd-d8919966121e

 

Accepted solutions (1)
2,116 Views
6 Replies
Replies (6)
Message 2 of 7

TA.Fehr
Advocate
Advocate

The screencast wasn't attaching to the initial post.

 

 

Message 3 of 7

AlexFielder
Advisor
Advisor

I'm struggling to see what the issue is; you're commenting out the line that creates the necessary* link between the resultant placed sketch symbol and the dimension it's connecting to.

 

Unless the issue is that the symbols get placed everywhere, when you only place them on the left + middle manually?

 

 

 

*necessary if you want the symbols to move like they do manually.

Message 4 of 7

TA.Fehr
Advocate
Advocate

The issue is that when I add the second point to "fix" the sketch, it changes the location relative to where it would be if I didn't. Because I'm intending to be able to add the numbers to the row of a table, it's important that I'm able to control where the number is getting fixed to.

I appreciate your prompt response and I hope I'm making sense.

Message 5 of 7

TA.Fehr
Advocate
Advocate
Accepted solution

Although I had seen this link before, I was able to do a little manipulation to make what I wanted work.

 

The issue is when you add a sketch symbol with a leader in one step, it adjusts the position arbitrarily outside of the user control. This became most noticeable when the center of my sketch symbol was not grounded to (0,0). When you add the sketch symbol without the leader the position is correct but the position does not remain relative.

 

However, if you insert the sketch symbol and add a leader afterward you can rectify this issue. (The 0.15 is an offset value I used for the size of the sketch symbol - setting this at 0 would place it on one of the 8 nodes.)

    Public Sub InsertSketchedSymboSample(oDim As Object, ByRef MaxP As Point2d, ByRef MinP As Point2d) 'ByRef Text As Box2d)
        Dim oDrawDoc As DrawingDocument
        oDrawDoc = _invApp.ActiveDocument
        ' Obtain a sketched symbol definition.
        Dim oSketchedSymbolDef As SketchedSymbolDefinition = Nothing
        For i = 1 To oDrawDoc.SketchedSymbolDefinitions.Count
            If oDrawDoc.SketchedSymbolDefinitions.Item(i).Name = "mySymbol" Then
                oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item(i)
                Exit For
            End If
        Next
        If oSketchedSymbolDef Is Nothing Then
            CreateSketchedSymbolDefinition()
            For i = 1 To oDrawDoc.SketchedSymbolDefinitions.Count
                If oDrawDoc.SketchedSymbolDefinitions.Item(i).Name = "mySymbol" Then
                    oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item(i)
                    Exit For
                End If
            Next
        End If
        Dim oSheet As Sheet
        oSheet = oDrawDoc.ActiveSheet
        Dim sPromptStrings(0) As String
        sPromptStrings(0) = dgvDimValues.RowCount - 1
        ' Create sketched symbol
        Dim oTG As TransientGeometry
        oTG = _invApp.TransientGeometry
        Dim oPoint As Point2d = Nothing
        For x = 0 To 315 Step 45
            Select Case x 'My.Settings.BalloonPos
                Case 0
                    oPoint = oTG.CreatePoint2d(MinP.X + (MaxP.X - MinP.X) / 2, MaxP.Y + 0.15)
                Case 45
                    oPoint = oTG.CreatePoint2d(MaxP.X + 0.15, MaxP.Y + 0.15)
                Case 90
                    oPoint = oTG.CreatePoint2d(MaxP.X + 0.15, MinP.Y + (MaxP.Y - MinP.Y) / 2)
                Case 135
                    oPoint = oTG.CreatePoint2d(MaxP.X + 0.15, MinP.Y - 0.15)
                Case 180
                    oPoint = oTG.CreatePoint2d(MinP.X + (MaxP.X - MinP.X) / 2, MinP.Y - 0.15)
                Case 225
                    oPoint = oTG.CreatePoint2d(MinP.X - 0.15, MinP.Y - 0.15)
                Case 270
                    oPoint = oTG.CreatePoint2d(MinP.X - 0.15, MinP.Y + (MaxP.Y - MinP.Y) / 2)
                Case 315
                    oPoint = oTG.CreatePoint2d(MinP.X - 0.15, MaxP.Y + 0.15)
            End Select
            Dim oSketchedSymbol As SketchedSymbol
            Dim oGI As GeometryIntent
            oGI = oSheet.CreateGeometryIntent(oDim, oPoint)
            Dim oCol As ObjectCollection
            oCol = _invApp.TransientObjects.CreateObjectCollection
            oCol.Add(oPoint)
            oCol.Add(oGI)
            oSketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, oPoint, 0, 1, sPromptStrings)
            oSketchedSymbol.Leader.AddLeader(oCol)
            oSketchedSymbol.LeaderVisible = False

        Next
    End Sub

for those who come across this in the future here is the modified code that I'm currently using:

 

Message 6 of 7

thanh.trantuan
Contributor
Contributor

Hi @TA.Fehr 

I'm interested in this topic, and I have a question that, could you feel free to give me the explanation

When I paste your code in visual studio, there is an error that "dgvDimValues" is not declared. So could you show me this kind of this variant

1.JPG

Thanks in advance

 

Message 7 of 7

TA.Fehr
Advocate
Advocate

You'll have to forgive me as I abandoned this project a long time ago.

 

dgvDimValues is a reference to the table in the video. This table was a way of detailing all the sketch symbols that were added by the program.

So if you want to use the code as written, you'll have to create a DataGridView table with the same columns and call it dgvDimValues. Or alternatively you can create your own table and substitute the name in the code. But that is the reason it is saying it's not declared is because it is referencing a table that you have not created (or have created with a different name)