.NET

Reply
Member
schwabg
Posts: 5
Registered: ‎09-12-2013
Message 1 of 2 (145 Views)
Accepted Solution

eNoDocument when Disposing DocumentLock

145 Views, 1 Replies
11-04-2013 02:43 PM

Hello, I am attempting to run a command over many files in a directory. I have a function that successfully iterates over the files in a directory and opens them, and then closes them again. However when I try to run a command on each file, my DocumentLock throws an eNoDocument exception. Maybe it has something to do with the fact that when my documents are opened, they have dialog boxes come up asking whether or not to show proxy images? Here is my code:

 

delegate void DoSomething(Document doc);

[CommandMethod("MASSINSERTGP4")] public void MassInsertGP4() { DoSomethingEnMasse(delegate(Document doc) { doc.SendStringToExecute("IEMBED all\n\n", true, false, false); }, true); } private void DoSomethingEnMasse(DoSomething something, bool savechanges) { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; try { PromptResult pr1 = ed.GetString("Directory path"); if (pr1.Status != PromptStatus.OK) { ed.WriteMessage("incorrect usage"); return; } ed.WriteMessage("Starting at " + DateTime.Now + "\n"); IEnumerable<string> files = new DirectoryInfo(pr1.StringResult).GetFiles().Where(x => x.Extension.ToLower() == ".dwg").Select(x => x.FullName); int i = 0; int count = files.Count(); foreach (string file in files) { try { i++; ed.WriteMessage("Processing " + i + " of " + count + ": " + file + "\n"); Document doc = Application.DocumentManager.Open(file, false); // DO THE THING THAT GETS DONE EN MASSE using (doc.LockDocument()) { something(doc); } if (savechanges) doc.CloseAndSave(file); else doc.CloseAndDiscard(); } catch (System.Exception ex) { ed.WriteMessage("Error occured: " + ex.Message + "\n" + ex.StackTrace); } } ed.WriteMessage("Done at " + DateTime.Now + "\n"); } catch(System.Exception ex) { ed.WriteMessage("Error occured: " + ex.Message + "\n" + ex.StackTrace); } }

 

My problem was that I did not understand that executing a command changes the document context and stops execution, so i was manually closing these documents, and then the execution would continue, and find no document.

Furthermore, I needed to change the attributes on my method to be
[CommandMethod("MASSINSERTGP4", CommandFlags.Session)]
and then everything was fixed.
Member
schwabg
Posts: 5
Registered: ‎09-12-2013
Message 2 of 2 (121 Views)

Re: eNoDocument when Disposing DocumentLock

11-04-2013 05:59 PM in reply to: schwabg
My problem was that I did not understand that executing a command changes the document context and stops execution, so i was manually closing these documents, and then the execution would continue, and find no document.

Furthermore, I needed to change the attributes on my method to be
[CommandMethod("MASSINSERTGP4", CommandFlags.Session)]
and then everything was fixed.
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.