前言
紧接上节。记录于此,以便日后查阅,若有错误之处,恳请前辈留言指正。
代码
#region CAD二次开发--第二节
//变换矩阵
/// <summary>
/// 使用数据数组初始化变换矩阵,数组包含旋转对象90度的数据
/// </summary>
void TransformBy1()
{
double[] dMatrix = new double[16];
dMatrix[0] = 0.0;
dMatrix[1] = -1.0;
dMatrix[2] = 0.0;
dMatrix[3] = 0.0;
dMatrix[4] = 1.0;
dMatrix[5] = 0.0;
dMatrix[6] = 0.0;
dMatrix[7] = 0.0;
dMatrix[8] = 0.0;
dMatrix[9] = 0.0;
dMatrix[10] = 1.0;
dMatrix[11] = 0.0;
dMatrix[12] = 0.0;
dMatrix[13] = 0.0;
dMatrix[14] = 0.0;
dMatrix[15] = 1.0;
Matrix3d acMat3d = new Matrix3d(dMatrix);
}
/// <summary>
/// 不用数据数组初始化变换矩阵,而是用Rotation函数返回一个实现对象旋转90度的变换矩阵
/// </summary>
void TransformBy2()
{
Matrix3d acMat3d = new Matrix3d();
//acMat3d = Matrix3d.Rotation(Math.PI / 2, curUCS.Zaxis, new Point3d(0, 0, 0));
}
/// <summary>
/// 移动圆
/// </summary>
[CommandMethod("MoveObject")]
public static void MoveObject()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block块表记录
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建圆 圆心(2,2) 半径0.5
Circle acCirc = new Circle();
acCirc.Center = new Point3d(2, 2, 0);
acCirc.Radius = 0.5;
//创建一个矩阵,使用(0,0,0)到(2,0,0)的矢量移动圆
Point3d acPt3d = new Point3d(0, 0, 0);
Vector3d acVec3d = acPt3d.GetVectorTo(new Point3d(2, 0, 0));
acCirc.TransformBy(Matrix3d.Displacement(acVec3d));
//将新对象添加到块表记录和事务
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
//保存新对象到数据库
acTrans.Commit();
}
}
/// <summary>
/// 旋转轻量级多段线
/// </summary>
[CommandMethod("RotateObject")]
public static void RotateObject()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block块表
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建轻量级多段线
Autodesk.AutoCAD.DatabaseServices.Polyline acPoly = new Autodesk.AutoCAD.DatabaseServices.Polyline();
acPoly.AddVertexAt(0, new Point2d(1, 2), 0, 0, 0);
acPoly.AddVertexAt(1, new Point2d(1, 3), 0, 0, 0);
acPoly.AddVertexAt(2, new Point2d(2, 3), 0, 0, 0);
acPoly.AddVertexAt(3, new Point2d(3, 3), 0, 0, 0);
acPoly.AddVertexAt(4, new Point2d(4, 4), 0, 0, 0);
acPoly.AddVertexAt(5, new Point2d(4, 2), 0, 0, 0);
//闭合多段线
acPoly.Closed = true;
Matrix3d curUCSMatrix = acDoc.Editor.CurrentUserCoordinateSystem;
CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;
//绕当前UCS的Z轴将多段线旋转45度,基点(4,4.25,0)
acPoly.TransformBy(Matrix3d.Rotation(0.7854, curUCS.Zaxis, new Point3d(4, 4.25, 0)));
//将新对象添加到块表记录和事务
acBlkTblRec.AppendEntity(acPoly);
acTrans.AddNewlyCreatedDBObject(acPoly, true);
//保存新对象到数据库
acTrans.Commit();
}
}
/// <summary>
/// 沿轴线镜像一个多段线
/// </summary>
[CommandMethod("MirrorObject")]
public static void MirrorObject()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建一个轻量级多段线
Autodesk.AutoCAD.DatabaseServices.Polyline acPoly = new Autodesk.AutoCAD.DatabaseServices.Polyline();
acPoly.AddVertexAt(0, new Point2d(1, 1), 0, 0, 0);
acPoly.AddVertexAt(1, new Point2d(1, 2), 0, 0, 0);
acPoly.AddVertexAt(2, new Point2d(2, 2), 0, 0, 0);
acPoly.AddVertexAt(3, new Point2d(3, 2), 0, 0, 0);
acPoly.AddVertexAt(4, new Point2d(4, 4), 0, 0, 0);
acPoly.AddVertexAt(5, new Point2d(4, 1), 0, 0, 0);
//设置顶点1到顶点2间的多段线段的凸度为-2
acPoly.SetBulgeAt(1, -2);
//闭合多段线
acPoly.Closed = true;
//将新对象添加到块表记录和事务
acBlkTblRec.AppendEntity(acPoly);
acTrans.AddNewlyCreatedDBObject(acPoly, true);
//创建原多段线的复本
Autodesk.AutoCAD.DatabaseServices.Polyline acPolyMirCopy = acPoly.Clone() as Autodesk.AutoCAD.DatabaseServices.Polyline;
acPolyMirCopy.ColorIndex = 5;
//定义镜像线
Point3d acPtFrom = new Point3d(0, 4.25, 0);
Point3d acPtTo = new Point3d(4, 4.25, 0);
Line3d acLine3d = new Line3d(acPtFrom, acPtTo);
//沿X轴方向翻转多段线
acPolyMirCopy.TransformBy(Matrix3d.Mirroring(acLine3d));
//将新对象添加到块表记录和事务
acBlkTblRec.AppendEntity(acPolyMirCopy);
acTrans.AddNewlyCreatedDBObject(acPolyMirCopy, true);
//保存新对象到数据库
acTrans.Commit();
}
}
/// <summary>
/// 创建多段线并缩放多段线
/// </summary>
[CommandMethod("ScaleObject")]
public static void ScaleObject()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建多段线
Autodesk.AutoCAD.DatabaseServices.Polyline acPoly = new Autodesk.AutoCAD.DatabaseServices.Polyline();
acPoly.AddVertexAt(0, new Point2d(1, 2), 0, 0, 0);
acPoly.AddVertexAt(1, new Point2d(1, 3), 0, 0, 0);
acPoly.AddVertexAt(2, new Point2d(2, 3), 0, 0, 0);
acPoly.AddVertexAt(3, new Point2d(3, 3), 0, 0, 0);
acPoly.AddVertexAt(4, new Point2d(4, 4), 0, 0, 0);
acPoly.AddVertexAt(5, new Point2d(4, 2), 0, 0, 0);
//闭合多段线
acPoly.Closed = true;
//缩小对象,系数0.5,基点(4,4.25,0)
acPoly.TransformBy(Matrix3d.Scaling(0.5, new Point3d(4, 4.25, 0)));
//将新对象添加到块表记录和事务
acBlkTblRec.AppendEntity(acPoly);
acTrans.AddNewlyCreatedDBObject(acPoly, true);
//保存新对象到数据库
acTrans.Commit();
}
}
static Point2d PolarPoints1(Point2d pPt, double dAng, double dDist)
{
return new Point2d(pPt.X + dDist * Math.Cos(dAng),
pPt.Y + dDist * Math.Sin(dAng));
}
[CommandMethod("PolarArrayObject")]
public static void PolarArrayObject()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开块表记录
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建圆,圆心(2,2),半径1
Circle acCirc = new Circle();
acCirc.Center = new Point3d(2, 2, 0);
acCirc.Radius = 1;
//将新对象添加到块表记录和事务
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
//创建4个对象的180度环形阵列
int nCount = 1;
//定义60度弧度值
double dAng = 1.0472;
//定义阵列基点(4,4,0)
Point2d acPt2dArrayBase = new Point2d(4, 4);
while (nCount < 4)
{
Entity acEntClone = acCirc.Clone() as Entity;
Extents3d acExts;
Point2d acPtObjBase;
//通常情况下,使用对象范围的左上角作为阵列对象上的点,除非
//要阵列对象是类似圆这样的对象(使用圆心)
Circle acCircArrObj = acEntClone as Circle;
if (acCircArrObj != null)//是圆
{
acPtObjBase = new Point2d(acCircArrObj.Center.X, acCircArrObj.Center.Y);
}
else//不是圆
{
acExts = acEntClone.Bounds.GetValueOrDefault();
acPtObjBase = new Point2d(acExts.MinPoint.X, acExts.MaxPoint.Y);
}
double dDist = acPt2dArrayBase.GetDistanceTo(acPtObjBase);
double dAngFromX = acPt2dArrayBase.GetVectorTo(acPtObjBase).Angle;
Point2d acPt2dTo = PolarPoints1(acPt2dArrayBase, (nCount * dAng) + dAngFromX, dDist);
Vector2d acVec2d = acPtObjBase.GetVectorTo(acPt2dTo);
Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));
/*
* 下列代码演示怎样旋转每个对象,就像Array命令所做的那样
acExts = acEntClone.Bounds.GetValueOrDefault();
acPtObjBase = new Point2d(acExts.MinPoint.X,
acExts.MaxPoint.Y);
//围绕左上范围点旋转对象
Matrix3d curUCSMatrix = acDoc.Editor.CurrentUserCoordinateSystem;
CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;
acEntClone.TransformBy(Matrix3d.Rotation(nCount*dAng,curUCS.Zaxis,
new Point3d(acPtObjBase.X,acPtObjBase.Y,0)));
*/
//添加新对象到块表记录和事务
acBlkTblRec.AppendEntity(acEntClone);
acTrans.AddNewlyCreatedDBObject(acEntClone, true);
nCount = nCount + 1;
}
//保存新对象到数据库
acTrans.Commit();
}
}
static Point2d PolarPoints2(Point2d pPt, double dAng, double dDist)
{
return new Point2d(pPt.X + dDist * Math.Cos(dAng),
pPt.Y + dDist * Math.Sin(dAng));
}
[CommandMethod("RectangularArrayObject")]
public static void RectangularArrayObject()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建Circle对象
Circle acCirc = new Circle();
acCirc.Center = new Point3d(200, 200, 0);
acCirc.Radius = 60;
//将新对象添加到块表记录和事务
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
//创建5×5矩形阵列
int nRows = 5;
int nColumns = 5;
//设置行列间距及阵列基角
double dRowOffset = 200;
double dColumnOffset = 300;
double dArrayAng = 0;
//获取到当前UCS坐标系X轴的角度
Matrix3d curUCSMatrix = acDoc.Editor.CurrentUserCoordinateSystem;
CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d;
Vector2d acVec2dAng = new Vector2d(curUCS.Xaxis.X, curUCS.Xaxis.Y);
//如果UCS坐标系是旋转的,相应地调整阵列的角度
dArrayAng = dArrayAng + acVec2dAng.Angle;
//使用对象范围的左上角作为阵列的基点
Extents3d acExts = acCirc.Bounds.GetValueOrDefault();
Point2d acPt2dArrayBase = new Point2d(acExts.MinPoint.X, acExts.MaxPoint.Y);
//跟踪为每列创建的对象
DBObjectCollection acDBObjCollCols = new DBObjectCollection();
acDBObjCollCols.Add(acCirc);
//创建每一行的对象(个数等于列数)
int nColumnsCount = 1;
while (nColumns > nColumnsCount)
{
Entity acEntClone = acCirc.Clone() as Entity;
acDBObjCollCols.Add(acEntClone);
//计算新位置
Point2d acPt2dTo = PolarPoints2(acPt2dArrayBase, dArrayAng, dColumnOffset * nColumnsCount);
Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);
Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));
//添加实体
acBlkTblRec.AppendEntity(acEntClone);
acTrans.AddNewlyCreatedDBObject(acEntClone, true);
nColumnsCount = nColumnsCount + 1;
}
//设置90度的弧度值
double dAng = Math.PI / 2;
//记录为每行创建的对象
DBObjectCollection acDBObjCollLv1s = new DBObjectCollection();
foreach (DBObject acObj in acDBObjCollCols)
{
acDBObjCollLv1s.Add(acObj);
}
//创建其余各行
foreach (Entity acEnt in acDBObjCollCols)
{
int nRowsCount = 1;
while (nRows > nRowsCount)
{
Entity acEntClone = acEnt.Clone() as Entity;
acDBObjCollLv1s.Add(acEntClone);
//计算新位置
Point2d acPt2dTo = PolarPoints2(acPt2dArrayBase, dArrayAng + dAng, dRowOffset * nRowsCount);
Vector2d acVec2d = acPt2dArrayBase.GetVectorTo(acPt2dTo);
Vector3d acVec3d = new Vector3d(acVec2d.X, acVec2d.Y, 0);
acEntClone.TransformBy(Matrix3d.Displacement(acVec3d));
acBlkTblRec.AppendEntity(acEntClone);
acTrans.AddNewlyCreatedDBObject(acEntClone, true);
nRowsCount = nRowsCount + 1;
}
}
//保存新对象到数据库
acTrans.Commit();
}
}
/// <summary>
/// 延长一条直线
/// </summary>
[CommandMethod("ExtendObject")]
public static void ExtendObject()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//从(4,4,0)到(7,7,0)创建1条直线段
Line acLine = new Line(new Point3d(4, 4, 0), new Point3d(7, 7, 0));
//将新对象添加到块表记录和事务
acBlkTblRec.AppendEntity(acLine);
acTrans.AddNewlyCreatedDBObject(acLine, true);
//更新显示
acDoc.Editor.Regen();
Autodesk.AutoCAD.ApplicationServices.Application.
ShowAlertDialog("Before extend");
//直线延长一倍
acLine.EndPoint = acLine.EndPoint + acLine.Delta;
//将新对象保存到数据库
acTrans.Commit();
}
}
/// <summary>
/// 分解多段线
/// </summary>
[CommandMethod("ExplodeObject")]
public static void ExplodeObject()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block块表Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建一个轻量级多段线
using (Autodesk.AutoCAD.DatabaseServices.Polyline acPoly = new Autodesk.AutoCAD.DatabaseServices.Polyline())
{
acPoly.AddVertexAt(0, new Point2d(1, 1), 0, 0, 0);
acPoly.AddVertexAt(1, new Point2d(1, 2), 0, 0, 0);
acPoly.AddVertexAt(2, new Point2d(2, 2), 0, 0, 0);
acPoly.AddVertexAt(3, new Point2d(3, 2), 0, 0, 0);
acPoly.AddVertexAt(4, new Point2d(4, 4), 0, 0, 0);
acPoly.AddVertexAt(5, new Point2d(4, 1), 0, 0, 0);
//在顶点3和顶点4之间设置隆起
acPoly.SetBulgeAt(3, -0.5);
//分解多段线
DBObjectCollection acDBObjectColl = new DBObjectCollection();
acPoly.Explode(acDBObjectColl);
foreach (Entity acEnt in acDBObjectColl)
{
//将新对象添加到块表记录和事务
acBlkTblRec.AppendEntity(acEnt);
acTrans.AddNewlyCreatedDBObject(acEnt, true);
}
//清除内存中的多段线
}
//保存新对象到数据库
acTrans.Commit();
}
}
/// <summary>
/// 编辑多段线
/// </summary>
[CommandMethod("EditPolyline")]
public static void EditPolyline()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建一个轻量级多段线
Autodesk.AutoCAD.DatabaseServices.Polyline acPoly = new Autodesk.AutoCAD.DatabaseServices.Polyline();
acPoly.AddVertexAt(0, new Point2d(1, 1), 0, 0, 0);
acPoly.AddVertexAt(1, new Point2d(1, 2), 0, 0, 0);
acPoly.AddVertexAt(2, new Point2d(2, 2), 0, 0, 0);
acPoly.AddVertexAt(3, new Point2d(3, 2), 0, 0, 0);
acPoly.AddVertexAt(4, new Point2d(4, 4), 0, 0, 0);
//将新对象添加到块表记录和事务
acBlkTblRec.AppendEntity(acPoly);
acTrans.AddNewlyCreatedDBObject(acPoly, true);
//在顶点3和顶点4之间设置隆起设置线段3的凸度
acPoly.SetBulgeAt(3, -0.5);
//添加一个新顶点
acPoly.AddVertexAt(5, new Point2d(4, 1), 0, 0, 0);
//设置线段4的起止宽度
acPoly.SetStartWidthAt(4, 0.1);
acPoly.SetEndWidthAt(4, 0.5);
//闭合多段线
acPoly.Closed = true;
//保存呢新对象到数据库
acTrans.Commit();
}
}
/// <summary>
/// 修改样条曲线的控制点
/// </summary>
[CommandMethod("EditSpline")]
public static void EditSpline()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建Point3d对象集合
Point3dCollection acPt3dColl = new Point3dCollection();
acPt3dColl.Add(new Point3d(1, 1, 0));
acPt3dColl.Add(new Point3d(5, 5, 0));
acPt3dColl.Add(new Point3d(10, 0, 0));
//设置样条曲线起止点的切线矢量
Vector3d acStartTan = new Vector3d(0.5, 0.5, 0);
Vector3d acEndTan = new Vector3d(0.5, 0.5, 0);
//创建样条曲线
Spline acSpline = new Spline(acPt3dColl, acStartTan, acEndTan, 4, 0);
//修改控制点
acSpline.SetControlPointAt(0, new Point3d(0, 3, 0));
//将新对象添加到块表记录和事务
acBlkTblRec.AppendEntity(acSpline);
acTrans.AddNewlyCreatedDBObject(acSpline, true);
//保存新对象到数据库
acTrans.Commit();
}
}
/// <summary>
/// 创建一个关联图案填充,并创建一个圆,将其作为填充的内部边界环
/// </summary>
[CommandMethod("EditHatchAppendLoop")]
public static void EditHatchAppendLoop()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建一个Arc对象作为填充的闭合边界
Arc acArc = new Arc(new Point3d(5, 3, 0), 3, 0, 3.141592);
acBlkTblRec.AppendEntity(acArc);
acTrans.AddNewlyCreatedDBObject(acArc, true);
//创建一个Line对象作为填充的闭合边界
Line acLine = new Line(acArc.StartPoint, acArc.EndPoint);
acBlkTblRec.AppendEntity(acLine);
acTrans.AddNewlyCreatedDBObject(acLine, true);
//将圆弧和直线添加到ObjectIdCollection
ObjectIdCollection acObjIdColl = new ObjectIdCollection();
acObjIdColl.Add(acArc.ObjectId);
acObjIdColl.Add(acLine.ObjectId);
//创建Hatch对象并添加到块表记录
Hatch acHatch = new Hatch();
acBlkTblRec.AppendEntity(acHatch);
acTrans.AddNewlyCreatedDBObject(acHatch, true);
//设置填充对象的属性
//关联属性必须在将填充对象添加到块表记录之后,执行AppendLoop之前设置
acHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
acHatch.Associative = true;
acHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl);
//创建一个圆对象作为填充的内部边界
Circle acCirc = new Circle();
acCirc.Center = new Point3d(5, 4.5, 0);
acCirc.Radius = 1;
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
//将圆添加到ObjectIdCollection
acObjIdColl.Clear();
acObjIdColl.Add(acCirc.ObjectId);
//追加圆为内部边界环并评估填充对象
acHatch.AppendLoop(HatchLoopTypes.Default, acObjIdColl);
acHatch.EvaluateHatch(true);
//将新对象保存到数据库
acTrans.Commit();
}
}
/// <summary>
/// 修改填充图案的间距
/// </summary>
[CommandMethod("EditHatchPatternScale")]
public static void EditHatchPatternScale()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建一个圆作为填充的边界
Circle acCirc = new Circle();
acCirc.Center = new Point3d(5, 3, 0);
acCirc.Radius = 3;
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
//将圆添加到objectIdCollection集合
ObjectIdCollection acObjIdColl = new ObjectIdCollection();
acObjIdColl.Add(acCirc.ObjectId);
//创建Hatch对象并添加到块表记录
Hatch acHatch = new Hatch();
acBlkTblRec.AppendEntity(acHatch);
acTrans.AddNewlyCreatedDBObject(acHatch, true);
//设置填充对象属性
//关联属性必须在将填充对象添加到块表记录之后、执行AppendLoop之前设置
acHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
acHatch.Associative = true;
acHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl);
//评估填充
acHatch.EvaluateHatch(true);
//填充图案的比例加2,重新评估填充
acHatch.PatternScale = acHatch.PatternScale + 2;
acHatch.SetHatchPattern(acHatch.PatternType, acHatch.PatternName);
acHatch.EvaluateHatch(true);
//将新对象保存到数据库
acTrans.Commit();
}
}
/// <summary>
/// 遍历Layers表
/// </summary>
[CommandMethod("DisplayLayerNames")]
public static void DisplayLayerNames()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开图层表
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
string sLayerNames = "";
foreach (ObjectId acObjId in acLyrTbl)
{
LayerTableRecord acLyrTblRec;
acLyrTblRec = acTrans.GetObject(acObjId,
OpenMode.ForRead) as LayerTableRecord;
sLayerNames = sLayerNames + "\n" + acLyrTblRec.Name;
}
Autodesk.AutoCAD.ApplicationServices.Application.
ShowAlertDialog("The layers in this drawing are: " + sLayerNames);
//关闭事务
}
}
/// <summary>
/// 创建并命名图层
/// </summary>
[CommandMethod("CreateAndAssignALayer")]
public static void CreateAndAssignAlayer()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开图层表
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
string sLayerName = "Center";
if (acLyrTbl.Has(sLayerName) == false)
{
LayerTableRecord acLyrTblRec = new LayerTableRecord();
//赋予图层颜色和名称(AutoCADColorIndex为1表示红色)
acLyrTblRec.Color = Autodesk.AutoCAD.Colors.Color.
FromColorIndex(ColorMethod.ByAci, 1);
acLyrTblRec.Name = sLayerName;
//以写模式升级打开图层表
acLyrTbl.UpgradeOpen();
//添加新图层到图层表,记录事务
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建一个圆
Circle acCirc = new Circle();
acCirc.Center = new Point3d(2, 2, 0);
acCirc.Radius = 1;
//设置圆所归属的图层
acCirc.Layer = sLayerName;
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
//保存修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 将图层设为当前图层
/// </summary>
[CommandMethod("SetLayerCurrent")]
public static void SetLayerCurrent()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开图层表
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
string sLayerName = "Center";
if (acLyrTbl.Has(sLayerName) == true)
{
//设置图层Center为当前图层
acCurDb.Clayer = acLyrTbl[sLayerName];
//保存修改
acTrans.Commit();
}
系统变量CLAYER设置当前图层
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CLAYER", "Center");
//保存修改
acTrans.Commit();
}
}
/// <summary>
/// 关闭图层
/// </summary>
[CommandMethod("TurnLayerOff")]
public static void TurnLayerOff()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开图层表
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
string sLayerName = "ABC";
LayerTableRecord acLyrTblRec;
if (acLyrTbl.Has(sLayerName) == false)
{
acLyrTblRec = new LayerTableRecord();
//给图层名赋值
acLyrTblRec.Name = sLayerName;
//以写模式升级打开图层表
acLyrTbl.UpgradeOpen();
//添加新图层到图层表,记录事务
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
else
{
acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
OpenMode.ForWrite) as LayerTableRecord;
}
//关闭图层
acLyrTblRec.IsOff = true;
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建一个圆
Circle acCirc = new Circle();
acCirc.Center = new Point3d(2, 2, 0);
acCirc.Radius = 1;
acCirc.Layer = sLayerName;
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
//提交修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 冻结图层
/// </summary>
[CommandMethod("FreezeLayer")]
public static void FreezeLayer()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开图层表
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
string sLayerName = "ABC";
LayerTableRecord acLyrTblRec;
if (acLyrTbl.Has(sLayerName) == false)
{
acLyrTblRec = new LayerTableRecord();
//给图层名赋值
acLyrTblRec.Name = sLayerName;
//以写模式升级打开图层表
acLyrTbl.UpgradeOpen();
//添加新图层到图层表,记录事务
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
else
{
acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
OpenMode.ForWrite) as LayerTableRecord;
}
//冻结图层
acLyrTblRec.IsFrozen = true;
//提交,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 锁定图层
/// </summary>
[CommandMethod("LockLayer")]
public static void LockLayer()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开图层表
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
string sLayerName = "ABC";
LayerTableRecord acLyrTblRec;
if (acLyrTbl.Has(sLayerName) == false)
{
acLyrTblRec = new LayerTableRecord();
//给图层名称赋值
acLyrTblRec.Name = sLayerName;
//以写模式升级打开图层表
acLyrTbl.UpgradeOpen();
//添加新图层到图层表,记录事务
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
else
{
acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
OpenMode.ForWrite) as LayerTableRecord;
}
//锁定图层
acLyrTblRec.IsLocked = true;
//提交修改、关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 设置图层颜色
/// </summary>
[CommandMethod("SetLayerColor")]
public static void SetLayerColor()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开图层表
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
//定义一个图层名数组
string[] sLayerNames = new string[3];
sLayerNames[0] = "ACIRed";
sLayerNames[1] = "TrueBlue";
sLayerNames[2] = "ColorBookYellow";
//定义一个图层颜色数组
Autodesk.AutoCAD.Colors.Color[] acColors =
new Autodesk.AutoCAD.Colors.Color[3];
acColors[0] = Autodesk.AutoCAD.Colors.Color.
FromColorIndex(ColorMethod.ByAci, 1);
acColors[1] = Autodesk.AutoCAD.Colors.Color.
FromRgb(23, 54, 232);
acColors[2] = Autodesk.AutoCAD.Colors.Color.
FromNames("PANTONE Yellow 0131 C",
"PANTONE(R) pastel coated");
int nCnt = 0;
//添加或修改图形中的每个图层
foreach (string sLayerName in sLayerNames)
{
LayerTableRecord acLyrTblRec;
if (acLyrTbl.Has(sLayerName) == false)
{
acLyrTblRec = new LayerTableRecord();
//设置图层名
acLyrTblRec.Name = sLayerName;
//以写模式升级打开图层表
if (acLyrTbl.IsWriteEnabled == false)
acLyrTbl.UpgradeOpen();
//添加新图层到图层表,记录事务
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
else
{
//如果图层已存在,则以写模式打开
acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
OpenMode.ForWrite) as LayerTableRecord;
}
//设置图层颜色
acLyrTblRec.Color = acColors[nCnt];
nCnt = nCnt + 1;
}
//提交修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 设置图层线性
/// </summary>
[CommandMethod("SetLayerLinetype")]
public static void SetLayerLinetype()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开图层表
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
string sLayerName = "ABC";
LayerTableRecord acLyrTblRec;
if (acLyrTbl.Has(sLayerName) == false)
{
acLyrTblRec = new LayerTableRecord();
//给图层名称赋值
acLyrTblRec.Name = sLayerName;
//以写模式升级打开图层表
acLyrTbl.UpgradeOpen();
//添加新图层到图层表,记录事务
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
}
else
{
acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
OpenMode.ForRead) as LayerTableRecord;
}
//以读模式打开图层表
LinetypeTable acLinTbl;
acLinTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
OpenMode.ForRead) as LinetypeTable;
if (acLinTbl.Has("Center") == true)
{
//以写模式升级打开图层表记录
acLyrTblRec.UpgradeOpen();
//设置图层线型
acLyrTblRec.LinetypeObjectId = acLinTbl["Center"];
}
//保存修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 删除图层
/// </summary>
[CommandMethod("EraseLayer")]
public static void EraseLayer()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开图层表
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
string sLayerName = "ABC";
if (acLyrTbl.Has(sLayerName) == true)
{
//检查删除图层是否安全
ObjectIdCollection acObjIdColl = new ObjectIdCollection();
acObjIdColl.Add(acLyrTbl[sLayerName]);
acCurDb.Purge(acObjIdColl);
if (acObjIdColl.Count > 0)
{
LayerTableRecord acLyrTblRec;
acLyrTblRec = acTrans.GetObject(acObjIdColl[0],
OpenMode.ForWrite) as LayerTableRecord;
try
{
//删除未引用图层
acLyrTblRec.Erase(true);
//保存修改,关闭事务
acTrans.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception Ex)
{
//不能删除
Autodesk.AutoCAD.ApplicationServices.Application.
ShowAlertDialog("Error:\n" + Ex.Message);
}
}
}
}
}
/// <summary>
/// 给对象指定颜色
/// </summary>
[CommandMethod("SetObjectColor")]
public static void SetObjectColor()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//为图层定义颜色数组
Autodesk.AutoCAD.Colors.Color[] acColors = new Autodesk.AutoCAD.Colors.Color[3];
acColors[0] = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 1);
acColors[1] = Autodesk.AutoCAD.Colors.Color.FromRgb(23, 54, 232);
acColors[2] = Autodesk.AutoCAD.Colors.Color.FromNames("PANTONE Yellow 0131 C",
"PANTONE(R) pastel coated");
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建一个圆并将其ACI值设为4
Point3d acPt = new Point3d(0, 3, 0);
Circle acCirc = new Circle();
acCirc.Center = acPt;
acCirc.Radius = 1;
acCirc.ColorIndex = 4;
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
int nCnt = 0;
while (nCnt < 3)
{
//创建圆的拷贝
Circle acCircCopy;
acCircCopy = acCirc.Clone() as Circle;
//沿Y轴移动复本
acPt = new Point3d(acPt.X, acPt.Y + 3, acPt.Z);
acCircCopy.Center = acPt;
//给复本设置新颜色
acCircCopy.Color = acColors[nCnt];
acBlkTblRec.AppendEntity(acCircCopy);
acTrans.AddNewlyCreatedDBObject(acCircCopy, true);
nCnt = nCnt + 1;
}
//提交修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 通过数据库设置当前颜色
/// </summary>
[CommandMethod("SetColorCurrent")]
public static void SetColorCurrent()
{
//获取当前文档
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
//设置当前颜色
acDoc.Database.Cecolor = Autodesk.AutoCAD.Colors.Color.
FromColorIndex(ColorMethod.ByLayer, 256);
使用系统变量CECOLOR设置当前颜色
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CECOLOR", "1");
}
/// <summary>
/// 将线型加载到图形中
/// </summary>
[CommandMethod("LoadLinetype")]
public static void LoadLinetype()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开线性表
LinetypeTable acLineTypTbl;
acLineTypTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
OpenMode.ForRead) as LinetypeTable;
string sLineTypName = "Center";
if (acLineTypTbl.Has(sLineTypName) == false)
{
//装载Center线型
acCurDb.LoadLineTypeFile(sLineTypName, "acad.lin");
}
//提交修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 为对象指定线型
/// </summary>
[CommandMethod("SetObjectLinetype")]
public static void SetObjectLinetype()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开线型表
LinetypeTable acLineTypTbl;
acLineTypTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
OpenMode.ForRead) as LinetypeTable;
string sLineTypName = "Center";
if (acLineTypTbl.Has(sLineTypName) == false)
{
acCurDb.LoadLineTypeFile(sLineTypName, "acad.lin");
}
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建一个圆
Circle acCirc = new Circle();
acCirc.Center = new Point3d(2, 2, 0);
acCirc.Radius = 1;
//设置线型
acCirc.Linetype = sLineTypName;
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
//保存修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 通过数据库设置当前线型
/// </summary>
[CommandMethod("SetLinetypeCurrent")]
public static void SetLinetypeCurrent()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开线型表
LinetypeTable acLineTypTbl;
acLineTypTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
OpenMode.ForRead) as LinetypeTable;
string sLineTypName = "Center";
if (acLineTypTbl.Has(sLineTypName) == true)
{
//将Center线型设置为当前线型
acCurDb.Celtype = acLineTypTbl[sLineTypName];
通过系统变量设置线型
//Autodesk.AutoCAD.ApplicationServices.Application.
// SetSystemVariable("CELTYPE", "Center");
//保存修改
acTrans.Commit();
}
//关闭事务
}
}
/// <summary>
/// 修改线型说明
/// </summary>
[CommandMethod("ChangeLinetypeDescription")]
public static void ChangeLinetypeDescription()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//Open the Linetype table record of the current linetype for write
//以写模式打开当前线型的线型表记录
LinetypeTableRecord acLineTypTblRec;
acLineTypTblRec = acTrans.GetObject(acCurDb.Celtype,
OpenMode.ForWrite) as LinetypeTableRecord;
//修改当前线型说明
acLineTypTblRec.AsciiDescription = "Exterior Wall";
//保存修改,关闭事务
acTrans.Commit();
}
}
[CommandMethod("SetObjectLinetypeScale")]
public static void SetObjectLinetypeScale()
{
//获取当前对象和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//保存当前线型
ObjectId acObjId = acCurDb.Celtype;
//设置全局线型比例
acCurDb.Ltscale = 3;
//以读模式打开线型表
LinetypeTable acLineTypTbl;
acLineTypTbl = acTrans.GetObject(acCurDb.LinetypeTableId,
OpenMode.ForRead) as LinetypeTable;
string sLineTypName = "Border";
if (acLineTypTbl.Has(sLineTypName) == false)
{
acCurDb.LoadLineTypeFile(sLineTypName, "acad.lin");
}
//将Border线型设置为当前线型
acCurDb.Celtype = acLineTypTbl[sLineTypName];
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建一个圆并将其线型比例设置为全尺寸的一半
Circle acCirc1 = new Circle();
acCirc1.Center = new Point3d(2, 2, 0);
acCirc1.Radius = 4;
acCirc1.LinetypeScale = 0.5;
acBlkTblRec.AppendEntity(acCirc1);
acTrans.AddNewlyCreatedDBObject(acCirc1, true);
//再创建一个圆
Circle acCirc2 = new Circle();
acCirc2.Center = new Point3d(12, 2, 0);
acCirc2.Radius = 4;
acBlkTblRec.AppendEntity(acCirc2);
acTrans.AddNewlyCreatedDBObject(acCirc2, true);
//恢复原来的活动线型
acCurDb.Celtype = acObjId;
//保存修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 列出图层中保存的图层状态
/// </summary>
[CommandMethod("ListLayerStates")]
public static void ListLayerStates()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
LayerStateManager acLyrStMan;
acLyrStMan = acCurDb.LayerStateManager;
DBDictionary acDbDict;
acDbDict = acTrans.GetObject(acLyrStMan.LayerStatesDictionaryId(true),
OpenMode.ForRead) as DBDictionary;
string sLayerStateNames = "";
//遍历
foreach (DBDictionaryEntry acDbDictEnt in acDbDict)
{
sLayerStateNames = sLayerStateNames + "\n" + acDbDictEnt.Key;
}
Autodesk.AutoCAD.ApplicationServices.Application.
ShowAlertDialog("The saved layer settings in this drawing are: " +
sLayerStateNames);
//关闭事务
}
}
/// <summary>
/// 通过LayerStateManager改变图层状态
/// </summary>
public static void AlertLayerState()
{
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
LayerStateManager acLyrStMan;
acLyrStMan = acCurDb.LayerStateManager;
}
/// <summary>
/// 保存图层颜色和线型
/// </summary>
[CommandMethod("SaveLayerColorAndLinetype")]
public static void SaveLayerColorAndLinetype()
{
//获取当前文档
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
LayerStateManager acLyrStMan;
acLyrStMan = acDoc.Database.LayerStateManager;
string sLyrStName = "ColorLinetype";
if (acLyrStMan.HasLayerState(sLyrStName) == false)
{
acLyrStMan.SaveLayerState(sLyrStName,
LayerStateMasks.Color | LayerStateMasks.LineType,
ObjectId.Null);
}
}
/// <summary>
/// 重命名图层状态
/// </summary>
[CommandMethod("RenameLayerState")]
public static void RenameLayerState()
{
//获取当前文档
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
LayerStateManager acLyrStMan;
acLyrStMan = acDoc.Database.LayerStateManager;
string sLyrStName = "ColorLinetype";
string sLyrStNewName = "OldColorLinetype";
if (acLyrStMan.HasLayerState(sLyrStName) == true &&
acLyrStMan.HasLayerState(sLyrStNewName) == false)
{
acLyrStMan.RenameLayerState(sLyrStName,sLyrStNewName);
}
}
/// <summary>
/// 删除图层状态
/// </summary>
[CommandMethod("RemoveLayerState")]
public static void RemoveLayerState()
{
//获取当前文档
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
LayerStateManager acLyrStMan;
acLyrStMan = acDoc.Database.LayerStateManager;
string sLyrStName = "ColorLinetype";
if (acLyrStMan.HasLayerState(sLyrStName) == true)
{
acLyrStMan.DeleteLayerState(sLyrStName);
}
}
/// <summary>
/// 恢复图形中图层的颜色和线型设置
/// </summary>
[CommandMethod("RestoreLayerState")]
public static void RestoreLayerState()
{
//获取当前文档
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
LayerStateManager acLyrStMan;
acLyrStMan = acDoc.Database.LayerStateManager;
string sLyrStName = "ColorLinetype";
if (acLyrStMan.HasLayerState(sLyrStName) == true)
{
acLyrStMan.RestoreLayerState(sLyrStName,
ObjectId.Null, 1,
LayerStateMasks.Color | LayerStateMasks.LineType);
}
}
/// <summary>
/// 导出保存的图层设置
/// </summary>
[CommandMethod("ExportLayerState")]
public static void ExportLayerState()
{
//获取当前文档
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
LayerStateManager acLyrStMan;
acLyrStMan = acDoc.Database.LayerStateManager;
string sLyrStName = "ColorLinetype";
if (acLyrStMan.HasLayerState(sLyrStName) == true)
{
acLyrStMan.ExportLayerState(sLyrStName, "C:\\my documents\\" +
sLyrStName + ".las");
}
}
/// <summary>
/// 导入保存的图层设置
/// </summary>
[CommandMethod("ImportLayerState")]
public static void ImportLayerState()
{
//获取当前文档
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
LayerStateManager acLyrStMan;
acLyrStMan = acDoc.Database.LayerStateManager;
string sLyrStFileName = "c:\\my documents\\ColorLinetype.las";
if (System.IO.File.Exists(sLyrStFileName))
{
try
{
acLyrStMan.ImportLayerState(sLyrStFileName);
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
Autodesk.AutoCAD.ApplicationServices.Application.
ShowAlertDialog(ex.Message);
}
}
}
/// <summary>
/// 设置文字字体
/// </summary>
[CommandMethod("UpdateTextFont")]
public static void UpdateTextFont()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以写模式打开当前文字样式
TextStyleTableRecord acTextStyleTblRec;
acTextStyleTblRec = acTrans.GetObject(acCurDb.Textstyle,
OpenMode.ForWrite) as TextStyleTableRecord;
//获取当前字体设置
FontDescriptor acFont;
acFont = acTextStyleTblRec.Font;
//将文字样式中的字体改为“PlayBill”
FontDescriptor acNewFont;
acNewFont = new FontDescriptor("PlayBill",
acFont.Bold,
acFont.Italic,
acFont.CharacterSet,
acFont.PitchAndFamily);
acTextStyleTblRec.Font = acNewFont;
acDoc.Editor.Regen();
//保存修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 改变字体文件
/// </summary>
[CommandMethod("ChangeFontFiles")]
public static void ChangeFontFiles()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以写模式打开当前文字样式
TextStyleTableRecord acTextStyleTblRec;
acTextStyleTblRec = acTrans.GetObject(acCurDb.Textstyle,
OpenMode.ForWrite) as TextStyleTableRecord;
//改用bothBig字体文件和常规字体文件
acTextStyleTblRec.BigFontFileName = "C:/AutoCAD/Fonts/bigfont.shx";
acTextStyleTblRec.FileName = "C:/AutoCAD/Fonts/italic.shx";
//保存修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 创建倾斜文字
/// </summary>
[CommandMethod("ObliqueText")]
public static void ObliqueText()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建一个单行文字对象
DBText acText = new DBText();
acText.Position = new Point3d(3, 3, 0);
acText.Height = 0.5;
acText.TextString = "Hello,World!";
//修改文字对象的倾斜角度为45度(弧度值0.707)
acText.Oblique = 0.707;
acBlkTblRec.AppendEntity(acText);
acTrans.AddNewlyCreatedDBObject(acText, true);
//保存修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 反向显示对象
/// </summary>
[CommandMethod("BackwardsText")]
public static void BackwardsText()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建一个单行文字对象
DBText acText = new DBText();
acText.Position = new Point3d(3, 3, 0);
acText.Height = 0.5;
acText.TextString = "Hello, World.";
//反向显示文字
acText.IsMirroredInX = true;
acBlkTblRec.AppendEntity(acText);
acTrans.AddNewlyCreatedDBObject(acText, true);
//保存修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 创建单行文字
/// </summary>
[CommandMethod("CreateText")]
public static void CreateText()
{
//获取当前文档及数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建一个单行文字对象
DBText acText = new DBText();
acText.Position = new Point3d(2, 2, 0);
acText.Height = 0.5;
acText.TextString = "Hello,World.";
//添加到模型空间
acBlkTblRec.AppendEntity(acText);
acTrans.AddNewlyCreatedDBObject(acText, true);
//保存修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 重新调整文字
/// </summary>
[CommandMethod("TextAlignment")]
public static void TextAlignment()
{
//获取当前文档及数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
string[] textString = new string[3];
textString[0] = "Left";
textString[1] = "Center";
textString[2] = "Right";
int[] textAlign = new int[3];
textAlign[0] = (int)TextHorizontalMode.TextLeft;
textAlign[1] = (int)TextHorizontalMode.TextCenter;
textAlign[2] = (int)TextHorizontalMode.TextRight;
Point3d acPtIns = new Point3d(3, 3, 0);
Point3d acPtAlign = new Point3d(3, 3, 0);
int nCnt = 0;
foreach (string strVal in textString)
{
//创建一个单行文字对象
DBText acText = new DBText();
acText.Position = acPtIns;
acText.Height = 0.5;
acText.TextString = strVal;
//设置文字的排列模式
acText.HorizontalMode = (TextHorizontalMode)textAlign[nCnt];
if (acText.HorizontalMode != TextHorizontalMode.TextLeft)
{
acText.AlignmentPoint = acPtAlign;
}
acBlkTblRec.AppendEntity(acText);
acTrans.AddNewlyCreatedDBObject(acText, true);
//在文字的对齐点上创建一个点对象
DBPoint acPoint = new DBPoint(acPtAlign);
acPoint.ColorIndex = 1;//红色
acBlkTblRec.AppendEntity(acPoint);
acTrans.AddNewlyCreatedDBObject(acPoint, true);
//调整插入点和对齐点
acPtIns = new Point3d(acPtIns.X, acPtIns.Y + 3, 0);
acPtAlign = acPtIns;
nCnt = nCnt + 1;
}
//设置点的样式为十字
Autodesk.AutoCAD.ApplicationServices.Application.
SetSystemVariable("PDMODE", 2);
//保存修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 创建一个多行文字对象
/// </summary>
[CommandMethod("CreateMText")]
public static void CreateMText()
{
//获取当期文档及数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block块表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建多行文字对象
MText acMText = new MText();
acMText.Location = new Point3d(2, 2, 0);
//属性赋值
acMText.Width = 4;
acMText.Contents = "This is a text string for the MText object.";
acBlkTblRec.AppendEntity(acMText);
acTrans.AddNewlyCreatedDBObject(acMText, true);
//保存修改,关闭事务
acTrans.Commit();
}
}
/// <summary>
/// 用控制字符格式化文字
/// </summary>
[CommandMethod("FormatMText")]
public static void FormatMText()
{
//获取当前文档和数据库
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开Block表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//以写模式打开Block表记录Model空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//创建多行文字对象
MText acMText = new MText();
acMText.Location = new Point3d(2, 2, 0);
acMText.Width = 4.5;
acMText.Contents = "{{\\H1.5x;Bigtext}\\A2;over text\\A1;/\\A0;under text}";
acBlkTblRec.AppendEntity(acMText);
acTrans.AddNewlyCreatedDBObject(acMText, true);
//保存修改,关闭事务
acTrans.Commit();
}
}
#endregion