cad二次开发(块表的创建) (1)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验三、块表的创建
一.实验目的:
通过本次掌握块表的基本创建方法
二.实验内容:
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("createblock")]
public void CreateBlock()
{
StartTransaction();
try
{
blocktable = (BlockTable)transaction.GetObject(db.BlockTableId, OpenMode.ForWrite);
BlockTableRecord blocktableRecord = new BlockTableRecord();
string blockName = "我的块表";
= 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); mit();
}
2、创建块表参照
代码:
[CommandMethod("insertblk")]
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);
mit();
}
3、创建带属性的块表
代码:
[CommandMethod("createattributeblk")]
public void CreateAttributeBlock()
{
StartTransaction();
blocktable = (BlockTable)transaction.GetObject(db.BlockTableId, OpenMode.ForWrite);
BlockTableRecord blocktablerec = new BlockTableRecord();
string blockName = "我的块表";
= 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);