.NET

Reply
Contributor
Luigi71
Posts: 22
Registered: ‎08-30-2011
Message 1 of 7 (365 Views)
Accepted Solution

Changed drawings not saved

365 Views, 6 Replies
05-06-2013 01:45 PM

Hi to all, in a multi-document autocad session, after making changes programmatically in all drawings opened, when closing autocad, I'm only asked to save the changes for the current drawing. The others drawings are closed without asking to save them. The changes are made as I'm expected, but autocad don't aks to save the modified drawings.

Any idea or suggestion?

 

Thank you to all

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 7 (320 Views)

Re: Changed drawings not saved

05-08-2013 07:05 AM in reply to: Luigi71

Hi Luigi, I didn't fully understand what you've expected,

here is working code tested on A2010 only, try it, just

chage directory path in the code

        [CommandMethod("baff")]
        public static void TestLoopFiles()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            using (DocumentLock doclock = doc.LockDocument())
            {
                string[] filenames = Directory.GetFiles(@"C:\Test\Temp", "*.dwg", SearchOption.TopDirectoryOnly);

                try
                {
                    foreach (string dwgname in filenames)
                    {
                        using (Database db = new Database(false, true))
                        {
                            db.ReadDwgFile(dwgname, FileOpenMode.OpenForReadAndAllShare, true, null);

                            if (db != HostApplicationServices.WorkingDatabase)

                                HostApplicationServices.WorkingDatabase = db;

                            using (Transaction tr = db.TransactionManager.StartTransaction())
                            {
                                DBDictionary ldict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);

                                foreach (DBDictionaryEntry dicent in ldict)
                                {
                                    Layout lay = (Layout)dicent.Value.GetObject(OpenMode.ForRead);

                                    //do your work in the every layout, e.g. draw circle:

                                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(lay.BlockTableRecordId, OpenMode.ForWrite);

                                    Circle circ = new Circle(new Point3d(0, 0, 0), new Vector3d(0, 0, 1), 1.2345);

                                    circ.ColorIndex = 1;

                                    btr.AppendEntity(circ);

                                    tr.AddNewlyCreatedDBObject(circ, true);

                                }

                            }

                            HostApplicationServices.WorkingDatabase = doc.Database;

                            db.RetainOriginalThumbnailBitmap = true;

                            db.SaveAs(dwgname, DwgVersion.Current);
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.StackTrace + "\n" + ex.StackTrace);
                }
                finally
                {
                    MessageBox.Show("Pokey");
                }
            }
        }

 

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
*Expert Elite*
norman.yuan
Posts: 984
Registered: ‎04-27-2009
Message 3 of 7 (308 Views)

Re: Changed drawings not saved

05-08-2013 10:31 AM in reply to: Luigi71

I can reproduce the issue you described. Here is what I did:

 

1. Write following test code:

 

namespace MultiDocChanges
{
    public class MyCommands
    {
        [CommandMethod("ChangeDocs")]
        public static void RunMyCommand()
        {
            foreach (Document doc in Application.DocumentManager)
            {
                using (DocumentLock dlock = doc.LockDocument())
                {
                    AddLineInDrawing(doc);

                    //Application.ShowAlertDialog("?");
                }
            }

            Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
        }

        private static void AddLineInDrawing(Document dwg)
        {
            using (Transaction tran = dwg.TransactionManager.StartTransaction())
            {
                Line line = new Line(new Point3d(1.0,1.0,0.0), new Point3d(20.0, 20.0, 0.0));
                line.SetDatabaseDefaults(dwg.Database);
                line.Visible = true;

                BlockTableRecord model = (BlockTableRecord)tran.GetObject(
                    SymbolUtilityServices.GetBlockModelSpaceId(dwg.Database), OpenMode.ForWrite);

                model.AppendEntity(line);
                tran.AddNewlyCreatedDBObject(line, true);

                tran.Commit();
            }
        }
    }
}

 The code is to loop through all drawings opened in AutoCAD and make change to each drawing (adding a line).

 

