.NET

Reply
Valued Contributor
Millerni456
Posts: 55
Registered: ‎01-23-2013
Message 1 of 2 (239 Views)

Saving an external/side Database

239 Views, 1 Replies
09-04-2013 06:09 AM

Hey all,

 

I've been coming up with issues writing changes to an external database (one that is lazy-loaded via Database.readDwgFile()).

 

While constructing this post I decided to do a couple more tests to make sure that my setup was not working.

What I found is that it was working!

 

Regardless, I would still like to add to the .NET forums by creating some documentation in case someone else needs it.


 

When working with an external database or side database that is NOT loaded into the AutoCAD editor, I needed to save changes that were made.  Normally, I'd expect that after a transaction is commited, then the changes would reflect...  this is NOT what happened.

 

So I've added this line of code in order to save the database after all changes were committed:

database.SaveAs(drawing.FullName, true, DwgVersion.Current, database.SecurityParameters);

 This method works as described in ObjectARX - Managed Class Reference documentation:


Database.SaveAs Method

Runs the save process on the database and writes the drawing information out to fileName. The fileName argument is taken as is. If no file extension is present, .dwg is not appended.

 

If the database executing the SaveAs() function is not the current database in the AutoCAD editor, then the thumbnail preview image is not saved to fileName.

 

To specify security parameters, set security that conveys your preferences.If the security is not properly initialized, the method fails. See SecurityParameters for more information on initializing this struct.

 

If bBakAndRename is false, then no .bak file is created, a full save is always done, and if the Database being saved is the main database for a document in AutoCAD, then the document's filename will NOT change to the saved name.


Now, when I open the drawing manually, I notice the changes made by the .NET assembly.

I also want to put up here, since it is partially relevant, that I am NOT required to Commit the Transaction for changes to take effect.  This appears to be the default behavior upon the closing of a transaction.  However, if the transaction is aborted, then no changes are made.

ADN Support Specialist
philippe.leefsma
Posts: 642
Registered: ‎06-02-2009
Message 2 of 2 (199 Views)

Re: Saving an external/side Database

09-11-2013 01:56 AM in reply to: Millerni456

Hi

 

Here is a C# sample that illustrates how to work with ReadDwgFile: it will prompt the user to select an entity in the current drawing, then load a side database and clone the object into it. I would recommend you follow that approach when working on a side database you want to modify.

 

[CommandMethod("wblockclone", CommandFlags.Session)]
static public void wblockclone()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    PromptEntityOptions peo = new PromptEntityOptions("\nSelect entity to copy: ");

    PromptEntityResult per = ed.GetEntity(peo);

    if (per.Status != PromptStatus.OK) 
        return;

    using(DocumentLock doclock = doc.LockDocument())
    {
        ObjectIdCollection ObjectcIds = new ObjectIdCollection();
        ObjectcIds.Add(per.ObjectId);

        Database destDb = new Database(false, true);
        destDb.ReadDwgFile("c:\\Temp\\DestDrawing.dwg", System.IO.FileShare.ReadWrite, true, "");

        using (Transaction Tx = destDb.TransactionManager.StartTransaction())
        {
            BlockTable bt = Tx.GetObject(
                destDb.BlockTableId, 
                OpenMode.ForRead) as BlockTable;

            BlockTableRecord btr = Tx.GetObject(
                bt[BlockTableRecord.ModelSpace], 
                OpenMode.ForWrite) as BlockTableRecord;

            IdMapping oIdMap = new IdMapping();

            destDb.WblockCloneObjects(
                ObjectcIds, 
                btr.ObjectId, 
                oIdMap, 
                DuplicateRecordCloning.Ignore, 
                false);

            Tx.Commit();
        }

        destDb.SaveAs(destDb.Filename, DwgVersion.Current);
    }
}

Regards,

Philippe.



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.