Hi Guys,
great thread here.
I try to use your code but get stuck on it.
What I want is: from an empty new drawing clone an Inventor dwg with WBlockCloneObjects.
When I open the inventor dwg in autocad it is just a layout but with Inventor objects in it.
With your code I get the ObjectIdCollection (count = 23 in my case)
Create a new layout with the size and name of the inventor layout name, but I can't get the inventor objects to clone in my new autocad layout.
Do you have a clue?
Code: (for cloning autocad dwg it works fine)
public static Database WblockCloneFromExternalDWGfile(string filename, bool isinventorfile)
{
using (DocumentLock targetDocumentLock = Active.Document.LockDocument())
{
Database targetDatabase = Active.WorkingDatabase;
Database sourchDatabase = new Database(false, true);
sourchDatabase.ReadDwgFile(filename, FileOpenMode.OpenForReadAndAllShare, true, "");
using (Transaction sourchTransaction = sourchDatabase.TransactionManager.StartTransaction())
{
BlockTable sourchBlockTable = (BlockTable)sourchDatabase.BlockTableId.GetObject(OpenMode.ForRead);
BlockTableRecord sourchSpace;
if (isinventorfile) { sourchSpace = (BlockTableRecord)sourchBlockTable[BlockTableRecord.PaperSpace].GetObject(OpenMode.ForRead); }
else { sourchSpace = (BlockTableRecord)sourchBlockTable[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForRead); }
ObjectIdCollection objectIDCollection = new ObjectIdCollection();
foreach (ObjectId objectID in sourchSpace) { objectIDCollection.Add(objectID); }
using (Transaction targetTransaction = targetDatabase.TransactionManager.StartTransaction())
{
BlockTable targetBlockTable = (BlockTable)targetDatabase.BlockTableId.GetObject(OpenMode.ForWrite);
BlockTableRecord targetSpace;
IdMapping idMapping = new IdMapping();
if (isinventorfile)
{
//DBDictionary sourchLayoutDirectory = (DBDictionary) sourchDatabase.LayoutDictionaryId.GetObject(OpenMode.ForRead);
//Layout sourchLayout = (Layout) sourchLayoutDirectory.GetAt("Blad").GetObject(OpenMode.ForRead);
//ObjectId newLayoutId = LayoutManager.Current.CreateLayout(sourchLayout.LayoutName);
//Layout targetLayout = (Layout)newLayoutId.GetObject(OpenMode.ForWrite);
//LayoutManager.Current.CurrentLayout = targetLayout.LayoutName;
////Layout targetLayout = new Layout();
////targetLayout.LayoutName = sourchLayout.LayoutName;
//targetLayout.CopyFrom(sourchLayout);
//BlockTableRecord targetBlockTableRecord = new BlockTableRecord();
//targetBlockTableRecord.Name = sourchLayout.LayoutName;
//targetBlockTable.Add(targetBlockTableRecord);
//targetTransaction.AddNewlyCreatedDBObject(targetBlockTableRecord, true);
//sourchDatabase.WblockCloneObjects(objectIDCollection, targetBlockTableRecord.ObjectId, idMapping, DuplicateRecordCloning.Replace, false);
//targetLayout.AddToLayoutDictionary(targetDatabase, targetBlockTableRecord.ObjectId);
////targetTransaction.AddNewlyCreatedDBObject(targetLayout, true);
DBDictionary sourchLayoutDirectory = (DBDictionary)sourchDatabase.LayoutDictionaryId.GetObject(OpenMode.ForRead);
Layout sourchLayout = (Layout)sourchLayoutDirectory.GetAt("Blad").GetObject(OpenMode.ForRead);
ObjectId targetLayoutId = LayoutManager.Current.CreateLayout(sourchLayout.LayoutName);
Layout targetLayout = (Layout)targetLayoutId.GetObject(OpenMode.ForWrite);
targetLayout.LayoutName = sourchLayout.LayoutName;
targetLayout.CopyFrom(sourchLayout);
targetDatabase.WblockCloneObjects(objectIDCollection, targetLayout.BlockTableRecordId, idMapping, DuplicateRecordCloning.Replace, false);
targetLayout.Initialize();
LayoutManager.Current.CurrentLayout = targetLayout.LayoutName;
}
else
{
targetSpace = (BlockTableRecord)targetBlockTable[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite);
targetDatabase.WblockCloneObjects(objectIDCollection, targetSpace.ObjectId, idMapping, DuplicateRecordCloning.Replace, false);
}
targetTransaction.Commit();
}
}
return targetDatabase;
}
}