事务处理

  1. ///打开块表记录
  2. BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

数学计算比较

  1. /// <summary>
  2. /// 四舍五入
  3. /// </summary>
  4. /// <param name="value">数值;类型:double</param>
  5. /// <param name="digits">小数点位数;默认:8位;类型:int</param>
  6. /// <returns>四舍五入后的数值</returns>
  7. public static double ToRound(this double value, int digits = 8) => Math.Round(value, digits, MidpointRounding.AwayFromZero);
  8. /// <summary>
  9. /// 带容差判断两个double是否相等
  10. /// </summary>
  11. /// <param name="value1">数值2;类型:double</param>
  12. /// <param name="value2">数值2;类型:double</param>
  13. /// <param name="dis">容差值,默认1e-06(含);类型:double</param>
  14. /// <returns>两个数值在容差范围内,则返回true,否则返回false</returns>
  15. public static bool EqOfDis(this double value1, double value2, double dis = 1E-06) => Math.Abs(value1 - value2) <= dis;
  16. /// <summary>
  17. /// 带容差判断两个Point3d是否相等
  18. /// </summary>
  19. /// <param name="value1">点2;类型:Point3d</param>
  20. /// <param name="value2">点2;类型:Point3d</param>
  21. /// <param name="dis">容差值,默认1e-06(不含);类型:double</param>
  22. /// <returns>两个数值在容差范围内,则返回true,否则返回false</returns>
  23. public static bool EqOfDis(this Point3d pt1, Point3d pt2, double dis = 1e-06) => Math.Abs(pt1.X - pt2.X) < dis && Math.Abs(pt1.Y - pt2.Y) < dis && Math.Abs(pt1.Z - pt2.Z) < dis;

图元操作

  1. /// <summary>
  2. /// 获取图元的中心点
  3. /// </summary>
  4. /// <param name="ent">图元对象;类型:Entity;</param>
  5. /// <returns>返回中心点;类型:Point3d;</returns>
  6. public static Point3d GetMidPt(this Entity ent)
  7. {
  8. if (ent is Entity)
  9. {
  10. Extents3d ex = ent.GeometricExtents;
  11. return new Point3d((ex.MinPoint.X + ex.MaxPoint.X) / 2.0, (ex.MinPoint.Y + ex.MaxPoint.Y) / 2.0, 0);
  12. }
  13. return new Point3d();
  14. }

窗体操作

  1. //窗口前置函数
  2. [DllImport("User32.dll", CharSet = CharSet.Unicode)]
  3. //===================================================
  4. public static extern bool SetForegroundWindow(IntPtr hWnd);
  5. //避免窗体重复打开
  6. using System;
  7. using System.Windows.Forms;
  8. using ZwSoft.ZwCAD.Geometry;
  9. using ZwSoft.ZwCAD.Runtime;
  10. using ZWApp = ZwSoft.ZwCAD.ApplicationServices.Application;
  11. [assembly: CommandClass(typeof(CADToExcel.StartApp))]
  12. namespace CADToExcel
  13. {
  14. public partial class StartApp
  15. {
  16. public static MainFrm Frm;
  17. [CommandMethod("CTE", CommandFlags.Session | CommandFlags.UsePickSet)]
  18. public static void CTE()
  19. {
  20. //以下代码放在插件控件的事件里面
  21. if (Frm == null || Frm.IsDisposed)
  22. {
  23. Frm = new MainFrm();
  24. //HookLib.ShowForm("MainFrm");
  25. ZWApp.ShowModelessDialog(Frm);
  26. }
  27. else
  28. {
  29. if (Frm.WindowState == FormWindowState.Minimized)
  30. {
  31. Frm.WindowState = FormWindowState.Normal;
  32. }
  33. Frm.Activate();
  34. }
  35. }
  36. }
  37. }

CAD从内存加载dll文件
assembly.load(file.readallbyte())