[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
Solved! Go to Solution.
Solved by Alexander.Rivilis. Go to Solution.
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); } } } } }
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
Check value of FIBERWORLD system varible. If its value is 0 then set NEXTFIBERWORLD to 1 and restrart AutoCAD. After run your's code.
Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"
Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
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:
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
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
Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"
Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
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
@mike_71 wrote:
...Thanks for your help!!!...
You can do it with Kudos and/or Accept as Solution.
@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.
Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"
Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn