[CommandMethod("2title")]
public void DrawMainTitle()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkRecId = ObjectId.Null;
if (!acBlkTbl.Has("MainTitle"))
{
BlockTableRecord acBlkTblRec = new BlockTableRecord();
acBlkTblRec.Name = "MainTitle";
// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(5, 5, 0);
acBlkTbl.UpgradeOpen();
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
Polyline acPoly = new Polyline();
acPoly.SetDatabaseDefaults();
acPoly.AddVertexAt(0, new Point2d(0, 55), 0, 0, 0);
acPoly.AddVertexAt(1, new Point2d(-185, 55), 0, 0, 0);
acPoly.AddVertexAt(2, new Point2d(-185, 0), 0, 0, 0);
acBlkTblRec.AppendEntity(acPoly);
acTrans.AddNewlyCreatedDBObject(acPoly, true);
Polyline acPoly1 = new Polyline();
acPoly1.SetDatabaseDefaults();
acPoly1.AddVertexAt(0, new Point2d(5, 60), 0, 0, 0);
acPoly1.AddVertexAt(1, new Point2d(-190, 60), 0, 0, 0);
acPoly1.AddVertexAt(2, new Point2d(-190, 5), 0, 0, 0);
acBlkTblRec.AppendEntity(acPoly1);
acTrans.AddNewlyCreatedDBObject(acPoly1, true);
AttributeDefinition developer = new AttributeDefinition();
developer.Position = new Point3d(-167, 31, 0);
developer.Prompt = "Developer";
developer.Tag = "Developer";
developer.TextString = "Сидоров";
developer.Height = 3.5;
developer.Justify = AttachmentPoint.BottomFit;
acBlkTblRec.AppendEntity(developer);
acTrans.AddNewlyCreatedDBObject(developer, true);
blkRecId = acBlkTblRec.Id;
}
else
{
blkRecId = acBlkTbl["MainTitle"];
}
// Insert the block into the current space
if (blkRecId != ObjectId.Null)
{
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
// Create and insert the new block reference
BlockReference acBlkRef = new BlockReference(acBlkTblRec.Origin, blkRecId);
acBlkRef.SetDatabaseDefaults();
BlockTableRecord acCurSpaceBlkTblRec;
acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.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)
{
// Add attributes from the block table record
foreach (ObjectId objID in acBlkTblRec)
{
DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;
if (dbObj is AttributeDefinition)
{
AttributeDefinition acAtt = dbObj as AttributeDefinition;
if (!acAtt.Constant)
{
AttributeReference acAttRef = new AttributeReference();
acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
acAttRef.TextString = acAtt.TextString;
acBlkRef.AttributeCollection.AppendAttribute(acAttRef);
acTrans.AddNewlyCreatedDBObject(acAttRef, true);
}
}
}
}
}
// Save the new object to the database
acTrans.Commit();
}
}