[CommandMethod("2Starttest")]
public static void CommandStarter()
{
try
{
List < string > files =SelectDrawings();
foreach ( string file in files )
{
using ( Database db = new(false, true) )
{
db.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, false, null);
List < string > blocks = GetAllBlockDefNames(db);
if ( blocks.Count > 0 )
{
foreach ( string name in blocks )
{
if ( name.Contains("Main") )
{
HostApplicationServices.WorkingDatabase = db;
ObjectId x = GetBlockDefByName(name, db);
ObjectIdCollection collection = new();
Transaction tr = db.TransactionManager.StartTransaction();
using ( tr )
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
for ( int i = 0; i < 5; i++ )
{
Polyline reviewedByRMRS = new();
Point2d basePoint = new(-325, 83 + (17*i));
reviewedByRMRS.SetDatabaseDefaults(db);
reviewedByRMRS.CreateRectangleByDimensions(basePoint, 80, 12);
reviewedByRMRS.LineWeight = LineWeight.LineWeight000;
reviewedByRMRS.Linetype = "-ITAT_Dashed";
reviewedByRMRS.LinetypeScale = 0.005;
reviewedByRMRS.Layer = "0";
btr.AppendEntity(reviewedByRMRS);
tr.AddNewlyCreatedDBObject(reviewedByRMRS, true);
collection.Add(reviewedByRMRS.ObjectId);
using ( MText mtext = new() )
{
mtext.Location = mtext.Location = reviewedByRMRS.GetPoint3dAt(1);
mtext.TextHeight = 2;
mtext.Attachment = AttachmentPoint.MiddleLeft;
mtext.Contents = "Новый текст";
mtext.Layer = "0";
mtext.Width = 50;
mtext.LineSpaceDistance = 2;
btr.AppendEntity(mtext);
tr.AddNewlyCreatedDBObject(mtext, true);
collection.Add(mtext.ObjectId);
}
}
AddObjectsToBlockDef(x, collection, db);
tr.Commit();
}
}
}
db.SaveAs(file, DwgVersion.Newest);
}
}
}
Application.ShowAlertDialog("All files processed");
}
catch ( System.Exception ex )
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
}
}
public static ObjectId GetBlockDefByName(string name, Database db)
{
try
{
using ( Transaction tr = db.TransactionManager.StartTransaction() )
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
foreach ( ObjectId idBtr in bt )
{
BlockTableRecord btr = tr.GetObject(idBtr, OpenMode.ForRead) as BlockTableRecord;
if ( btr.Name == name )
{
return btr.ObjectId;
}
}
tr.Commit();
}
}
catch ( System.Exception ex )
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"Cannot get block definition with name {name}");
}
return default;
}
public static List<string> SelectDrawings()
{
List<string> fileNames = new List<string>();
try
{
Autodesk.AutoCAD.Windows.OpenFileDialog ofd = new Autodesk.AutoCAD.Windows.OpenFileDialog("Выбрать чертеж", "", "dwg", "did", Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple);
DialogResult yy = ofd.ShowDialog();
if ( yy == DialogResult.OK )
{
foreach ( string file in ofd.GetFilenames() )
{
fileNames.Add(file);
}
}
}
catch ( Exception ex )
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
}
return fileNames;
}
public static List<string> GetAllBlockDefNames(Database db)
{
List<string> newObjects = new List<string>();
try
{
using ( Transaction tr = db.TransactionManager.StartTransaction() )
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
foreach ( ObjectId idBtr in bt )
{
BlockTableRecord btr = tr.GetObject(idBtr, OpenMode.ForRead) as BlockTableRecord;
newObjects.Add(btr.Name);
}
tr.Commit();
}
}
catch ( System.Exception ex )
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"Cannot get block list");
}
return newObjects;
}