CAD开发实验(四)
实验三AutoCAD图形块的创建与应用
一、主要内容
1、创建块定义及插入块参照
2、创建带有属性的块定义及插入带有属性的块参照
3、在对话框中查看块定义的图标
4、在当前文件中插入外部文件中的块
5、在当前文件中插入其他DWG文件
二、学时安排:2学时
三、预习内容
理解块的含义,回顾块和带属性块的定义
四、AutoCAD图形块的创建与应用
4.1创建块定义及插入块参照
4.1.1创建块定义
代码:创建块
publicDocumentpDoc=
XXXXXXXXX;
[CommandMethod("CreateBlock")]
publicvoidCreateBlock()
{DatabaseacCurDb=XXX;
using(TransactionacTrans=XXX.StartTransaction()){BlockTableacBlkTbl=XXX(XXX,XXX)asBlockTable;创建新的块表记录
BlockTableRecordacBlkTblRec=newBlockTableRecord();stringblkname="三角点";
XXX;
XXX(0,0,0);
Point3dpCenter=newPoint3d(0,0,0);
创建中心点
DBPointpDBPt=newDBPoint(pCenter);
创建圆
CirclepCir=newCircle(pCenter,newVector3d(0,0,1),0.25);创建三角形
PolylinepTri=newPolyline(4);
XXX(0,newPoint2d(0,1),0,0,0);
XXX(1,newPoint2d(0.866,-0.5),0,0,0);XXX(2,newPoint2d(-0.866,-0.5),0,0,0);XXX(3,newPoint2d(0,1),0,0,0);
给定块的原点
XXX
(0,0,0);
将图形实体添加到块表记录
acBlkTbXXX(pDBPt);
acBlkTbXXX(pCir);
acBlkTbXXX(pTri);
将块记录添加到块表
XXX(acBlkTblRec);
XXX(acBlkTblRec,true);
提交修改并销毁事务
XXX();
4.1.2插入卡块参照
代码:插入块
[CommandMethod("InsertBlock")]
publicvoidInsertBlock()
DatabaseacCurDb=XXX;
启动一个事务Startatransaction
using(TransactionacTrans=acCurDXXX.StartTransaction()){
BlockTableacBlkTbl=XXX(
XXX,XXX)asBlockTable;
以写方式打开模型空间块表记录
BlockTableRecordacBlkTblRec=XXX(
acBlkTbl[XXX,XXX)asBlockTableRecord;
获取三角点块,并创建块引用
BlockTableRecordpBlockTableRecord=XXX(
acBlkTbl["三角点"],XXX)asBlockTableRecord;
BlockReferencebref=newBlockReference(
newPoint3d(10,10,0),XXX);
BlockReferencebref2=newBlockReference(
newPoint3d(30,30,0),XXX);
XXX(bref2);
XXX(bref2,true);XXX();
4.2创建带有属性的块定义及插入带有属性的块参照
4.2.1创建带有属性的块定义
→创建一个名为“三角点”的带属性块的记录,包括以下实体:
一个三角形
一个名为三角点的属性
代码:region创建带属性的块
[CommandMethod("CreateAtrrBlock")]
publicvoidCreateAtrrBlock()
{DatabaseacCurDb=XXX;
using(TransactionacTrans=XXX.StartTransaction()){BlockTableacBlkTbl=XXX(XXX,
XXX)asBlockTable;BlockTableRecordacBlkTblRec=newBlockTableRecord();
stringblkname="三角点";
XXX;
XXX(0,0,0);
Point3dpCenter=newPoint3d(0,0,0);
创建中心点
DBPointpDBPt=newDBPoint(pCenter);
创建圆
CirclepCir=newCircle(pCenter,newVector3d(0,0,1),0.25);创建三角形
PolylinepTri=newPolyline(4);
XXX(0,newPoint2d(0,1),0,0,0);
XXX(1,newPoint2d(0.866,-0.5),0,0,0);
XXX(2,newPoint2d(-0.866,-0.5),0,0,0);
XXX(3,newPoint2d(0,1),0,0,0);
PolylinepPl=newPolyline
(2);
XXXAt(0,newPoint2d(0.9,0),0,0,0);
XXX(1,newPoint2d(4.5,0),0,0,0);
定义属性
Point3dpPosition=newPoint3d(1,0.2,0);
AttributeDefinitionpAtrDef=newAttributeDefinition(
pPosition,"洼地","三角点名","输入三角点名",XXX);
XXX;
Point3dpPosition2=newPoint3d(1,-1.2,0);
AttributeDefinitionpHVaue=newAttributeDefinition(
pPosition2,"0.00","高程值","输入高程值",XXX);XXX;
给定块的原点
XXX(0,0,0);
将图形实体添加到块表记录
XXX(pDBPt);
XXX(pCir);
XXX(pTri);
XXX(pPl);
XXX(pAtrDef);
XXX(pHVaue);
将块记录添加到块表
XXX(acBlkTblRec);
XXX(acBlkTblRec,true);
CreateBlockIconcbi=newCreateBlockIcon(XXX);
XXX();
endregion
4.2.2插入带有属性的块参照
region插入带属性的块
[CommandMethod("InsertAtrrBlock")]
publicvoidInsertAtrrBlock()
DatabaseacCurDb=XXX;
using(TransactionacTrans=XXX.StartTransaction()){
BlockTableacBlkTbl=XXX(XXX,
XXX)asBlockTable;
BlockTableRecordacBlkTblRec=
acTranXXX(acBlkTbl[XXX,
XXX)asBlockTableRecord;BlockTableRecordpBlockTableRecord=acTranXXX(
acBlkTbl["三角点"],XXX)as
BlockTableRecord;
BlockReferencebref=newBlockReference(
newPoint3d(10,10,0),XXX);XXX(bref);
XXX(bref,true);
foreach(ObjectIdidinpBlockTableRecord)
Entityent=(Entity)XXX(id,XXX,false);打开当前的对象!
if(entisAttributeDefinition)
AttributeReferenceattRef=newAttributeReference();
AttributeDefinitionattDef=(AttributeDefinition)ent;
XXX(attDef);
XXX.Height;
XXX.Rotation;
XXX.Tag;
if(XXX"三角点名")
XXX(11.3,10.3,0);
XXXtString="洼地";
elseif(XXX"高程值")
XXX(11,8.8,0);
XXX"65.99";
if(!XXX)
XXX();
XXX.AppendAttribute(attRef);
通知事务处理添加新创建的属性参照
XXX(attRef,true);
XXX();
endregion
4.3在对话框中查看块定义的图标
代码:[CommandMethod("ViewBlock")]
publicvoidViewBlock()
{ViewBlockfrmpViewBlockfrm=newViewBlockfrm();XXX;
XXX();