明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3796|回复: 8

CAD.NET API一日一练(4)初识多段线

[复制链接]
发表于 2011-10-25 20:01 | 显示全部楼层 |阅读模式
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using Autodesk.AutoCAD.Runtime;
  7. using Autodesk.AutoCAD.ApplicationServices;
  8. using Autodesk.AutoCAD.DatabaseServices;
  9. using Autodesk.AutoCAD.Geometry;
  10. using Autodesk.AutoCAD.EditorInput;




  11. namespace Base
  12. {
  13.     public class Class1
  14.     {

  15. //判断多段线的顺时针还是逆时针
  16.         #region clockwise
  17.         [CommandMethod("clockwise")]
  18.         public void clockwise()
  19.         {
  20.             Document doc = Application.DocumentManager.MdiActiveDocument;
  21.             Editor ed = doc.Editor;
  22.             Database db = doc.Database;

  23.             PromptEntityOptions options = new PromptEntityOptions("");
  24.             options.Message = "\n请选择多段线";
  25.             options.SetRejectMessage("\n所选择实体不是多段线,请重新选择");
  26.             options.AddAllowedClass(typeof(Polyline), true);
  27.             PromptEntityResult result = ed.GetEntity(options);
  28.             int i = 1;
  29.             if (result.Status == PromptStatus.OK)
  30.             {
  31.                 using (Transaction trans = db.TransactionManager.StartTransaction())
  32.                 {
  33.                     Polyline pl = trans.GetObject(result.ObjectId, OpenMode.ForRead) as Polyline;
  34.                     Vector3d vec1 = pl.GetPoint3dAt(0).GetAsVector();
  35.                     Vector3d vec2 = pl.GetPoint3dAt(1).GetAsVector();
  36.                     Vector3d vec = vec1.CrossProduct(vec2);
  37.                     if (vec.Z > 0)
  38.                     {
  39.                         ed.WriteMessage("\n逆时针");
  40.                     }
  41.                     else if (vec.Z < 0)
  42.                     {
  43.                         ed.WriteMessage("\n顺时针");
  44.                     }
  45.                     else
  46.                     {
  47.                         while (vec.Z == 0 & i + 1 <= (Convert.ToInt32(pl.EndParam)))
  48.                         {
  49.                             vec1 = pl.GetPoint3dAt(i).GetAsVector();
  50.                             vec2 = pl.GetPoint3dAt(i + 1).GetAsVector();
  51.                             vec = vec1.CrossProduct(vec2);                        
  52.                             if (vec.Z > 0)
  53.                             {
  54.                                 ed.WriteMessage("\n逆时针");
  55.                             }
  56.                             else if (vec.Z < 0)
  57.                             {
  58.                                 ed.WriteMessage("\n顺时针");
  59.                             }
  60.                             i++;
  61.                         }

  62.                         if (vec.Z == 0 & i == (Convert.ToInt32(pl.EndParam)))
  63.                         {
  64.                             ed.WriteMessage("\n该多段线通过原点且斜率恒定,无顺逆之分");
  65.                         }
  66.                     }
  67.                 }
  68.             }
  69.         }



  70.     //多段线的反向
  71.         #region plreverse
  72.         [CommandMethod("plreverse")]
  73.         public void plreverse()
  74.         {
  75.             Document doc = Application.DocumentManager.MdiActiveDocument;
  76.             Editor ed = doc.Editor;
  77.             Database db = doc.Database;
  78.             PromptEntityOptions options = new PromptEntityOptions("");
  79.             options.Message = "\n请选择多段线";
  80.             options.SetRejectMessage("\n所选择实体不是多段线,请重新选择");
  81.             options.AddAllowedClass(typeof(Polyline), true);
  82.             PromptEntityResult result = ed.GetEntity(options);
  83.             if (result.Status == PromptStatus.OK)
  84.             {
  85.                 using (Transaction trans = db.TransactionManager.StartTransaction())
  86.                 {
  87.                     BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  88.                     BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  89.                     Polyline pl2 = new Polyline();
  90.                     btr.AppendEntity(pl2);
  91.                     trans.AddNewlyCreatedDBObject(pl2,true);
  92.                     Polyline pl = trans.GetObject(result.ObjectId,OpenMode.ForWrite)as Polyline;
  93.                     int count=Convert .ToInt32(pl.EndParam);
  94.                     Point2dCollection ptcoll = new Point2dCollection();
  95.                     DoubleCollection bulges = new DoubleCollection();
  96.                     for (int i = 0; i <= count; i++)
  97.                     {
  98.                         ptcoll.Add(pl.GetPoint2dAt(i));
  99.                         bulges.Add(pl.GetBulgeAt(i));
  100.                     }                  
  101.                     for (int i =0; i <= count; i++)
  102.                     {
  103.                         pl2.AddVertexAt(i,ptcoll[count-i],0,0,0);                       
  104.                     }
  105.                     for (int i = 0; i <= count-1; i++)
  106.                     {
  107.                         pl2.SetBulgeAt(i, -bulges[count - i - 1]);
  108.                     }
  109.                   
  110.                     pl2.Draw();
  111.                    /* for(int i=0;i<=count;i++)
  112.                     {
  113.                         ed.WriteMessage("pl1的第{0}点凸度为{1},pl2的第{3}点凸度为{4}",i,pl.GetBulgeAt(i),count-i,pl2.GetBulgeAt(count-i));   
  114.                     }*/
  115.                     
  116.                     //pl.Erase();
  117.                     trans.Commit();
  118.                 }
  119.             }
  120.         }
  121.         #endregion
  122.     }
  123. }


发表于 2011-10-26 08:33 | 显示全部楼层
感谢每天的分享,学习了
发表于 2011-10-26 11:12 | 显示全部楼层
继续。。。。。。
发表于 2011-10-27 18:37 | 显示全部楼层
请教:如何定义“多段线的顺时针还是逆时针”?
发表于 2011-11-3 13:54 | 显示全部楼层
Mark,学习了!同问“顺时针”、“逆时针”指什么?
发表于 2011-11-4 17:10 | 显示全部楼层
netload出了问题,错误信息

令: netload
无法加载程序集。错误详细信息: System.BadImageFormatException:
未能加载文件或程序集“file:///D:\MyProgram\VS2010\testCAD\testCAD\bin\Debug\testCAD.dll”或它的
某一个依赖项。生成此程序集的运行时比当前加载的运行时新,无法加载此程序集。
文件名:“file:///D:\MyProgram\VS2010\testCAD\testCAD\bin\Debug\testCAD.dll”
   在 System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase,
Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark,
Boolean throwOnFileNotFound, Boolean forIntrospection)
   在 System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase,
Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark,
Boolean throwOnFileNotFound, Boolean forIntrospection)
   在 System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence
assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   在 System.Reflection.Assembly.InternalLoadFrom(String assemblyFile, Evidence
securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm,
Boolean forIntrospection, StackCrawlMark& stackMark)
   在 System.Reflection.Assembly.LoadFrom(String assemblyFile)
   在 Autodesk.AutoCAD.Runtime.ExtensionLoader.Load(String fileName)
   在 loadmgd()

警告: 程序集绑定日志记录被关闭。
要启用程序集绑定失败日志记录,请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD)设置为 1。
注意: 会有一些与程序集绑定失败日志记录关联的性能损失。
要关闭此功能,请移除注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog]。
 楼主| 发表于 2011-11-4 17:19 | 显示全部楼层
yanglin112 发表于 2011-11-3 13:54
Mark,学习了!同问“顺时针”、“逆时针”指什么?

其实就是线的倒向吧,
就是多段线是顺时针画出来的
还是逆时针画出来的
 楼主| 发表于 2011-11-4 17:20 | 显示全部楼层
Student 发表于 2011-10-27 18:37
请教:如何定义“多段线的顺时针还是逆时针”?

其实就是线的倒向吧,
就是多段线是顺时针画出来的
还是逆时针画出来的
发表于 2013-6-7 15:20 | 显示全部楼层
好贴,一定支持。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-5 23:32 , Processed in 0.920309 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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