Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

How to orient the text along the largest edge

ice_sid
Explorer

How to orient the text along the largest edge

ice_sid
Explorer
Explorer

Wishing everyone a great day!

I ask for advice on how to orient the text along the largest edge.

 In wood processing there is a need to control wood grain texture vector in detail. For most of the details, I think the solution is to find the largest face of the detail. Then place the symbol indicating the direction of the material in the text box. The task has been partially solved.

Some examples of code were collected on the forum pages, which provided the following.

' Get the current details document
Dim oPart As PartDocument = ThisApplication.ActiveDocument

' Get the part component definition
Dim oCompDef As PartComponentDefinition = oPart.ComponentDefinition
 
'Initialize variables to store the largest face and its area
Dim maxFace As Face = Nothing
Dim maxArea As Double = 0.0

' Find the edge with the largest area
For Each oFace As Face In oCompDef.SurfaceBodies.Item(1).Faces
    ' Calculate the face area
    Dim oAreaProps As Double
    oAreaProps = oFace.Evaluator.Area
    ' Check whether the current face is the face with the largest area
    If oAreaProps > maxArea Then
        maxArea = oAreaProps
        maxFace = oFace
    End If
Next

' Create a sketch on the plane of the selected face
Dim oSketch As Sketch = oCompDef.Sketches.Add(maxFace)

' Find the face contour with index 1
Dim oEdgeLoop As EdgeLoop = maxFace.EdgeLoops(1)

' Find the minimum and maximum point of the bounding parallel
Dim oMinPt As Point = oEdgeLoop.RangeBox.MinPoint 
Dim oMaxPt As Point = oEdgeLoop.RangeBox.MaxPoint

' Find the center of the bounding parallelepiped circuit
Dim CenterPt As Point = ThisApplication.TransientGeometry.CreatePoint((oMaxPt.X + oMinPt.X) / 2#, (oMaxPt.Y + oMinPt.Y) / 2#, (oMaxPt.Z + oMinPt.Z) / 2#)

' Let’s convert the coordinates of the center into the coordinates of the sketch
Dim oTextPt As Point2d = oSketch.ModelToSketchSpace(CenterPt)

'Add text "Largest face" to the sketch center
oText = "↔"
Dim oTextBox As TextBox = oSketch.TextBoxes.AddFitted(oTextPt, oText)

' Set new text height (centimeters)
		oFontSize = 6
		oTextBox.FormattedText = "<StyleOverride FontSize = '" & oFontSize & "'>" & oText & "</StyleOverride>"
		oTextBox.HorizontalJustification = kAlignTextCenter
		oTextBox.VerticalJustification = kAlignTextUpper

iLogicVb.UpdateWhenDone = True

 What are the ways to orient the text along the largest edge?

 

0 Likes
Reply
Accepted solutions (2)
392 Views
5 Replies
Replies (5)

Sergio.D.Suárez
Mentor
Mentor

Could you share some screenshots of the results you need? To see if there are simpler solutions


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

ice_sid
Explorer
Explorer

Yeah, here’s an example

Деталь с текстурой - пример 02.jpg

0 Likes

StKrause
Contributor
Contributor
Accepted solution

to search the longest edge and rotate the TextBox use:

 

' search the longest edge
Dim maxLen As Double
Dim maxEdge As Edge
For Each oEdge As Edge In oEdgeLoop.Edges
	eLen = ThisApplication.MeasureTools.GetMinimumDistance(oEdge.StartVertex.Point, oEdge.StopVertex.Point)
	If eLen > maxLen Then
		maxLen = eLen
		maxEdge = oEdge
	End If
Next
Dim oP1 As Point2d = oSketch.ModelToSketchSpace(maxEdge.StartVertex.Point)
Dim oP2 As Point2d = oSketch.ModelToSketchSpace(maxEdge.StopVertex.Point)
Ang = Atan((oP2.Y - oP1.Y) / (oP2.X - oP1.X))
' rotate TextBox
Dim oColl As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection() 
oColl.Add(oTextBox)
oTextBox.ShowBoundaries = True
oSketch.RotateSketchObjects(oColl, oSketch.ModelToSketchSpace(CenterPt), Ang, False, True)
Dim oHin As Vector2d = ThisApplication.TransientGeometry.CreateVector2d(1, 0)
Dim oHer As Vector2d = ThisApplication.TransientGeometry.CreateVector2d(-1, 0)
oSketch.MoveSketchObjects(oColl, oHin)
oSketch.MoveSketchObjects(oColl, oHer)

 

 

 

Michael.Navara
Advisor
Advisor
Accepted solution

In my opinion the longest edge must not be the right edge.

It is better to use semi-automatic rule, which allows you to pick the edge you want to align text to and use GeometricalConstraints to align text to this projected edge.

 

Dim pick = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeLinearFilter, "Pick the edge")
Dim edge As Edge = TryCast(pick, Edge)
If edge Is Nothing Then Return


Dim largestFace As Face = Nothing
Dim largestArea As Double = 0
For Each face As Face In edge.Faces
    'Skip non-planar faces
    If Not TypeOf (face.Geometry) Is Plane Then Continue For

    If face.Evaluator.Area > largestArea Then
        largestFace = face
        largestArea = face.Evaluator.Area
    End If
Next

'Return, when largest planar face wasn't found
If largestFace Is Nothing Then Return

Dim pointOnFace As Point = largestFace.PointOnFace

'Create sketch
Dim partDef As PartComponentDefinition = edge.Parent.ComponentDefinition
Dim sketch As PlanarSketch = partDef.Sketches.Add(largestFace)
Dim addByProjectingEntity As SketchEntity = sketch.AddByProjectingEntity(edge)
Dim textBox As Inventor.TextBox = sketch.TextBoxes.AddFitted(sketch.ModelToSketchSpace(pointOnFace), "<-->")
textBox.ShowBoundaries = True
sketch.GeometricConstraints.AddParallel(addByProjectingEntity, textBox.BoundaryGeometry(2))

ice_sid
Explorer
Explorer

Thank you all for the proposed solutions!

0 Likes