找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 516|回复: 0

[每日一码] 文字左对齐(C#)

[复制链接]

已领礼包: 6个

财富等级: 恭喜发财

发表于 2019-1-23 21:37:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×


                               
登录/注册后可看大图


[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Geometry;

namespace TextAlign
{
    public class Class1
    {
        [CommandMethod("TextAl")]
        public static void TextAlignmentType()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // Create a TypedValue array to define the filter criteria
                TypedValue[] acTypValAr = new TypedValue[1];
                acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "TEXT"), 0);
                // Assign the filter criteria to a SelectionFilter object
                SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);

                PromptSelectionResult acSSPrompt = doc.Editor.GetSelection(acSelFtr);
                if (acSSPrompt.Status == PromptStatus.OK)
                {
                    SelectionSet acSSet = acSSPrompt.Value;
                    DBText ent = tr.GetObject(acSSet[0].ObjectId, OpenMode.ForRead) as DBText;
                    double dblOrigX = ent.Position.X;
                    double dblOrigY = ent.Position.Y;
                    double dblOrigZ = ent.Position.Z;
                    Point3d acOrigPoint = new Point3d(dblOrigX, dblOrigY, dblOrigZ);
                    double dblTextSize = ent.Height * 1.6;
                    int intSSLength = acSSet.Count;
                    for (int intSSCount = 1; intSSCount < intSSLength; intSSCount++)
                    {
                        DBText entModText = tr.GetObject(acSSet[intSSCount].ObjectId, OpenMode.ForWrite) as DBText;
                        double dbOldX = entModText.Position.X;
                        double dbOldY = entModText.Position.Y;
                        double dbOldZ = entModText.Position.Z;
                        Point3d acOldPoint = new Point3d(dbOldX, dbOldY, dbOldZ);
                        double dblTextGap = intSSCount * dblTextSize;
                        double dbNewX = dblOrigX;
                        double dbNewY = dblOrigY - dblTextGap;
                        double dbNewZ = dblOrigZ;
                        Point3d acNewPoint = new Point3d(dbNewX, dbNewY, dbNewZ);
                        Vector3d acVector = acOldPoint.GetVectorTo(acNewPoint);
                        BlockTable acBlkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                        BlockTableRecord acBlkTblRec = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                        entModText.TransformBy(Matrix3d.Displacement(acVector));
                    }
                    tr.Commit();
                }
            }
        }
    }
}
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-4-27 07:23 , Processed in 0.227480 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表