Insert DWG file

Insert DWG file

Anonymous
Not applicable
4,353 Views
6 Replies
Message 1 of 7

Insert DWG file

Anonymous
Not applicable

I'm trying to insert a DWG file into a drawing. Most of what I can find for reference deals with existing blocks already defined in the block table. I did find this code and I'm getting an 'Unreferenced object' error (but not crashing, I can continue). Locking the document was the last thing I tried. Can someone direct me to a good reference. I've tested the filepath and it is correct. Thank you.

 

        public void InsertEquipment(string file)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                using (DocumentLock docLock = doc.LockDocument())
                {
                    Database sourceDb = new Database(false, true); //Temporary database to hold data for block we want to import

                    try
                    {
                        sourceDb.ReadDwgFile(file, System.IO.FileShare.Read, true, ""); //Read the DWG into a side database
                        db.Insert(file, sourceDb, false);
                        ed.WriteMessage("\nSUCCESS: " + file);
                    }

                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                    finally
                    {
                        sourceDb.Dispose();
                    }
                } 

                tr.Commit();
            }
        }

 

 

0 Likes
Accepted solutions (1)
4,354 Views
6 Replies
Replies (6)
Message 2 of 7

_gile
Consultant
Consultant

Hi,

 

Try like this (note the first argument for this Database.Insert overload is the block name, not the complete file name):

 

        public void InsertEquipment(string fileName, string blockName)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            using (DocumentLock docLock = doc.LockDocument())
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (bt.Has(blockName))
                    return;
                using (Database sourceDb = new Database(false, true)) //Temporary database to hold data for block we want to import
                {
                    try
                    {
                        sourceDb.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, true, ""); //Read the DWG into a side database
                        db.Insert(blockName, sourceDb, false);
                        ed.WriteMessage("\nSUCCESS: " + blockName);
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        Application.ShowAlertDialog(ex.Message);
                    }
                }
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 7

Anonymous
Not applicable

Right, the problem is i'm trying to insert a file I just created from a collection of solids into a different drawing, it doesn't contain any blocks. I could create a block from those objects first if I needed to. But I started off just trying to reproduce the editor workflow of inserting a DWG pragmatically. Thank you for your reply.

0 Likes
Message 4 of 7

_gile
Consultant
Consultant
Accepted solution

Sorry, I'm not sure to fully understand what you want to achieve.

The Database.Insert() method is overloaded.

 

If you want to insert the contents of the model space of the source database into the model space of the targeted database, use:

Database.Insert(Matrix3d transform, Database database, bool preserveSourceDatabase)

 

If you want to add a new block table record containing the source database model space in the bloc table of the targeted database (as the INSERT command does), use:

Database.Insert(string blockName, Database database, bool preserveSourceDatabase)

 

If you want to add a new block table record containing the entities contained in some block table record of the bloc table of the targeted database, use:

Database.Insert(string sourceBlockName, string destinationBlockName, Database database, bool preserveSourceDatabase)



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 7

ActivistInvestor
Mentor
Mentor

In your initial post you said:

 

"I'm trying to insert a DWG file into a drawing. "

 

No one can tell exactly what that means. E.g., insert a DWG into a drawing as a block, or insert the model space entities of a DWG file into a drawing as entities in the drawing's model space.

 

Also, you say the drawing you're trying to insert was just created. How?  Was it created by your code?

 

 


@Anonymouswrote:

Right, the problem is i'm trying to insert a file I just created from a collection of solids into a different drawing, it doesn't contain any blocks. I could create a block from those objects first if I needed to. But I started off just trying to reproduce the editor workflow of inserting a DWG pragmatically. Thank you for your reply.


 

0 Likes
Message 6 of 7

Anonymous
Not applicable

Thank you for your reply. After learning what I needed to know, I can see why my question wasn't clear. I'm away from my work PC and cannot post the other part of the code.  But come to find out what I'm trying to do is insert the source database model space into the block table on the targeted database. I'll work through it and post back, thank you.

0 Likes
Message 7 of 7

Anonymous
Not applicable

Thank you. I was able to gt it working. 

0 Likes