zoukankan      html  css  js  c++  java
  • CAD二开---框选文字批量对齐

    一.思路

              DBText除了有Position位置属性外,还有对齐属性AlignmentPosition,AlignmentPosition属性有以下可选择:

                               

    二.代码     

               通用类:    

    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Geometry;
    using Autodesk.Private.Windows.ToolBars;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace OperationCurrentView.CommLib
    {
        public class CommLib
        {
            public Document m_doc;
            public Database m_db;
            public Editor m_editor;
    
            public CommLib()
            {
                m_doc = Application.DocumentManager.MdiActiveDocument;
                m_db = m_doc.Database;
                m_editor = m_doc.Editor;
            }
    
            /// <summary>
            /// 显示字符串
            /// </summary>
            /// <param name="str"></param>
            public void CADDisplay(string str)
            {
                m_editor.WriteMessage(str);
            }
            /// <summary>
            /// 选择一个点
            /// </summary>
            /// <param name="optMsg"></param>
            /// <returns></returns>
            public Point3d SelectPoint(string optMsg)
            {
                PromptPointResult promptPtRes;
                PromptPointOptions promptPtOps = new PromptPointOptions("");
                //提示起点
                promptPtOps.Message = optMsg;
                promptPtRes = m_editor.GetPoint(promptPtOps);
                Point3d startPt = promptPtRes.Value;
    
                return startPt;
            }
            /// <summary>
            /// 获取选择集
            /// </summary>
            /// <param name="selectStr"></param>
            /// <param name="ptColl"></param>
            /// <param name="acTypeVal"></param>
            /// <returns></returns>
            public SelectionSet GetSelectionSet(string selectStr, Point3dCollection ptColl, TypedValue[] acTypeVal)
            {
                SelectionFilter filter = null;
                if (acTypeVal != null)
                    filter = new SelectionFilter(acTypeVal);     //过滤原则
                //请求在图形区域内选择对象
                PromptSelectionResult promptSelectRes;
                if (selectStr == "GetSelection")
                    promptSelectRes = m_editor.GetSelection(filter);
                else if (selectStr == "SelectAll")
                {
                    promptSelectRes = m_editor.SelectAll(filter);    //获取空间内未锁定和冻结的对象
                }
                else if (selectStr == "SelectCrossingPolygon")
                {
                    //选择由给定点定义的多边形内的所有对象以及与多边形相交的对象
                    promptSelectRes = m_editor.SelectCrossingPolygon(ptColl, filter);
                }
                else if (selectStr == "SelectCrossingWindow")
                {
                    //选择由两个点定义的窗口内的对象及与窗口相交的对象
                    promptSelectRes = m_editor.SelectCrossingWindow(ptColl[0], ptColl[1], filter);
                }
                else if (selectStr == "SelectWindow")
                {
                    //选择完全框入由两个点定义的矩形内的所有对象
                    promptSelectRes = m_editor.SelectWindow(ptColl[0], ptColl[1], filter);
                }
                else if (selectStr == "SelectWindowPolygon")
                {
                    //选择完全框入由点定义的多边形内的对象
                    promptSelectRes = m_editor.SelectWindowPolygon(ptColl, filter);
                }
                else if (selectStr == "SelectFence")
                {
                    //选择与选择围栏相交的所有对象,围栏选择与多边形选择类型.
                    promptSelectRes = m_editor.SelectFence(ptColl, filter);
                }
                else { return null; }
    
                if (promptSelectRes.Status == PromptStatus.OK)
                {
                    SelectionSet selectSet = promptSelectRes.Value;
                    CADDisplay($"选择的对象数量:{selectSet.Count}
    ");
                    return selectSet;
                }
                else
                {
                    CADDisplay("选择的对象数量为0");
                    return null;
                }
    
    
            }
    
        }
    }
    

           操作代码:

                         

            //批量对齐文字
            [CommandMethod("AligmentText")]
            public void AligmentTxt()
            {
                CommLib.CommLib commUtil = new CommLib.CommLib();
                commUtil.CADDisplay("
    这是文字批量对齐");
                Point3d pt = commUtil.SelectPoint("
    >>>请选择一个基点");
    
                TypedValue[] typedVals = new TypedValue[1];
                typedVals.SetValue(new TypedValue(0, "Text"), 0);
    
                //获取选择集
                SelectionSet selectSet = commUtil.GetSelectionSet("GetSelection", null, typedVals);
    
                Database db = commUtil.m_doc.Database;
                if (selectSet != null)
                {
                    foreach(SelectedObject item in selectSet)
                    {
                        if (item != null)
                        {
                            using(Transaction trans = db.TransactionManager.StartTransaction())
                            {
                                DBText txt = trans.GetObject(item.ObjectId, OpenMode.ForWrite) as DBText;
                                txt.HorizontalMode = TextHorizontalMode.TextRight;
                                if (txt.HorizontalMode != TextHorizontalMode.TextLeft)
                                {
                                    Point3d alipt = txt.AlignmentPoint;   //值为零   AlignmentPoint对齐点
                                    commUtil.CADDisplay($"
    {alipt.ToString()}");
                                    Point3d posPt = txt.Position;
                                    txt.AlignmentPoint = new Point3d(pt.X, posPt.Y, 0);
                                }
                                else
                                {
                                    Point3d posPt = txt.Position;
                                    txt.Position = new Point3d(pt.X, posPt.Y, 0);
                                }
                                trans.Commit();
                            }
                        }
                    }
                }
            }

    三.总结

               程序思路:

                           1、 选择一个基准对齐点(选择点)

                           2、 框选文字(选择集)

                           3、 遍历每个文字,更改其对齐方式为 左对齐  对齐点的X 与基准点保持一致

                           4、 完成程序

               根据文档,在Left 对齐方式下,不需要AlignmengtPoint,这个时候给AlignmengtPoint赋值无效会报错

                                                        

        

  • 相关阅读:
    二叉树的节点删除
    PHP开启错误日志详细说明
    jsonpath模块
    Gunicorn-配置详解
    Vmware创建虚拟机步骤说明,详细配置解释
    Python multiprocessing使用详解
    Python定时任务框架apscheduler
    Access-Control-Allow-Origin跨域解决及详细介绍
    web安全:x-content-type-options头设置
    sqlalchemy的基本操作大全
  • 原文地址:https://www.cnblogs.com/xiaowangzi1987/p/13162754.html
Copyright © 2011-2022 走看看