• C# CAD二次开发通过代码模拟 人工 画齿轮的算法思路


     

    1. [CommandMethod("FirstLine")]
    2. public void FirstLine()
    3. {
    4. Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
    5. //Thread th = new Thread(SayHello);
    6. //th.IsBackground = true;
    7. //th.Start();
    8. ed.WriteMessage("\n开始执行FirstLine\n");
    9. //获取当前活动图形数据库
    10. Database db = HostApplicationServices.WorkingDatabase;
    11. Point3d startPoint = new Point3d(0, 1000, 0);
    12. Point3d endPoint = new Point3d(0, 500, 0);
    13. Line line = new Line(startPoint, endPoint);
    14. Circle circle1 = new Circle(Point3d.Origin, Vector3d.ZAxis, 480);
    15. Point3dCollection pc1 = new Point3dCollection();
    16. circle1.IntersectWith(new Line(new Point3d(400,140,0), new Point3d(540, 60, 0)),
    17. Intersect.OnBothOperands, pc1, IntPtr.Zero, IntPtr.Zero);
    18. Point3dCollection pc2 = new Point3dCollection();
    19. circle1.IntersectWith(new Line(new Point3d(400, -140, 0), new Point3d(540, -60, 0)),
    20. Intersect.OnBothOperands, pc2, IntPtr.Zero, IntPtr.Zero);
    21. //以下画多段线
    22. Point2dCollection point2Ds = new Point2dCollection();
    23. point2Ds.Add(new Point2d(pc1[0].X,pc1[0].Y));
    24. //point2Ds.Add(new Point2d(400, 140));
    25. point2Ds.Add(new Point2d(540, 60));
    26. point2Ds.Add(new Point2d(540, -60));
    27. //point2Ds.Add(new Point2d(400, -140));
    28. point2Ds.Add(new Point2d(pc2[0].X, pc2[0].Y));
    29. Polyline polyline = new Polyline();
    30. int tmp = 0;
    31. foreach (var point2D in point2Ds)
    32. {
    33. polyline.AddVertexAt(tmp++,point2D,0,1,1);
    34. }
    35. Circle circle = new Circle(Point3d.Origin,Vector3d.ZAxis,400);
    36. using (Transaction trans = db.TransactionManager.StartTransaction())
    37. {
    38. BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
    39. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
    40. // btr.AppendEntity(line);
    41. btr.AppendEntity(polyline);
    42. // trans.AddNewlyCreatedDBObject(line, true);
    43. trans.AddNewlyCreatedDBObject(polyline, true);
    44. trans.Commit();
    45. }
    46. //line.ObjectId.ArrayPolarEntity(12, 360, new Point3d(0, 0, 0));
    47. List ents= polyline.ObjectId.ArrayPolarEntity(12, 360, new Point3d(0, 0, 0));
    48. using (Transaction trans = db.TransactionManager.StartTransaction())
    49. {
    50. BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
    51. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
    52. Point2d pStart, pEnd;
    53. Point3d pt1, pt2;
    54. CircularArc3d geArc = new CircularArc3d(circle1.Center, Vector3d.ZAxis, circle1.Radius);
    55. for (int i = 0; i < ents.Count; i++)
    56. {
    57. Polyline pl = (Polyline)ents[i];
    58. Polyline pl1 = (Polyline)ents[(i+1)%ents.Count];
    59. pStart = pl.GetPoint2dAt(0);
    60. pEnd = pl1.GetPoint2dAt(pl.NumberOfVertices - 1);
    61. pt1 = new Point3d(pStart.X, pStart.Y, 0);
    62. pt2 = new Point3d(pEnd.X,pEnd.Y,0);
    63. Arc arc =new Arc();
    64. arc.Center = geArc.Center;
    65. arc.Radius = geArc.Radius;
    66. arc.StartAngle = pt1.AngleFromXAxis(geArc.Center);
    67. arc.EndAngle = pt2.AngleFromXAxis(geArc.Center);
    68. btr.AppendEntity(arc);
    69. trans.AddNewlyCreatedDBObject(arc, true);
    70. }
    71. btr.AppendEntity(circle);
    72. trans.AddNewlyCreatedDBObject(circle, true);
    73. trans.Commit();//提交事务
    74. }
    75. System.Diagnostics.Debug.WriteLine("FirstLine 已经执行");
    76. }
    1. using Autodesk.AutoCAD.DatabaseServices;
    2. using Autodesk.AutoCAD.Geometry;
    3. using System;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6. using System.Text;
    7. using System.Threading.Tasks;
    8. namespace
    9. {
    10. static class Class2
    11. {
    12. public static double AngleFromXAxis(this Point3d pt1, Point3d pt2)
    13. {
    14. Vector2d vector=new Vector2d(pt1.X-pt2.X, pt1.Y-pt2.Y);
    15. return vector.Angle;
    16. }
    17. public static double DegreeToAngle(this Double degree)
    18. {
    19. return degree * Math.PI / 180;
    20. }
    21. ///
    22. /// 弧度转换角度
    23. ///
    24. /// 弧度制
    25. ///
    26. public static double AngleToDegree(this Double angle)
    27. {
    28. return angle * 180 / Math.PI;
    29. }
    30. ///
    31. /// 环形阵列
    32. ///
    33. /// 图形对象的ObjectId
    34. /// 图形数量
    35. /// 中心点到各个图形的夹角
    36. /// 中心点
    37. /// List 已经加入图形数据库
    38. public static List ArrayPolarEntity(this ObjectId entId, int num, double degree, Point3d center)
    39. {
    40. // 声明一个List集合 用于返回
    41. List entList = new List();
    42. // 打开事务处理
    43. using (Transaction trans = entId.Database.TransactionManager.StartTransaction())
    44. {
    45. // 打开块表
    46. BlockTable bt = (BlockTable)trans.GetObject(entId.Database.BlockTableId, OpenMode.ForRead);
    47. // 打开块表记录
    48. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
    49. Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
    50. // 限定阵列角度大小
    51. degree = degree > 360 ? 360 : degree;
    52. degree = degree < -360 ? 360 : degree;
    53. int divAngnum = num - 1;
    54. if (degree == 360 || degree == -360)
    55. {
    56. divAngnum = num;
    57. }
    58. // 计算变换矩阵
    59. for (int i = 0; i < num; i++)
    60. {
    61. Matrix3d mt = Matrix3d.Rotation((i * degree / divAngnum).DegreeToAngle(), Vector3d.ZAxis, center);
    62. Entity entA = ent.GetTransformedCopy(mt);
    63. btr.AppendEntity(entA);
    64. trans.AddNewlyCreatedDBObject(entA, true);
    65. entList.Add(entA);
    66. }
    67. ent.Erase();
    68. trans.Commit();
    69. }
    70. return entList;
    71. }
    72. }
    73. }

  • 相关阅读:
    C++学习笔记(九)
    碳达峰碳中和职业教学人才培养方案
    【kubernetes】【基础资源使用】kubernetes中的Deployment使用
    [贪心算法]忍者道具
    springboot+毕业设计管理系统 毕业设计-附源码221032
    【EI会议征稿】第四届智慧城市工程与公共交通国际学术会议(SCEPT 2024)
    【PingPong_注册安全分析报告】
    神经网络的深度指什么,深度神经网络有哪些
    vue:生命周期钩子,非单文件组件,组件嵌套,vueComponent构造函数及其原型链
    Qt(Python+Qt)QMainWindow的splitDockWidget方法将QDockWidget停靠窗分割排列
  • 原文地址:https://blog.csdn.net/laocooon/article/details/126026691