找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1654|回复: 1

[原创] 练习 选择集的路径定距及定数等分

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-8-5 22:21:51 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 csharp 于 2014-8-6 08:28 编辑

  1. using System;
  2. using Autodesk.AutoCAD.ApplicationServices;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.EditorInput;
  5. using Autodesk.AutoCAD.Geometry;
  6. using Autodesk.AutoCAD.Runtime;

  7. [assembly: CommandClass(typeof(ArrayPath.MyCommands))]

  8. namespace ArrayPath
  9. {
  10.     public class MyCommands
  11.     {
  12.         private double _dist = 0;
  13.         private int _num = 0;
  14.         [CommandMethod("arPath")]
  15.         public void MyCommand()
  16.         {

  17.             Document doc = Application.DocumentManager.MdiActiveDocument;
  18.             Database db = doc.Database;
  19.             Editor ed = doc.Editor;
  20.             Point3d basePoint;
  21.             bool flag;

  22.             PromptSelectionOptions pso = new PromptSelectionOptions();
  23.             pso.MessageForAdding = "\n选择阵列实体";

  24.             PromptSelectionResult psr = ed.GetSelection(pso);
  25.             if (psr.Status != PromptStatus.OK) return;
  26.             SelectionSet ss = psr.Value;

  27.             PromptPointOptions ppo = new PromptPointOptions("\n阵列基点<回车选集中心>");
  28.             ppo.AllowNone = true;

  29.             PromptPointResult ppr = ed.GetPoint(ppo);
  30.             if (ppr.Status == PromptStatus .OK )
  31.             {
  32.                 basePoint = ppr.Value;
  33.             }
  34.             else if (ppr.Status == PromptStatus .None )
  35.             {
  36.                 basePoint = SsetCenter(ss.GetObjectIds());
  37.             }
  38.             else
  39.             {
  40.                 return;
  41.             }
  42.             PromptEntityOptions peo = new PromptEntityOptions("\n选择路径");
  43.             peo.SetRejectMessage("\n请选择曲线");
  44.             peo.AddAllowedClass(typeof (Arc),false );
  45.             peo.AddAllowedClass(typeof (Line),false );
  46.             peo.AddAllowedClass(typeof (Spline),false );
  47.             peo.AddAllowedClass(typeof (Polyline),false);
  48.             PromptEntityResult per = ed.GetEntity(peo);
  49.             if (per.Status != PromptStatus .OK )
  50.             {
  51.                 return;
  52.             }
  53.             ObjectId cId = per.ObjectId;

  54.             //DefaultPamram defaultPamram = new DefaultPamram();
  55.             //增加关键字提示
  56.             PromptDistanceOptions pdo = new PromptDistanceOptions("\n拾取距离[等分(D)]", "Divid");
  57.             
  58.             //是否使用默认值
  59.             if (_dist  != 0)
  60.             {
  61.                 pdo.DefaultValue = _dist;
  62.             }
  63.             //允许回车要放到默认值后面,否则不出现默认值提示
  64.             pdo.AllowNone = true;//允许回车
  65.             //提示输入,拾取距离或输入距离
  66.             PromptDoubleResult pdr = ed.GetDistance(pdo);

  67.             //如果输入为关键字
  68.             if (pdr.StringResult == "Divid")
  69.             {
  70.                 //输入等分后提示输入 Int
  71.                 PromptIntegerOptions pio = new PromptIntegerOptions("\n输入等分数量");
  72.                 //确定是否有默认值
  73.                 if (_num != 0)
  74.                 {
  75.                     pio.DefaultValue = _num;
  76.                 }

  77.                 //允许回车要放到默认值后面
  78.                 pio.AllowNone = true;
  79.                 //提示输入等分数量 Int
  80.                 PromptIntegerResult pir = ed.GetInteger(pio);
  81.                 if (pir.Status == PromptStatus .None && _num != 0)
  82.                 {
  83.                      flag = true;
  84.                 }
  85.                 //如果成功,保存到默认值并标记是距离还是 Int
  86.                 else if (pir.Status == PromptStatus.OK)
  87.                 {
  88.                     _num = pir.Value;
  89.                      flag = true;
  90.                 }
  91.                 else
  92.                 {
  93.                     return;
  94.                 }
  95.             }
  96.             //有默认值时回车
  97.             else if (pdr.Status == PromptStatus .None && _dist != 0)
  98.             {
  99.                flag = false;
  100.             }
  101.             //直接拾取距离或输入数值
  102.             else if (pdr.Status == PromptStatus.OK)
  103.             {
  104.                 //保存距离并设置标志
  105.                 _dist = pdr.Value;
  106.                  flag = false;
  107.             }
  108.             else
  109.             {
  110.                 ed.WriteMessage(pdr.ToString());
  111.                 return;
  112.             }
  113.             using (Transaction tr = db.TransactionManager .StartTransaction())
  114.             {
  115.                 BlockTableRecord btr = (BlockTableRecord) tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  116.                 Curve cPath = (Curve) cId.GetObject(OpenMode.ForRead);
  117.                 double clen = cPath.GetDistanceAtParameter(cPath.EndParam);
  118.                 Vector3d normal = cPath.GetPlane().Normal ;
  119.                 double space;
  120.                 int n;
  121.                 if (flag)
  122.                 {
  123.                     space = clen/_num;
  124.                     n = (int) space;
  125.                 }
  126.                 else
  127.                 {
  128.                     space = _dist;
  129.                     n = Convert.ToInt32(clen / _dist);
  130.                     
  131.                 }
  132.                 ObjectId[] ids = ss.GetObjectIds();
  133.                 for (int i = 0; i < n; i++)
  134.                 {
  135.                     Point3d point = cPath .GetPointAtDist(space * i);
  136.                     Vector3d v = basePoint .GetVectorTo(point );//位移
  137.                     Vector3d v1 = cPath .GetFirstDerivative(point);//切线方向
  138.                     double an = v1.AngleOnPlane(cPath .GetPlane());//角度
  139.                     Matrix3d vmat = Matrix3d.Displacement(v);//位移矩阵
  140.                     Matrix3d mat = Matrix3d.Rotation(an, normal , point );//旋转矩阵
  141.                     for (int j = 0; j < ids.Length ; j++)
  142.                     {
  143.                         Entity aEnt = (Entity) ids[j].GetObject(OpenMode.ForRead);
  144.                         Entity nent = aEnt.GetTransformedCopy(vmat.PreMultiplyBy(mat));//右乘
  145.                         btr.AppendEntity(nent);
  146.                         tr.AddNewlyCreatedDBObject(nent, true);
  147.                     }

  148.                 }
  149.                 tr.Commit();
  150.             }
  151.             
  152.         }
  153.         /// <summary>
  154.         /// Entities Center
  155.         /// </summary>
  156.         /// <param name="ids"></param>
  157.         /// <returns></returns>
  158.         private Point3d SsetCenter(ObjectId[] ids)
  159.         {
  160.             Transaction tr = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction();
  161.             using (tr)
  162.             {
  163.                 Extents3d ext = new Extents3d();
  164.                 for (int i = 0; i < ids.Length; i++)
  165.                 {
  166.                     Entity ent = (Entity)ids[i].GetObject(OpenMode.ForRead);
  167.                     ext.AddExtents(ent.GeometricExtents);
  168.                 }
  169.                 Point3d bPoint = ext.MinPoint;
  170.                 Point3d uPoint = ext.MaxPoint;
  171.                 return new Point3d((bPoint.X + uPoint.X) / 2, (bPoint.Y + uPoint.Y) / 2, (bPoint.Z + uPoint.Z) / 2);
  172.             }

  173.         }
  174.     }
  175. }

Editor.GetXXX 关键字 默认值 回车

arpath08.rar

4.67 KB, 下载次数: 14, 下载积分: D豆 -1 , 活跃度 1

2008编译版,理论支持2008-2012支持UCS

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-8-5 22:43:51 | 显示全部楼层
暂不支持 UCS
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-1 14:41 , Processed in 0.426718 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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