.NET

Reply
Active Member
8 Posts
0 Kudos
Registered: ‎10-17-2012
Post 1 of 5
Accepted Solution

copy table from dwg

321 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*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 2 of 5

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
8 Posts
0 Kudos
Registered: ‎10-17-2012
Post 3 of 5

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
8 Posts
0 Kudos
Registered: ‎10-17-2012
Post 4 of 5

Re: copy table from dwg

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

Thanks, you help me out again=)

*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 5 of 5

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
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!