cad二次开发(块表的创建) (1)

2019-03-22 11:29

实验三、块表的创建

一. 实验目的:

通过本次掌握块表的基本创建方法

二. 实验内容:

1、创建块表的定义及块表参照的插入

2、创建带有属性的块表及其带有属性块表的插入 3、在对话框中查看块定义的图标 4、 在当前文件中插入外部文件的块 5、在当前文件中插入其他的DWG文件

三. 实验步骤:

1、创建块表

代码:

public class Block {

public Document document =

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Transaction transaction; Database db; BlockTable blocktable; Editor ed =

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

public void StartTransaction() { db = document.Database;

transaction = db.TransactionManager.StartTransaction(); }

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

StartTransaction(); try {

blocktable = (BlockTable)transaction.GetObject(db.BlockTableId, OpenMode.ForWrite);

BlockTableRecord blocktableRecord = new BlockTableRecord(); string blockName = \我的块表\; blocktableRecord.Name = blockName;

blocktableRecord.Origin = new Point3d(0, 0, 0);

Point3d center = new Point3d(0, 0, 0);

DBPoint dbpoint = new DBPoint(center);

Circle circle = new Circle(center, new Vector3d(0, 0, 1), 0.25); Polyline triangle = new Polyline(4); triangle.AddVertexAt(0, new Point2d(0, 1), 0, 0, 0); triangle.AddVertexAt(1, new Point2d(0.866, -0.5), 0, 0, 0); triangle.AddVertexAt(2, new Point2d(-0.866, -0.5), 0, 0, 0); triangle.AddVertexAt(3, new Point2d(0, 1), 0, 0, 0); blocktableRecord.Origin = new Point3d(0, 0, 0); blocktableRecord.AppendEntity(dbpoint); blocktableRecord.AppendEntity(circle); blocktableRecord.AppendEntity(triangle); blocktable.Add(blocktableRecord);

transaction.AddNewlyCreatedDBObject(blocktableRecord, true); CreateBlockIcon createBlockIcon = new CreateBlockIcon(blockName); transaction.Commit(); }

2、创建块表参照

代码:

[CommandMethod(\)]

public void InsertBlock() {

StartTransaction(); BlockTable blocktable =

(BlockTable)transaction.GetObject(db.BlockTableId, OpenMode.ForWrite); BlockTableRecord blockTblRec =

(BlockTableRecord)transaction.GetObject(blocktable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

BlockTableRecord pBlockTblRec = transaction.GetObject(blocktable[\我的块表\], OpenMode.ForRead) as BlockTableRecord;

BlockReference blocReference = new BlockReference(new Point3d(10, 10, 0), pBlockTblRec.ObjectId);

blockTblRec.AppendEntity(blocReference);

transaction.AddNewlyCreatedDBObject(blocReference, true); transaction.Commit();

}

3、创建带属性的块表

代码:

[CommandMethod(\)]

public void CreateAttributeBlock() {

StartTransaction();

blocktable = (BlockTable)transaction.GetObject(db.BlockTableId, OpenMode.ForWrite);

BlockTableRecord blocktablerec = new BlockTableRecord(); string blockName = \我的块表\; blocktablerec.Name = blockName;

blocktablerec.Origin = new Point3d(0, 0, 0); Point3d center = new Point3d(0, 0, 0); DBPoint dbpoint = new DBPoint(center);

Circle circle = new Circle(center, new Vector3d(0, 0, 1), 0.25); Polyline triangle = new Polyline(4);

triangle.AddVertexAt(0, new Point2d(0, 1), 0, 0, 0); triangle.AddVertexAt(1, new Point2d(0.866, -0.5), 0, 0, 0); triangle.AddVertexAt(2, new Point2d(-0.866, -0.5), 0, 0, 0); triangle.AddVertexAt(3, new Point2d(0, 1), 0, 0, 0); Point3d pPosition = new Point3d(1, 1, 0);

AttributeDefinition AttributeDef = new AttributeDefinition(pPosition, \坡头\, \我的块表名\, \输入我的块表名\, db.Textstyle); AttributeDef.Height = 1;

blocktablerec.Origin = new Point3d(1, 1, 0); blocktablerec.AppendEntity(dbpoint);

blocktablerec.AppendEntity(circle); blocktablerec.AppendEntity(triangle); blocktablerec.AppendEntity(AttributeDef); blocktable.Add(blocktablerec);

transaction.AddNewlyCreatedDBObject(blocktablerec, true); transaction.Commit();

}

4、创建带属性参照

代码:

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

StartTransaction();

blocktable = (BlockTable)transaction.GetObject(db.BlockTableId, OpenMode.ForWrite);

BlockTableRecord blockTblRec =

(BlockTableRecord)transaction.GetObject(blocktable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

BlockTableRecord pBlockTblRec = transaction.GetObject(blocktable[\我的块表\], OpenMode.ForRead) as BlockTableRecord;

BlockReference blocReference = new BlockReference(new Point3d(10, 10, 0), pBlockTblRec.ObjectId);

blockTblRec.AppendEntity(blocReference);

transaction.AddNewlyCreatedDBObject(blocReference, true);

foreach (ObjectId id in blockTblRec) {

Entity ent = transaction.GetObject(id, OpenMode.ForRead, false) as Entity;

if (ent is AttributeDefinition)

{ AttributeReference attriReference = new AttributeReference(); AttributeDefinition attriDefination = ent as AttributeDefinition; attriReference.SetPropertiesFrom(attriDefination); attriReference.Position = new Point3d(1, 1, 0); attriReference.Height = attriDefination.Height; attriReference.Rotation = attriDefination.Rotation; attriReference.Tag = attriDefination.Tag; attriReference.TextString = \坡头\;

if (!blocReference.IsWriteEnabled) {

blocReference.UpgradeOpen(); }

blocReference.AttributeCollection.AppendAttribute(attriReference);

transaction.AddNewlyCreatedDBObject(attriReference, true); } }

transaction.Commit();

5、在对话框中查看块定义的图标

5.1、添加一个类CreateBlockIcon.cs,用来导入AutoCad的图标

代码:

class CreateBlockIcon:IDisposable

{

[DllImport(\, CallingConvention =

CallingConvention.Cdecl, CharSet = CharSet.Unicode)]

private static extern int acedCommand(int type1, string command, int type2, string blockName, int end); public CreateBlockIcon(string blkname) {

acedCommand(5005, \, 5005, blkname, 5000); }

public void Dispose() { }

5.2、添加块表图标查看器的窗体


cad二次开发(块表的创建) (1).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:新手开服装加盟店在进货中要注意哪些问题

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

马上注册会员

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