找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 2515|回复: 2

[分享] 以点集为基础对autocad面进行三角网格化(.net版)

[复制链接]
发表于 2013-6-4 12:55:40 | 显示全部楼层 |阅读模式

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

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

×
Triangulating an AutoCAD polyface mesh from a set of points using .NET
A huge thank you to Zeljko Gjuranic for providing this code for a guest post. The code is based on a paper of Zeljko’s that was published in issue 11 of KoG magazine. The original paper is available in Croatian with an abstract in English.
The code in this post asks the user to select a set of points and then creates a polyface mesh by using Delaunay triangulation on those points.
[As we’re creating a polyface mesh we’re limited to 32,767 vertices. This is a known limitation when using the PolyFaceMesh object: with the new SubDMesh object in AutoCAD 2010 it should be possible to write a version that doesn’t suffer from this limitation. Something to look forward to from a future post…]
Here’s Zeljko’s C# code, re-formatted very slightly for posting:
  1. using Autodesk.AutoCAD.ApplicationServices;

  2. using Autodesk.AutoCAD.DatabaseServices;

  3. using Autodesk.AutoCAD.Runtime;

  4. using Autodesk.AutoCAD.EditorInput;

  5. using Autodesk.AutoCAD.Geometry;

  6. using System;



  7. public class Triangulate

  8. {

  9.   public bool circum(

  10.     double x1, double y1, double x2,

  11.     double y2, double x3, double y3,

  12.     ref double xc, ref double yc, ref double r)

  13.   {

  14.     // Calculation of circumscribed circle coordinates and

  15.     // squared radius



  16.     const double eps = 1e-6;

  17.     const double big = 1e12;

  18.     bool result = true;

  19.     double m1, m2, mx1, mx2, my1, my2, dx, dy;



  20.     if ((Math.Abs(y1 - y2) < eps) && (Math.Abs(y2 - y3) < eps))

  21.     {

  22.       result = false;

  23.       xc = x1; yc = y1; r = big;

  24.     }

  25.     else

  26.     {

  27.       if (Math.Abs(y2 - y1) < eps)

  28.       {

  29.         m2 = -(x3 - x2) / (y3 - y2);

  30.         mx2 = (x2 + x3) / 2;

  31.         my2 = (y2 + y3) / 2;

  32.         xc = (x2 + x1) / 2;

  33.         yc = m2 * (xc - mx2) + my2;

  34.       }

  35.       else if (Math.Abs(y3 - y2) < eps)

  36.       {

  37.         m1 = -(x2 - x1) / (y2 - y1);

  38.         mx1 = (x1 + x2) / 2;

  39.         my1 = (y1 + y2) / 2;

  40.         xc = (x3 + x2) / 2;

  41.         yc = m1 * (xc - mx1) + my1;

  42.       }

  43.       else

  44.       {

  45.         m1 = -(x2 - x1) / (y2 - y1);

  46.         m2 = -(x3 - x2) / (y3 - y2);

  47.         if (Math.Abs(m1 - m2) < eps)

  48.         {

  49.           result = false;

  50.           xc = x1;

  51.           yc = y1;

  52.           r = big;

  53.         }

  54.         else

  55.         {

  56.           mx1 = (x1 + x2) / 2;

  57.           mx2 = (x2 + x3) / 2;

  58.           my1 = (y1 + y2) / 2;

  59.           my2 = (y2 + y3) / 2;

  60.           xc = (m1 * mx1 - m2 * mx2 + my2 - my1) / (m1 - m2);

  61.           yc = m1 * (xc - mx1) + my1;

  62.         }

  63.       }

  64.     }

  65.     dx = x2 - xc;

  66.     dy = y2 - yc;

  67.     r = dx * dx + dy * dy;

  68.     return result;

  69.   }



  70.   [CommandMethod("TRIANG")]

  71.   public void TriangulateCommand()

  72.   {

  73.     const int maxpoints = 32767;



  74.     Document doc =

  75.       Application.DocumentManager.MdiActiveDocument;

  76.     Database db = doc.Database;

  77.     Editor ed = doc.Editor;



  78.     TypedValue[] tvs = { new TypedValue(0, "POINT") };

  79.     SelectionFilter sf =

  80.       new SelectionFilter(tvs);

  81.     PromptSelectionOptions pso = new PromptSelectionOptions();

  82.     pso.MessageForAdding = "Select Points:";

  83.     pso.AllowDuplicates = false;

  84.     PromptSelectionResult psr = ed.GetSelection(pso, sf);



  85.     if (psr.Status == PromptStatus.Error) return;

  86.     if (psr.Status == PromptStatus.Cancel) return;



  87.     SelectionSet ss = psr.Value;

  88.     int npts = ss.Count;

  89.     if (npts < 3)

  90.     {

  91.       ed.WriteMessage("Minimum 3 points must be selected!");

  92.       return;

  93.     }

  94.     if (npts > maxpoints)

  95.     {

  96.       ed.WriteMessage("Maximum nuber of points exceeded!");

  97.       return;

  98.     }



  99.     int i, j, k, ntri, ned, status1 = 0, status2 = 0;

  100.     bool status;



  101.     // Point coordinates



  102.     double[] ptx = new double[maxpoints + 3];

  103.     double[] pty = new double[maxpoints + 3];

  104.     double[] ptz = new double[maxpoints + 3];



  105.     // Triangle definitions



  106.     int[] pt1 = new int[maxpoints * 2 + 1];

  107.     int[] pt2 = new int[maxpoints * 2 + 1];

  108.     int[] pt3 = new int[maxpoints * 2 + 1];



  109.     // Circumscribed circle



  110.     double[] cex = new double[maxpoints * 2 + 1];

  111.     double[] cey = new double[maxpoints * 2 + 1];

  112.     double[] rad = new double[maxpoints * 2 + 1];

  113.     double xmin, ymin, xmax, ymax, dx, dy, xmid, ymid;

  114.     int[] ed1 = new int[maxpoints * 2 + 1];

  115.     int[] ed2 = new int[maxpoints * 2 + 1];



  116.     ObjectId[] idarray = ss.GetObjectIds();

  117.     Transaction tr =

  118.       db.TransactionManager.StartTransaction();

  119.     using (tr)

  120.     {

  121.       DBPoint ent;

  122.       k = 0;

  123.       for (i = 0; i < npts; i++)

  124.       {

  125.         ent =

  126.           (DBPoint)tr.GetObject(idarray[k], OpenMode.ForRead, false);

  127.         ptx = ent.Position[0];

  128.         pty = ent.Position[1];

  129.         ptz = ent.Position[2];

  130.         for (j = 0; j < i; j++)

  131.           if ((ptx == ptx[j]) && (pty == pty[j]))

  132.           {

  133.             i--; npts--; status2++;

  134.           }

  135.         k++;

  136.       }

  137.       tr.Commit();

  138.     }



  139.     if (status2 > 0)

  140.       ed.WriteMessage(

  141.         "\nIgnored {0} point(s) with same coordinates.",

  142.         status2

  143.       );



  144.     // Supertriangle



  145.     xmin = ptx[0]; xmax = xmin;

  146.     ymin = pty[0]; ymax = ymin;

  147.     for (i = 0; i < npts; i++)

  148.     {

  149.       if (ptx < xmin) xmin = ptx;

  150.       if (ptx > xmax) xmax = ptx;

  151.       if (pty < xmin) ymin = pty;

  152.       if (pty > xmin) ymax = pty;

  153.     }

  154.     dx = xmax - xmin; dy = ymax - ymin;

  155.     xmid = (xmin + xmax) / 2; ymid = (ymin + ymax) / 2;

  156.     i = npts;

  157.     ptx = xmid - (90 * (dx + dy)) - 100;

  158.     pty = ymid - (50 * (dx + dy)) - 100;

  159.     ptz = 0;

  160.     pt1[0] = i;

  161.     i++;

  162.     ptx = xmid + (90 * (dx + dy)) + 100;

  163.     pty = ymid - (50 * (dx + dy)) - 100;

  164.     ptz = 0;

  165.     pt2[0] = i;

  166.     i++;

  167.     ptx = xmid;

  168.     pty = ymid + 100 * (dx + dy + 1);

  169.     ptz = 0;

  170.     pt3[0] = i;

  171.     ntri = 1;

  172.     circum(

  173.       ptx[pt1[0]], pty[pt1[0]], ptx[pt2[0]],

  174.       pty[pt2[0]], ptx[pt3[0]], pty[pt3[0]],

  175.       ref cex[0], ref cey[0], ref rad[0]

  176.     );



  177.     // main loop

  178.     for (i = 0; i < npts; i++)

  179.     {

  180.       ned = 0;

  181.       xmin = ptx; ymin = pty;

  182.       j = 0;

  183.       while (j < ntri)

  184.       {

  185.         dx = cex[j] - xmin; dy = cey[j] - ymin;

  186.         if (((dx * dx) + (dy * dy)) < rad[j])

  187.         {

  188.           ed1[ned] = pt1[j]; ed2[ned] = pt2[j];

  189.           ned++;

  190.           ed1[ned] = pt2[j]; ed2[ned] = pt3[j];

  191.           ned++;

  192.           ed1[ned] = pt3[j]; ed2[ned] = pt1[j];

  193.           ned++;

  194.           ntri--;

  195.           pt1[j] = pt1[ntri];

  196.           pt2[j] = pt2[ntri];

  197.           pt3[j] = pt3[ntri];

  198.           cex[j] = cex[ntri];

  199.           cey[j] = cey[ntri];

  200.           rad[j] = rad[ntri];

  201.           j--;

  202.         }

  203.         j++;

  204.       }



  205.       for (j = 0; j < ned - 1; j++)

  206.         for (k = j + 1; k < ned; k++)

  207.           if ((ed1[j] == ed2[k]) && (ed2[j] == ed1[k]))

  208.           {

  209.             ed1[j] = -1; ed2[j] = -1; ed1[k] = -1; ed2[k] = -1;

  210.           }



  211.       for (j = 0; j < ned; j++)

  212.         if ((ed1[j] >= 0) && (ed2[j] >= 0))

  213.         {

  214.           pt1[ntri] = ed1[j]; pt2[ntri] = ed2[j]; pt3[ntri] = i;

  215.           status =

  216.             circum(

  217.               ptx[pt1[ntri]], pty[pt1[ntri]], ptx[pt2[ntri]],

  218.               pty[pt2[ntri]], ptx[pt3[ntri]], pty[pt3[ntri]],

  219.               ref cex[ntri], ref cey[ntri], ref rad[ntri]

  220.             );

  221.           if (!status)

  222.           {

  223.             status1++;

  224.           }

  225.           ntri++;

  226.         }

  227.     }



  228.     // removal of outer triangles

  229.     i = 0;

  230.     while (i < ntri)

  231.     {

  232.       if ((pt1 >= npts) || (pt2 >= npts) || (pt3 >= npts))

  233.       {

  234.         ntri--;

  235.         pt1 = pt1[ntri];

  236.         pt2 = pt2[ntri];

  237.         pt3 = pt3[ntri];

  238.         cex = cex[ntri];

  239.         cey = cey[ntri];

  240.         rad = rad[ntri];

  241.         i--;

  242.       }

  243.       i++;

  244.     }



  245.     tr = db.TransactionManager.StartTransaction();

  246.     using (tr)

  247.     {

  248.       BlockTable bt =

  249.         (BlockTable)tr.GetObject(

  250.           db.BlockTableId,

  251.           OpenMode.ForRead,

  252.           false

  253.         );

  254.       BlockTableRecord btr =

  255.         (BlockTableRecord)tr.GetObject(

  256.           bt[BlockTableRecord.ModelSpace],

  257.           OpenMode.ForWrite,

  258.           false

  259.         );



  260.       PolyFaceMesh pfm = new PolyFaceMesh();

  261.       btr.AppendEntity(pfm);

  262.       tr.AddNewlyCreatedDBObject(pfm, true);

  263.       for (i = 0; i < npts; i++)

  264.       {

  265.         PolyFaceMeshVertex vert =

  266.           new PolyFaceMeshVertex(

  267.             new Point3d(ptx, pty, ptz)

  268.           );

  269.         pfm.AppendVertex(vert);

  270.         tr.AddNewlyCreatedDBObject(vert, true);

  271.       }

  272.       for (i = 0; i < ntri; i++)

  273.       {

  274.         FaceRecord face =

  275.           new FaceRecord(

  276.             (short)(pt1 + 1),

  277.             (short)(pt2 + 1),

  278.             (short)(pt3 + 1),

  279.             0

  280.           );

  281.         pfm.AppendFaceRecord(face);

  282.         tr.AddNewlyCreatedDBObject(face, true);

  283.       }

  284.       tr.Commit();

  285.     }

  286.     if (status1 > 0)

  287.       ed.WriteMessage(

  288.         "\nWarning! {0} thin triangle(s) found!" +

  289.         " Wrong result possible!",

  290.         status1

  291.       );

  292.     Application.UpdateScreen();

  293.   }

  294. }

To see the command in action, first we create a set of DBPoints (using AutoCAD’s POINT command). I’ve created these points at different elevations, to put the code through its paces in three dimensions:
Running the TRIANG command and selecting these points creates a polyface mesh (here with PDMODE reset to 0):
And here’s a 3D view on this mesh, to see the depth:


评分

参与人数 1D豆 +3 收起 理由
Lisphk + 3 很给力!经验;技术要点;资料分享奖!

查看全部评分

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

已领礼包: 19个

财富等级: 恭喜发财

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

使用道具 举报

已领礼包: 7个

财富等级: 恭喜发财

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-28 06:23 , Processed in 0.257123 second(s), 40 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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