
目录
母亲节快乐~~~祝妈妈身体健康,心想事成~
一、封装事务处理函数之将一条直线添加到图形文件中
1、代码记录
2、代码讲解
二、封装事务处理函数之将多条直线添加到图形文件中
1、代码记录
2、代码讲解
三、封装事务处理函数之将一条直线添加到图形文件中
(一)、提供两个点坐标绘制直线
1、代码记录
2、代码讲解
(二)、给起点坐标、直线长度和该直线与X轴正方向的夹角绘制直线
1、代码记录
2、代码讲解
母亲节快乐~~~祝妈妈身体健康,心想事成~
复习一下昨天学到的声明直线,在cad图形文件中显示直线
盲敲一下,如果能盲敲出来说明昨天已经掌握了:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yztest3
{
public class Linetest
{
[CommandMethod("Linetest1")]
public void Linetest1()
{
//声明一条直线的两种方法
Line line1 = new Line();
Point3d start = new Point3d(0, 0, 0);
Point3d end = new Point3d(100, 100, 0);
line1.StartPoint = start;
line1.EndPoint = end;
Line line2 = new Line(new Point3d(100, 100, 0), new Point3d(200, 200, 0));
//声明图形数据库对象
Document doc = Application.DocumentManager.MdiActiveDocument;
Database da = doc.Database;
//开始事务处理
using (Transaction tt = da.TransactionManager.StartTransaction())
{
//声明块表
BlockTable bb = (BlockTable)tt.GetObject(da.BlockTableId, OpenMode.ForRead);
//声明块表记录
BlockTableRecord bbr = (BlockTableRecord)tt.GetObject(bb[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
//往块表记录添加直线
bbr.AppendEntity(line1);
bbr.AppendEntity(line2);
//更新
tt.AddNewlyCreatedDBObject(line1, true);
tt.AddNewlyCreatedDBObject(line2, true);
//提交事务
tt.Commit();
}
}
}
}
一、封装事务处理函数之将一条直线添加到图形文件中
今天学到了封装事务处理函数及绘制直线的函数,开始上函数了:
首先添加一个类,就叫AddEntityTool.cs
新添了一个小技巧,#region……#endregion中间的内容可以折叠起来
下面展示的代码分别存储在两个文件中,一个是创建的Linetest.cs文件,一个是AddEntityTool.cs类文件
1、代码记录AddEntityTool.cs代码如下:
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yztest3
{
public static partial class AddEntityTool//让这个类成为一个部分类
{
///
/// 将图形对象添加到图形文件中
///
/// 图形数据库
/// 图形对象
/// 图形的ObjectId
public static ObjectId AddEntityToModelSpace(this Database db, Entity ent)
{
//声明ObjectId,用于返回
ObjectId entId = ObjectId.Null;
//既然是把图形添加到cad文件中,肯定要提供对象
//line继承于Entity,那直接封装父类,以后直线啊圆弧啊都可以用
//开启事务处理 using
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;
}
}
}
Linetest.cs代码如下:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yztest3
{
public class Linetest
{
[CommandMethod("Linetest1")]
public void Linetest1()
{
Database db = HostApplicationServices.WorkingDatabase;
Line line1 = new Line(new Point3d(100, 100, 0), new Point3d(200, 100, 0));
db.AddEntityToModelSpace(line1);
}
}
}
2、代码讲解
public partial class AddEntityTool —— 部分类
public AddEntityToModelSpace(Database db, Entity ent) —— 封装函数需要返回值,不知道的时候使用void,可以后期再改,于是创建了 AddEntityToModelSpace()封装函数,函数需要传值,既然是把直线传递到图形文件中,所以就要把直线这个对象先传给函数,那么在代码里要传什么呢,Line?前两天查看继承关系的时候发现Entity和Line是继承关系,那就可以把Entity传给函数,这样Entity里面的直线啊弧线啊圆啊之后都可以使用。还需要一个参数,要向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);
//添加图形到块表记录
btr.AppendEntity(ent);
//更新数据
trans.AddNewlyCreatedDBObject(ent, true);
//提交事务
trans.Commit();
}
AppendEntity 返回的是 ObjectId,这里存储着图形,以后如果需要对图形进行修改颜色、更改起点终点等 *** 作都需要对ObjectId里面的图形进行 *** 作,所以把返回的 ObjectId 号返回出来。所以在前面需要添加声明ObjectId,刚开始直接声明为空Null,然后令AppendEntity返回值赋值给声明的ObjectId,这样以后可以直接对这个图形进行GetObject等 *** 作。最后不要忘了添加return,函数返回值确定了也要把void改成返回类型ObjectId。
Database db = HostApplicationServices.WorkingDatabase; —— 可以更快捷的进入当前使用图形数据库
三种封装函数使用方法:
(1)前面封装函数不改,在Linetest.cs文件里声明一个直线后添加如下代码:
AddEntityTool add=new AddEntityTool();
add.AddEntityModelSpace(db,line1);
//line1就是声明的直线
(2)public static ObjectId AddEntityToModelSpace(Database db, Entity ent) —— 使用静态 static
在Linetest.cs文件里声明一个直线后添加如下代码:
AddEntityTool.AddEntityToModelSpace(db,ent);
(3) public static partial class AddEntityTool —— 使用静态 static
public static ObjectId AddEntityToModelSpace(this Database db, Entity ent) —— 添加 this
在Linetest.cs文件里声明一个直线后添加如下代码:
db.AddEntityToModelSpace(line1);
二、封装事务处理函数之将多条直线添加到图形文件中 1、代码记录
AddEntityTool.cs代码如下:
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yztest3
{
public static partial class AddEntityTool//让这个类成为一个部分类
{
///
/// 将图形对象添加到图形文件中
///
/// 图形数据库
/// 图形对象,可变参数
/// 图形的ObjectId,数组返回
//一次加多个
public static ObjectId[] AddEntityToModelSpace(this Database db, params Entity[] ent)
{
//声明ObjectId,用于返回
ObjectId[] entId = new ObjectId[ent.Length];
//开启事务处理 using
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++)
{
//添加图形到块表记录
entId[i] = btr.AppendEntity(ent[i]);
//更新数据
trans.AddNewlyCreatedDBObject(ent[i], true);
}
//提交事务
trans.Commit();
}
return entId;
}
}
}
Linetest.cs
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yztest3
{
public class Linetest
{
[CommandMethod("Linetest1")]
public void Linetest1()
{
Database db = HostApplicationServices.WorkingDatabase;
Line line1 = new Line(new Point3d(100, 100, 0), new Point3d(200, 100, 0));
Line line2 = new Line(new Point3d(200, 100, 0), new Point3d(200, 200, 0));
Line line3 = new Line(new Point3d(200, 200, 0), new Point3d(100, 200, 0));
Line line4 = new Line(new Point3d(100, 200, 0), new Point3d(100, 100, 0));
db.AddEntityToModelSpace(line1, line2, line3, line4);
}
}
}
2、代码讲解
封装函数Entity参数需要修改成数组形式:Entity[ ] ,因为直线对象不止一条了,可以使用 params 可变参数
如果整体都用for循环进行,会出现一个问题,一条直线就需要开一次事务处理,浪费资源,可以从添加图形到块表记录开始进行for循环,循环的范围我们自己讲就是直线的条数,但在代码里就是指 ent.length Entity[]数组的长度。
注意需要更改的不光是循环里的 ent [ i ] ,还包括返回项 entId [ i ] ,所以还需要设置一个 ObjectId [ ] 数组,也就是说有多少个图元同时也会返回多少个图元对象的Id值。
Linetest.cs中 db.AddEntityToModelSpace(line1, line2, line3, line4); 可以直接选择几条直线
三、封装事务处理函数之将一条直线添加到图形文件中 (一)、提供两个点坐标绘制直线 1、代码记录
AddEntityTool.cs代码如下:
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yztest3
{
public static partial class AddEntityTool//让这个类成为一个部分类
{
///
/// 绘制直线
///
/// 图形数据库
/// 起点坐标
/// 终点坐标
/// ObjectId
public static ObjectId AddLineToModelSpace(this Database db,Point3d startPoint,Point3d endPoint)
{
return db.AddEntityToModelSpace(new Line(startPoint, endPoint));
}
}
}
Linetest.cs
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yztest3
{
public class Linetest
{
[CommandMethod("Linetest1")]
public void Linetest1()
{
Database db = HostApplicationServices.WorkingDatabase;
db.AddLineToModelSpace(new Point3d(100, 100, 0), new Point3d(200, 100, 0));
}
}
}
2、代码讲解
直接通过两点坐标制作出一条直线,
设置三个函数参数:图形数据库、起点坐标、终点坐标——this Database db,Point3d startPoint,Point3d endPoint
声明一条直线,然后调用AddEntityToModelSpace函数
Line line1=new Line(startPoint,endPoint);
db.AddEntityToModelSpace(new Line(startPoint,endPoint));
于是发现可以去掉声明直接留取第二句
db.AddEntityToModelSpace(new Line(startPoint,endPoint));
要返回 ObjectId 直接添加 return
return db.AddEntityToModelSpace(new Line(startPoint,endPoint));
(二)、给起点坐标、直线长度和该直线与X轴正方向的夹角绘制直线
1、代码记录
这里涉及到弧度角和角度角相转化的封装函数,再创建一个新的类,叫做BaseTool.cs
BaseTool.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yztest3
{
public static partial class BaseTool
{
///
/// 角度转换弧度
///
/// 角度值
/// 弧度
public static double DegreeToAngle(this Double degree)
{
return degree * Math.PI / 180;
}
///
/// 弧度转换角度
///
/// 弧度值
/// 角度
public static double AngleToDegree(this Double angle)
{
return angle * 180 / Math.PI;
}
}
}
AddEntityTool.cs代码如下:
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yztest3
{
public static partial class AddEntityTool//让这个类成为一个部分类
{
///
/// 绘制直线
///
/// 图形数据库
/// 起点坐标
/// 直线长度
/// 与X轴正方向的夹角
/// ObjectId
public static ObjectId AddLineToModelSpace(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.AddEntityToModelSpace(new Line(startPoint, endPoint));
}
}
}
Linetest.cs
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yztest3
{
public class Linetest
{
[CommandMethod("Linetest1")]
public void Linetest1()
{
Database db = HostApplicationServices.WorkingDatabase;
db.AddLineToModelSpace(new Point3d(200, 200, 0), 200, 60);
}
}
}
2、代码讲解
弧度角度转换看提示即可
知道起点坐标,直线长度,与x轴夹角,就可以计算出终点坐标
终点的x坐标=起点x坐标+长度*cos(弧度角);
终点的y坐标=起点y坐标+长度*sin(弧度角);
需要四个参数:图形数据库、起点坐标、长度、角度 —— this Database db,Point3d startPoint,Double length,Double degree
Linetest.cs里写入起点坐标、长度、角度,再运行一下就可以实现了,还可以在cad测一下角度,验证一下。
总结一下吧,其实封装这在C++就学过了些许皮毛,但是时间隔得太久了,突然看代码还是很吃力,所以能不复制粘贴尽量自己打代码,有时间还是得回顾一下封装部分,已经三天了还算是开了一个好一点的头,明天就没那么多活可以多学一点了,我也是一点点把思路感觉不太顺的地方都记下来了,如果以后有实在不懂的地方还是得回头好好琢磨一下,今天就到这里啦,明天继续~
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)