......省略一些using
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Customization;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Internal;
using MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using MgdAcDocument = Autodesk.AutoCAD.ApplicationServices.Document;
/// <summary>
/// 文字排版
/// </summary>
[CommandMethod("sText_Sort")]
public void sText_Sort()
{
Document doc = MgdAcApplication.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
ObjectIdCollection ids = STools.GetSelection();
if (ids.Count == 0) return;
using (Transaction trans = doc.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl;
acBlkTbl = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec;
acBlkTblRec = trans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
List<DBText> sDBTxt = new List<DBText>();
foreach (ObjectId id in ids)
{
DBText sT = trans.GetObject(id, OpenMode.ForWrite) as DBText;
sDBTxt.Add(sT);
}
//从上到下排序(即按点的Y值从大到小排序)
sDBTxt.Sort(delegate(DBText txt1, DBText txt2) { return txt2.Position.Y.CompareTo(txt1.Position.Y); });
PromptPointOptions optStr1 = new PromptPointOptions("
[垂直排版(V)/等分字距(A)]<A>:");
optStr1.Keywords.Add("V");
optStr1.Keywords.Add("A");
optStr1.Keywords.Default = "A";
optStr1.AppendKeywordsToMessage = false;
PromptPointResult optRes = ed.GetPoint(optStr1);
if (optRes.Status != PromptStatus.Keyword) return;
switch (optRes.StringResult)
{
case "V":
double sDOB = 0;
if (STools.IsRegisterValueExit("间距默认值"))
{
sDOB = Convert.ToDouble(STools.GetRegisterData("间距默认值"));
}
else
{
STools.WriteRegister("间距默认值", 0.67);
sDOB = 0.67;
}
PromptDoubleOptions optDouble = new PromptDoubleOptions("
输入间距倍数");
optDouble.DefaultValue = sDOB;
optDouble.AllowNegative = false;
optDouble.AllowNone = false;
optDouble.AllowZero = false;
PromptDoubleResult resDouble = ed.GetDouble(optDouble);
if (resDouble.Status == PromptStatus.OK)
{
STools.WriteRegister("间距默认值", resDouble.Value);
sDOB = resDouble.Value;
}
else
{
return;
}
PromptPointOptions optPoint = new PromptPointOptions("
选择起始点[回车表示最上面不动]");
optPoint.AllowNone = true;
PromptPointResult resPoint = ed.GetPoint(optPoint);
Point3d sBase;
double iOffset = 0;
//当默认回车时
if (resPoint.Value.X == 0 && resPoint.Value.Y == 0)
{
sBase = new Point3d(sDBTxt[0].Position.X, sDBTxt[0].Position.Y, sDBTxt[0].Position.Z);
}
else
{
sBase = new Point3d(resPoint.Value.X,resPoint.Value.Y,resPoint.Value.Z);
iOffset = resPoint.Value.X - sDBTxt[0].Position.X;
}
int i = 0;
foreach (DBText sText_V in sDBTxt)
{
trans.GetObject(sText_V.ObjectId, OpenMode.ForWrite);
double k = sBase.Y - (1 + sDOB) * sDBTxt[0].Height * i;
Point3d sPoint = new Point3d(sText_V.Position.X + iOffset, k, sText_V.Position.Z);
//保证对齐的关键
sText_V.VerticalMode = TextVerticalMode.TextBase;
sText_V.HorizontalMode = TextHorizontalMode.TextLeft;
sText_V.Position = sPoint;
++i;
}
break;
case "A":
Point3d maxPoint = new Point3d(sDBTxt[0].Position.X, sDBTxt[0].Position.Y, sDBTxt[0].Position.Z);
Point3d minPoint = new Point3d(sDBTxt[sDBTxt.Count - 1].Position.X, sDBTxt[sDBTxt.Count - 1].Position.Y, sDBTxt[sDBTxt.Count - 1].Position.Z);
double n = (maxPoint.Y - minPoint.Y) / (sDBTxt.Count - 1);
double increment = maxPoint.Y;
foreach (DBText sTxt in sDBTxt)
{
//下面这句是配合写入数据库的,否则Commit()可能不起作用
trans.GetObject(sTxt.ObjectId, OpenMode.ForWrite);
Point3d coorIncrement = new Point3d(sTxt.Position.X, increment, sTxt.Position.Z);
//保证对齐的关键
sTxt.VerticalMode = TextVerticalMode.TextBase;
sTxt.HorizontalMode = TextHorizontalMode.TextLeft;
sTxt.Position = coorIncrement;
increment -= n;
}
break;
default:
break;
}
trans.Commit();
}
}