ReadDwgFile doc.Database.Insert:eSelfReference

ReadDwgFile doc.Database.Insert:eSelfReference

wokeyiyognshenme
Advocate Advocate
1,982 Views
8 Replies
Message 1 of 9

ReadDwgFile doc.Database.Insert:eSelfReference

wokeyiyognshenme
Advocate
Advocate

I have a bloks.dwg, there are many block definition in the  blocks.dwg. one of them called "ABC"

 

I want import "ABC" in my current dwg.

The following code doesn't work:   eSelfReference !!!why ???

 

 

            Document doc = CADAPP.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
 
            try
            {
                using (Database dbSource = new Database(false, true))
                {    
                    dbdbSource .ReadDwgFile(@"d:\blocks.dwg", System.IO.FileShare.Read, true, null);
                    dbdbSource .CloseInput(true);
 
                    using (DocumentLock docLock = doc.LockDocument()) 
                    {
                        using (Transaction trans = doc.TransactionManager.StartTransaction())
                        {
                            //insert it as a new block
                            ObjectId id = doc.Database.Insert("ABC", dbdbSource , false);
                            trans.Commit();                           
                        }
                    }
                }
            }
            catch (Exception eee)
            {
                MessageBox.Show(eee.Message);
            }

 

0 Likes
1,983 Views
8 Replies
Replies (8)
Message 2 of 9

norman.yuan
Mentor
Mentor

@wokeyiyognshenme wrote:

... there are many block definition in the  blocks.dwg. one of them called "ABC"

 

 So, the DWG file you are to insert into current drawing database IS NOT A BLOCK drawing, rather, it is a drawing containing many block definitions. If you insert a drawing into the current database, the inserted drawing would becomes a BLOCK DEFINITION in the current drawing with the name supplied in the Database.Insert() method. So, you want the entire "blocks.dwg" inserted into current database as a block definition with name "ABC", but the intended block definition has already contained a block definition inside with the name "ABC", hence the error.

 

If you want to import a block definition from external drawing, which is not a block drawing and may contains multiple block definitions, you DO NOT INSERT the entire drawing; instead, you use WBlockCloneObjects() to bring in one or more block definitions in interest.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 9

_gile
Consultant
Consultant

Hi,

 

To import a BlockTableRecord from an external Database, you should use WBlockCloneObjects.

EDIT, @norman.yuan replied faster.

 

 

        public static void ImportABC()
        {
            var targetDb = HostApplicationServices.WorkingDatabase;
            var ids = new ObjectIdCollection();
            using (var sourceDb = new Database(false, true))
            {
                sourceDb.ReadDwgFile(@"D:\blocks.dwg", FileOpenMode.OpenForReadAndReadShare, false, null);
                using (var tr = sourceDb.TransactionManager.StartTransaction())
                {
                    var bt = (BlockTable)tr.GetObject(sourceDb.BlockTableId, OpenMode.ForRead);
                    if (bt.Has("ABC"))
                    {
                        ids.Add(bt["ABC"]);
                    }
                    tr.Commit();
                }
                if (ids.Count == 1)
                {
                    var mapping = new IdMapping();
                    targetDb.WblockCloneObjects(ids, targetDb.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
                }
            }
        }

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 9

wokeyiyognshenme
Advocate
Advocate

your code doesn't work。

 

private void button8_Click(object sender, EventArgs e)
{
Document doc = CADAPP.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;

using (doc.LockDocument())
{
ImportABC();
}
}

 

 // Error: ePermanentlyErased。

targetDb.WblockCloneObjects(ids, targetDb.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);

0 Likes
Message 5 of 9

wokeyiyognshenme
Advocate
Advocate

how to use WBlockCloneObjects() could you please give an example

0 Likes
Message 6 of 9

_gile
Consultant
Consultant
Could you try it again, I corrected the code.


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 9

wokeyiyognshenme
Advocate
Advocate


your code doesn't work.

I try it twice .


first time :
Unhandled Access Violation Reading 0x0170 Exception at CC3F58BFh

second time:
Unhandled Access Violation Reading 0x0128 Exception at CC3F58BFh

 

private void button10_Click(object sender, EventArgs e)
        {
            Document doc = CADAPP.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;          

            using (doc.LockDocument())
            {
                ImportBlocks_gile(db, @"d:\abclqh.dwg");
            }
        }

 
        public  void ImportBlocks_gile (Database destDb, string filename)
        {
            var ids = new ObjectIdCollection();
            using (var sourceDb = new Database(false, true))
            {
                sourceDb.ReadDwgFile(filename, FileOpenMode.OpenForReadAndReadShare, false, null);
                using (var tr = sourceDb.TransactionManager.StartTransaction())
                {
                    var bt = (BlockTable)tr.GetObject(sourceDb.BlockTableId, OpenMode.ForRead);
                    foreach (ObjectId id in bt)
                    {
                        var btr = (BlockTableRecord)tr.GetObject(id, OpenMode.ForRead);
                        if (!btr.IsAnonymous && !btr.IsLayout)
                            ids.Add(id);
                    }
                    tr.Commit();
                }
                var mapping = new IdMapping();
                destDb.WblockCloneObjects(ids, destDb.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
            }
        }
0 Likes
Message 8 of 9

_gile
Consultant
Consultant

It seems there's some confusion between you two topics.

This one was to import a single block named "ABC".



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 9 of 9

wokeyiyognshenme
Advocate
Advocate

there are many block in the xxx.dwg

 

1 import all of the block

2 import only one 

 

both are ok.

0 Likes