Hi
i try to copy one table from dwg to current dwg which is opened. I have a little success - my table is copied to current dwg. But when i click text in cell or try to activate table, AutoCAD hangs.
I think that my code does not copy the table. And these table that i see in the current dwg this is table from source dwg.
It's my opinion
Now i want hearing some suggestions, solutions or advices. Help.
Solved! Go to Solution.
Solved by Barabasishe. Go to Solution.
Try this way, but don't forget to rename the source file name in the code:
[CommandMethod("ty", CommandFlags.Session)] public void tryCopyTable() { //source drawing name: string fname = @"c:\test\workingdrawing.dwg"; ObjectId id = ObjectId.Null; ObjectIdCollection ids = new ObjectIdCollection(); IdMapping map = new IdMapping(); bool ok = false; try { DocumentCollection dm = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager; Document doc = dm.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; using (doc.LockDocument()) { using (Transaction tr = doc.TransactionManager.StartTransaction()) { Document doc2 = dm.Open(fname, false); while (!dm.IsApplicationContext) { // get a time to next drawing to wake up System.Threading.Thread.Sleep(1); } Database db2 = doc2.Database; Editor ed2 = doc2.Editor; ed2.WriteMessage("\nYou're may use editor in the drawing: \"{0}\"\n", fname); using (Transaction tr2 = doc2.TransactionManager.StartTransaction()) { id = ed2.GetEntity("\nSelect table to copy: ").ObjectId; DBObject obj = (DBObject)tr2.GetObject(id, OpenMode.ForRead, false, true); Table tbl = obj as Table; if (tbl == null) return; map = new IdMapping(); ids = new ObjectIdCollection(); ids.Add(id); tr2.Commit(); } dm.MdiActiveDocument = doc; HostApplicationServices.WorkingDatabase = doc.Database; BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false) as BlockTableRecord; db2.WblockCloneObjects(ids, btr.ObjectId, map, DuplicateRecordCloning.Replace, false); id = ed.SelectLast().Value.GetObjectIds()[0]; DBObject clone = (DBObject)tr.GetObject(id, OpenMode.ForRead, false, true); Table copytbl = clone as Table; if (!copytbl.IsWriteEnabled) copytbl.UpgradeOpen(); Point3d pt = ed.GetPoint("\nPick new insertion point: \n").Value; copytbl.TransformBy(Matrix3d.Displacement(pt - copytbl.Position)); tr.Commit(); doc2.CloseAndDiscard(); ok = true; } } } catch (System.Exception ex) { ok = false; MessageBox.Show(ex.Message + "\n" + ex.StackTrace); } finally { if (ok) { MessageBox.Show("Pokey!"); } } }
Yeah.
My code is similar to yours, but i miss some part of code:
DBObject clone = (DBObject)tr.GetObject(id, OpenMode.ForRead, false, true); Table copytbl = clone as Table; if (!copytbl.IsWriteEnabled) copytbl.UpgradeOpen(); Point3d pt = new Point3d(0, 0, 0); copytbl.TransformBy(Matrix3d.Displacement(pt - copytbl.Position));
Now, my solution of this problem (without Editor and choice table, only search):
[CommandMethod("CPTBL")] public void copyTable() { Document document = Application.DocumentManager.MdiActiveDocument; Database database = document.Database; Database tempDatabase = loadDWG(@"..\myFile.dwg"); ObjectIdCollection idColl = new ObjectIdCollection(); ObjectId idCopiedTable = ObjectId.Null; try { if (tempDatabase != null) { ObjectId idLayout = getLayoutFromDictionary(ref tempDatabase, "Model"); using (Transaction tempTransaction = tempDatabase.TransactionManager.StartTransaction()) { BlockTableRecord tempRecord = tempTransaction.GetObject(idLayout, OpenMode.ForRead, false) as BlockTableRecord; foreach (ObjectId idEntity in tempRecord) { Entity entity = tempTransaction.GetObject(idEntity, OpenMode.ForRead, false) as Entity; Table table = entity as Table; if (table != null) { idCopiedTable = table.ObjectId; idColl.Add(table.ObjectId); } tempTransaction.Commit(); } } if (idColl.Count != 0) { using (Transaction transact = database.TransactionManager.StartTransaction()) { BlockTableRecord record = transact.GetObject(database.CurrentSpaceId, OpenMode.ForWrite, false) as BlockTableRecord; IdMapping mapping = new IdMapping(); tempDatabase.WblockCloneObjects(idColl, record.ObjectId, mapping, DuplicateRecordCloning.Replace, false); foreach (ObjectId idRec in record) { Entity entity = transact.GetObject(idRec, OpenMode.ForRead, false) as Entity; Table table = entity as Table; if (table != null) { idCopiedTable = table.ObjectId; } } Table cloneTable = transact.GetObject(idCopiedTable, OpenMode.ForRead, false, true) as Table; if (!cloneTable.IsWriteEnabled) { cloneTable.UpgradeOpen(); } Point3d pointInsertion = new Point3d(0, 0, 0); cloneTable.Position = pointInsertion; transact.Commit(); } } tempDatabase.CloseInput(true); } } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace); } }
Glad I could help
Cheers