.NET

Reply
Active Member
Barabasishe
Posts: 8
Registered: ‎10-17-2012
Message 1 of 5 (313 Views)
Accepted Solution

copy table from dwg

313 Views, 4 Replies
11-21-2012 01:55 AM

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.

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);
}
}

 

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 5 (300 Views)

Re: copy table from dwg

11-21-2012 09:24 AM in reply to: Barabasishe

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!");
                }
            }
        }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Member
Barabasishe
Posts: 8
Registered: ‎10-17-2012
Message 3 of 5 (284 Views)

Re: copy table from dwg

11-21-2012 08:31 PM in reply to: Hallex

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);
            }
        }

 

 

Active Member
Barabasishe
Posts: 8
Registered: ‎10-17-2012
Message 4 of 5 (279 Views)

Re: copy table from dwg

11-21-2012 08:43 PM in reply to: Hallex

Thanks, you help me out again=)

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 5 of 5 (276 Views)

Re: copy table from dwg

11-21-2012 10:08 PM in reply to: Barabasishe

Glad I could help

Cheers :smileyhappy:

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.