TheSwamp

Code Red => .NET => Topic started by: autogis on October 11, 2016, 02:51:27 PM

Title: Correct way to add existing dwg file to a new drawing using ObjectArx
Post by: autogis on October 11, 2016, 02:51:27 PM
I am creating a new dwg file as follows:

Code: [Select]
DocumentCollection acDocMgr = acApp.DocumentManager;
Document acDoc = acDocMgr.Add("acad");
acApp.DocumentManager.MdiActiveDocument = acDoc;


The next thing I want to do is to import an existing dwg file into this new drawing.  My question is, what is the correct method to import an existing dwg drawing into a new drawing?   The main concern is that the new drawing not be modified in any way.  Here are some methods I have seen but would like to make sure I am not modifying the original drawing that is being imported into the new drawing:

Method 1 with DocumentManager

Code: [Select]
DocumentCollection acDocMgr = Application.DocumentManager;
acDocMgr.Open(strFileName, false);

Method 2, with ReadDwgFile

Code: [Select]
Using db As New Database(False, True)
db.ReadDwgFile(strFileName, FileOpenMode.OpenForReadAndWriteNoShare, True, "")


Again, all I am looking for is the correct method to import an existing dwg into a new drawing without modifying the original dwg file.  Thanks.
Title: Re: Correct way to add existing dwg file to a new drawing using ObjectArx
Post by: Jeff_M on October 11, 2016, 03:16:19 PM
Database.Insert(matrixTransform, sourceDb, preserveSourceDb) will insert the source database's Modelspace into the database invoking the method.
Title: Re: Correct way to add existing dwg file to a new drawing using ObjectArx
Post by: autogis on October 11, 2016, 03:58:53 PM
Jeff thanks!   Quick question since the new drawing would be currently open, what do I set the first parameter (blockName) to?  Here is what I have so far:

Code: [Select]
// Get the current document and database
Document doc = acApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = acApp.DocumentManager.MdiActiveDocument.Database;

// Create a temporary database to import an existing file
using (Database tmpDB = new Database(false, false))
{
string strPath = @"C:\a\1.dwg";
// Read the 'strPath' file into the new database             
tmpDB.ReadDwgFile(strPath, System.IO.FileShare.Read, true, "");
// Try to insert our the dwg file as a block and get the return id of
// the new block table record.
ObjectId BlockID = db.Insert(blockName, tmpDB, true);
}
Title: Re: Correct way to add existing dwg file to a new drawing using ObjectArx
Post by: Jeff_M on October 11, 2016, 04:08:41 PM
If you use the the method I showed, the first parameter is a Matrix3d for the transformation. You probably want this to be with no transformation. You do not supply the name, and you need not insert the block definition to modelspace as the function does that for you.

"Inserts the Model Space of the database pointed to by dataBase into the Model Space of the database invoking the insert() function. All objects being inserted have the xform matrix passed into their transformBy() function during the insertion process."
Title: Re: Correct way to add existing dwg file to a new drawing using ObjectArx
Post by: MexicanCustard on October 12, 2016, 07:56:57 AM
If your first step is to clone a drawing and open it. Then why not File.Copy() first.  Then just open the new file?
Title: Re: Correct way to add existing dwg file to a new drawing using ObjectArx
Post by: autogis on October 13, 2016, 03:37:01 PM
Thanks Jeff, I ended up using your suggestion and set the matrixTransform parameter to null.   That worked fine.
Title: Re: Correct way to add existing dwg file to a new drawing using ObjectArx
Post by: autogis on October 21, 2016, 10:41:25 AM
Jeff, I thought it was working but I am having an issue.  When I create the first drawing and use the "db.insert" method, it works correct.  But if I leave the autocad open and create another drawing, the "db.insert" no longer inserts the drawing.