TheSwamp

Code Red => .NET => Topic started by: alexborodulin11 on August 25, 2011, 09:08:27 PM

Title: Iterate through documents and close drawings
Post by: alexborodulin11 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
Title: Re: Iterate through documents and close drawings
Post by: jgr on August 25, 2011, 09:28:23 PM
Try with this:
http://through-the-interface.typepad.com/through_the_interface/2007/03/closing_all_ope.html
Title: Re: Iterate through documents and close drawings
Post by: Jeff H 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
Title: Re: Iterate through documents and close drawings
Post by: alexborodulin11 on August 25, 2011, 10:04:24 PM
the same result: current drawing is still open
Title: Re: Iterate through documents and close drawings
Post by: kdub_nz 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();
            }
        }
    }
}
Title: Re: Iterate through documents and close drawings
Post by: alexborodulin11 on August 25, 2011, 10:45:11 PM
Jeff:
Adding

CommandFlags.Session

solved the problem
Thank you!

Alex