明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4555|回复: 8

[基础] .NET中如何把圆转成多义线?

[复制链接]
发表于 2011-1-5 10:16 | 显示全部楼层 |阅读模式
本帖最后由 SUGAR1122 于 2011-1-5 10:19 编辑

如何把圆转成多义线? 这样可以控制圆的线宽。
没查到相关信息,在CAD命令下可以直接用ployline,选圆弧,输入直径,再选择原来的起点,好像生成的圆不是真正的圆,用bo命令可以生成,感觉不是很恰当,也没法用.net控制。
用.NET有什么其他方法吗?

发表于 2011-1-5 16:05 | 显示全部楼层
就用polyline生成两节圆弧
 楼主| 发表于 2011-1-5 20:53 | 显示全部楼层
我看到别的程序生成的好像不是2个圆弧啊,不知道怎么做的,圆环?
发表于 2011-1-5 21:54 | 显示全部楼层
同楼上观点,顺便给你写个代码吧:
  1. using System;
  2. using Autodesk.AutoCAD.Runtime;
  3. using Autodesk.AutoCAD.Geometry;
  4. using Autodesk.AutoCAD.ApplicationServices;
  5. using Autodesk.AutoCAD.DatabaseServices;
  6. using Autodesk.AutoCAD.EditorInput;
  7. namespace CADTest
  8. {
  9.     public class Class1
  10.     {
  11.         [CommandMethod("c2p")]
  12.         public void CircleToPloyline()
  13.         {
  14.             Document doc = Application.DocumentManager.MdiActiveDocument;
  15.             Database db = doc.Database;
  16.             Editor ed = doc.Editor;
  17.             Transaction trans = db.TransactionManager.StartTransaction();
  18.             BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  19.             BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  20.             PromptSelectionResult psr = ed.GetSelection();
  21.             //获取选择集,这里就不过滤了
  22.             SelectionSet ss = null;
  23.             if (psr.Status == PromptStatus.OK)
  24.             {
  25.                 ss = psr.Value;
  26.                 foreach (SelectedObject so in ss)
  27.                 {
  28.                     Circle c = trans.GetObject(so.ObjectId, OpenMode.ForWrite) as Circle;
  29.                     double r = c.Radius;
  30.                     Point3d cc = c.Center;
  31.                     Point2d p1 = new Point2d(cc.X + r, cc.Y);
  32.                     Point2d p2 = new Point2d(cc.X - r, cc.Y);
  33.                     Polyline poly = new Polyline();
  34.                     poly.AddVertexAt(0, p1, 1, 0, 0);
  35.                     poly.AddVertexAt(1, p2, 1, 0, 0);
  36.                     poly.AddVertexAt(2, p1, 1, 0, 0);
  37.                     btr.AppendEntity(poly);
  38.                     trans.AddNewlyCreatedDBObject(poly, true);
  39.                     c.Erase(true);
  40.                 }
  41.             }
  42.             trans.Commit();
  43.             trans.Dispose();
  44.         }

  45.         [CommandMethod("GET")]
  46.         public void GetEntityType()
  47.         {
  48.             Document doc = Application.DocumentManager.MdiActiveDocument;
  49.             Database db = doc.Database;
  50.             Editor ed = doc.Editor;
  51.             PromptEntityOptions peo = new PromptEntityOptions("请选择一个实体");
  52.             PromptEntityResult per = null;
  53.             try
  54.             {
  55.                 per = ed.GetEntity(peo);
  56.                 if (per.Status == PromptStatus.OK)
  57.                 {
  58.                     ObjectId id = per.ObjectId;
  59.                     Transaction trans = db.TransactionManager.StartTransaction();
  60.                     Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, true);

  61.                     ed.WriteMessage("\n实体ObjectId为:" + ent.ObjectId + "\n实体类型为:" + ent.GetType().FullName);
  62.                     trans.Commit();
  63.                     trans.Dispose();
  64.                 }
  65.             }
  66.             catch (Autodesk.AutoCAD.Runtime.Exception exc)
  67.             {
  68.                 ed.WriteMessage("发生异常,原因为:" + exc.Message);
  69.             }
  70.         }
  71.     }
  72. }
发表于 2011-1-5 22:04 | 显示全部楼层
本帖最后由 cdinten 于 2011-1-5 22:06 编辑

这个是效果图:

如果看不了,可以上我的博客看,地址在这里

本帖子中包含更多资源

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

x
 楼主| 发表于 2011-1-6 10:56 | 显示全部楼层
