Here a code to create a anonymous block :
static public ObjectId InsertBlockInstance(String blockName, Point3d point = new Point3d(), String layerName = "")
{
ObjectId returnID = new ObjectId();
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction myT = db.TransactionManager.StartTransaction())
{
//Get the block definition "Check".
BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord blockDef = bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
//Also open modelspace - we'll be adding our BlockReference to it
BlockTableRecord ms = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;
//Create new BlockReference, and link it to our block definition
using (BlockReference blockRef = new BlockReference(point, blockDef.ObjectId))
{
//SET THE LAYER
if (layerName == "") //SET TO THE CURRENT LAYER
blockRef.LayerId = db.Clayer;
else
blockRef.Layer = layerName;
BlockTableRecord blkTblRcd = myT.GetObject(blockRef.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
blkTblRcd.Name = "*U";
//Add the block reference to modelspace
returnID = ms.AppendEntity(blockRef);
myT.AddNewlyCreatedDBObject(blockRef, true);
//Iterate block definition to find all non-constant
// AttributeDefinitions
foreach (ObjectId id in blockDef)
{
DBObject obj = id.GetObject(OpenMode.ForRead);
AttributeDefinition attDef = obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
//This is a non-constant AttributeDefinition
//Create a new AttributeReference
using (AttributeReference attRef = new AttributeReference())
{
attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
//attRef.TextString = "";
attRef.TextString = attDef.TextString;
//Add the AttributeReference to the BlockReference
blockRef.AttributeCollection.AppendAttribute(attRef);
myT.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
//Our work here is done
myT.Commit();
}
Can't find what you're looking for? Ask the community or share your knowledge.