事务处理
///打开块表记录
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
数学计算比较
/// <summary>
/// 四舍五入
/// </summary>
/// <param name="value">数值;类型:double</param>
/// <param name="digits">小数点位数;默认:8位;类型:int</param>
/// <returns>四舍五入后的数值</returns>
public static double ToRound(this double value, int digits = 8) => Math.Round(value, digits, MidpointRounding.AwayFromZero);
/// <summary>
/// 带容差判断两个double是否相等
/// </summary>
/// <param name="value1">数值2;类型:double</param>
/// <param name="value2">数值2;类型:double</param>
/// <param name="dis">容差值,默认1e-06(含);类型:double</param>
/// <returns>两个数值在容差范围内,则返回true,否则返回false</returns>
public static bool EqOfDis(this double value1, double value2, double dis = 1E-06) => Math.Abs(value1 - value2) <= dis;
/// <summary>
/// 带容差判断两个Point3d是否相等
/// </summary>
/// <param name="value1">点2;类型:Point3d</param>
/// <param name="value2">点2;类型:Point3d</param>
/// <param name="dis">容差值,默认1e-06(不含);类型:double</param>
/// <returns>两个数值在容差范围内,则返回true,否则返回false</returns>
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;
图元操作
/// <summary>
/// 获取图元的中心点
/// </summary>
/// <param name="ent">图元对象;类型:Entity;</param>
/// <returns>返回中心点;类型:Point3d;</returns>
public static Point3d GetMidPt(this Entity ent)
{
if (ent is Entity)
{
Extents3d ex = ent.GeometricExtents;
return new Point3d((ex.MinPoint.X + ex.MaxPoint.X) / 2.0, (ex.MinPoint.Y + ex.MaxPoint.Y) / 2.0, 0);
}
return new Point3d();
}
窗体操作
//窗口前置函数
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
//===================================================
public static extern bool SetForegroundWindow(IntPtr hWnd);
//避免窗体重复打开
using System;
using System.Windows.Forms;
using ZwSoft.ZwCAD.Geometry;
using ZwSoft.ZwCAD.Runtime;
using ZWApp = ZwSoft.ZwCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof(CADToExcel.StartApp))]
namespace CADToExcel
{
public partial class StartApp
{
public static MainFrm Frm;
[CommandMethod("CTE", CommandFlags.Session | CommandFlags.UsePickSet)]
public static void CTE()
{
//以下代码放在插件控件的事件里面
if (Frm == null || Frm.IsDisposed)
{
Frm = new MainFrm();
//HookLib.ShowForm("MainFrm");
ZWApp.ShowModelessDialog(Frm);
}
else
{
if (Frm.WindowState == FormWindowState.Minimized)
{
Frm.WindowState = FormWindowState.Normal;
}
Frm.Activate();
}
}
}
}
CAD从内存加载dll文件
assembly.load(file.readallbyte())