How to create a text field in a sketch in an ipt

How to create a text field in a sketch in an ipt

Tom_BeyerFMVHA
Contributor Contributor
250 Views
4 Replies
Message 1 of 5

How to create a text field in a sketch in an ipt

Tom_BeyerFMVHA
Contributor
Contributor

Hi, 

 

i am trying to create a text field in a sketch during the editing of an ipt. the text field should simply contain the component number.

 

' Set a reference to the part document
Dim oDoc As Inventor.Document
oDoc = ThisDoc.Document

' Set a reference to the component definition
Dim oCompDef As PartComponentDefinition
oCompDef = oDoc.ComponentDefinition

' Create a new sketch on the X-Y work plane (WorkPlanes(3) is typically the X-Y plane)
Dim oSketch As PlanarSketch
oSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))

' Edit the sketch
oSketch.Edit

' Project the origin point
Dim oOriginSketchPoint As SketchPoint
oOriginSketchPoint = oSketch.AddByProjectingEntity(oCompDef.WorkPoints.Item(1))

' Add a text field containing the part name or a parameter
Dim oText As String = oDoc.DisplayName ' Use the part name as text
' Alternatively, use a parameter: oText = Parameter("ParameterName")

' Define the position for the text (offset from the origin point)
Dim oTextPoint As Point2d
oTextPoint = oSketch.SketchPoints.Add(10, 10).Geometry ' Adjust coordinates as needed

' Add the text to the sketch
Dim oSketchText As SketchText
oSketchText = oSketch.SketchTexts.Add(oTextPoint, oText)

' Format the text (optional)
oSketchText.FontSize = 5.0 ' Set font size
oSketchText.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextLeft ' Set alignment

' Exit the sketch
oSketch.ExitEdit()

  the sketch is successfully created but “Sketchtext” is not declared. I hope you can help me. 

 

Thank you 

0 Likes
Accepted solutions (2)
251 Views
4 Replies
Replies (4)
Message 2 of 5

rzsla
Contributor
Contributor
Accepted solution

Instead of SketchText use TextBox. So,for example, 

Dim oSketch As PlanarSketch
Dim oTextPosition As Point2d
Dim oTextBox As Inventor.TextBox
oTextPosition = oTransGeom.CreatePoint2d(closestpoint.X + 5, closestpoint.Y + 5)
oTextBox = oSketch.TextBoxes.AddFitted(oTextPosition, "Part Number")
 
0 Likes
Message 3 of 5

Tom_BeyerFMVHA
Contributor
Contributor

Hi rzsla, i had tried, but it doesnt work for me. I get several declaration errors I think we need the right declaration for the print ordner?

0 Likes
Message 4 of 5

rzsla
Contributor
Contributor

What errors are you talking about? At least, attach screenshots or updated code so someone can help you

0 Likes
Message 5 of 5

Tom_BeyerFMVHA
Contributor
Contributor
Accepted solution

Now it works. Thank you and here is the code:

 

' Set a reference to the part document
Dim oDoc As Inventor.Document
oDoc = ThisDoc.Document

' Set a reference to the component definition
Dim oCompDef As PartComponentDefinition
oCompDef = oDoc.ComponentDefinition

' Create a new sketch on the X-Y work plane
Dim oSketch As PlanarSketch
oSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))

' Start editing the sketch
oSketch.Edit

' Project origin point into the sketch
Dim oOriginSketchPoint As SketchPoint
oOriginSketchPoint = oSketch.AddByProjectingEntity(oCompDef.WorkPoints.Item(1))

' Set the position for the text box (for example, offset slightly from the origin point)
Dim oTransGeom As TransientGeometry
oTransGeom = ThisApplication.TransientGeometry
Dim oTextPosition As Point2d
oTextPosition = oTransGeom.CreatePoint2d(oOriginSketchPoint.Geometry.X + 5, oOriginSketchPoint.Geometry.Y + 5)

' Add a text box to the sketch at the calculated position
Dim oTextBox As Inventor.TextBox
oTextBox = oSketch.TextBoxes.AddFitted(oTextPosition, iProperties.Value("Project", "Part Number"))

' Optionally, you can adjust the text box properties like size or font here

' End editing the sketch
oSketch.ExitEdit