1. 单行文字的添加
第一步: 首先在CAD中新增中文样式
输入ST命令:
第二步:代码开发
/// <summary>
/// 添加文本信息
/// </summary>
[CommandMethod("AddText")]
public void AddText()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var docDatabase = doc.Database;
using (var trans = docDatabase.TransactionManager.StartTransaction())
{
//打开块表
TextStyleTable textStyleTable = (TextStyleTable)trans.GetObject(docDatabase.TextStyleTableId, OpenMode.ForRead);
var objectId = textStyleTable["chinese"];
DBText text = new DBText
{
TextString = "wuk测试添加单行文本",
//文本坐标
Position = new Point3d(500,500,0),
Height = 500,
//宽度因子
WidthFactor = 0.8,
TextStyleId = objectId
};
docDatabase.AddEnityToModelSpace(text);
trans.Commit();
}
}
效果如下:
2. 修改和克隆图形
/// <summary>
/// 添加文本信息
/// </summary>
[CommandMethod("EditEntity")]
public void EditEntity()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var docDatabase = doc.Database;
var selectEntity = Selected.selectEntities()[0];
if (selectEntity is Circle)
{
using (var trans = docDatabase.TransactionManager.StartTransaction())
{
//修改对象
Circle circle = (Circle)selectEntity.ObjectId.GetObject(OpenMode.ForWrite);
circle.Radius = 600;
circle.Color = Color.FromRgb(255, 0, 0);
trans.Commit();
}
}
// 克隆对象,原对象不变
if (selectEntity is Circle myCircle)
{
var circle = myCircle.Clone() as Circle;
if (circle != null)
{
circle.Radius = 500;
circle.Color = Color.FromRgb(255, 0, 0);
//修改坐标
circle.Center = new Point3d(100, 100, 100);
docDatabase.AddEnityToModelSpace(circle);
}
}
}
图示结果:
3. 图形形变
/// <summary>
/// 图形形变
/// </summary>
[CommandMethod("EditEntityTest")]
public void EditEntityTest()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var docDatabase = doc.Database;
var selectEntity = Selected.selectEntities()[0];
using (var trans = docDatabase.TransactionManager.StartTransaction())
{
//修改对象
Entity entity = (Entity)selectEntity.ObjectId.GetObject(OpenMode.ForWrite);
Matrix3d matrix3d;
//平移
// matrix3d = Matrix3d.Displacement(new Vector3d(200,200,0));
// entity.TransformBy(matrix3d);
// //旋转 参数1:旋转角度 2:旋转轴心(负则反方向顺时针) 3:旋转中心
// matrix3d = Matrix3d.Rotation(Math.PI/6,Vector3d.ZAxis,Point3d.Origin);
// //缩放 参数1:缩放比例 2:缩放中心
// matrix3d = Matrix3d.Scaling(2,new Point3d(50,50,0));
// 镜像 两种方式
matrix3d = Matrix3d.Mirroring(new Plane(Point3d.Origin,new Point3d(0,1,0),new Point3d(0,0,1)));
matrix3d = Matrix3d.Mirroring(new Line3d(Point3d.Origin, new Point3d(0, 1, 0)));
entity.TransformBy(matrix3d);
trans.Commit();
}
}
旋转
4. 块参照
/// <summary>
/// 图形形变
/// </summary>
[CommandMethod("EditEntityTest1")]
public void EditEntityTest1()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var docDatabase = doc.Database;
var block = Selected.selectEntities()[0];
if (block is BlockReference blockReference)
{
using (var trans = docDatabase.TransactionManager.StartTransaction())
{
// String name = blockReference.BlockName;
// var ro = blockReference.Rotation;
// var factors = blockReference.ScaleFactors;
// var position = blockReference.Position;
//修改块参照数据
BlockReference blockR = (BlockReference)block.ObjectId.GetObject(OpenMode.ForWrite);
blockR.Rotation = Math.PI / 6;
blockR.ScaleFactors = new Scale3d(2, 2, 2);
trans.Commit();
}
}
}