private void BatchFiles(string pathDirectory)
{
DirectoryInfo dir = new DirectoryInfo(pathDirectory);
FileInfo[] dwgFiles = dir.GetFiles("*.dwg");
foreach (FileInfo dwgFile in dwgFiles)
{
using (Database database = new Database(false, true))
{
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
database.ReadDwgFile(dwgFile.FullName, FileShare.Read, true, null);
BlockTableRecord modelSpace = transaction.GetObject(database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
ObjectId attributeDefinitionId = modelSpace
.OfType<ObjectId>()
.Where(id => id.ObjectClass.DxfName.Equals("ATTDEF") &&
(transaction.GetObject(id, OpenMode.ForRead) as AttributeDefinition).Tag.Equals("UC-EMLP"))
.FirstOrDefault();
if (attributeDefinitionId != ObjectId.Null)
{
AttributeDefinition attributeDefinition = transaction.GetObject(attributeDefinitionId, OpenMode.ForRead) as AttributeDefinition;
Point3d alignmentPoint = attributeDefinition.AlignmentPoint;
using (AttributeDefinition newAttributeDefinition = new AttributeDefinition())
{
newAttributeDefinition.Justify = AttachmentPoint.TopCenter;
newAttributeDefinition.AlignmentPoint = new Point3d(0, alignmentPoint.Y - 4.5, 0);
newAttributeDefinition.Tag = "TERM_PWR";
newAttributeDefinition.Height = 3.5;
newAttributeDefinition.TextString = " ";
newAttributeDefinition.LockPositionInBlock = true;
newAttributeDefinition.Verifiable = true;
modelSpace.AppendEntity(newAttributeDefinition);
transaction.AddNewlyCreatedDBObject(newAttributeDefinition, true);
}
}
transaction.Commit();
}
database.SaveAs(dwgFile.FullName, true, DwgVersion.Current, null);
}
}
DirectoryInfo[] directories = dir.GetDirectories();
foreach (DirectoryInfo directory in directories)
{
BatchFiles(directory.FullName);
}
}