<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Add Text aligned to selected Lines in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12994174#M2781</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is necessary to define the plane on which each text is. The direction of the line defines the X axis of this plane, but there can be an infinite number of planes around this axis. It is therefore necessary to also define the Y axis of this plane.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example that uses an arbitrary axis algorithm (inspired by &lt;A href="https://help.autodesk.com/view/OARX/2025/ENU/?guid=GUID-E19E5B42-0CC7-4EBA-B29F-5E1D595149EE" target="_blank" rel="noopener"&gt;the algorithm used for Object Coordinate Systems&lt;/A&gt;) to define this Y axis.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;[CommandMethod("TEST")]
public void Test()
{
    var doc = AcAp.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;

    var filter = new SelectionFilter(new[] { new TypedValue(0, "LINE"), new TypedValue(8, "SPR") });
    var psr = ed.GetSelection(filter);
    if (psr.Status != PromptStatus.OK) return;

    using (var tr = db.TransactionManager.StartTransaction())
    {
        var currentSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
        foreach (var id in psr.Value.GetObjectIds())
        {
            var line = (Line)tr.GetObject(id, OpenMode.ForRead);
            var vector = line.StartPoint.GetVectorTo(line.EndPoint);
            var origin = line.StartPoint + vector * 0.5;
            var xAxis = vector.GetNormal();

            // Arbitrary algorithm to define the Y axis
            Vector3d yAxis =
                (Math.Abs(xAxis.X) &amp;lt; 0.015625 &amp;amp;&amp;amp; Math.Abs(xAxis.Y) &amp;lt; 0.015625) ?
                xAxis.CrossProduct(Vector3d.YAxis).GetNormal() :
                Vector3d.ZAxis.CrossProduct(xAxis).GetNormal();

            var zAxis = xAxis.CrossProduct(yAxis).GetNormal();

            var transform = Matrix3d.AlignCoordinateSystem(
                Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
                origin, xAxis, yAxis, zAxis);

            var text = new DBText
            {
                TextString = $"{line.Length:0.00} mm",
                Justify = AttachmentPoint.BottomCenter,
                AlignmentPoint = Point3d.Origin
            };
            text.TransformBy(transform);
            currentSpace.AppendEntity(text);
            tr.AddNewlyCreatedDBObject(text, true);
        }
        tr.Commit();
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 01 Sep 2024 13:45:30 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2024-09-01T13:45:30Z</dc:date>
    <item>
      <title>Add Text aligned to selected Lines</title>
      <link>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12993624#M2779</link>
      <description>&lt;P&gt;this should be quite simple, but i can not find the way to add a Text for each selected Line, so that the Text gets aligned to the center of the line, even if the lines change in the Z axis&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Until now i have this&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public void PrintLinesLenght()
        {
            Database db = doc.Database;
            Editor ed = doc.Editor;
            try
            {
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    ed.WriteMessage("Selecting Lines SPR... ");

                    //set the filter with two criteria: Line and LayerName
                    TypedValue[] typedValues = new TypedValue[2];
                    typedValues.SetValue(new TypedValue((int)DxfCode.Start, "LINE"), 0); //index 0 in array
                    typedValues.SetValue(new TypedValue((int)DxfCode.LayerName, "SPR"), 1); //index 1

                    //assign the filter criteria to a selection filter object
                    SelectionFilter filter = new SelectionFilter(typedValues);

                    //request for objects to be selected by the user 
                    PromptSelectionResult psr;
                    psr = ed.GetSelection(filter);
                    ObjectId[] objectIds = null;

                    if (psr.Status == PromptStatus.OK)
                    {
                        SelectionSet selectionSet = psr.Value;
                        objectIds = selectionSet.GetObjectIds();
                        ed.WriteMessage("Numbers of Objects selected: " + objectIds.Length.ToString());
                        //AcAp.ShowAlertDialog("Number of objects selected: " + );
                        foreach (ObjectId objectId in objectIds)
                        {
                            Line line = trans.GetObject(objectId, OpenMode.ForRead, true) as Line;
                            string length = line.Length.ToString();
                            SetUcsBy2Points(line.StartPoint, line.EndPoint);

                            Vector3d vector = line.EndPoint - line.StartPoint;

                            Point3d midpoint = line.StartPoint + vector * 0.5;

                            CreateText(trans, midpoint, length + " mm.", 0.00);

                            ed.CurrentUserCoordinateSystem = Matrix3d.Identity;
                        }
                    }
                    else
                    {
                        AcAp.ShowAlertDialog("Nothing was selected.");
                    }

                    trans.Commit();

                }
            }
            catch (System.Exception ex)
            {
                AcAp.ShowAlertDialog("Error found: " + ex.Message + " Found in: " + ex.TargetSite.ToString());
            }
        }

private void SetUcsBy2Points(Point3d pt1, Point3d pt2)
        {
            Editor ed = doc.Editor;
            Vector3d zAxis = ed.CurrentUserCoordinateSystem.CoordinateSystem3d.Zaxis;
            Vector3d xAxis = pt1.GetVectorTo(pt2).GetNormal();
            Vector3d yAxis = zAxis.CrossProduct(xAxis).GetNormal();
            Matrix3d mat = Matrix3d.AlignCoordinateSystem(
            Point3d.Origin,
            Vector3d.XAxis,
            Vector3d.YAxis,
            Vector3d.ZAxis,
            pt1,
            xAxis,
            yAxis,
            zAxis);
            ed.CurrentUserCoordinateSystem = mat;
        }

public void CreateText(Transaction tr, Point3d InsertionPoint, string txt, double ang)
        {
            Database db = doc.Database;

            BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
            BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

            using (DBText mtx = new DBText())
            {
                mtx.TextString = txt;
                mtx.Position = InsertionPoint;
                mtx.Height = 10;
                mtx.Rotation = ang;
                
                btr.AppendEntity(mtx);
                tr.AddNewlyCreatedDBObject(mtx, true);
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;So far I'm getting this &lt;EM&gt;Non Unitary UCS Y Axis; Normalizing&amp;nbsp;&lt;/EM&gt;Message&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and the Text do not get aligned&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="Screenshot 2024.png" style="width: 405px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1404434i9112B87B9FD8ECF9/image-dimensions/405x335?v=v2" width="405" height="335" role="button" title="Screenshot 2024.png" alt="Screenshot 2024.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will honestly appreciate any help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot&lt;/P&gt;</description>
      <pubDate>Sat, 31 Aug 2024 22:28:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12993624#M2779</guid>
      <dc:creator>emmanuel_paniaguaEJTWE</dc:creator>
      <dc:date>2024-08-31T22:28:15Z</dc:date>
    </item>
    <item>
      <title>Re: Add Text aligned to selected Lines</title>
      <link>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12993762#M2780</link>
      <description>&lt;P&gt;The image you show does not clearly show what you're trying to achieve.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, is the text left-justified with its insertion point at the midpoint of the line?&lt;/P&gt;&lt;P&gt;Or, is the text center-justified, etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An image showing what you expect might be helpful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Aside from that, the current UCS has no role in the operation you want to do. Depending on if the lines all lie in the XY plane, or have endpoints with differing elevations/z-ordinates, the operation only requires you to compute a transformation from the WCS origin to the ECS of each line using the midpoint as the origin, the endpoint as the x-axis, and the normal as the Z-axis. The current UCS has nothing to do with that and you shouldn't need to change it unless if you're using the Command() method to draw the text, which isn't the case here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once you come up with a transformation matrix for each line, you create each text object at the WCS origin, and then transform it by the matrix and yer done.&lt;/P&gt;</description>
      <pubDate>Sun, 01 Sep 2024 02:33:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12993762#M2780</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-09-01T02:33:29Z</dc:date>
    </item>
    <item>
      <title>Re: Add Text aligned to selected Lines</title>
      <link>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12994174#M2781</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is necessary to define the plane on which each text is. The direction of the line defines the X axis of this plane, but there can be an infinite number of planes around this axis. It is therefore necessary to also define the Y axis of this plane.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example that uses an arbitrary axis algorithm (inspired by &lt;A href="https://help.autodesk.com/view/OARX/2025/ENU/?guid=GUID-E19E5B42-0CC7-4EBA-B29F-5E1D595149EE" target="_blank" rel="noopener"&gt;the algorithm used for Object Coordinate Systems&lt;/A&gt;) to define this Y axis.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;[CommandMethod("TEST")]
public void Test()
{
    var doc = AcAp.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;

    var filter = new SelectionFilter(new[] { new TypedValue(0, "LINE"), new TypedValue(8, "SPR") });
    var psr = ed.GetSelection(filter);
    if (psr.Status != PromptStatus.OK) return;

    using (var tr = db.TransactionManager.StartTransaction())
    {
        var currentSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
        foreach (var id in psr.Value.GetObjectIds())
        {
            var line = (Line)tr.GetObject(id, OpenMode.ForRead);
            var vector = line.StartPoint.GetVectorTo(line.EndPoint);
            var origin = line.StartPoint + vector * 0.5;
            var xAxis = vector.GetNormal();

            // Arbitrary algorithm to define the Y axis
            Vector3d yAxis =
                (Math.Abs(xAxis.X) &amp;lt; 0.015625 &amp;amp;&amp;amp; Math.Abs(xAxis.Y) &amp;lt; 0.015625) ?
                xAxis.CrossProduct(Vector3d.YAxis).GetNormal() :
                Vector3d.ZAxis.CrossProduct(xAxis).GetNormal();

            var zAxis = xAxis.CrossProduct(yAxis).GetNormal();

            var transform = Matrix3d.AlignCoordinateSystem(
                Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
                origin, xAxis, yAxis, zAxis);

            var text = new DBText
            {
                TextString = $"{line.Length:0.00} mm",
                Justify = AttachmentPoint.BottomCenter,
                AlignmentPoint = Point3d.Origin
            };
            text.TransformBy(transform);
            currentSpace.AppendEntity(text);
            tr.AddNewlyCreatedDBObject(text, true);
        }
        tr.Commit();
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Sep 2024 13:45:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12994174#M2781</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-09-01T13:45:30Z</dc:date>
    </item>
    <item>
      <title>Re: Add Text aligned to selected Lines</title>
      <link>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12994552#M2782</link>
      <description>That works perfect, i still have to go through the algorithm. I don't understand the 0.015625 business, to be honest &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;I'm more than glad to get an answer from you Gilles, i visited your website for UI inspiration many times.&lt;BR /&gt;Thanks again!</description>
      <pubDate>Sun, 01 Sep 2024 19:06:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12994552#M2782</guid>
      <dc:creator>emmanuel_paniaguaEJTWE</dc:creator>
      <dc:date>2024-09-01T19:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: Add Text aligned to selected Lines</title>
      <link>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12996018#M2783</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/15606478"&gt;@emmanuel_paniaguaEJTWE&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;That works perfect, i still have to go through the algorithm. I don't understand the 0.015625 business, to be honest &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;The test against 0.015625 is a safeguard to ensure that when the X-Axis is very close to the Z-axis, the code uses a stable method to compute an orthogonal vector (Y-Axis) by switching to a cross product with the Y-axis. This prevents issues that could arise from the cross product of two nearly parallel vectors (which would result in a very small or zero vector).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, you really don't need the arbitrary axis algorithm to solve this problem, Lines have a normal (extrusion direction), which means that you already have two axes:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static void TransformTo(this DBText dbText, Line line)
{
   var xAxis = (line.EndPoint - line.StartPoint).GetNormal();
   var mat = Matrix3d.AlignCoordinateSystem(
       Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
       line.StartPoint, xAxis, line.Normal.CrossProduct(xAxis), line.Normal);
   dbText.TransformBy(mat);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Sep 2024 15:07:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12996018#M2783</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-09-02T15:07:31Z</dc:date>
    </item>
    <item>
      <title>Re: Add Text aligned to selected Lines</title>
      <link>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12996118#M2784</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4476837"&gt;@ActivistInvestor&lt;/a&gt;&amp;nbsp; a écrit &lt;BR /&gt;However, you really don't need the arbitrary axis algorithm to solve this problem, Lines have a normal (extrusion direction), which means that you already have two&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The Normal property of a line is not reliable and can be not perpendicular to the line. In fact, the Normal property of a line indicates the Z axis of the current UCS at the time the line was drawn (the thickness direction).&lt;/P&gt;
&lt;P&gt;If you draw a line in WCS from (0, 0, 0) to (20, 15, 10) its Normal property will be (0, 0, 1).&lt;/P&gt;</description>
      <pubDate>Mon, 02 Sep 2024 18:28:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12996118#M2784</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-09-02T18:28:05Z</dc:date>
    </item>
    <item>
      <title>Re: Add Text aligned to selected Lines</title>
      <link>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12996262#M2785</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;. Wow, I completely forgot about that. Doing a quick search of my old code base on the term CrossProduct, turned up the following, and it is indeed what's used by other code in lieu of the Line's Normal property:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static Vector3d GetNormal(this Line line)
{
   Vector3d dir = (line.EndPoint - line.StartPoint).GetNormal();
   return dir.CrossProduct(
      new Vector3d(dir.Y - dir.Z, dir.Z - dir.X, dir.X - dir.Y)).GetNormal();
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Sep 2024 18:49:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12996262#M2785</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-09-02T18:49:04Z</dc:date>
    </item>
    <item>
      <title>Re: Add Text aligned to selected Lines</title>
      <link>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12996475#M2786</link>
      <description>&lt;P&gt;See the above replies.&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;is correct that the AAA must be used when dealing with a single Line entity.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, if you're dealing with multiple lines, some of which may be coplanar, and you would like the text associated with each line to be in the same plane the coplanar lines are in, the solution becomes a bit more complicated, and doesn't involve using the AAA.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Sep 2024 22:42:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12996475#M2786</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-09-02T22:42:54Z</dc:date>
    </item>
    <item>
      <title>Re: Add Text aligned to selected Lines</title>
      <link>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12999653#M2787</link>
      <description>Thanks a lot for all the insights you have shared. It helps a lot</description>
      <pubDate>Wed, 04 Sep 2024 07:07:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/add-text-aligned-to-selected-lines/m-p/12999653#M2787</guid>
      <dc:creator>emmanuel_paniaguaEJTWE</dc:creator>
      <dc:date>2024-09-04T07:07:46Z</dc:date>
    </item>
  </channel>
</rss>