非常感谢cdinten ,提供代码和效果,效果很好,谢谢
发表于 2011-9-18 23:50 | 显示全部楼层
本帖最后由 fdafsaf 于 2011-9-18 23:51 编辑
  1. using System;
  2. using Autodesk.AutoCAD.Runtime;
  3. using Autodesk.AutoCAD.Geometry;
  4. using Autodesk.AutoCAD.ApplicationServices;
  5. using Autodesk.AutoCAD.DatabaseServices;
  6. using Autodesk.AutoCAD.EditorInput;
  7. namespace CADTest
  8. {
  9.     public class Class1
  10.     {
  11.         [CommandMethod("c2p")]
  12.         public void CircleToPloyline()
  13.         {
  14.             Document doc = Application.DocumentManager.MdiActiveDocument;
  15.             Database db = doc.Database;
  16.             Editor ed = doc.Editor;
  17.             Transaction trans = db.TransactionManager.StartTransaction();
  18.             BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  19.             BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  20.             PromptSelectionResult psr = ed.GetSelection();
  21.             //获取选择集,这里就不过滤了
  22.             SelectionSet ss = null;
  23.             if (psr.Status == PromptStatus.OK)
  24.             {
  25.                 ss = psr.Value;
  26.                 foreach (SelectedObject so in ss)
  27.                 {
  28.                     Circle c = trans.GetObject(so.ObjectId, OpenMode.ForWrite) as Circle;
  29.                     double r = c.Radius;
  30.                     Point3d cc = c.Center;
  31.                     Point2d p1 = new Point2d(cc.X + r, cc.Y);
  32.                     Point2d p2 = new Point2d(cc.X - r, cc.Y);
  33.                     Polyline poly = new Polyline();
  34.                     poly.AddVertexAt(0, p1, 1, 0, 0);
  35.                     poly.AddVertexAt(1, p2, 1, 0, 0);
  36.                     poly.AddVertexAt(2, p1, 1, 0, 0);
  37.                     btr.AppendEntity(poly);
  38.                     trans.AddNewlyCreatedDBObject(poly, true);
  39.                     c.Erase(true);
  40.                 }
  41.             }
  42.             trans.Commit();
  43.             trans.Dispose();
  44.         }

  45.         [CommandMethod("GET")]
  46.         public void GetEntityType()
  47.         {
  48.             Document doc = Application.DocumentManager.MdiActiveDocument;
  49.             Database db = doc.Database;
  50.             Editor ed = doc.Editor;
  51.             PromptEntityOptions peo = new PromptEntityOptions("请选择一个实体");
  52.             PromptEntityResult per = null;
  53.             try
  54.             {
  55.                 per = ed.GetEntity(peo);
  56.                 if (per.Status == PromptStatus.OK)
  57.                 {
  58.                     ObjectId id = per.ObjectId;
  59.                     Transaction trans = db.TransactionManager.StartTransaction();
  60.                     Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, true);

  61.                     ed.WriteMessage("\n实体ObjectId为:" + ent.ObjectId + "\n实体类型为:" + ent.GetType().FullName);
  62.                     trans.Commit();
  63.                     trans.Dispose();
  64.                 }
  65.             }
  66.             catch (Autodesk.AutoCAD.Runtime.Exception exc)
  67.             {
  68.                 ed.WriteMessage("发生异常,原因为:" + exc.Message);
  69.             }
  70.         }
  71.     }
  72. }

发表于 2011-11-1 13:40 | 显示全部楼层
  1. <!DOCTYPE HTML>
  2. <html>
  3.     <head>
  4.         <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  5.         <title>HTML5每日一练之Canvas标签的应用-坐标变换与路径结合使用</title>
  6.                 <script type="text/javascript">
  7.         window.onload = function()
  8.         {
  9.                         createPic();
  10.                 }
  11.                
  12.                 //创建图形
  13.                 function createPic()
  14.                 {
  15.                         var canvas = document.getElementById("W3Cfuns_canvas");
  16.                         var context = canvas.getContext("2d");
  17.                         context.fillStyle = "#d4d4d4";
  18.                         context.fillRect(0, 0, 400, 300);
  19.                         //绘制图形
  20.                         context.translate(200, 50);
  21.                         for(var i = 0; i < 50; i++)
  22.                         {
  23.                                 context.translate(25, 25);
  24.                                 context.scale(0.95, 0.95);
  25.                                 context.rotate(Math.PI / 8);
  26.                                 createStar(context);//此方法专门绘制五角星
  27.                                 context.fill();
  28.                         }
  29.                 }
  30.                
  31.                 //创建五角星的方法
  32.                 function createStar(c)
  33.                 {
  34.                         var n = 0;
  35.                         var dx = 100;
  36.                         var dy = 0;
  37.                         var s = 50;
  38.                         var x = Math.sin(0);
  39.                         var y = Math.cos(0);
  40.                         var dig = Math.PI / 5 * 4;
  41.                         //创建路径
  42.                         c.beginPath();
  43.                         c.fillStyle = toRGB(parseInt(Math.random()*(255 - 0 + 1) + 0),parseInt(Math.random()*(255 - 0 + 1) + 0),parseInt(Math.random()*(255 - 0 + 1) + 0));
  44.                         for(var i = 0; i < 5; i++)
  45.                         {
  46.                                 x = Math.sin(i * dig);
  47.                                 y = Math.cos(i * dig);
  48.                                 c.lineTo(dx + x * s, dy + y * s);
  49.                         }
  50.                         c.closePath();
  51.                 }
  52.                
  53.                 //小于10补零
  54.                 function addZero(string){return string.length == 2 ? string : '0' + string;}
  55.                
  56.                 //随即颜色
  57.                 function toRGB(redValue, greenValue, blueValue)
  58.                 {
  59.                         var
  60.                                 rgbR = addZero(redValue.toString(16), 2),
  61.                                 rgbG = addZero(greenValue.toString(16), 2),
  62.                                 rgbB = addZero(blueValue.toString(16), 2);
  63.                         var rgb = "#" + rgbR + rgbG + rgbB;
  64.                         return rgb;
  65.                 }
  66.         </script>
  67.     </head>
  68.    
  69.     <body>
  70.             <canvas id="W3Cfuns_canvas" width="400" height="300"></canvas>
  71.     </body>
  72. </html>
发表于 2012-3-7 19:39 | 显示全部楼层
本帖最后由 机灵鼠 于 2012-3-7 19:40 编辑
复制代码

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

本版积分规则

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

GMT+8, 2024-4-19 06:41 , Processed in 0.209244 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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