Code Red > .NET
Stacking Mtext
nobody:
Trying to restack some text appropriately. I've tried a few different ways but at a loss. See below. Started by checking if the first text item had "two" in it, then checked if the position.X was < then position of the 2nd text, then swapped them. Tried a few different checks which worked for the most part except when the text was at a strange angle.
I know there has to be a more efficient way to do this. Below code updated from original post and works for many cases but not all. Any ideas welcome. Thanks!
--- Code - C#: --- private void swaptext() { using (help.AcadDocumentLock) { using (var t = help.StartTransaction) { try { MText tx1 = (MText)ids[0].GetObject(OpenMode.ForRead); MText tx2 = (MText)ids[1].GetObject(OpenMode.ForRead); string cont1 = tx1.Contents; string cont2 = tx2.Contents; Point3d pos1 = tx1.Location; Point3d pos2 = tx2.Location; if ((tx1.Contents.Contains("TWO")) && (pos1.X < pos2.X)) { tx1.UpgradeOpen(); tx2.UpgradeOpen(); tx1.Contents = cont2; tx2.Contents = cont1; } else if ((tx2.Contents.Contains("TWO")) && (pos2.X < pos1.X)) { tx1.UpgradeOpen(); tx2.UpgradeOpen(); tx1.Contents = cont2; tx2.Contents = cont1; } else if ((tx1.Contents.Contains("TWO")) && (pos1.Y > pos2.Y)) { tx1.UpgradeOpen(); tx2.UpgradeOpen(); tx1.Contents = cont2; tx2.Contents = cont1; } else if ((tx2.Contents.Contains("TWO")) && (pos2.Y > pos1.Y)) { tx1.UpgradeOpen(); tx2.UpgradeOpen(); tx1.Contents = cont2; tx2.Contents = cont1; } ids.Clear(); } caTWOh (System.Exception ex) { help.AcadDocument.Editor.WriteMessage("Error: ==>\n{0}\nTrace: ==>\n{1}", ex.Message, ex.StackTrace); } t.Commit(); } } }
- Modified code to match closest working solution
nobody:
Actually the graphic I made for this post gave me some ideas... I think if I check the angle from the first string position to the second string is not in the first 180-degrees might work :)
nobody:
Oy...eludes me again :( Any help appreciated. Thank you!
--- Code - C#: ---public static double anglefromxpos(Point3d pt1, Point3d pt2) { CoordinateSystem3d systemd = Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem.CoordinateSystem3d; Plane plane = new Plane(systemd.Origin, systemd.Xaxis, systemd.Yaxis); Vector3d vectord = (Vector3d)(pt2 - pt1); return vectord.AngleOnPlane(plane); } private void swaptext() { using (help.AcadDocumentLock) { using (var t = help.StartTransaction) { try { MText tx1 = (MText)ids[0].GetObject(OpenMode.ForRead); MText tx2 = (MText)ids[1].GetObject(OpenMode.ForRead); string conts1 = tx1.Contents; string conts2 = tx2.Contents; Point3d pos1 = tx1.Location; Point3d pos2 = tx2.Location; if ((tx1.Contents.Contains("TWO")) && (anglefromxpos(pos1,pos2) < 3.14159) && (pos1.Y > pos2.Y)) { tx1.Contents = conts2; tx2.Contents = conts1; } if ((tx2.Contents.Contains("TWO")) && (anglefromxpos(pos2, pos1) < 3.14159) && (pos2.Y > pos1.Y)) { tx2.Contents = conts1; tx1.Contents = conts2; } ids.Clear(); } catch (System.Exception ex) { help.AcadDocument.Editor.WriteMessage("Error: ==>\n{0}\nTrace: ==>\n{1}", ex.Message, ex.StackTrace); } t.Commit(); } } }
Bryco:
anglefromxpos(pos1,pos2)-tx1.rotation < 3.14159) sounds better to find if it is left or right
nobody:
The first I post I modified seems to get better results...though still fails on some.
Navigation
[0] Message Index
[#] Next page
Go to full version