[CommandMethodAttribute("TEST_GROUP", "TTNELTOBLOCK", "TTNELTOBLOCK", CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.Redraw)]
public void ElementsToBlock()
{
//Select one AS element and get Assembly elements as ACAD ObjectId
DocumentManager.LockCurrentDocument();
Autodesk.AdvanceSteel.CADAccess.Transaction asTrans = Autodesk.AdvanceSteel.CADAccess.TransactionManager.StartTransaction();
IEnumerable<Autodesk.AdvanceSteel.CADLink.Database.ObjectId> connectedASobjIDs;//Vault for set of AS elements
Autodesk.AdvanceSteel.CADLink.Database.ObjectId selectedASid = UserInteraction.SelectObject();//AS selection
List<Autodesk.AutoCAD.DatabaseServices.ObjectId> acadObjIds = new List<Autodesk.AutoCAD.DatabaseServices.ObjectId>();//ACAD Id list
if (DatabaseManager.Open(selectedASid) is AtomicElement selectedASatomicEl)
{
selectedASatomicEl.GetConnectedObjects(out connectedASobjIDs, AtomicElement.eAssemblyLocation.kInShop);
foreach (Autodesk.AdvanceSteel.CADLink.Database.ObjectId objASid in connectedASobjIDs)
{
if (DatabaseManager.Open(objASid) is FilerObject filObj)
{
Autodesk.AdvanceSteel.CADLink.Database.ObjectId reprId = DatabaseManager.GetReprId(filObj);
Autodesk.AutoCAD.DatabaseServices.ObjectId acadObjId =
new Autodesk.AutoCAD.DatabaseServices.ObjectId(reprId.AsOldId());
acadObjIds.Add(acadObjId);
}
}
}
asTrans.Commit();
DocumentManager.UnlockCurrentDocument();
//Create ACAD block from Assembly Elements
Autodesk.AutoCAD.ApplicationServices.Document acadDoc =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = acadDoc.Database;
ObjectIdCollection ids = new ObjectIdCollection();
using (Autodesk.AutoCAD.DatabaseServices.Transaction acadTrans = db.TransactionManager.StartTransaction())
{
BlockTable bt = acadTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord modelSpace = acadTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
BlockTableRecord blockDef = new BlockTableRecord();
blockDef.Name = "A1";
bt.UpgradeOpen();
ObjectId blockDefId = bt.Add(blockDef);
acadTrans.AddNewlyCreatedDBObject(blockDef, true);
foreach (ObjectId objectId in acadObjIds)
{
Entity entity = acadTrans.GetObject(objectId, OpenMode.ForRead) as Entity;
Entity entityCopy = entity.Clone() as Entity;
blockDef.AppendEntity(entityCopy);
acadTrans.AddNewlyCreatedDBObject(entityCopy, true);
}
//Copy block
if (bt.Has("A1"))
{
ids.Add(bt["A1"]);
}
acadTrans.Commit();
}
//Get path
string folderPath = acadDoc.Database.Filename.Replace(".dwg", "") + "\\Assemblies";
string filePath = folderPath + "\\A1.dwg";
//Create folder
if (!System.IO.Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);// Create folder
}
DirectoryInfo folderInfo = new DirectoryInfo(folderPath);
folderInfo.Attributes &= ~FileAttributes.ReadOnly; // Remove read-only attribute
//Check file exsisting
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
//Create new DB and transaction
using (var newDb = new Database(false, true))
{
newDb.SaveAs(filePath, false, DwgVersion.Current, newDb.SecurityParameters); //Вот тут ошибка
//newDb.SaveAs(filePath, DwgVersion.Current);
//newDb.ReadDwgFile(templatePath, FileOpenMode.OpenForReadAndAllShare, true, null);
using (Autodesk.AutoCAD.DatabaseServices.Transaction newAcadTrans =
newDb.TransactionManager.StartTransaction())
{
IdMapping iMap = new IdMapping();
BlockTableRecord modelSpace = newAcadTrans.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite) as BlockTableRecord;
newDb.WblockCloneObjects(ids, newDb.BlockTableId, iMap, DuplicateRecordCloning.Ignore, false);
BlockTable bt = newAcadTrans.GetObject(newDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord blockDef = newAcadTrans.GetObject(bt["A1"], OpenMode.ForRead) as BlockTableRecord;
BlockReference blockRef =
new BlockReference(new Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0), blockDef.ObjectId);
newAcadTrans.AddNewlyCreatedDBObject(blockRef, true);
newAcadTrans.Commit();
}
newDb.Save();
}
}