Constraining Model Annotations When Adding Them With Code

Constraining Model Annotations When Adding Them With Code

acanx21
Enthusiast Enthusiast
505 Views
2 Replies
Message 1 of 3

Constraining Model Annotations When Adding Them With Code

acanx21
Enthusiast
Enthusiast

We are currently trying to use model annotations to call out information in our assemblies. We came up with the idea of making a sketch in the assembly that has sketchpoints we put in for the leaders so that when we replace components in the assembly to make the next one, we don't have to worry about the annotation disappearing. Especially when the assembly is just shrinking or growing.

 

I wrote code to make it so that a user can click on all the sketchpoints in the sketch and the leader annotations get added, but they currently do not get constrained to those points so as soon as we go to the next assembly and grow/shrink the assembly and sketch, the annotations don't move. 

 

Is there any way to make it so when the annotations are getting added, they also constrain to the point?

 

I've tried to do this in the past with placing UCS as well and was not able to get them to constrain, so I'm hoping it is possible and I just haven't found the way yet. 

 

This is my current code for adding the annotations based on the sketchpoints selected. It adds them all to be on the XY plane.

 

Sub AddCallouts()
Dim Doc As Document
Dim oDef As AssemblyComponentDefinition
Dim oTG As TransientGeometry
Dim dViewRepMgr As RepresentationsManager
Dim dWeldView As DesignViewRepresentations

Set InvApp = ThisApplication 'Marshal.GetActiveObject("Inventor.Application")
Set Doc = InvApp.ActiveDocument
Set oDef = Doc.ComponentDefinition

Set oTG = InvApp.TransientGeometry

Set dViewRepMgr = oDef.RepresentationsManager
Set dWeldView = dViewRepMgr.DesignViewRepresentations

Dim oFace As WorkPlane
Set oFace = oDef.WorkPlanes.Item(3)

Dim oAnnoPlaneDef As AnnotationPlaneDefinition
Set oAnnoPlaneDef = oDef.ModelAnnotations.CreateAnnotationPlaneDefinitionUsingPlane(oFace) 'this line was crashing Inventor

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


Dim selectedSketchPoints As Object
Set selectedSketchPoints = Doc.SelectSet

If selectedSketchPoints.count = 0 Then
    MsgBox ("No sketch points are selected.")
    Exit Sub
End If

Dim numX As Integer
numX = 1

'For Each oPoint In oDef.Sketches.Item(1).SketchPoints
For Each oPoint In selectedSketchPoints
    oLeaderPoints.Clear
    
    Dim wp As Point
    Set wp = oPoint.Geometry3d
    
    Call oLeaderPoints.Add(oTG.CreatePoint(oPoint.Geometry3d.x + 5, oPoint.Geometry3d.Y + 5, oPoint.Geometry3d.Z))
    Call oLeaderPoints.Add(wp)

    Dim oLeaderIntent As GeometryIntent
    Set oLeaderIntent = oDef.CreateGeometryIntent(oFace, wp)
    
    Call oLeaderPoints.Add(oLeaderIntent)
    
    Dim oLeaderDef As ModelLeaderNoteDefinition
    Set oLeaderDef = oDef.ModelAnnotations.ModelLeaderNotes.CreateDefinition(oLeaderPoints, numX & "/DXXX", oAnnoPlaneDef)
    
    Dim oLeader As ModelLeaderNote
    Set oLeader = oDef.ModelAnnotations.ModelLeaderNotes.Add(oLeaderDef)

    numX = numX + 1
Next

End Sub

 

0 Likes
Accepted solutions (1)
506 Views
2 Replies
Replies (2)
Message 2 of 3

acanx21
Enthusiast
Enthusiast

When I add the leader manually vs when I add it with code, I get different Definitions for the leader. 

 

They are getting added to the same sketchpoint in the sketch. When I make it manually I also make sure to put it on the same XY plane.

 

However, constraints only show up as being possible in the manually added leader.

 

Should I be adding the coded one differently or is it just not possible to access the constraints when adding them with code?

0 Likes
Message 3 of 3

acanx21
Enthusiast
Enthusiast
Accepted solution

Figured it out

 

Sub AnnotationAdd()
    Dim Doc As Document
    Set Doc = ThisApplication.ActiveDocument

    Dim oDef As AssemblyComponentDefinition
    Set oDef = Doc.ComponentDefinition

    Dim oAnnoPlaneDef As AnnotationPlaneDefinition
    Set oAnnoPlaneDef = oDef.ModelAnnotations.CreateAnnotationPlaneDefinitionUsingPlane(oDef.WorkPlanes.item(3))

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

    Dim selectedSketchPoints As Object
    Set selectedSketchPoints = Doc.selectSet

    If selectedSketchPoints.count = 0 Then
        MsgBox "No sketch points are selected."
        Exit Sub
    End If

    Dim numX As Integer
    numX = 1

    Dim oPoint As SketchPoint
    For Each oPoint In selectedSketchPoints
        oLeaderPoints.Clear

        ' Offset text position: +5, +5
        Dim textPosition As Point
        Set textPosition = ThisApplication.TransientGeometry.CreatePoint(oPoint.Geometry3d.x + 15, oPoint.Geometry3d.y + 15, oPoint.Geometry3d.Z)
        oLeaderPoints.Add textPosition

        ' Anchor arrowhead to the sketch point
        Dim oLeaderIntent As GeometryIntent
        Set oLeaderIntent = oDef.CreateGeometryIntent(oPoint)
        oLeaderPoints.Add oLeaderIntent

        ' Create and add the leader note
        Dim oLeaderDef As ModelLeaderNoteDefinition
        Set oLeaderDef = oDef.ModelAnnotations.ModelLeaderNotes.CreateDefinition(oLeaderPoints, numX & "/DXXX", oAnnoPlaneDef)

        Dim oLeader As ModelLeaderNote
        Set oLeader = oDef.ModelAnnotations.ModelLeaderNotes.Add(oLeaderDef)

        numX = numX + 1
    Next
End Sub