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

2020-04-14 18:26

pBlockTableRecord.ObjectId); acBlkTblRec.AppendEntity(bref2);

acTrans.AddNewlyCreatedDBObject(bref2, true); acTrans.Commit(); } }

4.2创建带有属性的块定义及插入带有属性的块参照 4.2.1 创建带有属性的块定义

→创建一个名为“三角点”的带属性块的记录,包括以下实体:

? 一个点 ? 一个圆 ? 一个三角形

? 一个名为三角点的属性

代码:

#region 创建带属性的块

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

Database acCurDb = pDoc.Database;

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) {

BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite) as BlockTable;

BlockTableRecord acBlkTblRec = new BlockTableRecord(); string blkname = \三角点\; acBlkTblRec.Name = blkname;

acBlkTblRec.Origin = new Point3d(0, 0, 0); Point3d pCenter = new Point3d(0, 0, 0); //创建中心点

DBPoint pDBPt = new DBPoint(pCenter);

45

//创建圆

Circle pCir = new Circle(pCenter, new Vector3d(0, 0, 1), 0.25); //创建三角形

Polyline pTri = new Polyline(4);

pTri.AddVertexAt(0, new Point2d(0, 1), 0, 0, 0); pTri.AddVertexAt(1, new Point2d(0.866, -0.5), 0, 0, 0); pTri.AddVertexAt(2, new Point2d(-0.866, -0.5), 0, 0, 0); pTri.AddVertexAt(3, new Point2d(0, 1), 0, 0, 0); Polyline pPl = new Polyline(2);

pPl.AddVertexAt(0, new Point2d(0.9, 0), 0, 0, 0); pPl.AddVertexAt(1, new Point2d(4.5, 0), 0, 0, 0); //定义属性

Point3d pPosition = new Point3d(1, 0.2, 0);

AttributeDefinition pAtrDef = new AttributeDefinition(pPosition, \洼地\, \三角点名\, \输入三角点名\, acCurDb.Textstyle); pAtrDef.Height = 1;

Point3d pPosition2 = new Point3d(1, -1.2, 0);

AttributeDefinition pHVaue = new AttributeDefinition(pPosition2, \, \高程值\, \输入高程值\, acCurDb.Textstyle); pHVaue.Height = 1; //给定块的原点

acBlkTblRec.Origin = new Point3d(0, 0, 0); //将图形实体添加到块表记录 acBlkTblRec.AppendEntity(pDBPt); acBlkTblRec.AppendEntity(pCir); acBlkTblRec.AppendEntity(pTri); acBlkTblRec.AppendEntity(pPl); acBlkTblRec.AppendEntity(pAtrDef); acBlkTblRec.AppendEntity(pHVaue); //将块记录添加到块表 acBlkTbl.Add(acBlkTblRec);

acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);

// CreateBlockIcon cbi = new CreateBlockIcon(acBlkTblRec.Name); acTrans.Commit(); } }

#endregion

→使用AutoCAD插入块命令浏览及插入上述定义的块

46

4.2.2 插入带有属性的块参照

→打开块表BlockTable找到名为三角点的带属性的块表记录,创建块引用插入到当前活动块表记录中

代码:

#region 插入带属性的块

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

Database acCurDb = pDoc.Database;

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) {

BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord. ModelSpace],OpenMode.ForWrite) as BlockTableRecord; BlockTableRecord pBlockTableRecord = acTrans.GetObject( acBlkTbl[\三角点\], OpenMode.ForRead) as BlockTableRecord; BlockReference bref = new BlockReference(new Point3d(10, 10, 0), pBlockTableRecord.ObjectId); acBlkTblRec.AppendEntity(bref);

acTrans.AddNewlyCreatedDBObject(bref, true); foreach (ObjectId id in pBlockTableRecord) {

Entity ent = (Entity)acTrans.GetObject(id, OpenMode.ForRead, false); //打开当前的对象!

47

if (ent is AttributeDefinition) {

AttributeReference attRef = new AttributeReference(); AttributeDefinition attDef = (AttributeDefinition)ent; attRef.SetPropertiesFrom(attDef); attRef.Height = attDef.Height; attRef.Rotation = attDef.Rotation; attRef.Tag = attDef.Tag; if (attDef.Tag == \三角点名\) {

attRef.Position = new Point3d(11.3, 10.3, 0); attRef.TextString = \洼地\; }

else if (attDef.Tag == \高程值\) {

attRef.Position = new Point3d(11, 8.8, 0); attRef.TextString = \; }

if (!bref.IsWriteEnabled) {

bref.UpgradeOpen(); }

bref.AttributeCollection.AppendAttribute(attRef); //通知事务处理添加新创建的属性参照

acTrans.AddNewlyCreatedDBObject(attRef, true); } }

acTrans.Commit(); } }

#endregion

48

实验四 用户交互、选择集及扩展属性

一、主要内容

1、基于用户交互式的信息输入 2、创建选择集

3、给图形实体添加扩展属性

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

回顾用户交互命令的使用、选择集创建的几种方式,

四、实验步骤

引用:

using System;

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

using System.Threading.Tasks;

using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using System.IO;

[assembly: CommandClass(typeof(实验四.UserInteraction))] namespace 实验四 {

public class UserInteraction { } }

4.1 用户交互

代码:

49


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

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

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

马上注册会员

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