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 = "我的块表"; 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("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); transaction.Commit(); }
3、创建带属性的块表
[CommandMethod("createattributeblk")] 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("insertattributeblk")] 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、在对话框中查看块定义的属性
class CreateBlockIcon:IDisposable { [DllImport("acad.exe", 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, "BlockIcon", 5005, blkname, 5000); } public void Dispose() { }
public partial class BlockLookingForm : Form { public Document docu; Database DB; BlockTable blockTable; public BlockLookingForm(Document docu, Database DB, BlockTable blockTable) { InitializeComponent(); this.docu = docu; this.DB = DB; this.blockTable = blockTable; } private void btnLooking_Click(object sender, EventArgs e) {Database database = docu.Database; try {using (Transaction trans = database.TransactionManager.StartTransaction()) { blockTable = trans.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord blockTableRecord = trans.GetObject(blockTable[txtBlockName.Text.Trim()], OpenMode.ForRead) as BlockTableRecord; int Count = 0; foreach (ObjectId id in blockTableRecord) {Entity entity = trans.GetObject(id, OpenMode.ForRead, false) as Entity; if (entity is AttributeDefinition) { txtAttri.Text = "有"; } Count++; } txtEntity.Text = Count.ToString(); Bitmap bitmap = blockTableRecord.PreviewIcon; if (bitmap != null) { System.Drawing.Image image = System.Drawing.Image.FromHbitmap(bitmap.GetHbitmap()); pictureBox1.Image = image; pictureBox1.Refresh(); } trans.Commit(); } } catch (Exception ex) { MessageBox.Show("有错误"+ex.Message);