- UID
- 6847
- 积分
- 1065
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-6-23
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
- using System;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.EditorInput;
- //using static LayerAndTextTools.layermanager;
- //using static ZgxCommomLib.StaticDataBase;
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using Autodesk.AutoCAD.Colors;
- using static Test.HelperTools;
- //using ZgxCommomLib;
- [assembly:CommandClass(typeof(Test.Command))]
- namespace Test
- {
- public class Command
- {
- [CommandMethod("llll")]
- public void FastDimension()
- {
- #region 初始化变量
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = Application.DocumentManager.MdiActiveDocument.Database;
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- List<AlignedDimension> alignedDimensionsList = new List<AlignedDimension>();//承载新创建尺寸线
- Point3d xLine1 = new Point3d();
- Point3d xLine2 = new Point3d();
- //固定边界投影线
- Point3d xl1Oringinal = new Point3d();
- Point3d xl2Oringinal = new Point3d();
- //标注方向,X & Y
- Vector3d xDirection = new Vector3d();
- Vector3d yDirection = new Vector3d();
- //尺寸边线长度
- Double xlLength = db.Dimtxt * 500.0;
- #endregion
- bool bySelection = false;
- PromptPointOptions ppo = new PromptPointOptions("\n尺寸标注起点,右键选择尺寸线");
- ppo.AllowNone = true;
- PromptPointResult ppr = ed.GetPoint(ppo);
- if (ppr.Status != PromptStatus.OK)
- {
- bySelection = true;
- goto userToSelect;//去选择尺寸线
- }
- CreateLayer("_0_tcc_dimension", 191);//标注图层
- xLine1 = ppr.Value;
- Vector3d tmpVector = new Vector3d(1, 0, 0);
- //初始化一个尺寸JIG
- AlignedDimJig alignedDim = new AlignedDimJig(xLine1,
- xLine1.TransformBy(Matrix3d.Displacement(tmpVector * xlLength * 2)),
- xlLength);
- //设置Jig参数
- alignedDim.Counter = 1;
- alignedDim.BasePoint = xLine1;//拖动基点
- alignedDim.UserInputControls1 = UserInputControls.Accept3dCoordinates |
- UserInputControls.NoZeroResponseAccepted |
- UserInputControls.NoNegativeResponseAccepted |
- UserInputControls.NullResponseAccepted;//|UserInputControls.GovernedByOrthoMode;
- using (alignedDim)
- {
- // 1 创建尺寸线,拖动改变长度
- PromptResult pr = ed.Drag(alignedDim);
- if (pr.Status == PromptStatus.OK)
- {
- alignedDim.Counter = 2;
- }
- else
- {
- alignedDim.AlignedDimension.Dispose();
- ed.WriteMessage("\n disposed !");
- return;
- }
- // 2 改变尺寸线位置
- alignedDim.UserInputControls1 = UserInputControls.Accept3dCoordinates |
- UserInputControls.NoZeroResponseAccepted |
- UserInputControls.NoNegativeResponseAccepted |
- UserInputControls.NullResponseAccepted;//| UserInputControls.GovernedByOrthoMode;
- alignedDim.BasePoint = alignedDim.AlignedDimension.XLine2Point;
- alignedDim.UseBasePoint = false;
- //拖拽开始
- pr = ed.Drag(alignedDim);
- if (pr.Status == PromptStatus.OK)
- {
- AlignedDimension alDim = alignedDim.AlignedDimension;
- db.AddToCurrentSpace(alDim);
- //添加到list
- alignedDimensionsList.Add(alDim);
- //尺寸界线长度
- xlLength = alDim.XLine2Point.DistanceTo(alDim.DimLinePoint);
- //标注方向
- xDirection = (alDim.XLine2Point - alDim.XLine1Point).GetNormal();
- yDirection = (alDim.DimLinePoint - alDim.XLine2Point).GetNormal();
- //固定边界投影线
- xl1Oringinal = alDim.XLine1Point;
- xl2Oringinal = alDim.XLine2Point;
- //原终点改为起点,作为新尺寸起点
- xLine1 = alDim.XLine2Point;
- xLine2 = alDim.XLine1Point;
- }
- else
- {
- alignedDim.GetEntity().Dispose();
- ed.WriteMessage("\n 操作取消 !");
- alignedDimensionsList = null;
- return;
- }
- }//using alignDim
- userToSelect:
- if (bySelection)
- {
- ed.WriteMessage("\n 用户选择 !");
- PromptEntityOptions peo = new PromptEntityOptions("\n选择尺寸,右键退出");
- peo.SetRejectMessage("必须是尺寸标注");
- peo.AddAllowedClass(typeof(AlignedDimension), false);
- peo.AllowNone = true;
- PromptEntityResult al = ed.GetEntity(peo);
- if (al.Status != PromptStatus.OK)
- return;
- using (OpenCloseTransaction tr = db.TransactionManager.StartOpenCloseTransaction())
- {
- AlignedDimension e = tr.GetObject(al.ObjectId, OpenMode.ForRead) as AlignedDimension;
- alignedDimensionsList.Add(e);
- xl1Oringinal = e.XLine1Point;
- xl2Oringinal = e.XLine2Point;
- xlLength = e.DimLinePoint.DistanceTo(e.XLine2Point);
- xDirection = (xl2Oringinal - xl1Oringinal).GetNormal();
- yDirection = (e.DimLinePoint - xl2Oringinal).GetNormal();
- xLine1 = e.XLine2Point;
- xLine2 = e.XLine1Point;
- bySelection = false;
- tr.Commit();
- }
- }
- // 3 连续标注
- bool loop = true;
- while (loop)
- {
- AlignedDimJig newAlignedDim = new AlignedDimJig(xLine1, xLine2, xlLength);
- using (newAlignedDim)
- {
- newAlignedDim.Counter = 3;
- newAlignedDim.Xl1PointOringinal = xl1Oringinal;
- newAlignedDim.Xl2PointOringinal = xl2Oringinal;
- newAlignedDim.XlLineLength = xlLength;
- newAlignedDim.DimXDirectionNormal = xDirection;
- newAlignedDim.DimYDirectionNormal = yDirection;
- bool isOutSide = true;
- PromptResult pr = ed.Drag(newAlignedDim);
- if (pr.Status == PromptStatus.OK)
- {
- AlignedDimension newDim = newAlignedDim.AlignedDimension;
- loop = true;
- xLine1 = newDim.XLine2Point;
- xLine2 = newDim.XLine1Point;
- //判断是否为零长度尺寸
- if (xLine1.Equals(xLine2))
- {
- newDim.Dispose();
- newDim = null;
- ed.WriteMessage("\n零长度尺寸,已删除。");
- }
- //非零长度尺寸
- else
- {
- db.AddToCurrentSpace(newDim);//添加到当前空间
- Point3d start = xLine1;
- using (OpenCloseTransaction tr1 = db.TransactionManager.StartOpenCloseTransaction())
- {
- foreach (AlignedDimension dim in alignedDimensionsList)
- {
- if (start.IsBetween(dim.XLine1Point, dim.XLine2Point) == true)
- {
- isOutSide = false;//在尺寸线内部
- tr1.GetObject(newDim.ObjectId, OpenMode.ForWrite);
- ed.WriteMessage("\n在尺寸线内部");
- //重合,产生零尺寸
- newDim.XLine1Point = dim.XLine2Point;
- newDim.DimLinePoint = dim.XLine2Point.TransformBy
- (Matrix3d.Displacement(yDirection * xlLength));
- if (newDim.XLine1Point.Equals(newDim.XLine2Point))
- {
- newDim.Erase();
- newDim = null;
- ed.WriteMessage("\n零长度尺寸,已删除。");
- break;
- }
- else
- {
- tr1.GetObject(dim.ObjectId, OpenMode.ForWrite);
- dim.XLine2Point = dim.XLine2Point.TransformBy
- (Matrix3d.Displacement(start - dim.XLine2Point));
- dim.DimLinePoint = dim.DimLinePoint.TransformBy
- (Matrix3d.Displacement(start - dim.XLine2Point));
- start = xLine2;
- break;
- }
- }
- }// foreach
- if (isOutSide == true)
- {
- //查询与curPt最近距离点
- var nearestPt =
- (from x in
- (
- from xl1 in alignedDimensionsList
- select xl1.XLine1Point
- ).Union(from xl2 in alignedDimensionsList
- select xl2.XLine2Point)
- orderby x.DistanceTo(newDim.XLine2Point)
- select x).Take(1).ToArray()[0];
- tr1.GetObject(newDim.ObjectId, OpenMode.ForWrite);
- newDim.XLine1Point = nearestPt;
- //再次避免零长度尺寸线产生
- if (newDim.XLine1Point.Equals(newDim.XLine2Point))
- {
- newDim.Erase();
- newDim = null;
- ed.WriteMessage("\n零长度尺寸,已删除。");
- }
- }
- tr1.Commit();
- }// using tr1
- if (newDim != null)
- alignedDimensionsList.Add(newDim);
- }
- }
- else
- {
- newAlignedDim.AlignedDimension.Dispose();
- ed.WriteMessage("\n 标注完成!");
- loop = false;
- alignedDimensionsList = null;
- return;
- }
- } //end of using (AlignedDim newAlignedDim = new AlignedDim(xl1, xl2, xlLength))
- }// end of loop
- }
- }
- public class AlignedDimJig : EntityJig, IDisposable
- {
- #region // 声明变量字段
- //private DelEntityJigModify _modifyEntity;
- private Vector3d _dimXDirectionNormal, _dimYDirectionNormal;
- private Point3d _xl1PointOringinal, _xl2PointOringinal;
- private Point3d _xl1Point, _xl2Point;
- private Point3d _BasePoint;
- private Point3d _dimLinePoint;
- private Point3d _curPt;
- private double _xlLineLength;
- private double _dimTextHt;
- private AlignedDimension _alignedDimension;
- private int _counter;
- private int _rotate;
- private UserInputControls _userInputControls = UserInputControls.Accept3dCoordinates |
- UserInputControls.NoZeroResponseAccepted |
- UserInputControls.NoNegativeResponseAccepted |
- UserInputControls.NullResponseAccepted;
- private bool _useBasePoint = true;
- public List<AlignedDimension> _alignedDimensionsList = new List<AlignedDimension>();
- //public DelEntityJigModify ModifyEntity { get => _modifyEntity; set => _modifyEntity = value; }
- public Vector3d DimXDirectionNormal { get => _dimXDirectionNormal; set => _dimXDirectionNormal = value; }
- public Vector3d DimYDirectionNormal { get => _dimYDirectionNormal; set => _dimYDirectionNormal = value; }
- public Point3d Xl1PointOringinal { get => _xl1PointOringinal; set => _xl1PointOringinal = value; }
- public Point3d Xl2PointOringinal { get => _xl2PointOringinal; set => _xl2PointOringinal = value; }
- public Point3d DimLinePoint { get => _dimLinePoint; set => _dimLinePoint = value; }
- public Point3d CurPt { get => _curPt; set => _curPt = value; }
- public double XlLineLength { get => _xlLineLength; set => _xlLineLength = value; }
- public double DimTextHt { get => _dimTextHt; set => _dimTextHt = value; }
- public AlignedDimension AlignedDimension { get => _alignedDimension; set => _alignedDimension = value; }
- public Point3d Xl1Point { get => _xl1Point; set => _xl1Point = value; }
- public Point3d Xl2Point { get => _xl2Point; set => _xl2Point = value; }
- public int Counter { get => _counter; set => _counter = value; }
- public Point3d BasePoint { get => _BasePoint; set => _BasePoint = value; }
- public int Rotate { get => _rotate; set => _rotate = value; }
- public UserInputControls UserInputControls1 { get => _userInputControls; set => _userInputControls = value; }
- public bool UseBasePoint { get => _useBasePoint; set => _useBasePoint = value; }
- #endregion
- // 派生类的构造函数.
- public AlignedDimJig(Point3d xl1, Point3d xl2, double xlLinelength)
- : base(new AlignedDimension())
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = Application.DocumentManager.MdiActiveDocument.Database;
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- _xl1Point = xl1;
- _xl2Point = xl2;
- _xlLineLength = xlLinelength;
- _dimLinePoint = GetDimLinePoint(_xl1Point, _xl2Point, xlLinelength);
- _alignedDimension = (AlignedDimension)(Entity);
- _alignedDimension.XLine1Point = _xl1Point;
- _alignedDimension.XLine2Point = _xl2Point;
- _alignedDimension.DimLinePoint = _dimLinePoint;
- _alignedDimension.SetDatabaseDefaults();
- _alignedDimension.Layer = "_0_tcc_dimension";
- #region MyRegion
- using (OpenCloseTransaction tr = db.TransactionManager.StartOpenCloseTransaction())
- {
- DimStyleTableRecord btr = tr.GetObject(db.Dimstyle, OpenMode.ForRead) as DimStyleTableRecord;
- _alignedDimension.Annotative = btr.Annotative;
- tr.Commit();
- }
- #endregion
- //_alignedDimension.Annotative = AnnotativeStates.True;
- }
- public AlignedDimJig(Entity alDim)
- : base(alDim)
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = Application.DocumentManager.MdiActiveDocument.Database;
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- _alignedDimension = (AlignedDimension)(Entity);
- _xl1Point = _alignedDimension.XLine1Point;
- _xl2Point = _alignedDimension.XLine2Point;
- _dimLinePoint = _alignedDimension.DimLinePoint;
- _xlLineLength = _xl2Point.DistanceTo(_xl2Point);
- _alignedDimension.SetDatabaseDefaults();
- _alignedDimension.Layer = "_0_tcc_dimension";
- #region MyRegion
- //using (OpenCloseTransaction tr = db.TransactionManager.StartOpenCloseTransaction())
- //{
- // DimStyleTableRecord btr = tr.GetObject(db.Dimstyle, OpenMode.ForRead) as DimStyleTableRecord;
- // _alignedDimension.Annotative = btr.Annotative;
- // tr.Commit();
- //}
- #endregion
- //_alignedDimension.Annotative = AnnotativeStates.True;
- }
- //获取尺寸线位置
- public Point3d GetDimLinePoint(Point3d xl1, Point3d xl2, double dimLineLength)
- {
- Matrix3d mat = (Matrix3d.Displacement(dimLineLength * (xl2 - xl1).GetNormal().RotateBy(Math.PI * -0.5, Vector3d.ZAxis)));
- return xl2.TransformBy(mat);
- }
- //获取尺寸线位置
- public Point3d GetDimLinePoint(Point3d xl2, double dimLineLength)
- {
- return xl2.TransformBy(Matrix3d.Displacement(DimYDirectionNormal * dimLineLength));
- }
- protected override bool Update()
- {
- try
- {
- switch (_counter)
- {
- case 1:
- {
- _xl2Point = _curPt;
- _dimLinePoint = GetDimLinePoint(_xl1Point, _xl2Point, _xlLineLength);
- }
- break;
- case 2:
- {
- _dimLinePoint = CurPt;
- _xl1Point = _alignedDimension.XLine1Point;
- _xl2Point = _alignedDimension.XLine2Point;
- _xl1PointOringinal = _xl1Point;
- _xl2PointOringinal = _xl2Point;
- //_dimXDirectionNormal = (_xl2Point - _xl1Point).GetNormal();
- //_dimYDirectionNormal = _dimXDirectionNormal.RotateBy(Math.PI * -0.5, Vector3d.ZAxis);
- }
- break;
- case 3:
- {
- //Vector3d vTmp = (_alignedDimension.DimLinePoint - _alignedDimension.XLine1Point)-;
- _xl2Point = _curPt.GetClosePointTo(_xl1PointOringinal, _xl2PointOringinal);
- //_dimLinePoint = GetDimLinePoint(_xl2Point, _xlLineLength);
- _dimLinePoint = Xl2Point.TransformBy(Matrix3d.Displacement(_dimYDirectionNormal * _xlLineLength));
- #region MyRegion
- //bool isOutsSide = true;
- //foreach (AlignedDimension dim in _alignedDimensionsList)
- //{
- // if (_xl2Point.IsBetween(dim.XLine1Point, dim.XLine2Point) == true)
- // {
- // isOutsSide = false;
- // break;
- // }
- //}
- //if (isOutsSide==true)
- //{
- // List<Point3d> pts = new List<Point3d>();
- // for (int i = 0; i < _alignedDimensionsList.Count; i++)
- // {
- // pts.Add(_alignedDimensionsList.XLine1Point);
- // pts.Add(_alignedDimensionsList.XLine2Point);
- // }
- // var nearestPt = (from c in pts
- // orderby c.DistanceTo(_xl2Point)
- // select c).Take(1).ToList()[0];
- // _xl1Point = nearestPt;
- // _BasePoint = nearestPt;
- //}
- #endregion
- }
- break;
- }
- _alignedDimension.XLine1Point = _xl1Point;
- _alignedDimension.XLine2Point = _xl2Point;
- _alignedDimension.DimLinePoint = _dimLinePoint;
- }
- catch
- {
- // 此处不需要处理.
- }
- return true;
- }
- protected override SamplerStatus Sampler(JigPrompts prompts)
- {
- JigPromptPointOptions jigPPR = new JigPromptPointOptions("\n尺寸终点");
- // 设置拖拽的光标类型.
- jigPPR.UserInputControls = _userInputControls;
- jigPPR.Cursor = CursorType.RubberBand;
- // 设置拖动光标基点.
- jigPPR.BasePoint = _xl1Point;
- jigPPR.UseBasePoint = UseBasePoint;
- // 用AcquireAngle函数得到用户输入的角度值.
- PromptPointResult resJigPPR = prompts.AcquirePoint(jigPPR);
- if (_curPt != resJigPPR.Value)
- {
- // 保存当前角度值.
- _curPt = resJigPPR.Value;
- }
- else
- {
- return SamplerStatus.NoChange;
- }
- if (resJigPPR.Status == PromptStatus.Cancel)
- {
- return SamplerStatus.Cancel;
- }
- if (resJigPPR.Status == PromptStatus.None)
- {
- return SamplerStatus.Cancel;
- }
- else
- {
- return SamplerStatus.OK;
- }
- }
- // GetEntity函数用于得到派生类的实体.
- public Entity GetEntity()
- {
- return Entity;
- }
- #region IDisposable Support
- private bool disposedValue = false; // 要检测冗余调用
- protected virtual void Dispose(bool disposing)
- {
- if (!disposedValue)
- {
- if (disposing)
- {
- // TODO: 释放托管状态(托管对象)。
- }
- // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
- // TODO: 将大型字段设置为 null。
- disposedValue = true;
- }
- }
- // TODO: 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。
- // ~AlignedDim() {
- // // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
- // Dispose(false);
- // }
- // 添加此代码以正确实现可处置模式。
- public void Dispose()
- {
- // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
- Dispose(true);
- // TODO: 如果在以上内容中替代了终结器,则取消注释以下行。
- // GC.SuppressFinalize(this);
- }
- #endregion
- // setPromptCounter过程用于控制不同的拖拽.
- }
- public static class HelperTools
- {
- public static ObjectId AddToCurrentSpace(this Database db, Entity ent)
- {
- ObjectId entId;//用于返回添加到模型空间中的实体ObjectId
- //定义一个指向当前数据库的事务处理,以添加直线
- using (OpenCloseTransaction trans = db.TransactionManager.StartOpenCloseTransaction())
- {
- //以写方式打开模型空间块表记录.
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- entId = btr.AppendEntity(ent);//将图形对象的信息添加到块表记录中
- trans.AddNewlyCreatedDBObject(ent, true);//把对象添加到事务处理中
- trans.Commit();//提交事务处理
- }
- return entId; //返回实体的ObjectId
- }
- public static bool IsBetween(this Point3d pt, Point3d p1, Point3d p2)
- {
- if (pt.Equals(1) | pt.Equals(p2))
- return true;
- else
- return p1.GetVectorTo(pt).GetNormal().Equals(pt.GetVectorTo(p2).GetNormal());
- }
- public static Point3d GetClosePointTo(this Point3d pt0, Point3d pt1, Point3d pt2)
- {
- using (Line3d line3 = new Line3d(pt1, pt2))
- {
- return line3.GetClosestPointTo(pt0).Point;
- }
- }
- public static void CreateLayer(string layname, short color)
- {
- // 获取当前文档和数据库
- Document acDoc = Application.DocumentManager.MdiActiveDocument;
- Database acCurDb = acDoc.Database;
- // 启动事务
- using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
- {
- // 以读模式打开图层表
- LayerTable acLyrTbl;
- acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
- OpenMode.ForRead) as LayerTable;
- string sLayerName = layname;
- if (acLyrTbl.Has(sLayerName) == false)
- {
- LayerTableRecord acLyrTblRec = new LayerTableRecord();
- // 赋予图层颜色和名称,AutoCADColorIndex(ACI)为1表示红色
- acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, color);
- acLyrTblRec.Name = sLayerName;
- // 以写模式升级打开图层表
- acLyrTbl.UpgradeOpen();
- // 添加新图层到图层表,记录事务
- acLyrTbl.Add(acLyrTblRec);
- acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
- }
- // 保存修改,关闭事务
- acTrans.Commit();
- }
- }
- }
- }
|
|