AutoCAD二次开发实验二
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Ellipse pEllipse = new Ellipse(new Point3d(150, 150, 0), new Vector3d(0, 0, 50), new Vector3d(50, 50, 0), 0.8, 0, 0);
//获得特定块表记录
BlockTableRecordpBlkTblRec = (BlockTableRecord)trans.GetObject(pBlkTbl[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
//将图形对象的记录加到块表记录中
pBlkTblRec.AppendEntity(pArc);
btr.AppendEntity(pEllipse);
trans.AddNewlyCreatedDBObject(pEllipse, true);
trans.Commit();
}
}
6,创建样条曲线
代码如下:
//创建样条曲线
[CommandMethod("CreatSpline")]
public void CreateSpline()
{
//创建直线
Point3d pt1 = new Point3d(2000, 2000, 0);
Point3d pt2 = new Point3d(1000, 1000, 0);
Line lineEnt = new Line(pt1, pt2);
//获得块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
curveSegments.Add(entity);
regions =Region.CreateFromCurves(curveSegments);
RegionnewRegion = (Region)regions[0];
newRegion.SetDatabaseDefaults();
BlockTablebt = (BlockTable)Trans.GetObject(db.BlockTableId,OpenMode.ForRead);
{
Point2d p1 = new Point2d(50, 10);
Point2d p2 = new Point2d(100, 30);
Point2d p3 = new Point2d(150, 60);
Point2d p4 = new Point2d(200, 90);
Point2d p5 = new Point2d(250, 120);
实验二AutoCAD基本图形对象创建与编辑
1,创建直线对象
代码如下:
[CommandMethod("CreatLine")]
public void creatline()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
//修改颜色
pEnt.ColorIndex = 5;
pEnt.Close();
trans.Commit();
}
}
2,创建圆对象
代码如下:[CommandMethod("CreatCir")]
publicvoidCircle()
{
//声明圆对象
Circlec =newCircle();
c.Center =newPoint3d(0, 0, 0);
{
Point3dcenter =newPoint3d(50, 50, 0);
Circleentity =newCircle(center,Vector3d.ZAxis, 150);
DBObjectCollectioncurveSegments =newDBObjectCollection();
DBObjectCollectionregions =newDBObjectCollection();
using(Transactiontrans = db.TransactionManager.StartTransaction())
{
//创建圆弧,指定圆心,半径,起始角,终止角
ArcpArc =newArc(newPoint3d(100,100,0),3000,0,30);
//获得块表
BlockTablepBlkTbl = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForRead);
//获得特定块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
//把图形对象的记录加入到块表中
pModifyEntID = btr.AppendEntity(lineEnt);
trans.AddNewlyCreatedDBObject(c,true);//将圆添加到事务处理中
trans.Commit();//提交事务
}
}
3,创建圆弧对象
代码如下:
//创建圆弧
[CommandMethod("CreatArc")]
publicvoidCreatArc()
{
Databasedb =HostApplicationServices.WorkingDatabase;
using(Transactiontrans = db.TransactionManager.StartTransaction())
{
//打开要修改的图形对象,定义一个实体对象
EntitypEnt = (Entity)trans.GetObject(pModifyEntID,OpenMode.ForWrite);
Point2dCollection pc = new Point2dCollection();
pc.Add(p1);
pc.Add(p2);
pc.Add(p3);
pc.Add(p4);
pc.Add(p5);
for (int i = 0; i < 5; i++)
{
pl.AddVertexAt(i, pc[i], 0.5, 5, 5);
//将直线添加到事务处理中
trans.AddNewlyCreatedDBObject(lineEnt, true);
trans.Commit();
}
}
运行效果如下图所示
修改直线
代码如下:
[CommandMethod("ModifyLine")]
publicvoidModifyLine()
{
Databasedb =HostApplicationServices.WorkingDatabase;
//将圆弧添加到事务处理中
trans.AddNewlyCreatedDBObject(pArc,true);
trans.Commit();
}
}
4,创建多段线:
代码如下:
//创建多段线
[CommandMethod("CreatPloyline")]
public void createPloyline1()
{
Polyline pl = new Polyline(5);
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btr.AppendEntity(spline);
trans.AddNewlyCreatedDBObject(spline, true);
c.Radius = 100;
Databasedb =HostApplicationServices.WorkingDatabase;
//启动事务
using(Transactiontrans = db.TransactionManager.StartTransaction())
{
//创建块表
BlockTablec_block = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForRead);//读取,0
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Point3d p1 = new Point3d(50, 30, 0);
BlockTableRecordc_block_record = (BlockTableRecord)trans.GetObject(c_block[BlockTableRecord.ModelSpace],OpenMode.ForWrite);//写入,1
c_block_record.AppendEntity(c);//块表记录追加实体,把图形对象的记录加入到块表记录
trans.Commit();
}
}
7,创建面域
代码如下:
//创建面域°
[CommandMethod("Polygon")]
publicvoidCreatePolygon()
{
Databasedb =HostApplicationServices.WorkingDatabase;
using(TransactionTrans = db.TransactionManager.StartTransaction())
Point3d p2 = new Point3d(100, 60, 0);
Point3d p3 = new Point3d(150, 520, 0);
Point3d来自百度文库p4 = new Point3d(200, 240, 0);
Point3d p5 = new Point3d(250, 300, 0);
}
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
Point3dCollection pc = new Point3dCollection();
pc.Add(p1);
pc.Add(p2);
pc.Add(p3);
pc.Add(p4);
pc.Add(p5);
Spline spline = new Spline(pc, 2, 5);
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
btr.AppendEntity(pl);
trans.AddNewlyCreatedDBObject(pl, true);
trans.Commit();
}
}
5,创建椭圆
代码如下:
//创建椭圆
[CommandMethod("CEllipse")]
public void Ellipse()
{
Database db = HostApplicationServices.WorkingDatabase;
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Ellipse pEllipse = new Ellipse(new Point3d(150, 150, 0), new Vector3d(0, 0, 50), new Vector3d(50, 50, 0), 0.8, 0, 0);
//获得特定块表记录
BlockTableRecordpBlkTblRec = (BlockTableRecord)trans.GetObject(pBlkTbl[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
//将图形对象的记录加到块表记录中
pBlkTblRec.AppendEntity(pArc);
btr.AppendEntity(pEllipse);
trans.AddNewlyCreatedDBObject(pEllipse, true);
trans.Commit();
}
}
6,创建样条曲线
代码如下:
//创建样条曲线
[CommandMethod("CreatSpline")]
public void CreateSpline()
{
//创建直线
Point3d pt1 = new Point3d(2000, 2000, 0);
Point3d pt2 = new Point3d(1000, 1000, 0);
Line lineEnt = new Line(pt1, pt2);
//获得块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
curveSegments.Add(entity);
regions =Region.CreateFromCurves(curveSegments);
RegionnewRegion = (Region)regions[0];
newRegion.SetDatabaseDefaults();
BlockTablebt = (BlockTable)Trans.GetObject(db.BlockTableId,OpenMode.ForRead);
{
Point2d p1 = new Point2d(50, 10);
Point2d p2 = new Point2d(100, 30);
Point2d p3 = new Point2d(150, 60);
Point2d p4 = new Point2d(200, 90);
Point2d p5 = new Point2d(250, 120);
实验二AutoCAD基本图形对象创建与编辑
1,创建直线对象
代码如下:
[CommandMethod("CreatLine")]
public void creatline()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
//修改颜色
pEnt.ColorIndex = 5;
pEnt.Close();
trans.Commit();
}
}
2,创建圆对象
代码如下:[CommandMethod("CreatCir")]
publicvoidCircle()
{
//声明圆对象
Circlec =newCircle();
c.Center =newPoint3d(0, 0, 0);
{
Point3dcenter =newPoint3d(50, 50, 0);
Circleentity =newCircle(center,Vector3d.ZAxis, 150);
DBObjectCollectioncurveSegments =newDBObjectCollection();
DBObjectCollectionregions =newDBObjectCollection();
using(Transactiontrans = db.TransactionManager.StartTransaction())
{
//创建圆弧,指定圆心,半径,起始角,终止角
ArcpArc =newArc(newPoint3d(100,100,0),3000,0,30);
//获得块表
BlockTablepBlkTbl = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForRead);
//获得特定块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
//把图形对象的记录加入到块表中
pModifyEntID = btr.AppendEntity(lineEnt);
trans.AddNewlyCreatedDBObject(c,true);//将圆添加到事务处理中
trans.Commit();//提交事务
}
}
3,创建圆弧对象
代码如下:
//创建圆弧
[CommandMethod("CreatArc")]
publicvoidCreatArc()
{
Databasedb =HostApplicationServices.WorkingDatabase;
using(Transactiontrans = db.TransactionManager.StartTransaction())
{
//打开要修改的图形对象,定义一个实体对象
EntitypEnt = (Entity)trans.GetObject(pModifyEntID,OpenMode.ForWrite);
Point2dCollection pc = new Point2dCollection();
pc.Add(p1);
pc.Add(p2);
pc.Add(p3);
pc.Add(p4);
pc.Add(p5);
for (int i = 0; i < 5; i++)
{
pl.AddVertexAt(i, pc[i], 0.5, 5, 5);
//将直线添加到事务处理中
trans.AddNewlyCreatedDBObject(lineEnt, true);
trans.Commit();
}
}
运行效果如下图所示
修改直线
代码如下:
[CommandMethod("ModifyLine")]
publicvoidModifyLine()
{
Databasedb =HostApplicationServices.WorkingDatabase;
//将圆弧添加到事务处理中
trans.AddNewlyCreatedDBObject(pArc,true);
trans.Commit();
}
}
4,创建多段线:
代码如下:
//创建多段线
[CommandMethod("CreatPloyline")]
public void createPloyline1()
{
Polyline pl = new Polyline(5);
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btr.AppendEntity(spline);
trans.AddNewlyCreatedDBObject(spline, true);
c.Radius = 100;
Databasedb =HostApplicationServices.WorkingDatabase;
//启动事务
using(Transactiontrans = db.TransactionManager.StartTransaction())
{
//创建块表
BlockTablec_block = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForRead);//读取,0
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Point3d p1 = new Point3d(50, 30, 0);
BlockTableRecordc_block_record = (BlockTableRecord)trans.GetObject(c_block[BlockTableRecord.ModelSpace],OpenMode.ForWrite);//写入,1
c_block_record.AppendEntity(c);//块表记录追加实体,把图形对象的记录加入到块表记录
trans.Commit();
}
}
7,创建面域
代码如下:
//创建面域°
[CommandMethod("Polygon")]
publicvoidCreatePolygon()
{
Databasedb =HostApplicationServices.WorkingDatabase;
using(TransactionTrans = db.TransactionManager.StartTransaction())
Point3d p2 = new Point3d(100, 60, 0);
Point3d p3 = new Point3d(150, 520, 0);
Point3d来自百度文库p4 = new Point3d(200, 240, 0);
Point3d p5 = new Point3d(250, 300, 0);
}
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
Point3dCollection pc = new Point3dCollection();
pc.Add(p1);
pc.Add(p2);
pc.Add(p3);
pc.Add(p4);
pc.Add(p5);
Spline spline = new Spline(pc, 2, 5);
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
btr.AppendEntity(pl);
trans.AddNewlyCreatedDBObject(pl, true);
trans.Commit();
}
}
5,创建椭圆
代码如下:
//创建椭圆
[CommandMethod("CEllipse")]
public void Ellipse()
{
Database db = HostApplicationServices.WorkingDatabase;