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

eNoDocument when Disposing DocumentLock

1 REPLY 1
SOLVED
Reply
Message 1 of 2
schwabg
696 Views, 1 Reply

eNoDocument when Disposing DocumentLock

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); } }

 

1 REPLY 1
Message 2 of 2
schwabg
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.

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