Author Topic: Trouble Opening and Closing Drawing  (Read 3961 times)

0 Members and 1 Guest are viewing this topic.

sling blade

  • Guest
Trouble Opening and Closing Drawing
« on: October 06, 2011, 10:54:18 PM »
Hi,

I am trying to copy and rename my current drawing to a folder. I then close the current drawing and open the renamed copy.

However it seems I can't both open the renamed drawing and close the original. When I run my code the original drawing does not close.

However if I remove the portion of my code that opens the other drawing then the original closes as expected.

Here is my code. Any clues?

Code: [Select]
[CommandMethod("TestClose", CommandFlags.Session)]
        public void TestCloseDocument()
        {
            string fullName = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name;
            string projectDirectory = System.IO.Path.GetDirectoryName(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name);
            string drawingNameWithOutExt = System.IO.Path.GetFileNameWithoutExtension(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name);
            string ext = System.IO.Path.GetExtension(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name);
   
            // create directory if it doesn't exists
            string newTempDir = string.Concat(projectDirectory, "\\temp");
            if (!Directory.Exists(newTempDir))
            {
                Directory.CreateDirectory(newTempDir);
            }


            string fullNewDrawingName = string.Concat(newTempDir, "\\", drawingNameWithOutExt, "_1", ext);
             
            if (!File.Exists(fullNewDrawingName))
                File.Copy(fullName, fullNewDrawingName);

            // if I comment this bit out I can close the drawing
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(fullNewDrawingName, false);

            try
            {
                foreach (Autodesk.AutoCAD.ApplicationServices.Document doc in Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager)
                {
                    if (doc.Name == fullName)
                    {
                        doc.CloseAndDiscard();

                        return;
                    }
                }
            }
            catch (SystemException ex)
            {
                string message = ex.ToString();
            }

        }


Jeff H

  • Needs a day job
  • Posts: 6028
Re: Trouble Opening and Closing Drawing
« Reply #1 on: October 06, 2011, 11:30:29 PM »
Welcome sling blade and hopefully you will get a biscuit
 
 
The drawing is busy and have tried changing the return to a break and moving the open call to end?
 
Code: [Select]

        [CommandMethod("TestClose", CommandFlags.Session)]
        public void TestCloseDocument()
        {
            string fullName = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name;
            string projectDirectory = System.IO.Path.GetDirectoryName(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name);
            string drawingNameWithOutExt = System.IO.Path.GetFileNameWithoutExtension(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name);
            string ext = System.IO.Path.GetExtension(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name);
            // create directory if it doesn't exists
            string newTempDir = string.Concat(projectDirectory, "\\temp");
            if (!Directory.Exists(newTempDir))
            {
                Directory.CreateDirectory(newTempDir);
            }

            string fullNewDrawingName = string.Concat(newTempDir, "\\", drawingNameWithOutExt, "_1", ext);
            if (!File.Exists(fullNewDrawingName))
                File.Copy(fullName, fullNewDrawingName);
 
            try
            {
                foreach (Autodesk.AutoCAD.ApplicationServices.Document doc in Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager)
                {
                    if (doc.Name == fullName)
                    {
                        doc.CloseAndDiscard();
                        break;
                    }
                }
                //
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(fullNewDrawingName, false);
            }
            catch (SystemException ex)
            {
                string message = ex.ToString();
            }
        }
 

sling blade

  • Guest
Re: Trouble Opening and Closing Drawing
« Reply #2 on: October 08, 2011, 05:13:29 PM »
Hi,

I moved the open call to the end and replaced return with break but it doesn't seem to make a difference.

BTW: doc.CloseAndDiscard throws an error and so the following line with the return statement was never called, that why I didn't catch that.

This means in your example it won't call the Open method either, it would need to be place outside the try catch.

I can either open the newly copied drawing or close the current drawing but I can't do both in the same command and it doesn't matter which order I do them in.

Can you confirm this behavior?

Jeff H

  • Needs a day job
  • Posts: 6028
Re: Trouble Opening and Closing Drawing
« Reply #3 on: October 08, 2011, 07:59:13 PM »
In 2012 it worked fine for me with code in previous post

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Trouble Opening and Closing Drawing
« Reply #4 on: October 08, 2011, 09:42:09 PM »
Scott,

Your description differs from your code.

I have assumed that you WANT to save the current document with a new name ONLY if that name does not exist.

If you want to discard the current doc and open the PREVIOUSLY named file then my code will need to be revised.

This is my hack at the problem :

Code - C#: [Select]
  1. // kdub 2011-10-09
  2. // theSwamp : http://www.theswamp.org/index.php?topic=39669.msg449677
  3.  
  4. using System;
  5. using System.IO;
  6. using Autodesk.AutoCAD.ApplicationServices;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.Runtime;
  9.  
  10. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  11.  
  12. [assembly: CommandClass(typeof(TestCloseDocument.TestCommands))]
  13.  
  14. namespace TestCloseDocument
  15. {
  16.    internal class TestCommands
  17.    {
  18.        [CommandMethod("TestClose_1", CommandFlags.Session)]
  19.        public void TestCloseDocument_1() {
  20.            var currentQualifiedName = AcadApp.DocumentManager.MdiActiveDocument.Name;
  21.  
  22.            // create directory if it doesn't exists
  23.            var tempFolder = string.Concat(Path.GetDirectoryName(currentQualifiedName), @"\temp");
  24.            if (!Directory.Exists(tempFolder)) {
  25.                Directory.CreateDirectory(tempFolder);
  26.            }
  27.            var newQualifiedName = string.Concat(tempFolder, @"\",
  28.                                                 Path.GetFileNameWithoutExtension(currentQualifiedName), "_1",
  29.                                                 Path.GetExtension(currentQualifiedName));
  30.            //---------------------
  31.            if (File.Exists(newQualifiedName)) {
  32.                AcadApp.ShowAlertDialog("File exists :" + newQualifiedName);
  33.                return;
  34.            }
  35.            // else do the mojo
  36.            var doc = AcadApp.DocumentManager.MdiActiveDocument;
  37.  
  38.            // Save the current document
  39.            doc.Database.SaveAs(currentQualifiedName, true, DwgVersion.Current, doc.Database.SecurityParameters);
  40.  
  41.            // SaveAs with the new Name
  42.            doc.Database.SaveAs(newQualifiedName, true, DwgVersion.Current, doc.Database.SecurityParameters);
  43.        }
  44.    }
  45. }
  46.  



Regards
Kerry


edit->kdub code=csharp formatting
« Last Edit: July 30, 2012, 11:14:31 PM by Kerry »
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Trouble Opening and Closing Drawing
« Reply #5 on: October 08, 2011, 10:24:18 PM »
If you are happy to overwrite the existing file, try something like this :

Code - C#: [Select]
  1.        [CommandMethod("TestClose_2", CommandFlags.Session)]
  2.        public void TestCloseDocument_2()
  3.        {
  4.            var currentQualifiedName = AcadApp.DocumentManager.MdiActiveDocument.Name;
  5.  
  6.            // create directory if it doesn't exists
  7.            var tempFolder = string.Concat(Path.GetDirectoryName(currentQualifiedName), @"\temp");
  8.            if( !Directory.Exists(tempFolder) )
  9.            {
  10.                Directory.CreateDirectory(tempFolder);
  11.            }
  12.            var newQualifiedName = string.Concat(tempFolder, @"\",
  13.                                                 Path.GetFileNameWithoutExtension(currentQualifiedName), "_1",
  14.                                                 Path.GetExtension(currentQualifiedName));
  15.            //---------------------
  16.            var doc = AcadApp.DocumentManager.MdiActiveDocument;
  17.  
  18.            // Save the current document
  19.            // comment out the next line if you DON'T want to save the existing before renaming
  20.            doc.Database.SaveAs(currentQualifiedName, true, DwgVersion.Current, doc.Database.SecurityParameters);
  21.  
  22.            // SaveAs with the new Name
  23.            // Overwrite any existing file.
  24.            doc.Database.SaveAs(newQualifiedName, true, DwgVersion.Current, doc.Database.SecurityParameters);
  25.        }
  26.  

edit->kdub code=csharp formatting
« Last Edit: July 30, 2012, 11:14:04 PM by Kerry »
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

sling blade

  • Guest
Re: Trouble Opening and Closing Drawing
« Reply #6 on: October 08, 2011, 11:53:23 PM »
That is what I should have been trying to do in the first place. I don't know what I was thinking!

It works as expected now.

Awesome, thanks for that.