[CommandMethod("Test")]
public static void Test()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
//Выбор блока для копирования
var resPrompt = doc.Editor.GetEntity("Выбери блок");
//Копирование блока
TestCopyBtr(resPrompt.ObjectId, "TestBlockName");
// Создание слоя
TestCreateLayer("TestLayerName");
}
private static void TestCreateLayer(string layerName)
{
Database db = HostApplicationServices.WorkingDatabase;
using (var t = db.TransactionManager.StartTransaction())
{
var lt = t.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
if (!lt.Has(layerName))
{
// Если слоя нет, то он создается.
var ltrMarks = new LayerTableRecord();
ltrMarks.Name = layerName;
ltrMarks.IsPlottable = false;
lt.UpgradeOpen();
lt.Add(ltrMarks);
t.AddNewlyCreatedDBObject(ltrMarks, true);
}
t.Commit();
}
}
public static ObjectId TestCopyBtr(ObjectId idBlref, string name)
{
ObjectId idBtrCopy = ObjectId.Null;
Database db = HostApplicationServices.WorkingDatabase;
using (var t = db.TransactionManager.StartTransaction())
{
var blRefSource = t.GetObject(idBlref, OpenMode.ForRead) as BlockReference;
var btrSource = t.GetObject(blRefSource.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
var bt = t.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
//проверка имени блока
if (bt.Has(name))
{
idBtrCopy = bt[name];
}
else
{
var btrCopy = btrSource.Clone() as BlockTableRecord;
btrCopy.Name = name;
bt.UpgradeOpen();
idBtrCopy = bt.Add(btrCopy);
t.AddNewlyCreatedDBObject(btrCopy, true);
// Копирование объектов блока
ObjectIdCollection ids = new ObjectIdCollection();
foreach (ObjectId idEnt in btrSource)
{
ids.Add(idEnt);
}
IdMapping map = new IdMapping();
db.DeepCloneObjects(ids, idBtrCopy, map, true);
}
t.Commit();
}
return idBtrCopy;
}