Author Topic: Iterate through documents and close drawings  (Read 2485 times)

0 Members and 1 Guest are viewing this topic.

alexborodulin11

  • Guest
Iterate through documents and close drawings
« on: August 25, 2011, 09:08:27 PM »
Why after iterating through all drawings with closing each document similar to code shown below, one drawing is still on the screen. How to iterate and close all drawings?

  Dim docs As DocumentCollection
        Dim doc As Document
                docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
        For Each doc In docs
            'do things,
'then
'doc.CloseAndDiscard() or
'doc.CloseAndSave(doc.Name)
        Next


Jeff H

  • Needs a day job
  • Posts: 6140
Re: Iterate through documents and close drawings
« Reply #2 on: August 25, 2011, 09:44:07 PM »
Welcome to the Swamp!!!
 
 
Are you in Application execution context?-----Session command flag
 
This works fine for me
 
Code: [Select]

         <CommandMethod("CloseAllDrawings", CommandFlags.Session)> _
        Public Sub CloseAllDrawings()
            Dim docs As DocumentCollection = Application.DocumentManager
            For Each doc As Document In docs
                doc.CloseAndDiscard()
            Next
        End Sub

 
This will leave one the current document open.
It executes in the 'document context'
 
 
Code: [Select]

        <CommandMethod("CloseAllDrawingsWithoutSession")> _
        Public Sub CloseAllDrawingsWithoutSession()
            Dim docs As DocumentCollection = Application.DocumentManager
            For Each doc As Document In docs
                Try
                    doc.CloseAndDiscard()
                Catch ex As System.Exception
                End Try
            Next
        End Sub

If I remember correctly since it executes in 'document context' it is locked automatically not allowing it to close

alexborodulin11

  • Guest
Re: Iterate through documents and close drawings
« Reply #3 on: August 25, 2011, 10:04:24 PM »
the same result: current drawing is still open

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2070
  • class keyThumper<T>:ILazy<T>
Re: Iterate through documents and close drawings
« Reply #4 on: August 25, 2011, 10:34:11 PM »

Works for me

Code: [Select]
// (C) kdub
//
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(CloseAllDocuments.MyCommands))]

namespace CloseAllDocuments
{
    public class MyCommands
    {
        [CommandMethod("CloseAllDrawings", CommandFlags.Modal | CommandFlags.Session)]
        public void CloseAllDrawings()
        {
            DocumentCollection docs = Application.DocumentManager;
            foreach (Document doc in docs)
            {
                doc.CloseAndDiscard();
            }
        }
    }
}
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00


some people complain about loading the dishwasher

Sometimes the question is more important than the answer.

alexborodulin11

  • Guest
Re: Iterate through documents and close drawings
« Reply #5 on: August 25, 2011, 10:45:11 PM »
Jeff:
Adding

CommandFlags.Session

solved the problem
Thank you!

Alex