public void InsertSpaceTag(string spaceDesignation, string buildingNumber, string idOneC, Point3d insertionPoint)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
//Point3d insertionPoint=insertionPoint;// = doc.Editor.GetPoint("Set point").Value;
using (Transaction acTrans = db.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl = acTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkRecId = ObjectId.Null;
if (!acBlkTbl.Has("SpaceTag"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "SpaceTag";
// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 4.5;
acBlkTblRec.AppendEntity(acCirc);
acBlkTbl.UpgradeOpen();
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
// Add an attribute definition to the block
using (AttributeDefinition posNumber = new AttributeDefinition())
{
posNumber.Position = new Point3d(0, 0, 0);
posNumber.Prompt = "Position Number";
posNumber.Tag = "PosNumber";
posNumber.TextString = "1";
posNumber.Height = acCirc.Radius * 0.7;
posNumber.Justify = AttachmentPoint.MiddleCenter;
acBlkTblRec.AppendEntity(posNumber);
}
using (AttributeDefinition designation = new AttributeDefinition())
{
designation.Position = new Point3d(0, -5, 0);
designation.Prompt = "Designation";
designation.Tag = "Designation";
designation.TextString = spaceDesignation;
designation.Invisible = true;
designation.Height = 5;
designation.Justify = AttachmentPoint.MiddleCenter;
acBlkTblRec.AppendEntity(designation);
}
using (AttributeDefinition spaceNumber = new AttributeDefinition())
{
spaceNumber.Position = new Point3d(0, -10, 0);
spaceNumber.Prompt = "Space number";
spaceNumber.Tag = "SpaceNumber";
spaceNumber.TextString = buildingNumber;
spaceNumber.Invisible = true;
spaceNumber.Height = 5;
spaceNumber.Justify = AttachmentPoint.MiddleCenter;
acBlkTblRec.AppendEntity(spaceNumber);
}
using (AttributeDefinition mainWatertightCompartment = new AttributeDefinition())
{
mainWatertightCompartment.Position = new Point3d(0, -15, 0);
mainWatertightCompartment.Prompt = "Main watertight compartment";
mainWatertightCompartment.Tag = "MWC";
mainWatertightCompartment.TextString = "01";
mainWatertightCompartment.Invisible = true;
mainWatertightCompartment.Height = 5;
mainWatertightCompartment.Justify = AttachmentPoint.MiddleCenter;
acBlkTblRec.AppendEntity(mainWatertightCompartment);
}
using (AttributeDefinition gastightContour = new AttributeDefinition())
{
gastightContour.Position = new Point3d(0, -20, 0);
gastightContour.Prompt = "Gastight contour";
gastightContour.Tag = "GastightContour";
gastightContour.TextString = "015";
gastightContour.Invisible = true;
gastightContour.Height = 5;
gastightContour.Justify = AttachmentPoint.MiddleCenter;
acBlkTblRec.AppendEntity(gastightContour);
}
using (AttributeDefinition fireRatingClass = new AttributeDefinition())
{
fireRatingClass.Position = new Point3d(0, -25, 0);
fireRatingClass.Prompt = "Fire rating class";
fireRatingClass.Tag = "FireRatingClass";
fireRatingClass.TextString = "1";
fireRatingClass.Invisible = true;
fireRatingClass.Height = 5;
fireRatingClass.Justify = AttachmentPoint.MiddleCenter;
acBlkTblRec.AppendEntity(fireRatingClass);
}
using (AttributeDefinition OneCId = new AttributeDefinition())
{
OneCId.Position = new Point3d(0, -25, 0);
OneCId.Prompt = "One C Id";
OneCId.Tag = "OneCId";
OneCId.TextString = idOneC;
OneCId.Invisible = true;
OneCId.Height = 5;
OneCId.Justify = AttachmentPoint.MiddleCenter;
acBlkTblRec.AppendEntity(OneCId);
}
blkRecId = acBlkTblRec.Id;
}
}
}
else
{
blkRecId = acBlkTbl["SpaceTag"];
}
// Insert the block into the current space
if (blkRecId != ObjectId.Null)
{
BlockTableRecord acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
// Create and insert the new block reference
using (BlockReference acBlkRef = new BlockReference(insertionPoint, blkRecId))
{
BlockTableRecord acCurSpaceBlkTblRec = acTrans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
// Verify block table record has attribute definitions associated with it
if (acBlkTblRec.HasAttributeDefinitions)
{
foreach (ObjectId objID in acBlkTblRec)
{
DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead);
if (dbObj is AttributeDefinition)
{
AttributeDefinition acAtt = dbObj as AttributeDefinition;
if (!acAtt.Constant)
{
using (AttributeReference acAttRef = new AttributeReference())
{
acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
if ( acAttRef.Tag== "OneCId")
{
acAttRef.TextString = idOneC;
}
if (acAttRef.Tag == "SpaceNumber")
{
acAttRef.TextString = buildingNumber;
}
if (acAttRef.Tag == "Designation")
{
acAttRef.TextString = spaceDesignation;
}
if (acAttRef.Tag == "PosNumber")
{
acAttRef.TextString = idOneC;
}
else acAttRef.TextString = acAtt.TextString;
acBlkRef.AttributeCollection.AppendAttribute(acAttRef);
acTrans.AddNewlyCreatedDBObject(acAttRef, true);
}
}
}
}
}
}
}
acTrans.Commit();
}
}