001集—— 块表、快表记录、块参照详解 —— ifox CADc#二次开发
本集为块表、快表记录、块参照的基本操作。见下图:
代码如下:
namespace IfoxDemo;
public static class FZK
{
[CommandMethod("xx")]
public static void 块相关操作()
{
using var tr = new DBTrans();
if (tr.BlockTable.Has("*Model_Space"))
{
Env.Editor.WriteMessage("有模型空间!");//判断是否有模型空间
}
if (tr.BlockTable.Has("啊块"))//判断是否有块"啊块"
{
Env.Editor.WriteMessage("有啊块!");
}
foreach (var item in tr.BlockTable)
{
BlockTableRecord btr = tr.GetObject(item, OpenMode.ForRead) as BlockTableRecord;
btr.Name.Print();//依次输出快表记录的名称
}
tr.BlockTable.GetRecordNames().ForEach(action: (blockname) => ("ifox语法:" + blockname).Print());//ifox语法:另一种形式输出快表记录名称
//统计块参照"啊块"的数量 =与==不用搞错
tr.ModelSpace.GetEntities<BlockReference>().Where(bref => bref.GetBlockName() == "啊块").ForEach(action: (bref) => (" 统计快:+" + 1).Print());
tr.ModelSpace.GetEntities<BlockReference>().Where(bref => bref.GetBlockName() == "啊块").Count().Print();//输出块参照“啊块”的数量
//添加属性块
BlockTableRecord btr1 = new BlockTableRecord();
Line line1 = new Line() { StartPoint = new Point3d(0, 0, 0), EndPoint = new Point3d(10, 10, 0)};
Line line2 = new Line() { StartPoint = new Point3d(0, 0, 0), EndPoint = new Point3d(10, 0, 0) };
Circle circle = new Circle() { Center = Point3d .Origin ,Radius = 5};
var att1 = new AttributeDefinition()
{
Position = new Point3d(10,10, 0),
Tag = "标记tagTest1",
Height = 1,
ColorIndex = 2,
TextString = "值valueTest1"
};
var att2 = new AttributeDefinition()
{
Position = new Point3d(10, 0, 0),
Tag = "标记tagTest2",
Height = 1,
TextString = "值valueTest2"
};
var att3 = new AttributeDefinition()
{
Position = new Point3d(0, 0, 0),
Tag = "标记tagTest3",
Height = 1,
HorizontalMode = TextHorizontalMode.TextCenter,
VerticalMode = TextVerticalMode.TextVerticalMid,
ColorIndex = 1,
TextString = "值valueTest3"
};
ObjectId btrNew = tr.BlockTable.Add("新增属性块1",line1, line2,circle, att1, att2,att3);
"块已创建".Print();
for (int i = 1; i < 10; i++)
{//循环插入9个块
tr.ModelSpace.InsertBlock(new Point3d ( i * 20 ,0,0), btrNew);//将创建的快表记录插入到模型空间
}
ObjectId blkrefid = tr.ModelSpace.InsertBlock(new Point3d( 0, 0, 0), btrNew);//将创建的快表记录插入到模型空间
var blkref = blkrefid.GetObject(OpenMode.ForRead) as BlockReference;//获取块参照
string blkrefName = blkref.GetBlockName().Print();//块参照对应的块的名字输出到命令行
//统计模型空间中块参照对应的特点钉子块的数量
($"模型空间中\"{blkrefName}\"的数量为").Print();
tr.ModelSpace.GetEntities<BlockReference>().Where(bref => bref.GetBlockName() == blkrefName).Count().Print();
Env.Editor.ZoomExtents(20);
}
}
引用
global using System;
global using IfoxDemo;
global using System.Collections.Generic;
global using System.Linq;
global using System.Text;
global using System.Threading.Tasks;
global using Autodesk.AutoCAD.ApplicationServices;
global using Autodesk.AutoCAD.EditorInput;
global using Autodesk.AutoCAD.Runtime;
global using Autodesk.AutoCAD.Geometry;
global using Autodesk.AutoCAD.DatabaseServices;
global using IFoxCAD.Cad;
global using Application = Autodesk.AutoCAD.ApplicationServices.Application;
global using Polyline = Autodesk.AutoCAD.DatabaseServices.Polyline;
global using Autodesk.AutoCAD.Features.PointCloud.PointCloudColorMapping;
global using Autodesk.AutoCAD.GraphicsInterface;
global using IFoxCAD.Basal;
global using System.Diagnostics;
global using System.Reflection;
global using System.Runtime.Remoting.Channels;
global using System.Windows;
global using System.Windows.Shapes;
global using Autodesk.AutoCAD.MacroRecorder;
global using System.Net.NetworkInformation;
global using System.Windows.Controls;
global using MessageBox = System.Windows.MessageBox;
global using Autodesk.AutoCAD.Colors;
global using Ellipse = Autodesk.AutoCAD.DatabaseServices.Ellipse;
global using Line = Autodesk.AutoCAD.DatabaseServices.Line;
global using Exception = Autodesk.AutoCAD.Runtime.Exception;
global using System.IO;
namespace IfoxDemo
{
public class GlobalUsing
{
}
public static partial class AddEntityTools
{
public static bool GetPoint(this Editor ed, out Point3d point, string message)
{
PromptPointResult pr;
PromptPointOptions ps = new PromptPointOptions(message);
pr = ed.GetPoint(ps);
if (pr.Status == PromptStatus.OK)
{
point = pr.Value;
return true;
}
else
{
ed.WriteMessage("\n用户取消了点的选择.");
point = pr.Value;
return false;
}
}
public static string AddLayer(this Database db, string layname, short colorindex)
{
if ((colorindex < 0) || (colorindex > 256))
{
colorindex = 0;
};
var pt = Point3d.Origin;
Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(out pt, "asdf");
// 获得当前文档和数据库 Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
//Database db = acDoc.Database;
// 启动一个事务 Start a transaction
using (Transaction acTrans = db.TransactionManager.StartTransaction())
{
// 以只读方式打开图层表 Open the Layer table for read
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(db.LayerTableId,
OpenMode.ForRead) as LayerTable;
if (acLyrTbl.Has(layname) == false)
{
LayerTableRecord acLyrTblRec = new LayerTableRecord();
// Assign the layer the ACI color 1 and a name
acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, colorindex);
acLyrTblRec.Name = layname;
// Upgrade the Layer table for write
acLyrTbl.UpgradeOpen();
// Append the new layer to the Layer table and the transaction
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
// 以只读方式打开块表 Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(db.BlockTableId,
OpenMode.ForRead) as BlockTable;
// 以写方式打开模型空间块表记录 Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// Save the changes and dispose of the transaction
acTrans.Commit();
}
return layname;
}
/// <summary>
/// 将图形对象添加到图形文件中
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="ent">图形对象</param>
/// <returns>图形的ObjectId</returns>
public static ObjectId AddEntityToModeSpace(this Database db, Entity ent)
{
// 声明ObjectId 用于返回
ObjectId entId = ObjectId.Null;
// 开启事务处理
if (ent.IsNewObject)
{
using (DocumentLock acLckDoc = Application.DocumentManager.MdiActiveDocument.LockDocument())
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
// 添加图形到块表记录
entId = btr.AppendEntity(ent);
// 更新数据信息
trans.AddNewlyCreatedDBObject(ent, true);
// 提交事务
trans.Commit();
}
}
}
return entId;
}
public static ObjectId AddEntityToModeSpace(this Database db,Document doc, Entity ent)
{
// 声明ObjectId 用于返回
ObjectId entId = ObjectId.Null;
// 开启事务处理
if (ent.IsNewObject)
{
using (DocumentLock acLckDoc = doc.LockDocument())
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
// 添加图形到块表记录
entId = btr.AppendEntity(ent);
// 更新数据信息
trans.AddNewlyCreatedDBObject(ent, true);
// 提交事务
trans.Commit();
}
}
}
return entId;
}
/// <summary>
/// 添加多个图形对象到图形文件中
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="ent">图形对象 可变参数 传入一个数组</param>
/// <returns>图形的ObjectId 数组返回</returns>
public static ObjectId[] AddEntityToModeSpace(this Database db, params Entity[] ent)
{
// 声明ObjectId 用于返回
ObjectId[] entId = new ObjectId[ent.Length];
// 开启事务处理
using (DocumentLock acLckDoc = Application.DocumentManager.MdiActiveDocument.LockDocument())
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
for (int i = 0; i < ent.Length; i++)
{
// 将图形添加到块表记录
if (ent[i].IsNewObject)
{
entId[i] = btr.AppendEntity(ent[i]);
// 更新数据信息
trans.AddNewlyCreatedDBObject(ent[i], true);
}
// Z.ed.Redraw(ent[i]);//动态显示
}
// 提交事务
trans.Commit();
}
}
return entId;
}
/// <summary>
/// 绘制直线
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="startPoint">起点坐标</param>
/// <param name="endPoint">终点坐标</param>
/// <returns></returns>
public static ObjectId AddLineToModeSpace(this Database db, Point3d startPoint, Point3d endPoint)
{
return db.AddEntityToModeSpace(new Line(startPoint, endPoint));
}
/// <summary>
/// 起点坐标,角度,长度 绘制直线
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="startPoint">起点</param>
/// <param name="length">长苏</param>
/// <param name="degree">角度</param>
/// <returns></returns>
public static ObjectId AddLineToModeSpace(this Database db, Point3d startPoint, Double length, Double degree)
{
// 利用长度和角度以及起点 计算终点坐标
double X = startPoint.X + length * Math.Cos(degree.DegreeToAngle());
double Y = startPoint.Y + length * Math.Sin(degree.DegreeToAngle());
Point3d endPoint = new Point3d(X, Y, 0);
return db.AddEntityToModeSpace(new Line(startPoint, endPoint));
}
// 封装圆弧对象函数
/// <summary>
/// 绘制圆弧
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="center">中心</param>
/// <param name="radius">半径</param>
/// <param name="startDegree">起始角度</param>
/// <param name="endDegree">终止角度</param>
/// <returns></returns>
public static ObjectId AddArcToModeSpace(this Database db, Point3d center, double radius, double startDegree, double endDegree)
{
return db.AddEntityToModeSpace(new Arc(center, radius, startDegree.DegreeToAngle(), endDegree.DegreeToAngle()));
}
/// <summary>
/// 三点画圆弧
/// </summary>
/// <param name="db">数据库对象</param>
/// <param name="startPoint">起点</param>
/// <param name="pointOnArc">圆弧上一点</param>
/// <param name="endPoint">终点</param>
/// <returns></returns>
public static ObjectId AddArcToModeSpace(this Database db, Point3d startPoint, Point3d pointOnArc, Point3d endPoint)
{
// 先判断是否在同一条直线上
if (startPoint.IsOnOneLine(pointOnArc, endPoint))
{
return ObjectId.Null;
}
// 创建几何对象
CircularArc3d cArc = new CircularArc3d(startPoint, pointOnArc, endPoint);
// 通过几何对象获取其属性
double radius = cArc.Radius; //半径
/**************************************
Point3d center = cArc.Center; // 所在圆的圆心
Vector3d cs = center.GetVectorTo(startPoint); // 圆心到起点的向量
Vector3d ce = center.GetVectorTo(endPoint); // 圆心到终点的向量
Vector3d xVector = new Vector3d(1, 0, 0); // x正方向的向量
// 圆弧起始角度
double startAngle = cs.Y > 0 ? xVector.GetAngleTo(cs) : -xVector.GetAngleTo(cs);
// 圆弧终止角度
double endAngle = ce.Y > 0 ? xVector.GetAngleTo(ce) : -xVector.GetAngleTo(ce);
********************************************/
// 创建圆弧对象
Arc arc = new Arc(cArc.Center, cArc.Radius, cArc.Center.GetAngleToXAxis(startPoint), cArc.Center.GetAngleToXAxis(endPoint));
// 加入到图形数据库
return db.AddEntityToModeSpace(arc);
}
/// <summary>
/// 绘制圆
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="center">圆心</param>
/// <param name="radius">半径</param>
/// <returns></returns>
public static ObjectId AddCircleModeSpace(this Database db, Point3d center, double radius)
{
return db.AddEntityToModeSpace(new Circle((center), new Vector3d(0, 0, 1), radius));
}
/// <summary>
/// 两点绘制圆
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="point1">第一个电</param>
/// <param name="point2">第二个点</param>
/// <returns></returns>
public static ObjectId AddCircleModeSpace(this Database db, Point3d point1, Point3d point2)
{
// 获取两点的中心点
Point3d center = point1.GetCenterPointBetweenTwoPoint(point2);
// 获取半径
double radius = point1.GetDistanceBetweenTwoPoint(center);
return db.AddCircleModeSpace(center, radius);
}
/// <summary>
/// 三点绘制圆
/// </summary>
/// <param name="db"></param>
/// <param name="point1"></param>
/// <param name="point2"></param>
/// <param name="point3"></param>
/// <returns></returns>
public static ObjectId AddCircleModeSpace(this Database db, Point3d point1, Point3d point2, Point3d point3)
{
// 先判断三点是否在同一直线上
if (point1.IsOnOneLine(point2, point3))
{
return ObjectId.Null;
}
// 声明几何类Circular3d对象
CircularArc3d cArc = new CircularArc3d(point1, point2, point3);
return db.AddCircleModeSpace(cArc.Center, cArc.Radius);
}
/// <summary>
/// 绘制折线多段线
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="isClosed">是否闭合</param>
/// <param name="contantWidth">线宽</param>
/// <param name="vertices">多线段的顶点 可变参数</param>
/// <returns></returns>
public static ObjectId AddPolyLineToModeSpace(this Database db, bool isClosed, double contantWidth, params Point2d[] vertices)
{
if (vertices.Length < 2) // 顶点个数小于2 无法绘制
{
return ObjectId.Null;
}
// 声明一个多段线对象
Polyline pline = new Polyline();
// 添加多段线顶点
for (int i = 0; i < vertices.Length; i++)
{
pline.AddVertexAt(i, vertices[i], 0, 0, 0);
}
if (isClosed)
{
pline.Closed = true;
}
// 设置多段线的线宽
pline.ConstantWidth = contantWidth;
return db.AddEntityToModeSpace(pline);
}
/// <summary>
/// 绘制矩形
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="point1">第一个点</param>
/// <param name="point2">对角点</param>
/// <returns></returns>
public static ObjectId AddRectToModeSpace(this Database db, Point2d point1, Point2d point2)
{
// 声明多段线
Polyline pLine = new Polyline();
// 计算矩形的四个顶点坐标
Point2d p1 = new Point2d(Math.Min(point1.X, point2.X), Math.Min(point1.Y, point2.Y));
Point2d p2 = new Point2d(Math.Max(point1.X, point2.X), Math.Min(point1.Y, point2.Y));
Point2d p3 = new Point2d(Math.Max(point1.X, point2.X), Math.Max(point1.Y, point2.Y));
Point2d p4 = new Point2d(Math.Min(point1.X, point2.X), Math.Max(point1.Y, point2.Y));
// 添加多段线顶点
pLine.AddVertexAt(0, p1, 0, 0, 0); // 参数 索引值 传入点 多段线凸度 起始宽度 终止宽度
pLine.AddVertexAt(0, p2, 0, 0, 0);
pLine.AddVertexAt(0, p3, 0, 0, 0);
pLine.AddVertexAt(0, p4, 0, 0, 0);
pLine.Closed = true; // 闭合
return db.AddEntityToModeSpace(pLine);
}
/// <summary>
/// 绘制正多边形
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="center">多边形所在内接圆圆心</param>
/// <param name="radius">所在圆半径</param>
/// <param name="sideNum">边数</param>
/// <param name="startDegree">起始角度</param>
/// <returns></returns>
public static ObjectId AddPolygonToModeSpace(this Database db, Point2d center, double radius, int sideNum, double startDegree)
{
// 声明一个多段线对象
Polyline pLine = new Polyline();
// 判断边数是否符合要求
if (sideNum < 3)
{
return ObjectId.Null;
}
Point2d[] point = new Point2d[sideNum]; // 有几条边就有几个点
double angle = startDegree.DegreeToAngle();
// 计算每个顶点坐标
for (int i = 0; i < sideNum; i++)
{
point[i] = new Point2d(center.X + radius * Math.Cos(angle), center.Y + radius * Math.Sin(angle));
pLine.AddVertexAt(i, point[i], 0, 0, 0);
angle += Math.PI * 2 / sideNum;
}
// 闭合多段线
pLine.Closed = true;
return db.AddEntityToModeSpace(pLine);
}
/// <summary>
/// 绘制椭圆
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="center">椭圆中心</param>
/// <param name="majorRadius">长轴长度</param>
/// <param name="shortRadius">短轴长度</param>
/// <param name="degree">长轴与X夹角 角度值</param>
/// <param name="startDegree">起始角度</param>
/// <param name="endDegree">终止角度</param>
/// <returns></returns>
public static ObjectId AddEllipseToModeSpace(this Database db, Point3d center, double majorRadius, double shortRadius, double degree, double startDegree, double endDegree)
{
// 计算相关参数
double ratio = shortRadius / majorRadius;
Vector3d majorAxis = new Vector3d(majorRadius * Math.Cos(degree.AngleToDegree()), majorRadius * Math.Sin(degree.DegreeToAngle()), 0);
Ellipse elli = new Ellipse(center, Vector3d.ZAxis, majorAxis, ratio, startDegree.DegreeToAngle(), endDegree.DegreeToAngle()); // VVector3d.ZAxis 等价于 new Vector3d(0,0,1) 平行于z轴法向量
return db.AddEntityToModeSpace(elli);
}
/// <summary>
/// 三点绘制椭圆
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="majorPoint1">长轴端点1</param>
/// <param name="majorPoint2">长轴端点2</param>
/// <param name="shortRadius">短轴的长度</param>
/// <returns>ObjectId</returns>
public static ObjectId AddEllipseToModeSpace(this Database db, Point3d majorPoint1, Point3d majorPoint2, double shortRadius)
{
// 椭圆圆心
Point3d center = majorPoint1.GetCenterPointBetweenTwoPoint(majorPoint2);
// 短轴与长轴的比例
double ratio = 2 * shortRadius / majorPoint1.GetDistanceBetweenTwoPoint(majorPoint2);
// 长轴的向量
Vector3d majorAxis = majorPoint2.GetVectorTo(center);
Ellipse elli = new Ellipse(center, Vector3d.ZAxis, majorAxis, ratio, 0, 2 * Math.PI);
return db.AddEntityToModeSpace(elli);
}
/// <summary>
/// 绘制椭圆 两点
/// </summary>
/// <param name="db"></param>
/// <param name="point1">所在矩形的顶点</param>
/// <param name="point2">所在矩形的顶点2</param>
/// <returns></returns>
public static ObjectId AddEllipseToModeSpace(this Database db, Point3d point1, Point3d point2)
{
// 椭圆圆心
Point3d center = point1.GetCenterPointBetweenTwoPoint(point2);
double ratio = Math.Abs((point1.Y - point2.Y) / (point1.X - point2.X));
Vector3d majorVector = new Vector3d(Math.Abs((point1.X - point2.X)) / 2, 0, 0);
// 声明椭圆对象
Ellipse elli = new Ellipse(center, Vector3d.ZAxis, majorVector, ratio, 0, 2 * Math.PI);
return db.AddEntityToModeSpace(elli);
}
}
public static partial class BaseTools
{
/// <summary>
/// 角度转化为弧度
/// </summary>
/// <param name="degree">角度值</param>
/// <returns></returns>
public static double DegreeToAngle(this Double degree)
{
return degree * Math.PI / 180;
}
/// <summary>
/// 弧度转换角度
/// </summary>
/// <param name="angle">弧度制</param>
/// <returns></returns>
public static double AngleToDegree(this Double angle)
{
return angle * 180 / Math.PI;
}
/// <summary>
/// 判断三个点是否在同一直线上
/// </summary>
/// <param name="firstPoint">第一个点</param>
/// <param name="secondPoint">第二个点</param>
/// <param name="thirdPoint">第三个点</param>
/// <returns></returns>
public static bool IsOnOneLine(this Point3d firstPoint, Point3d secondPoint, Point3d thirdPoint)
{
Vector3d v21 = secondPoint.GetVectorTo(firstPoint);
Vector3d v23 = secondPoint.GetVectorTo(thirdPoint);
if (v21.GetAngleTo(v23) == 0 || v21.GetAngleTo(v23) == Math.PI)
{
return true;
}
else
{
return false;
}
}
public static double GetAngleToXAxis(this Point3d startPoint, Point3d endPoint)
{
// 声明一个与X轴平行的向量
Vector3d temp = new Vector3d(1, 0, 0);
// 获取七点到终点的向量
Vector3d VsToe = startPoint.GetVectorTo(endPoint);
return VsToe.Y > 0 ? temp.GetAngleTo(VsToe) : -temp.GetAngleTo(VsToe);
}
public static double GetDistanceBetweenTwoPoint(this Point3d point1, Point3d point2)
{
return (Math.Sqrt(Math.Pow((point1.X - point2.X), 2) + Math.Pow((point1.Y - point2.Y), 2) + Math.Pow((point1.Z + point2.Z), 2)));
}
/// <summary>
/// 获取两点的中心点
/// </summary>
/// <param name="point1">第一个点</param>
/// <param name="point2">第二个点</param>
/// <returns></returns>
public static Point3d GetCenterPointBetweenTwoPoint(this Point3d point1, Point3d point2)
{
return new Point3d((point1.X + point2.X) / 2, (point1.Y + point2.Y) / 2, (point1.Z + point2.Z) / 2);
}
}
public static partial class BlockTools
{
public static void EraseBlockSelectonScreen(this Database db)
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
TypedValue[] tv = new TypedValue[]//类型值
{
new TypedValue((int)DxfCode.Start,"INSERT"),
new TypedValue((int)DxfCode.LayerName,"0")
};
SelectionFilter sf = new SelectionFilter(tv);//选择过滤器
PromptSelectionResult psr = ed.GetSelection(sf);
if (psr.Status == PromptStatus.OK)
{
ObjectId[] ids = psr.Value.GetObjectIds();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < ids.Length; i++)
{
DBObject obj = ids[i].GetObject(OpenMode.ForWrite);
obj.Erase();//对象可以直接删除
}
tr.Commit();
}
}
}
public static bool EraseBlock(this ObjectId objId)
{
Database db = HostApplicationServices.WorkingDatabase;
bool isErase = false;
try
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
if (bt.Has(objId))
{
BlockTableRecord bEra = (BlockTableRecord)objId.GetObject(OpenMode.ForWrite);
bEra.Erase();
}
trans.Commit();
isErase = true;
}
return isErase;
}
catch (Exception)
{
return isErase;
throw;
}
}
public static void ChangeBlockColor(this Database db, ObjectId objId, short colorindex)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
if (bt.Has(objId))
{
BlockTableRecord btr = (BlockTableRecord)objId.GetObject(OpenMode.ForWrite);
foreach (var item in btr)
{
item.ChangeEntityColor(colorindex);
}
btr.DowngradeOpen();
}
trans.Commit();
}
}
public static ObjectId ChangeBlockColor(this Database db, string btrName, short colorindex)
{
ObjectId btrId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
if (bt.Has(btrName))
{
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[btrName], OpenMode.ForWrite);
//BlockTableRecord btr = new BlockTableRecord();
foreach (var item in btr)
{
item.ChangeEntityColor(colorindex);
//Entity entcc = (Entity) item.GetObject(OpenMode.ForWrite);
//entcc.
}
//bt.UpgradeOpen();
//bt.Add(btr);
//trans.AddNewlyCreatedDBObject(btr, true);
btr.DowngradeOpen();
btrId = bt[btrName];
}
trans.Commit();
}
return btrId;
}
public static ObjectId AddBlock<T>(this Database db, string btrName, List<T> ents) where T : Entity
{
ObjectId btrId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
if (bt.Has(btrName))
{//如果块名已存在,那么删除已有块
BlockTableRecord bEra = (BlockTableRecord)trans.GetObject(bt[btrName], OpenMode.ForWrite);
bEra.Erase();
BlockTableRecord btr = new BlockTableRecord();
btr.Name = btrName;
for (int i = 0; i < ents.Count; i++)
{
// ents[i].ColorIndex = 1;块中item的颜色
btr.AppendEntity(ents[i]);
}
bt.UpgradeOpen();
bt.Add(btr);
trans.AddNewlyCreatedDBObject(btr, true);
bt.DowngradeOpen();
btrId = bt[btrName];
//如果块名已存在,那么新建一个块名副本
//BlockTableRecord btr = new BlockTableRecord();
//while (bt.Has(btrName))
//{
// btr.Name = btrName + "_复件";
// btrName = btr.Name;
//}
//for (int i = 0; i < ents.Count; i++)
//{
// btr.AppendEntity(ents[i]);
//}
//bt.UpgradeOpen();
//bt.Add(btr);
//trans.AddNewlyCreatedDBObject(btr, true);
//bt.DowngradeOpen();
//btrId = bt[btrName];
}
else if (!bt.Has(btrName))
{
BlockTableRecord btr = new BlockTableRecord();
btr.Name = btrName;
for (int i = 0; i < ents.Count; i++)
{
btr.AppendEntity(ents[i]);
}
bt.UpgradeOpen();
bt.Add(btr);
trans.AddNewlyCreatedDBObject(btr, true);
bt.DowngradeOpen();
btrId = bt[btrName];
}
trans.Commit();
}
return btrId;
}
public static ObjectId AddBlock<T>(this Database db, string btrName, List<ObjectId> objIds)
{
ObjectId btrId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
if (bt.Has(btrName))
{//如果块名已存在,那么删除已有块
BlockTableRecord bEra = (BlockTableRecord)trans.GetObject(bt[btrName], OpenMode.ForWrite);
bEra.Erase();
BlockTableRecord btr = new BlockTableRecord();
btr.Name = btrName;
for (int i = 0; i < objIds.Count; i++)
{
// ents[i].ColorIndex = 1;块中item的颜色
btr.AppendEntity( (Entity)objIds[i].GetObject(OpenMode.ForWrite));
}
bt.UpgradeOpen();
bt.Add(btr);
trans.AddNewlyCreatedDBObject(btr, true);
bt.DowngradeOpen();
btrId = bt[btrName];
//如果块名已存在,那么新建一个块名副本
//BlockTableRecord btr = new BlockTableRecord();
//while (bt.Has(btrName))
//{
// btr.Name = btrName + "_复件";
// btrName = btr.Name;
//}
//for (int i = 0; i < ents.Count; i++)
//{
// btr.AppendEntity(ents[i]);
//}
//bt.UpgradeOpen();
//bt.Add(btr);
//trans.AddNewlyCreatedDBObject(btr, true);
//bt.DowngradeOpen();
//btrId = bt[btrName];
}
else if (!bt.Has(btrName))
{
BlockTableRecord btr = new BlockTableRecord();
btr.Name = btrName;
for (int i = 0; i < objIds.Count; i++)
{
// ents[i].ColorIndex = 1;块中item的颜色
btr.AppendEntity((Entity)objIds[i].GetObject(OpenMode.ForWrite));
}
bt.UpgradeOpen();
bt.Add(btr);
trans.AddNewlyCreatedDBObject(btr, true);
bt.DowngradeOpen();
btrId = bt[btrName];
}
trans.Commit();
}
return btrId;
}
public static ObjectId AddAttBlock(this Database db, string btrName, List<Entity> ents, Point3d pt, string tag, string prompt, string teststring, int height)
{
ObjectId btrId = ObjectId.Null;
AttributeDefinition attd2 = new AttributeDefinition();
attd2.Position = pt;
attd2.Tag = tag;
attd2.Prompt = prompt;
attd2.TextString = teststring;
attd2.Height = height;
List<Entity> ents2 = new List<Entity>();
foreach (var item in ents)
{
ents2.Add(item);
}
ents2.Add(attd2);
return AddBlock(db, btrName, ents2);
}
public static ObjectId InsertBlock(this Database db, ObjectId blockRecordId, Point3d point)
{
ObjectId btrId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
//foreach (var item in bt)//获取模型空间、图纸空间、块名等快表记录名称
//{
// BlockTableRecord btr = (BlockTableRecord)item.GetObject(OpenMode.ForRead);
// string na = btr.Name;
//}
if (bt.Has(blockRecordId))
{
BlockReference brefe = new BlockReference(point, blockRecordId);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btrId = btr.AppendEntity(brefe);
trans.AddNewlyCreatedDBObject(brefe, true);
}
trans.Commit();
}
return btrId;
}
public static ObjectId InsertBlock(this Database db, ObjectId blockRecordId, Point3d point, double rotation, double scalefactorX, double scalefactorY, double scalefactorZ)
{
//Scale3d scale = new Scale3d(scalefactorX * 0.03937, scalefactorY * 0.03937, scalefactorZ * 0.03937);
Scale3d scale = new Scale3d(scalefactorX , scalefactorY , scalefactorZ );
ObjectId btrId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
//foreach (var item in bt)//获取模型空间、图纸空间、块名等快表记录名称
//{
// BlockTableRecord btr = (BlockTableRecord)item.GetObject(OpenMode.ForRead);
// string na = btr.Name;
//}
if (bt.Has(blockRecordId))
{
BlockReference brefe = new BlockReference(point, blockRecordId);
brefe.Rotation = rotation;
brefe.ScaleFactors = scale;
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btrId = btr.AppendEntity(brefe);
trans.AddNewlyCreatedDBObject(brefe, true);
}
trans.Commit();
}
return btrId;
}
public static ObjectId InsertAttrBlock(this Database db, ObjectId blockRecordId, Point3d point, double rotation, double scalefactorX, double scalefactorY, double scalefactorZ)
{
Scale3d scale = new Scale3d(scalefactorX * 0.03937, scalefactorY * 0.03937, scalefactorZ * 0.03937);
ObjectId btrId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
if (bt.Has(blockRecordId))
{
BlockReference br = new BlockReference(point, blockRecordId);
br.Rotation = rotation;
br.ScaleFactors = scale;
//模型空间的快表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btrId = btr.AppendEntity(br);
//添加属性定义 属性块的快表记录
BlockTableRecord btr2 = (BlockTableRecord)blockRecordId.GetObject(OpenMode.ForRead);
if (btr2.HasAttributeDefinitions)
{
foreach (var item in btr2)
{
DBObject obj = item.GetObject(OpenMode.ForRead);
if (obj is AttributeDefinition)
{
//声明属性参照
AttributeReference attrefe = new AttributeReference();
attrefe.SetAttributeFromBlock((AttributeDefinition)obj, br.BlockTransform);
br.AttributeCollection.AppendAttribute(attrefe);
trans.AddNewlyCreatedDBObject(attrefe, true);
}
}
}
trans.AddNewlyCreatedDBObject(br, true);
}
trans.Commit();
}
return btrId;
}
/// <summary>
/// 修改块属性值
/// </summary>
/// <param name="blockRefId">块参照的objectid</param>
/// <param name="attrNameValues"></param>
public static void ChangeBlockAttr(this ObjectId blockRefId, Dictionary<string, string> attrNameValues)
{
using (Transaction tr = blockRefId.Database.TransactionManager.StartTransaction())
{
if (blockRefId != ObjectId.Null)
{
BlockReference br = (BlockReference)blockRefId.GetObject(OpenMode.ForRead);
foreach (ObjectId item in br.AttributeCollection)
{
AttributeReference attref = item.GetObject(OpenMode.ForRead) as AttributeReference;
//判断属性字典是否包含要更改的属性值
if (attrNameValues.ContainsKey(attref.Tag.ToString()))
{
attref.UpgradeOpen();
attref.TextString = attrNameValues[attref.Tag.ToString()].ToString();
attref.DowngradeOpen();
}
}
}
tr.Commit();
}
}
#region
//修改标注样式
//{ using (Transaction tr = db.TransactionManager.StartTransaction())
// {
// DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
// if (dst.Has("Standard"))
// {
// DimStyleTableRecord dstr = (DimStyleTableRecord)dst["Standard"].GetObject(OpenMode.ForRead);
// dstr.UpgradeOpen();
// dstr.Dimblk = myc;
// dstr.Dimasz= 2;
// dstr.DowngradeOpen();
// db.SetDimstyleData(dstr);
// }
//tr.Commit();
#endregion
}
public static partial class EditEntityTools
{
/// <summary>
/// 改变图形颜色
/// </summary>
/// <param name="c1Id">图形的ObjectId</param>
/// <param name="colorIndex">颜色索引</param>
/// <returns>图形的ObjectId</returns> 图形已经添加图形数据库
public static ObjectId ChangeEntityColor(this ObjectId c1Id, int colorIndex)
{
// 图形数据库
Database db = HostApplicationServices.WorkingDatabase;
// 开启事务处理
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
// 获取图形对象
Entity ent1 = (Entity)c1Id.GetObject(OpenMode.ForWrite);
// 设置颜色
ent1.ColorIndex = colorIndex;
// 提交事务处理
trans.Commit();
}
return c1Id;
}
/// <summary>
/// 改变图形颜色 图形没有添加到图形数据库
/// </summary>
/// <param name="ent">图形对象</param>
/// <param name="colorIndex">颜色索引</param>
/// <returns></returns>
public static void ChangeEntityColor(this Entity ent, int colorIndex)
{
// 判断图形的IsNewlyObject
if (ent.IsNewObject)
{
ent.ColorIndex = colorIndex;
}
// 不是新图形就调用上面的方法
else
{
ent.ObjectId.ChangeEntityColor(colorIndex);
}
}
/// <summary>
/// 移动图形 图形已经加入到图形数据库中
/// </summary>
/// <param name="entId">图形对象的ObjectId</param>
/// <param name="sourcePoint">参考原点</param>
/// <param name="targetPoint">参考目标点</param>
public static void MoveEntity(this ObjectId entId, Point3d sourcePoint, Point3d targetPoint)
{
// 打开当前图形数据库
Database db = HostApplicationServices.WorkingDatabase;
// 开启事务处理
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
//Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);
// 打开图形
Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
// 计算变换矩阵
Vector3d vectoc = sourcePoint.GetVectorTo(targetPoint);
Matrix3d mt = Matrix3d.Displacement(vectoc);
ent.TransformBy(mt);
// 提交事务处理
trans.Commit();
}
}
/// <summary>
/// 移动图形 图形没有加到图形数据库中
/// </summary>
/// <param name="entId">图形对象的ObjectId</param>
/// <param name="sourcePoint">参考原点</param>
/// <param name="targetPoint">参考目标点</param>
public static void MoveEntity(this Entity ent, Point3d sourcePoint, Point3d targetPoint)
{
// 判断图形对象的IsNewlyObject属性
if (ent.IsNewObject)
{
// 计算变换矩阵
Vector3d vector = sourcePoint.GetVectorTo(targetPoint);
Matrix3d mt = Matrix3d.Displacement(vector);
ent.TransformBy(mt);
}
else
{
ent.ObjectId.MoveEntity(sourcePoint, targetPoint);
}
}
/// <summary>
/// 复制图形 图形已经加入到图形数据库中
/// </summary>
/// <param name="entId">图形对象的ObjectId</param>
/// <param name="sourcePoint">参考原点</param>
/// <param name="targetPoint">参考目标点</param>
public static Entity CopyEntity(this ObjectId entId, Point3d sourcePoint, Point3d targetPoint)
{
// 声明一个图形对象
Entity entR;
// 当前图形数据库
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
// Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);
// 打开图形
Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
// 计算变换矩阵
Vector3d vectoc = sourcePoint.GetVectorTo(targetPoint);
Matrix3d mt = Matrix3d.Displacement(vectoc);
entR = ent.GetTransformedCopy(mt);
btr.AppendEntity(entR);
trans.AddNewlyCreatedDBObject(entR, true);
// 提交事务处理
trans.Commit();
}
return entR;
}
/// <summary>
/// 复制图形 图形没有加到图形数据库中
/// </summary>
/// <param name="ent">图形对象</param>
/// <param name="sourcePoint">参考原点</param>
/// <param name="targetPoint">参考目标点</param>
public static Entity CopyEntity(this Entity ent, Point3d sourcePoint, Point3d targetPoint)
{
//声明一个图形对象
Entity entR;
// 判断图形对象的IsNewlyObject属性
if (ent.IsNewObject)
{
// 计算变换矩阵
Vector3d vector = sourcePoint.GetVectorTo(targetPoint);
Matrix3d mt = Matrix3d.Displacement(vector);
entR = ent.GetTransformedCopy(mt);
}
else
{
entR = ent.ObjectId.CopyEntity(sourcePoint, targetPoint);
}
return entR;
}
/// <summary>
/// 旋转图形 图形在数据库中
/// </summary>
/// <param name="ent">图形对象</param>
/// <param name="center">旋转中心</param>
/// <param name="degree">旋转角度</param>
public static void RotateEntity(this ObjectId entId, Point3d center, double degree)
{
// 当前图形数据库
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
//Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);
// 打开图形
Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
// 计算变换矩阵
Matrix3d mt = Matrix3d.Rotation(degree.DegreeToAngle(), Vector3d.ZAxis, center);
ent.TransformBy(mt);
// 提交事务处理
trans.Commit();
}
}
/// <summary>
/// 旋转图形 图形不在数据库中
/// </summary>
/// <param name="ent">图形对象</param>
/// <param name="center">旋转中心</param>
/// <param name="degree">旋转角度</param>
public static void RotateEntity(this Entity ent, Point3d center, double degree)
{
// 判断图形对象的IsNewlyObject属性
if (ent.IsNewObject)
{
// 计算变换矩阵
Matrix3d mt = Matrix3d.Rotation(degree.DegreeToAngle(), Vector3d.ZAxis, center);
ent.TransformBy(mt);
}
else
{
ent.ObjectId.RotateEntity(center, degree);
}
}
/// <summary>
/// 镜像图形
/// </summary>
/// <param name="ent">图形对象的ObjectId</param>
/// <param name="point1">第一个镜像点</param>
/// <param name="point2">第二个镜像点</param>
/// <param name="isEraseSource">是否删除原图形</param>
/// <returns>返回新的图形对象 已加入图形数据库的情况</returns>
public static Entity MirrorEntity(this ObjectId entId, Point3d point1, Point3d point2, bool isEraseSource)
{
// 声明一个图形对象用于返回
Entity entR;
// 计算镜像的变换矩阵
Matrix3d mt = Matrix3d.Mirroring(new Line3d(point1, point2));
Database db = entId.Database;
// 打开事务处理
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
// 打开原图形
Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);
// 这里得到的图形的IsNewLyObject = true
entR = ent.GetTransformedCopy(mt);
btr.AppendEntity(entR);
trans.AddNewlyCreatedDBObject(entR, true);
// 判断是否删除原对象
if (isEraseSource)
{
ent.Erase();
}
trans.Commit();
}
return entR;
}
/// <summary>
/// 镜像图形
/// </summary>
/// <param name="ent">图形对象</param>
/// <param name="point1">第一个镜像点</param>
/// <param name="point2">第二个镜像点</param>
/// <param name="isEraseSource">是否删除原图形</param>
/// <returns>返回新的图形对象 已加入图形数据库的情况</returns>
public static Entity MirrorEntity(this Entity ent, Point3d point1, Point3d point2, bool isEraseSource)
{
// 声明一个图形对象用于返回
Entity entR;
if (ent.IsNewObject == true)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
// 计算镜像的变换矩阵
Matrix3d mt = Matrix3d.Mirroring(new Line3d(point1, point2));
entR = ent.GetTransformedCopy(mt);
btr.AppendEntity(entR);
trans.AddNewlyCreatedDBObject(ent, true);
trans.Commit();
}
}
else
{
entR = ent.ObjectId.MirrorEntity(point1, point2, isEraseSource);
}
return entR;
}
/// <summary>
/// 缩放图形 图形已经加到图形数据库中
/// </summary>
/// <param name="entId">图形对象的ObjectId</param>
/// <param name="basePoint">缩放的基点</param>
/// <param name="facter">缩放比例</param>
public static void ScaleEntity(this ObjectId entId, Point3d basePoint, double facter)
{
// 计算缩放矩阵
Matrix3d mt = Matrix3d.Scaling(facter, basePoint);
// 启动事务处理
using (Transaction trans = entId.Database.TransactionManager.StartTransaction())
{
// 打开要缩放的图形对象
Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
ent.TransformBy(mt);
trans.Commit();
}
}
/// <summary>
/// 缩放图形 图形没有加到数据库中情况
/// </summary>
/// <param name="ent">图形对象</param>
/// <param name="basePoint">缩放基点</param>
/// <param name="facter">缩放比例</param>
public static void ScaleEntity(this Entity ent, Point3d basePoint, double facter)
{
// 判断图形对象的IsNewlyObject属性
if (ent.IsNewObject == true)
{
// 计算缩放矩阵
Matrix3d mt = Matrix3d.Scaling(facter, basePoint);
ent.TransformBy(mt);
}
else
{
ent.ObjectId.ScaleEntity(basePoint, facter);
}
}
/// <summary>
/// 删除图形对象
/// </summary>
/// <param name="entId">图形对象的ObjectId</param>
public static void EraseEntity(this ObjectId entId)
{
// 打开事务处理
using (Transaction trans = entId.Database.TransactionManager.StartTransaction())
{
try
{
// 打开要删除的图形对象
Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
if (!ent.IsErased)
{
ent.Erase();
trans.Commit();
}
}
catch (Exception)
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n对象不存在!!!");
throw;
}
}
}
public static void Zoom(this Database db)
{
Application.DocumentManager.MdiActiveDocument.SendStringToExecute("._zoom _e ", true, false, false);
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n已完成!");
}
public static void EraseAll(this Database db)
{
// 打开事务处理
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
// 打开要删除的图形对象
foreach (ObjectId objid in btr)
{
objid.EraseEntity();
}
trans.Commit();
}
}
//public static void EraseEntity(this Entity ent)
//{
// // 打开事务处理
// using (Transaction trans = ent.ObjectId.Database.TransactionManager.StartTransaction())
// {
// // 打开要删除的图形对象
// ent.ObjectId.GetObject(OpenMode.ForWrite);
// ent.Erase();
// trans.Commit();
// }
//}
/// <summary>
/// 矩形阵列
/// </summary>
/// <param name="entId">图形对象的ObjectId</param>
/// <param name="rowNum">行数</param>
/// <param name="columnNum">列数</param>
/// <param name="disRow">行间距</param>
/// <param name="disColumn">列间距</param>
/// <returns>List</returns> 已加入图形数据库
public static List<Entity> ArrayRectEntity(this ObjectId entId, int rowNum, int columnNum, double disRow, double disColumn)
{
// 声明一个Entity类型集合 用于返回
List<Entity> entList = new List<Entity>();
// 当前图形数据库
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
//Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);
// 打开图形
Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
// 计算变换矩阵
for (int i = 0; i < rowNum; i++)
{
for (int j = 0; j < columnNum; j++)
{
Matrix3d mt = Matrix3d.Displacement(new Vector3d(j * disColumn, i * disRow, 0));
Entity entA = ent.GetTransformedCopy(mt);
btr.AppendEntity(entA);
trans.AddNewlyCreatedDBObject(entA, true);
entList.Add(entA);
}
}
ent.Erase(); // 删除多余的图形
// 提交事务处理
trans.Commit();
}
return entList;
}
/// <summary>
/// 矩形阵列
/// </summary>
/// <param name="entS">图形对象</param>
/// <param name="rowNum">行数</param>
/// <param name="columnNum">列数</param>
/// <param name="disRow">行间距</param>
/// <param name="disColumn">列间距</param>
/// <returns>List</returns> 没有加入图形数据库
public static List<Entity> ArrayRectEntity(this Entity entS, int rowNum, int columnNum, double disRow, double disColumn)
{
if (entS.IsNewObject == true)
{
// 声明一个Entity类型集合 用于返回
List<Entity> entList = new List<Entity>();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
for (int i = 0; i < rowNum; i++)
{
for (int j = 0; j < columnNum; j++)
{
Matrix3d mt = Matrix3d.Displacement(new Vector3d(j * disColumn, i * disRow, 0));
Entity entA = entS.GetTransformedCopy(mt);
btr.AppendEntity(entA);
trans.AddNewlyCreatedDBObject(entA, true);
entList.Add(entA);
}
}
trans.Commit();
}
return entList;
}
else
{
return entS.ArrayRectEntity(rowNum, columnNum, disRow, disColumn);
}
}
/// <summary>
/// 环形阵列
/// </summary>
/// <param name="entId">图形对象的ObjectId</param>
/// <param name="num">图形数量</param>
/// <param name="degree">中心点到各个图形的夹角</param>
/// <param name="center">中心点</param>
/// <returns>List</returns> 已经加入图形数据库
public static List<Entity> ArrayPolarEntity(this ObjectId entId, int num, double degree, Point3d center)
{
// 声明一个List集合 用于返回
List<Entity> entList = new List<Entity>();
// 打开事务处理
using (Transaction trans = entId.Database.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(entId.Database.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
// 限定阵列角度大小
degree = degree > 360 ? 360 : degree;
degree = degree < -360 ? 360 : degree;
int divAngnum = num - 1;
if (degree == 360 || degree == -360)
{
divAngnum = num;
}
// 计算变换矩阵
for (int i = 0; i < num; i++)
{
Matrix3d mt = Matrix3d.Rotation((i * degree / divAngnum).DegreeToAngle(), Vector3d.ZAxis, center);
Entity entA = ent.GetTransformedCopy(mt);
btr.AppendEntity(entA);
trans.AddNewlyCreatedDBObject(entA, true);
entList.Add(entA);
}
ent.Erase();
trans.Commit();
}
return entList;
}
/// <summary>
/// 环形阵列
/// </summary>
/// <param name="ent">图形对象</param>
/// <param name="num">图形数量</param>
/// <param name="degree">中心点到各个图形的夹角</param>
/// <param name="center">中心点</param>
/// <returns>List</returns>
public static List<Entity> ArrayPolarEntity(this Entity ent, int num, double degree, Point3d center)
{
if (ent.IsNewObject == true)
{
// 声明一个List集合 用于返回
List<Entity> entList = new List<Entity>();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
degree = degree > 360 ? 360 : degree;
degree = degree < -360 ? -360 : degree;
int divAngnum = num - 1;
if (degree == 360 || degree == -360)
{
divAngnum = num;
}
for (int i = 0; i < num; i++)
{
Matrix3d mt = Matrix3d.Rotation((i * degree / divAngnum).DegreeToAngle(), Vector3d.ZAxis, center);
Entity entA = ent.GetTransformedCopy(mt);
btr.AppendEntity(entA);
trans.AddNewlyCreatedDBObject(entA, true);
entList.Add(entA);
}
trans.Commit();
}
return entList;
}
else
{
return ent.ObjectId.ArrayPolarEntity(num, degree, center);
}
}
}
}