Message 1 of 17
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have two File1.dwg and File2.dwg with some contents in both of them.
I want to add File1 contents to the contents of File2
please help. My code is
public void file2file(string fromFile, string toFile) {
// define souce DB
Database fromDb = new Database(false, true);
// define object collection
ObjectIdCollection acObjIdColl = new ObjectIdCollection();
// Lock the current document
using (fromDb) {
// open source file
fromDb.ReadDwgFile(fromFile, FileOpenMode.OpenForReadAndAllShare, false, null);
// Start a transaction
using (Transaction acTrans = fromDb.TransactionManager.StartTransaction()) {
// Open the Block table record for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(fromDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForRead) as BlockTableRecord;
// copy/paste each object
foreach (ObjectId id in fromBtr) {
Entity ent = (Entity)fromTr.GetObject(id, OpenMode.ForWrite);
acObjIdColl.Add(ent.ObjectId);
}
// Save the new objects to the database
acTrans.Commit();
}
// Unlock the document
}
// Create a new drawing to copy the objects to
Database acDbNewDoc = new Database(false, true);
using (acDbNewDoc) {
// open source file
acDbNewDoc.ReadDwgFile(toFile, FileOpenMode.OpenForReadAndAllShare, false, null);
// Start a transaction in the new database
using (Transaction acTrans = acDbNewDoc.TransactionManager.StartTransaction()) {
// Open the Block table for read
BlockTable acBlkTblNewDoc;
acBlkTblNewDoc = acTrans.GetObject(acDbNewDoc.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for read
BlockTableRecord acBlkTblRecNewDoc;
acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc[BlockTableRecord.ModelSpace],
OpenMode.ForRead) as BlockTableRecord;
//foreach (Entity id1 in acObjIdColl) {
foreach (Entity nent in acDBObjColl) {
destBtr.AppendEntity(nent);
destTr.AddNewlyCreatedDBObject(nent, true);
}
// Save the copied objects to the database
acTrans.Commit();
}
// Unlock the document
}
// Set the new document current
acDbNewDoc.SaveAs(toFile, DwgVersion.Current);
}
Moderator edit: Put code in code window.
Solved! Go to Solution.