using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
namespace pavli
{
public class pavli
{
[CommandMethod("PAVPLTEST")]
public void PAVLI()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
//LayoutManager layMng = LayoutManager.Current;
//string curLayoutName = layMng.CurrentLayout;
ObjectId psid = db.CurrentSpaceId;
string curLayoutName = LayoutManager.Current.CurrentLayout;
string msLayoutName = "";
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// BlockTableRecord для текущего пространства
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;
BlockTableRecord btrMS = tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead) as BlockTableRecord;
// Текущий лист
Layout layout = tr.GetObject(btr.LayoutId, OpenMode.ForRead) as Layout;
// Лист модели
Layout layoutMS = tr.GetObject(btrMS.LayoutId, OpenMode.ForRead) as Layout;
msLayoutName = layoutMS.LayoutName;
tr.Commit();
}
//Копируем определение блока из др.файла
ObjectIdCollection ids = new ObjectIdCollection();
using (Database newDB = new Database(false, false))
{
newDB.ReadDwgFile(@"D:\1.dwg", System.IO.FileShare.Read, false, string.Empty);
newDB.CloseInput(true);
using (BlockTable bt = newDB.BlockTableId.Open(OpenMode.ForRead) as BlockTable)
{
if (bt.Has("Штсcc")) ids.Add(bt["Штсcc"]);
else return;
}
if (ids.Count > 0)
{
IdMapping iMap = new IdMapping();
{
using (DocumentLock dl = doc.LockDocument())
{
db.WblockCloneObjects(ids, db.BlockTableId, iMap, DuplicateRecordCloning.Ignore, false);
}
}
}
}
//Выбираем все примитивы текущего пространства листа и удаляем их
TypedValue[] acTypValAr = new TypedValue[1];
acTypValAr.SetValue(new TypedValue(410, curLayoutName), 0);
SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);
PromptSelectionResult acSSPrompt = ed.SelectAll(acSelFtr);
SelectionSet acSSet = acSSPrompt.Value;
ObjectIdCollection acObjIdColl = new ObjectIdCollection(acSSet.GetObjectIds());
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (ObjectId id in acObjIdColl)
{
var objObject = tr.GetObject(id, OpenMode.ForWrite);
objObject.Erase();
}
tr.Commit();
}
//Как альтернатива - рабочий вариант№2 с переключением
//LayoutManager.Current.CurrentLayout = msLayoutName;
//LayoutManager.Current.CurrentLayout = curLayoutName;
//Вставляем блок на лист
using (Transaction tr2 = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr2.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr2.GetObject(bt["Штсcc"], OpenMode.ForWrite) as BlockTableRecord;
BlockTableRecord cs = tr2.GetObject(psid, OpenMode.ForWrite) as BlockTableRecord; //Ошибка тут eNullObjectId
Point3d pins = new Point3d(0, 0, 0);
BlockReference blRefTest = new BlockReference(pins, bt["Штсcc"]);
ObjectId bRefId = cs.AppendEntity(blRefTest);
tr2.AddNewlyCreatedDBObject(blRefTest, true);
tr2.Commit();
}
ed.Regen();
}
}
}