实验三、块表的创建
一. 实验目的:
通过本次掌握块表的基本创建方法
二. 实验内容:
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、添加块表图标查看器的窗体