.NET

Reply
Contributor
Mike_71
Posts: 24
Registered: ‎03-06-2014
Message 1 of 8 (317 Views)
Accepted Solution

CloseAndDiscard throws an exception.

317 Views, 7 Replies
03-06-2014 12:02 PM
[Autodesk.AutoCAD.Runtime.CommandMethod("CD", CommandFlags.Session)]
        public static void CD()
        {
            DocumentCollection docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
            foreach(Document doc in docs)
            {
                if (doc.IsReadOnly)
                    doc.CloseAndDiscard();
                else
                {
                    if (docs.MdiActiveDocument != doc)
                    {
                        docs.MdiActiveDocument = doc;
                    }

                    doc.CloseAndSave(doc.Name);
                }
            }
        }

 The above code throws an exception from COM. Development environment VS2013, Autocad 2012, ObjectArx 2012. Anyone know why this code throws an exception???

 

Regards,

 

Mike

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

Re: CloseAndDiscard throws an exception.

03-06-2014 12:26 PM in reply to: Mike_71

You have to use LockDocument to avoid this problem

See code, not tested just from the scratch

           [Autodesk.AutoCAD.Runtime.CommandMethod("CdD", CommandFlags.Session)]
           public static void CD()
           {
               DocumentCollection docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
               Document doc = docs.MdiActiveDocument;
               using (DocumentLock doclock = doc.LockDocument())
               {
                   foreach (Document dwg in docs)
                   {
                       if ((dwg.IsReadOnly) && (!dwg.IsNamedDrawing))
                       {
                           dwg.CloseAndDiscard();
                       }
                       else
                       {
                           if (docs.MdiActiveDocument != dwg)
                           {
                               docs.MdiActiveDocument = dwg;
                           }
                           if (dwg != doc)
                           {
                               dwg.CloseAndSave(doc.Name);
                           }
                       }
                   }
               }
           }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Contributor
Mike_71
Posts: 24
Registered: ‎03-06-2014
Message 3 of 8 (279 Views)

Re: CloseAndDiscard throws an exception.

03-07-2014 04:33 AM in reply to: Hallex

Thanks for the reply, however, it still throws an exception. I have a feeling it is because I have only a single drawing file open at the time of execution. I ran the code with multiple files open at the same time and it did close the documents that were not the active one without complaint. That, I  am assuming, is why you check to see if the doc == dwg and do nothing if true.

 

How can I close a document when it is the only one open?

 

Regards,

 

Mike

Moderator
Alexander.Rivilis
Posts: 1,398
Registered: ‎04-09-2008
Message 4 of 8 (274 Views)

Re: CloseAndDiscard throws an exception.

03-07-2014 05:25 AM in reply to: Mike_71

Check value of FIBERWORLD system varible. If its value is 0 then set NEXTFIBERWORLD to 1 and restrart AutoCAD. After run your's code.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Contributor
Mike_71
Posts: 24
Registered: ‎03-06-2014
Message 5 of 8 (262 Views)

Re: CloseAndDiscard throws an exception.

03-07-2014 07:46 AM in reply to: Alexander.Rivilis

Changing FIBERWORLD didn't help either. I don't understand how hard this can possibly be. All I want to do is have a command that:

 

  1. Creates a new drawing
  2. Sets new drawing as the active document.
  3. Close all other drawings other than the new ActiveDocument

See code below:

 

///----------------------------------------------------------------------------
        /// <summary>
        /// Creates a new detail from scratch.
        /// </summary>
        ///----------------------------------------------------------------------------
        [Autodesk.AutoCAD.Runtime.CommandMethod("DET_NewDetail", CommandFlags.Session)]
        public static void DET_NewDetail()
        {
            string fileName = @"C:\Temp\Test1234.dwg";

            Document doc = CreateNewDrawing(fileName);
            CloseDrawings(doc);
        }

   ///----------------------------------------------------------------------------
        /// <summary>
        /// Creates a new drawing from a template.
        /// </summary>
        ///----------------------------------------------------------------------------
        private static Document CreateNewDrawing(object data)
        {
            Document newDoc = null;
            try
            {
                string fileName = data as string;
                if (!File.Exists(fileName))
                {
                    using (Database db = new Database(true, false))
                    {
                        db.SaveAs(fileName, DwgVersion.Current);
                        if (File.Exists(fileName))
                            newDoc = Application.DocumentManager.Open(fileName);
                    }
                }
            }
            catch
            { }
            return newDoc;
        }
        ///----------------------------------------------------------------------------
        /// <summary>
        /// Closes all drawings other than the current active document.
        /// </summary>
        ///----------------------------------------------------------------------------
        private static void CloseDrawings(object data)
        {
            try
            {
                Document doc = data as Document;
                if (doc != null && Application.DocumentManager.MdiActiveDocument != data)
                    Application.DocumentManager.MdiActiveDocument = doc;

                foreach (Document dwg in Application.DocumentManager)
                {
                    if (dwg != Application.DocumentManager.MdiActiveDocument)
                    {
                        if (dwg.IsReadOnly)
                            dwg.CloseAndDiscard();
                        else
                            dwg.CloseAndSave(dwg.Name);
                    }
                }
            }
            catch
            { }
        }

 Any help would be appreciated. Again, this is for Autocad 2012.

 

Regards,

 

Mike

Moderator
Alexander.Rivilis
Posts: 1,398
Registered: ‎04-09-2008
Message 6 of 8 (259 Views)

Re: CloseAndDiscard throws an exception.

03-07-2014 07:55 AM in reply to: Mike_71

1. What value of FIBERWORLD is now?

2. It is impossible to close nonactive document.

So in order to close document you have to make it active before execute CloseAndDiscard or CloseAndSave methods.

 

This issue was discussed here: http://adn-cis.org/forum/index.php?topic=500.0


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Contributor
Mike_71
Posts: 24
Registered: ‎03-06-2014
Message 7 of 8 (245 Views)

Re: CloseAndDiscard throws an exception.

03-07-2014 11:55 AM in reply to: Alexander.Rivilis

I have it working, see code below. I think it had to do with active vs non-active document as you have said. Not really sure, rewrote the code and now it works without exceptions.

 

///----------------------------------------------------------------------------
        /// <summary>
        /// Creates a new detail from scratch.
        /// </summary>
        ///----------------------------------------------------------------------------
        [Autodesk.AutoCAD.Runtime.CommandMethod("DET_NewDetail", CommandFlags.Session)]
        public static void DET_NewDetail()
        {
            CloseDrawings();
            CreateNewDrawing(@"C:\Temp\Test123.dwg");
        }

    ///----------------------------------------------------------------------------
        /// <summary>
        /// Creates a new drawing from a template.
        /// </summary>
        ///----------------------------------------------------------------------------
        private static Document CreateNewDrawing(string fileName)
        {
            Document doc = null;
            try
            {
                using (Database db = new Database(true, true))
                {
                    db.SaveAs(fileName, DwgVersion.Current);
                    if (File.Exists(fileName))
                    {
                        doc = Application.DocumentManager.Open(fileName, false);
                        Application.DocumentManager.MdiActiveDocument = doc;
                    }
                }
            }
            catch
            {
            }
            return doc;
        }
        ///----------------------------------------------------------------------------
        /// <summary>
        /// Closes all drawings.
        /// </summary>
        ///----------------------------------------------------------------------------
        private static void CloseDrawings()
        {
            try
            {
                if (!Application.DocumentManager.IsApplicationContext)
                    return;

                DocumentCollection docs = Application.DocumentManager;
                foreach(Document doc in docs)
                {
                    if (doc.IsReadOnly)
                        doc.CloseAndDiscard();
                    else
                    {
                        if (docs.MdiActiveDocument != doc)
                            docs.MdiActiveDocument = doc;

                        int isModified = System.Convert.ToInt32(Application.GetSystemVariable("DBMOD"));
                        if (isModified == 0)
                            doc.CloseAndDiscard();
                        else
                            doc.CloseAndSave(doc.Name);
                    }
                }
            }
            catch
            { }
        }

 Thanks for your help!!! Fiberworld I believe was initialy 0 for .net Debugging purposes. I have changed it back to 1 but it made no difference to the original code.

 

Regards,

 

Mike

Moderator
Alexander.Rivilis
Posts: 1,398
Registered: ‎04-09-2008
Message 8 of 8 (241 Views)

Re: CloseAndDiscard throws an exception.

03-07-2014 12:06 PM in reply to: Mike_71

Mike_71 wrote:
...Thanks for your help!!!...

You can do it with Kudos and/or Accept as Solution. :smileyhappy:

 


Mike_71 wrote:
...Fiberworld I believe was initialy 0 for .net Debugging purposes. I have changed it back to 1 but it made no difference to the original code...

After testing guys investigate that with Fiberworld == 0 impossible close both active and nonactive document.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

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 to get help installing your software.

Ask the Community