CAD二次开发实验报告(6)

2020-04-14 18:26

实验二 AutoCAD基本图形对象创建与编辑

一、主要内容

1. 创建直线 2. 创建圆 3. 创建圆弧 4. 创建多段线 5. 创建椭圆 6. 样条曲线 7. 创建面域 8. 创建文字 9. 创建填充 10. 创建尺寸标注 11. 创建三维实体

二、学时安排:2学时 三、预习内容

回顾AutoCAD图形对象创建的一般过程。四、基本图形对象创建与编辑

基本图形对象创建:添加引用

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

using System.Threading.Tasks; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.EditorInput; using System.Windows;

20

4.1 创建直线对象

① 创建直线并返回新建直线对象标识

代码:[CommandMethod(\)]

public void CreateLine() {

using (Transaction trans = db.TransactionManager.StartTransaction()) {

//创建直线

Point3d pt1 = new Point3d(20, 20, 0); Point3d pt2 = new Point3d(100, 100, 0); Line lineEnt = new Line(pt1, pt2); lineEnt.ColorIndex = 1; //获得块表

BlockTable bt = (BlockTable)trans.GetObject( db.BlockTableId, OpenMode.ForRead); //获得特定块表记录

BlockTableRecord btr = (BlockTableRecord)trans.GetObject( bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); //把图形对象的记录加入到块表记录

pModifyEntID = btr.AppendEntity(lineEnt); //将直线添加到事务处理中

trans.AddNewlyCreatedDBObject(lineEnt, true); trans.Commit(); }

}

② 比较Autodesk.AutoCAD.DatabaseServices.Line、

Autodesk.AutoCAD.Geometry.Line2d和Autodesk.AutoCAD.Geometry.Line3d

③ 修改新建直线对象

代码:

[CommandMethod(\)] public void ModifyLine() {

21

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); foreach (ObjectId pEntId in btr) {

//打开要修改的图形对象

Entity pEnt = (Entity)trans.GetObject(pEntId, OpenMode.ForWrite); if (pEnt.Layer == \房屋\) //修改颜色

pEnt.ColorIndex = 1; pEnt.Close(); }

trans.Commit(); }

}

4.2 创建圆

使用Autodesk.AutoCAD.DatabaseServices.Circle类创建圆

代码:

[CommandMethod(\)] public void Circle() {

Circle c = new Circle();

c.Center = new Point3d(0, 0, 0); c.Radius = 100;

Database db = HostApplicationServices.WorkingDatabase;

using (Transaction trans = db.TransactionManager.StartTransaction()) {

//创建块表

BlockTable c_block = (BlockTable)trans.GetObject( db.BlockTableId, OpenMode.ForRead);//读取,0

BlockTableRecord c_block_record = (BlockTableRecord)trans.GetObject( c_block[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

c_block_record.AppendEntity(c);//块表记录追加实体,把图形对象的记录加入到块表记录 trans.AddNewlyCreatedDBObject(c, true);//将圆添加到事务处理中 trans.Commit();//提交事务 }

}

22

4.3 创建圆弧

→使用Autodesk.AutoCAD.DatabaseServices.Arc类创建一圆弧

代码:

[CommandMethod(\)]//创建圆弧 public void CreatArc() {

Database db = HostApplicationServices.WorkingDatabase;

using (Transaction trans = db.TransactionManager.StartTransaction()) {

Arc pArc = new Arc(new Point3d(50, 50, 0), 100, 0, 100); BlockTable bt = (BlockTable)trans.GetObject( db.BlockTableId, OpenMode.ForRead);

BlockTableRecord btr = (BlockTableRecord)trans.GetObject( bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); btr.AppendEntity(pArc);

trans.AddNewlyCreatedDBObject(pArc, true); trans.Commit(); }

}

→给定圆弧外一点,计算该点到圆弧的切线,具体操作如下: ①将圆弧转成Autodesk.AutoCAD.Geometry.CircularArc2d

23

②使用CircularArc2d 的GetTangent(Autodesk.AutoCAD.Geometry.Point2d point)方法计算切线,该方法返回结果为Autodesk.AutoCAD.Geometry.Line2d类的实例对象

③将Line2d实例对象转换成Autodesk.AutoCAD.DatabaseServices.Line 4.4 创建多段线 1)创建Polyline

①创建一条具有5个顶点的、且带有2-3个凸度的多段线 ②遍历多段线的所有片段Segment,并判断每个片段类型

③将Polyline转换成Polyline2d并分别执行CurveFit和SplineFit操作 2)创建Polyline2d

①分别创建CubicSplinePoly(三次拟合)、QuadSplinePoly(四次拟合)、FitCurvePoly(曲线拟合)和SimplePoly(无拟合)四种类型的Polyline2d,顶点个数为5,并比较它们之间的差异

②分别遍历四种类型Polyline2d的所有片段,并判断每个片段类型,能读取所有片段吗?为什么?

③把Polyline2d转换成Polyline,行吗? 3)创建Polyline3d

代码:

[CommandMethod(\)]//创建多段线 public void createPloyline1() {

Polyline pl = new Polyline(5);

Database db = HostApplicationServices.WorkingDatabase;

using (Transaction trans = db.TransactionManager.StartTransaction()) {

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);

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++) {

24


CAD二次开发实验报告(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:(全国通用版)2019版高考数学一轮复习 第二单元 函数的概念及其

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: