API - Adding text on drawing that moves with view

API - Adding text on drawing that moves with view

Jef_E
Collaborator Collaborator
2,633 Views
3 Replies
Message 1 of 4

API - Adding text on drawing that moves with view

Jef_E
Collaborator
Collaborator

Hi,

 

I'm making a little tool that will add some text on a drawing. Problem is, I want it to be attached to a view. So when I drag the view to a new location the text also moves. How can I achieve this?

 

I found this sample code in the programming help but it doesn't do the trick.

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

    ' Set a reference to the active sheet.
    Dim oActiveSheet As Sheet
    Set oActiveSheet = oDrawDoc.ActiveSheet
    
    ' Set a reference to the GeneralNotes object
    Dim oGeneralNotes As GeneralNotes
    Set oGeneralNotes = oActiveSheet.DrawingNotes.GeneralNotes
    
    Dim oTG As TransientGeometry
    Set oTG = ThisApplication.TransientGeometry

    ' Create text with simple string as input. Since this doesn't use
    ' any text overrides, it will default to the active text style.
    Dim sText As String
    sText = "Drawing Notes"
    
    Dim oGeneralNote As GeneralNote
    Set oGeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(3, 18), sText)

    ' Create text using various overrides.
    sText = "Notice: All holes larger than 0.500 n are to be lubricated."
    Set oGeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(3, 16), sText)

    ' Create a set of notes that are numbered and aligned along the left.
    Dim dYCoord As Double
    dYCoord = 14
    Dim dYOffset As Double
    Dim oStyle As TextStyle
    Set oStyle = oGeneralNotes.Item(1).TextStyle
    dYOffset = oStyle.FontSize * 1.5

    ' Simple single line text.
    Set oGeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "1.")
    sText = "This is note 1."
    Set oGeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)

    ' Two line text. The two lines are defined using the  tag within the text string.
    dYCoord = dYCoord - (oGeneralNote.FittedTextHeight + 0.5)
    Set oGeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "2.")
    sText = "This is note 2,  which contains two lines."
    Set oGeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)

    ' Single line of text.
    dYCoord = dYCoord - (oGeneralNote.FittedTextHeight + 0.5)
    Set oGeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "3.")
    sText = "This is note 3."
    Set oGeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)

    ' Three lines of text.
    dYCoord = dYCoord - (oGeneralNote.FittedTextHeight + 0.5)
    Set oGeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "4.")
    sText = "This is note 4,  which contains  several lines."
    Set oGeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)

    dYCoord = dYCoord - (oGeneralNote.FittedTextHeight + 0.5)
    Set oGeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "5.")
    
    sText = "Here is the last and final line of text."
    Set oGeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
End Sub


Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Accepted solutions (1)
2,634 Views
3 Replies
Replies (3)
Message 2 of 4

ekinsb
Alumni
Alumni
Accepted solution

If you play a bit with the different drawing notes in Inventor, you'll see that the Text command (general note) creates text on the sheet that is not related to any views. There are however two ways that you can create text that is associated to a view.  One way is to create leader text.  This is like a general note but has a leader to geometry in a view.  This provides a connection to a view and moving the view will move the text.  Once the leader text has been created you can even delete the leader and the association remains.  I slightly modified the leader text sample from the API help to show this.

 

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

    ' Set a reference to the active sheet.
    Dim oActiveSheet As Sheet
    Set oActiveSheet = oDrawDoc.ActiveSheet

    ' Set a reference to the drawing curve segment.
    ' This assumes that a drawing curve is selected.
    Dim oDrawingCurveSegment As DrawingCurveSegment
    Set oDrawingCurveSegment = oDrawDoc.SelectSet.Item(1)

    ' Set a reference to the drawing curve.
    Dim oDrawingCurve As DrawingCurve
    Set oDrawingCurve = oDrawingCurveSegment.Parent

    ' Get the mid point of the selected curve
    ' assuming that the selected curve is linear
    Dim oMidPoint As Point2d
    Set oMidPoint = oDrawingCurve.MidPoint

    ' Set a reference to the TransientGeometry object.
    Dim oTG As TransientGeometry
    Set oTG = ThisApplication.TransientGeometry

    Dim oLeaderPoints As ObjectCollection
    Set oLeaderPoints = ThisApplication.TransientObjects.CreateObjectCollection

    ' Create a leader point to define the position of the text.
    Call oLeaderPoints.Add(oTG.CreatePoint2d(oMidPoint.x, oMidPoint.y + 1))

    ' Create an intent and add to the leader points collection.
    ' This is the geometry that the leader text will attach to.
    Dim oGeometryIntent As GeometryIntent
    Set oGeometryIntent = oActiveSheet.CreateGeometryIntent(oDrawingCurve)

    Call oLeaderPoints.Add(oGeometryIntent)

    ' Create text with simple string as input. Since this doesn't use
    ' any text overrides, it will default to the active text style.
    Dim sText As String
    sText = "API Leader Note"

    Dim oLeaderNote As LeaderNote
    Set oLeaderNote = oActiveSheet.DrawingNotes.LeaderNotes.Add(oLeaderPoints, sText)

    oLeaderNote.Leader.AllNodes.Item(1).Delete
End Sub

 

The other way is to create a new sketch associated with the view and create text within the sketch.  In a drawing you can create sketches on the sheet and for a view.  You need to create the sketch on a view for this to work.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 4

Jef_E
Collaborator
Collaborator

Yeah thats what I mean! Thank you, I have made a follow-up question on this matter.

 

Here is the link,

https://forums.autodesk.com/t5/inventor-customization/api-tool-for-orientation-annotations-on-drawin...

 

The tool that i'm making is to have a good alternative for this question I asked during the answer day. So that I can resolve this issue for me.

https://forums.autodesk.com/t5/inventor-answer-day/drawing-annotating-orientations/m-p/6008855#M2047

 

Thanks for the help!



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 4 of 4

Jef_E
Collaborator
Collaborator

Ive might been a little quick with accepting the solution, altough it is an anwser to my question and it works.

 

I have encountered some problems while testing the code:

- I want to have my text at an precise point. But when I use this point, it's too close (or whatever) to create the leader you get a extended line type or somthing and then it results in an error on deleting the leader.

 

This shouldn't be a problem ( I tought ) I tried it with generating an offset from this specific point, and if large enough, the code results in text on the drawing with the leader removed. But.. Not at the position I would like.. Ive tried a bunch of things but can't seem to find how to move the position after the leader has been deleted?

 

When I just change this for example nothing happens. (position before move in this case = 50;50)

Dim oLeaderNote As LeaderNote
oLeaderNote = oSheet.DrawingNotes.LeaderNotes.Add(oLeaderPoints, oText)
oLeaderNote.Leader.AllNodes.Item(1).Delete()

oLeaderNote.Position.X = 0
oLeaderNote.Position.Y = 0

How do you change the location of the text? I also looked into the nodes but could not figure it out.

The code works for me but isn't 100% perfect at the moment for my needs.



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes