根据CAD中包含的内容和坐标堆叠CAD文本

时间:2017-08-19 03:25:40

标签: c# .net autocad autocad-plugin

请参阅附图,显示我需要如何在CAD中堆叠文本。我尝试的一切都未能解决所有情况。

基本上,我使用文本的坐标来尝试确定如何正确排列它。任何帮助将不胜感激。我的工作如下:

       private void swaptext()
    {
        using (ac.AcadDocumentLock)
        {
            using (var t = ac.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")) && (pos1.X < pos2.X))
                    {
                            tx1.Contents = conts2;
                            tx2.Contents = conts1;
                    }

                    else if ((tx1.Contents.Contains("TWO")) && (pos1.Y < pos2.Y))
                    {

                            tx2.Contents = conts1;
                            tx1.Contents = conts2;
                    }


                    ids.Clear();
                }
                catch (System.Exception ex)
                {
                 ac.AcadDocument.Editor.WriteMessage("Error: ==>\n{0}\nTrace: ==>\n{1}", ex.Message, ex.StackTrace);
                }
                t.Commit();
            }
        }
    }

Stacking Text

1 个答案:

答案 0 :(得分:0)

如果您只是将文本旋转回0度,那么您可以只比较哪个文本具有更高的Y坐标。那一个应该包含“#34; ONE&#34;

这个词
private void swaptext()
    {
        using (ac.AcadDocumentLock)
        {
            using (var t = ac.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;
                    Extents3d Ex1 = tx1.GeometricExtents;
                    Extents3d Ex2 = tx2.GeometricExtents;
                    Point3d pos1 = Ex1.MaxPoint.RotateBy(-tx1.Rotation, tx1.Normal, tx1.Location);
                    Point3d pos2 = Ex2.MaxPoint.RotateBy(-tx2.Rotation, tx2.Normal, tx2.Location);

                    if ((tx1.Contents.Contains("TWO")) && (pos1.Y > pos2.Y))
                    {
                        tx1.Contents = conts2;
                        tx2.Contents = conts1;
                    }
                    else
                        if ((tx2.Contents.Contains("TWO")) && (pos2.Y > pos1.Y))
                        {
                            tx1.Contents = conts2;
                            tx2.Contents = conts1;
                        }

                    ids.Clear();
                }
                catch (System.Exception ex)
                {
                    ac.AcadDocument.Editor.WriteMessage("Error: ==>\n{0}\nTrace: ==>\n{1}", ex.Message, ex.StackTrace);
                }
                t.Commit();
            }
        }
    }