Author Topic: CloseAndDiscard 2010 vs 2011 & 2012  (Read 3207 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
CloseAndDiscard 2010 vs 2011 & 2012
« on: June 10, 2011, 01:49:46 PM »
I am, once again, at a loss why something works in one version but not another. This code works great in Acad2010:
Code: [Select]
        [CommandMethod("NewCommand", CommandFlags.Session)]
        public void newcommandcommand()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            progFrm = new ProgressForm();
            string dwgName = doc.Name + " - edited";
            DocumentLock doclock = doc.LockDocument();

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                //edit a bunch of stuff in the drawing
                tr.Commit();
             }

            try
            {
                db.SaveAs(dwgName, DwgVersion.Current);
            }
            catch
            {
                ed.WriteMessage("\nDrawing edited but unable to save. Please perform a manual save if desired.");
            }


            if (File.Exists(dwgName))
            {
                Document newdoc = Application.DocumentManager.Open(dwgName, false);
                Application.DocumentManager.MdiActiveDocument = newdoc;
                doc.CloseAndDiscard();
                newdoc.Editor.WriteMessage("\nDrawing edited and saved as: " + dwgName);
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
         }
However, in 2011 it throws a "Drawing is busy" exception. And, worse, in 2012 it throws a Fatal Error and shuts down. In both cases it fails at the "doc.CloseAndDiscard()" line. Anyone see what I'm missing to be able to close the drawing in which the command started?

Jeff H

  • Needs a day job
  • Posts: 6144
Re: CloseAndDiscard 2010 vs 2011 & 2012
« Reply #1 on: June 10, 2011, 02:54:12 PM »
Hey Jeff,



In 2012 it worked for me if I close the document before opening the new one.

another change was how the new doc was named

Code: [Select]
        [CommandMethod("NewCommand", CommandFlags.Session)]
        public void newcommandcommand()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            //progFrm = new ProgressForm();
            string fileName = Path.GetFileNameWithoutExtension(doc.Name) + " - edited" + ".dwg";
            string dwgName = Path.GetDirectoryName(doc.Name) + "\\" + fileName;
            DocumentLock doclock = doc.LockDocument();

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                //edit a bunch of stuff in the drawing
                tr.Commit();
            }

            try
            {
                db.SaveAs(dwgName, DwgVersion.Current);
            }
            catch
            {
                ed.WriteMessage("\nDrawing edited but unable to save. Please perform a manual save if desired.");
            }


            if (File.Exists(dwgName))
            {
                doc.CloseAndDiscard();
                Document newdoc = Application.DocumentManager.Open(dwgName, false);
                Application.DocumentManager.MdiActiveDocument = newdoc;               
                newdoc.Editor.WriteMessage("\nDrawing edited and saved as: " + dwgName);
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: CloseAndDiscard 2010 vs 2011 & 2012
« Reply #2 on: June 10, 2011, 03:06:49 PM »
Oops, I had edited for posting the drawing name part and missed the ".dwg" portion.

And whatdoya know....changing the location of the DiscardAndClose fixed it for 2011 as well. Thanks, Jeff. I wonder why having another drawing open would trip it up....

Jeff H

  • Needs a day job
  • Posts: 6144
Re: CloseAndDiscard 2010 vs 2011 & 2012
« Reply #3 on: June 10, 2011, 03:14:11 PM »
I am not sure but I think it has something to do with starting the command in 'drawingA' then making 'drawingB' active and trying to close it.


Just guessing that the drawing that started the command that calls CloseAndDiscard must be active to call CloseAndDiscard on itself.

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: CloseAndDiscard 2010 vs 2011 & 2012
« Reply #4 on: June 10, 2011, 03:35:19 PM »
Ah, yes, that does make sense. Thanks again!