2. Start AutoCAD, opened 3 drawings. Before running the code, make sure all drawing is saved. At this stage, I enter (getvar "DBMOD") at command line for each drawing (use Ctrl+Tab, to switch drawing, so that DBMOD would not be changed accidently due to mouse move), I get 0, meaning all 3 drawiing have no change to be saved.

 

3. Run the code. As result, all 3 drawiings are changed.

 

4. Close each drawing (clicking "x" on drawing window. Only the current active drawinig prompts for saving change, other drawings simply close (thus, the change made by code is lost.

 

5. After running the code, instead of closing each drawing, I enter (getvar "DBMOD") at command line (switch to each drawing by Ctrl+Tab), only the drawing that is active when the code is running has DBMOD value being greater than 1, other drawings' DBMOD remain as 0. This explains why saving change is not prompted.

 

6. Even funny thing is, the line drawn by the code is only visible iin the drawing that is active when the code runs. on other drawings, the line is not visible, nor can be selected by mouse (using selecting window). But, if press Ctrl+A (select all), the line's grips show up (indicating being selected), and quick selecting tool in the property window can also select the invisible line. "regen" command does not bring the line into visible.

 

7. After running the code, as I said, the drawings that are not active has DBMOD value equal to 0. If I save the drawing anyway (by press Ctrl+S, or "Save" menu), then close the drawiing and reopen it, the invisible line added by the code is now visible, which proves that the code did add a line to the drawing.

 

So, I'd consider it might be a bug: when the drawing is not current active drawing, The change to the drawing's database somehow does not make it visible. Also, if the database change does not result in graphic change, DBMOD is not updated, thus no saving prompt.

 

 

 

Contributor
Luigi71
Posts: 22
Registered: ‎08-30-2011
Message 4 of 7 (281 Views)

Re: Changed drawings not saved

05-09-2013 03:15 AM in reply to: Hallex

Thankyou Allex for the kindly answer. I didn't tested you snippet, it seems to me to understand that you suggest to open  drawing individually, make changes and then save them. My issue is slightly different: I'm working with more than one document, my application make some changes in all documents of the current session. Scrolling through the documents i can see the modifications are made, but when I close AutoCAD (the application not the documents), I'm asked to save only the active drawing, the other drawings are closed without saving them and thus losing the changes. On the other hand, as per Norman suggestion more below, I think it can be a bug.

Contributor
Luigi71
Posts: 22
Registered: ‎08-30-2011
Message 5 of 7 (279 Views)

Re: Changed drawings not saved

05-09-2013 03:32 AM in reply to: norman.yuan

Hi Norman and thank you for the analysis of the issue, as per your suggestion it seems to be a bug (maybe). Looping through the documents my application do the changes keeping reference to the documents database, but the active drawing still remains the same. When my application has done the changes, I switch to the other documents and I can see the modifications were made, so the application has worked correctly.

It seems that the only way to solve the problem is that the application has to activate the document and then make the changes. Doing so when closing AutoCAD I'm asked to save all the documents that were modified.

 

So I have noted  two situations:

 

  • Looping documents only referencing the document database, but not activating the related drawing, I'm asked to save only the active drawing;
  • Looping documents referencing the document database and activating the related drawing, I'm asked to save all the changed drawings;

Thank you

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 6 of 7 (245 Views)

Re: Changed drawings not saved

05-09-2013 11:22 PM in reply to: norman.yuan
This is a known issue with DBMOD. It's value is not updated when changes are made to non-active documents. The graphics are also not updated after switching to a document that was modified while it was not active.
Contributor
Luigi71
Posts: 22
Registered: ‎08-30-2011
Message 7 of 7 (234 Views)

Re: Changed drawings not saved

05-10-2013 03:50 AM in reply to: Luigi71

Hi DiningPhilosopher, ok established that it is an issue, there's a way to change programmatically the value of DBMOD of the drawing? If so, the problem is solved elegantly, while remaining on the same document without scrolling all open documents, we can do our edit tasks, change DBMOD of the modified documents in a silent  way and you're done.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

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