.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Issues with closeandsave drawings

4 REPLIES 4
Reply
Message 1 of 5
e.g.
434 Views, 4 Replies

Issues with closeandsave drawings

Hi,

 

I have to process a bunch of drawings (Autocad 2014, win7x64), just to open and make sure they are saved in model space. The code I am using is:

 

[CommandMethod("Test2", CommandFlags.Session)]
        public static void Test2()
        {

            var docs = apps.Application.DocumentManager;
            var lstFiles = new List<string>();

            if (docs.Count > 0)
            {
                apps.DocumentCollectionExtension.CloseAll(docs);
            }

            using (System.Windows.Forms.OpenFileDialog opd = new System.Windows.Forms.OpenFileDialog())
            {
                opd.Title = "Select dwg files:";
                opd.Multiselect = true;
                opd.Filter = "dwg files|*.dwg";

                if (opd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    foreach (string s in opd.FileNames)
                    {
                        if (!lstFiles.Contains(s))
                        {
                            lstFiles.Add(s);
                        }
                    }
                }
            }


            if (lstFiles.Count != 0)
            {

                foreach (string s in lstFiles)
                {
                    var doc = apps.DocumentCollectionExtension.Open(docs, s, false);

                    using (Database db = doc.Database)
                    {
                        using (apps.DocumentLock docLock = doc.LockDocument())
                        {
                            using (Transaction tr = db.TransactionManager.StartTransaction())
                            {
                                try
                                {
                                    apps.Application.SetSystemVariable("TILEMODE", (object)1);

                                }
                                catch (System.Exception ex)
                                {
                                    apps.Application.ShowAlertDialog(ex.Message);
                                }
                                tr.Commit();
                            }
                        }
                    }

                    apps.DocumentExtension.CloseAndSave(doc, s);
                }
            }

        }

 

 

But for some reason, it doesn't work; I get the following error:

 

An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code

Additional information: Collection was modified; enumeration operation may not execute.

If there is a handler for this exception, the program may be safely continued.

 

Any idea why this is happening?

 

Thanks,

 

e.g.

4 REPLIES 4
Message 2 of 5
Ajilal.Vijayan
in reply to: e.g.

Hi,

 

Tested the code on 2013 & 2014, but didnt brings any error.

Message 3 of 5
jeff
in reply to: e.g.

Remove

using (Database db = doc.Database)

 

closing the document will handle the database associated with it.

 

 

You can also find your answers @ TheSwamp
Message 4 of 5
_gile
in reply to: e.g.

Hi,

 

Try making the opened document active:

docs.MdiActiveDocument = doc;

 

The DocumentExtension and DocumentCollectionExtension classes define extension methods which can be called as instance methods:

 

docs.closeAll(); instead of DocumentCollectionExtension.CloseAll(docs);

 

docs.Open(s, false); instead of DocumentCollectionExtension.Open(docs, s, false);

 

doc.CloseAndSave(s); instead of DocumentExtension.CloseAndSave(doc, s);



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 5
e.g.
in reply to: _gile

Hi,

 

after intensive testing, it seems that it was something wrong with netframework. I used VS2012, I've downgraded to VS2010, uninstall netframework 4.5, reinstall netframework 4, repair Autocad, then upgraded again to VS2012 and since then, no error. I've even disabled one of the videocards (my laptop has two, some kind of battery life saving method which I kind of not trust it anymore.) because one of the error messages was saying something related to "A display driver file (.HDI) is missing or has been corrupted...".

 

Anyways, since reinstalling the netframework, seems to be ok.

 

Thanks, one more time to all of you,

 

e.g.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost