马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 csharp 于 2014-8-6 08:28 编辑
- using System;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- [assembly: CommandClass(typeof(ArrayPath.MyCommands))]
- namespace ArrayPath
- {
- public class MyCommands
- {
- private double _dist = 0;
- private int _num = 0;
- [CommandMethod("arPath")]
- public void MyCommand()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- Point3d basePoint;
- bool flag;
- PromptSelectionOptions pso = new PromptSelectionOptions();
- pso.MessageForAdding = "\n选择阵列实体";
- PromptSelectionResult psr = ed.GetSelection(pso);
- if (psr.Status != PromptStatus.OK) return;
- SelectionSet ss = psr.Value;
- PromptPointOptions ppo = new PromptPointOptions("\n阵列基点<回车选集中心>");
- ppo.AllowNone = true;
- PromptPointResult ppr = ed.GetPoint(ppo);
- if (ppr.Status == PromptStatus .OK )
- {
- basePoint = ppr.Value;
- }
- else if (ppr.Status == PromptStatus .None )
- {
- basePoint = SsetCenter(ss.GetObjectIds());
- }
- else
- {
- return;
- }
- PromptEntityOptions peo = new PromptEntityOptions("\n选择路径");
- peo.SetRejectMessage("\n请选择曲线");
- peo.AddAllowedClass(typeof (Arc),false );
- peo.AddAllowedClass(typeof (Line),false );
- peo.AddAllowedClass(typeof (Spline),false );
- peo.AddAllowedClass(typeof (Polyline),false);
- PromptEntityResult per = ed.GetEntity(peo);
- if (per.Status != PromptStatus .OK )
- {
- return;
- }
- ObjectId cId = per.ObjectId;
- //DefaultPamram defaultPamram = new DefaultPamram();
- //增加关键字提示
- PromptDistanceOptions pdo = new PromptDistanceOptions("\n拾取距离[等分(D)]", "Divid");
-
- //是否使用默认值
- if (_dist != 0)
- {
- pdo.DefaultValue = _dist;
- }
- //允许回车要放到默认值后面,否则不出现默认值提示
- pdo.AllowNone = true;//允许回车
- //提示输入,拾取距离或输入距离
- PromptDoubleResult pdr = ed.GetDistance(pdo);
- //如果输入为关键字
- if (pdr.StringResult == "Divid")
- {
- //输入等分后提示输入 Int
- PromptIntegerOptions pio = new PromptIntegerOptions("\n输入等分数量");
- //确定是否有默认值
- if (_num != 0)
- {
- pio.DefaultValue = _num;
- }
- //允许回车要放到默认值后面
- pio.AllowNone = true;
- //提示输入等分数量 Int
- PromptIntegerResult pir = ed.GetInteger(pio);
- if (pir.Status == PromptStatus .None && _num != 0)
- {
- flag = true;
- }
- //如果成功,保存到默认值并标记是距离还是 Int
- else if (pir.Status == PromptStatus.OK)
- {
- _num = pir.Value;
- flag = true;
- }
- else
- {
- return;
- }
- }
- //有默认值时回车
- else if (pdr.Status == PromptStatus .None && _dist != 0)
- {
- flag = false;
- }
- //直接拾取距离或输入数值
- else if (pdr.Status == PromptStatus.OK)
- {
- //保存距离并设置标志
- _dist = pdr.Value;
- flag = false;
- }
- else
- {
- ed.WriteMessage(pdr.ToString());
- return;
- }
- using (Transaction tr = db.TransactionManager .StartTransaction())
- {
- BlockTableRecord btr = (BlockTableRecord) tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- Curve cPath = (Curve) cId.GetObject(OpenMode.ForRead);
- double clen = cPath.GetDistanceAtParameter(cPath.EndParam);
- Vector3d normal = cPath.GetPlane().Normal ;
- double space;
- int n;
- if (flag)
- {
- space = clen/_num;
- n = (int) space;
- }
- else
- {
- space = _dist;
- n = Convert.ToInt32(clen / _dist);
-
- }
- ObjectId[] ids = ss.GetObjectIds();
- for (int i = 0; i < n; i++)
- {
- Point3d point = cPath .GetPointAtDist(space * i);
- Vector3d v = basePoint .GetVectorTo(point );//位移
- Vector3d v1 = cPath .GetFirstDerivative(point);//切线方向
- double an = v1.AngleOnPlane(cPath .GetPlane());//角度
- Matrix3d vmat = Matrix3d.Displacement(v);//位移矩阵
- Matrix3d mat = Matrix3d.Rotation(an, normal , point );//旋转矩阵
- for (int j = 0; j < ids.Length ; j++)
- {
- Entity aEnt = (Entity) ids[j].GetObject(OpenMode.ForRead);
- Entity nent = aEnt.GetTransformedCopy(vmat.PreMultiplyBy(mat));//右乘
- btr.AppendEntity(nent);
- tr.AddNewlyCreatedDBObject(nent, true);
- }
- }
- tr.Commit();
- }
-
- }
- /// <summary>
- /// Entities Center
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- private Point3d SsetCenter(ObjectId[] ids)
- {
- Transaction tr = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction();
- using (tr)
- {
- Extents3d ext = new Extents3d();
- for (int i = 0; i < ids.Length; i++)
- {
- Entity ent = (Entity)ids[i].GetObject(OpenMode.ForRead);
- ext.AddExtents(ent.GeometricExtents);
- }
- Point3d bPoint = ext.MinPoint;
- Point3d uPoint = ext.MaxPoint;
- return new Point3d((bPoint.X + uPoint.X) / 2, (bPoint.Y + uPoint.Y) / 2, (bPoint.Z + uPoint.Z) / 2);
- }
- }
- }
- }
Editor.GetXXX 关键字 默认值 回车 |