[CAD_RTM.CommandMethod("COMM_GET_FILES_ELEMENTS")]
public void acad_comm_sborka()
{
// Корневой каталог
string Root = @"С:\TEST\";
SearchOption so = SearchOption.AllDirectories;
// перечень файлов
string[] fns = System.IO.Directory.GetFiles(Root, "*.dwg", so);
// Список всех примитивов из файлов
List<CAD_DBS.Entity> ents = new List<CAD_DBS.Entity>();
// Кол-во ошибок
int err = 0;
foreach (string f in fns)
{
CAD_DBS.Database db = new CAD_DBS.Database(false, true);
db.ReadDwgFile(f, System.IO.FileShare.Read, false, null);
try
{
using (CAD_DBS.Transaction tr = db.TransactionManager.StartTransaction())
{
CAD_DBS.BlockTable bt = (CAD_DBS.BlockTable)tr.GetObject(db.BlockTableId, CAD_DBS.OpenMode.ForRead, false, true);
CAD_DBS.BlockTableRecord btr = (CAD_DBS.BlockTableRecord)tr.GetObject(bt[CAD_DBS.BlockTableRecord.ModelSpace], CAD_DBS.OpenMode.ForRead, false, true);
CAD_DBS.BlockTableRecordEnumerator enu = btr.GetEnumerator();
enu.Reset();
while (enu.MoveNext())
{
CAD_DBS.Entity ent = tr.GetObject(enu.Current, CAD_DBS.OpenMode.ForRead) as CAD_DBS.Entity;
if (ent is CAD_DBS.BlockReference)
{
// выбираем все элементы блока кроме вставок блока
CAD_DBS.DBObjectCollection coll = new CAD_DBS.DBObjectCollection();
(ent as CAD_DBS.BlockReference).Explode(coll);
foreach (CAD_DBS.DBObject dbo in coll)
if (!(dbo is CAD_DBS.BlockReference))
if (dbo is CAD_DBS.Entity )
ents.Add((CAD_DBS.Entity)dbo.Clone());
}
else
ents.Add((CAD_DBS.Entity)ent.Clone());
}
tr.Commit();
}
}
catch
{
err++;
}
db.CloseInput(true);
}
if (err > 0)
{
MessageBox.Show("Ошибок при обработке файлов: " + err.ToString());
}
acad_Func.add_Ents(ents, true);
}
public static class acad_Func
{
public static void add_Ents(List<CAD_DBS.Entity> ents,
bool WithMsg = false)
{
try
{
CAD_APS.Document doc =
CAD_APS.Application.DocumentManager.MdiActiveDocument;
CAD_DBS.Database db = doc.Database;
using (doc.LockDocument())
{
using (CAD_DBS.Transaction tr = db.TransactionManager.StartTransaction())
{
CAD_DBS.BlockTable bt = (CAD_DBS.BlockTable)tr.GetObject(db.BlockTableId, CAD_DBS.OpenMode.ForWrite);
CAD_DBS.BlockTableRecord btr =
(CAD_DBS.BlockTableRecord)tr.GetObject(bt[CAD_DBS.BlockTableRecord.ModelSpace],
CAD_DBS.OpenMode.ForWrite);
foreach (CAD_DBS.Entity ent in ents)
{
if (ent != null)
{
CAD_DBS.ObjectId id = btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
else
{
}
}
tr.Commit();
}
}
}
catch (System.Exception ex)
{
if (WithMsg)
{
MessageBox.Show("Ошибка добавления элементов" + '\n' + ex.Message);
}
}
}
}