namespace MarksUtilities
{
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
public class InsertPosMark
{
protected Point3d GetUserPoint( )
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Document acCurDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acCurDoc.Database;
PromptPointOptions pickPoint = new PromptPointOptions("Specify the insertion point: ");
PromptPointResult userPointResult = ed.GetPoint(pickPoint);
return userPointResult.Value;
}
[CommandMethod("InsBlockWithAnAttribute")]
public void InsertBlockWithAnAttribute( )
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
Point3d insertionPoint = GetUserPoint( );
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction( ))
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkRecId = ObjectId.Null;
if (!acBlkTbl.Has("PosMark"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord( ))
{
acBlkTblRec.Name = "PosMark";
// 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 = "Помещение";
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 = "01525";
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.SetDatabaseDefaults(acBlkTbl.Database);
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;
fireRatingClass.Position= new Point3d(0,-50,0);
fireRatingClass.AlignmentPoint=new Point3d(0,50,0);
fireRatingClass.AdjustAlignment(acBlkTbl.Database);
acBlkTblRec.AppendEntity(fireRatingClass);
}
blkRecId = acBlkTblRec.Id;
}
}
}
else
{
blkRecId = acBlkTbl ["PosMark"];
}
// 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(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)
{
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);
acAttRef.TextString = acAtt.TextString;
acBlkRef.AttributeCollection.AppendAttribute(acAttRef);
acTrans.AddNewlyCreatedDBObject(acAttRef, true);
}
}
}
}
}
}
}
acTrans.Commit( );
}
acCurDb.ObjectAppended += AcCurDb_ObjectAppended;
}
private void AcCurDb_ObjectAppended(object sender, ObjectEventArgs e)
{
this.InsertBlockWithAnAttribute();
}
}
}