Author Topic: Wblock and saveas version  (Read 7671 times)

0 Members and 1 Guest are viewing this topic.

Glenn R

  • Guest
Re: Wblock and saveas version
« Reply #15 on: September 23, 2009, 10:25:58 AM »
Give this a whirl. Proof of concept only. Usual warnings apply - add error traps etc. Will only work on current document, so you will have to plant events for all documents that are open and opened.

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace WblockTests
{
    public class Entrypoint : IExtensionApplication
    {
        #region Class vars

        Document currentDoc;
        Database currentDb, wblockedDb;
        Editor ed;

        bool wblockCommandStarted;

        #endregion

        #region IExtensionApplication Members

        public void Initialize()
        {
            // grab standarad things of interest
            currentDoc = acadApp.DocumentManager.MdiActiveDocument;
            currentDb = currentDoc.Database;
            ed = currentDoc.Editor;

            // plant doc events
            currentDoc.CommandWillStart += new CommandEventHandler(currentDoc_CommandWillStart);
            currentDoc.CommandCancelled += new CommandEventHandler(currentDoc_CommandCancelled);
            currentDoc.CommandEnded += new CommandEventHandler(currentDoc_CommandEnded);
            currentDoc.CommandFailed += new CommandEventHandler(currentDoc_CommandFailed);

            // plant database events
            Database.DatabaseConstructed += new EventHandler(Database_DatabaseConstructed);
        }

        void currentDoc_CommandFailed(object sender, CommandEventArgs e)
        {
            if (wblockCommandStarted)
                wblockCommandStarted = false;
        }

        void currentDoc_CommandEnded(object sender, CommandEventArgs e)
        {
            if (wblockCommandStarted)
                wblockCommandStarted = false;
        }

        void currentDoc_CommandCancelled(object sender, CommandEventArgs e)
        {
            if (wblockCommandStarted)
                wblockCommandStarted = false;
        }

        void Database_DatabaseConstructed(object sender, EventArgs e)
        {
            if (wblockCommandStarted)
            {
                wblockedDb = sender as Database;
                // plant some events for this wblocked dbase
                wblockedDb.SaveComplete += new DatabaseIOEventHandler(wblockedDb_SaveComplete);
            }
        }

        void wblockedDb_SaveComplete(object sender, DatabaseIOEventArgs e)
        {
            if (wblockCommandStarted)
            {
                if (wblockedDb.LastSavedAsVersion != DwgVersion.AC1800)
                {
                    wblockedDb.SaveAs(e.FileName, DwgVersion.AC1800);
                }
            }
        }

        void currentDoc_CommandWillStart(object sender, CommandEventArgs e)
        {
            if (e.GlobalCommandName == "WBLOCK")
            {
                wblockCommandStarted = true;
            }
        }

        public void Terminate()
        {
            // Nothing to see here - move along...
        }

        #endregion
    }
}

I think you will get the idea.

Cheers,
Glenn.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Wblock and saveas version
« Reply #16 on: September 23, 2009, 09:28:09 PM »
Glenn this looks pretty trick, I haven't had time to check it out yet but will do tomorrow. I like the way you made the new event handler.

Glenn R

  • Guest
Re: Wblock and saveas version
« Reply #17 on: September 24, 2009, 03:44:44 AM »
Thanks Bryco.

You could shorten what I previously posted, to this:

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace WblockTests
{
    public class Entrypoint : IExtensionApplication
    {
        #region Class vars

        Document currentDoc;
        Database currentDb, wblockedDb;
        Editor ed;

        bool wblockCommandStarted;

        #endregion

        #region IExtensionApplication Members

        public void Initialize()
        {
            // grab standarad things of interest
            currentDoc = acadApp.DocumentManager.MdiActiveDocument;
            currentDb = currentDoc.Database;
            ed = currentDoc.Editor;

            // plant doc events
            currentDoc.CommandWillStart += CommandStartedHandler;
            currentDoc.CommandCancelled += CommandTerminatedHandler;
            currentDoc.CommandEnded += CommandTerminatedHandler;
            currentDoc.CommandFailed += CommandTerminatedHandler;

            // plant database events
            Database.DatabaseConstructed += new EventHandler(Database_DatabaseConstructed);
        }

        void CommandTerminatedHandler(object sender, CommandEventArgs e)
        {
            if (wblockCommandStarted)
                wblockCommandStarted = false;
        }

        void Database_DatabaseConstructed(object sender, EventArgs e)
        {
            if (wblockCommandStarted)
            {
                wblockedDb = sender as Database;
                // plant some events for this wblocked dbase
                wblockedDb.SaveComplete += new DatabaseIOEventHandler(wblockedDb_SaveComplete);
            }
        }

        void wblockedDb_SaveComplete(object sender, DatabaseIOEventArgs e)
        {
            if (wblockCommandStarted)
            {
                if (wblockedDb.LastSavedAsVersion != DwgVersion.AC1800)
                {
                    wblockedDb.SaveAs(e.FileName, DwgVersion.AC1800);
                }
            }
        }

        void CommandStartedHandler(object sender, CommandEventArgs e)
        {
            if (e.GlobalCommandName == "WBLOCK")
            {
                wblockCommandStarted = true;
            }
        }

        public void Terminate()
        {
            // Nothing to see here - move along...
        }

        #endregion
    }
}

« Last Edit: September 24, 2009, 04:06:39 AM by Glenn R »

Glenn R

  • Guest
Re: Wblock and saveas version
« Reply #18 on: September 24, 2009, 03:49:18 AM »
Or...you could use anonymous methods... :evil:

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace WblockTests
{
    public class Entrypoint : IExtensionApplication
    {
        #region Class vars

        Document currentDoc;
        Database currentDb, wblockedDb;
        Editor ed;

        bool wblockCommandStarted;

        #endregion

        #region IExtensionApplication Members

        public void Initialize()
        {
            // grab standarad things of interest
            currentDoc = acadApp.DocumentManager.MdiActiveDocument;
            currentDb = currentDoc.Database;
            ed = currentDoc.Editor;

            // plant doc events
            currentDoc.CommandWillStart +=new CommandEventHandler(currentDoc_CommandWillStart);

            currentDoc.CommandCancelled += delegate(object sender, CommandEventArgs e)
            {
                if (wblockCommandStarted)
                    wblockCommandStarted = false;
            };

            currentDoc.CommandEnded += delegate(object sender, CommandEventArgs e)
            {
                if (wblockCommandStarted)
                    wblockCommandStarted = false;
            };

            currentDoc.CommandFailed += delegate(object sender, CommandEventArgs e)
            {
                if (wblockCommandStarted)
                    wblockCommandStarted = false;
            };

            // plant database events
            Database.DatabaseConstructed += new EventHandler(Database_DatabaseConstructed);
        }

        void Database_DatabaseConstructed(object sender, EventArgs e)
        {
            if (wblockCommandStarted)
            {
                wblockedDb = sender as Database;
                // plant some events for this wblocked dbase
                wblockedDb.SaveComplete += new DatabaseIOEventHandler(wblockedDb_SaveComplete);
            }
        }

        void wblockedDb_SaveComplete(object sender, DatabaseIOEventArgs e)
        {
            if (wblockCommandStarted)
            {
                if (wblockedDb.LastSavedAsVersion != DwgVersion.AC1800)
                {
                    wblockedDb.SaveAs(e.FileName, DwgVersion.AC1800);
                }
            }
        }

        void currentDoc_CommandWillStart(object sender, CommandEventArgs e)
        {
            if (e.GlobalCommandName == "WBLOCK")
            {
                wblockCommandStarted = true;
            }
        }

        public void Terminate()
        {
            // Nothing to see here - move along...
        }

        #endregion
    }
}
« Last Edit: September 24, 2009, 04:05:17 AM by Glenn R »

Glenn R

  • Guest
Re: Wblock and saveas version
« Reply #19 on: September 24, 2009, 04:18:20 AM »
Or...a combination of the 2  :lmao:  :evil:

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace WblockTests
{
    public class Entrypoint : IExtensionApplication
    {
        #region Class vars

        Document currentDoc;
        Database currentDb, wblockedDb;
        Editor ed;

        bool wblockCommandStarted;

        #endregion

        #region IExtensionApplication Members

        public void Initialize()
        {
            // grab standarad things of interest
            currentDoc = acadApp.DocumentManager.MdiActiveDocument;
            currentDb = currentDoc.Database;
            ed = currentDoc.Editor;

            // plant doc events
            currentDoc.CommandCancelled += CommandTerminatedHandler;
            currentDoc.CommandEnded += CommandTerminatedHandler;
            currentDoc.CommandFailed += CommandTerminatedHandler;

            currentDoc.CommandWillStart += delegate(object sender, CommandEventArgs e)
            {
                if (e.GlobalCommandName.TrimStart('+', '-') == "WBLOCK")
                {
                    wblockCommandStarted = true;
                }
            };

            // plant database events
            Database.DatabaseConstructed += delegate(object sender, EventArgs e)
            {
                if (wblockCommandStarted)
                {
                    wblockedDb = sender as Database;
                    wblockedDb.SaveComplete += delegate(object senderDb, DatabaseIOEventArgs eDb)
                    {
                        if (wblockCommandStarted)
                        {
                            if (wblockedDb.LastSavedAsVersion != DwgVersion.AC1800)
                            {
                                wblockedDb.SaveAs(eDb.FileName, DwgVersion.AC1800);
                            }
                        }
                    };
                }
            };
        }

        void CommandTerminatedHandler(object sender, CommandEventArgs e)
        {
            if (wblockCommandStarted)
                wblockCommandStarted = false;
        }

        public void Terminate()
        {
            // Nothing to see here - move along...
        }

        #endregion
    }
}

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Wblock and saveas version
« Reply #20 on: September 24, 2009, 09:30:49 AM »
Glenn it works great but I noticed you dont have a
wblockedDb.SaveComplete -= new DatabaseIOEventHandler(wblockedDb_SaveComplete);
Do you need one?
Also it runs wblockedDb.SaveAs(e.FileName, DwgVersion.AC1800); Perhaps changing the version refires the event, I see no problem with this, just trying to get a better understanding.

Glenn R

  • Guest
Re: Wblock and saveas version
« Reply #21 on: September 24, 2009, 09:38:16 AM »
Glenn it works great but I noticed you dont have a
wblockedDb.SaveComplete -= new DatabaseIOEventHandler(wblockedDb_SaveComplete);
Do you need one?

I used to put those in, especially when Acad was shutting down (docs closing etc.) but I found it can cause troubles, which TonyT confirmed, so now I generally don't. As the Dbase that you're attaching the event to is temporary anyway, it gets destroyed very quickly and I don't see the harm in it for this.

If you want to put one in, you will need to remove it BEFORE the dbase is destroyed.

Also it runs wblockedDb.SaveAs(e.FileName, DwgVersion.AC1800); Perhaps changing the version refires the event, I see no problem with this, just trying to get a better understanding.

You're correct - the SaveAs definitely re-fires the event. Normally, you would stop event re-entry by using some sort of flag - right? In this case, the 'flag' is the 'if' statement testing for the version.

If you've detected and re-saved the dbase already, it's already in the correct format, so the conditional test will fail. Does that explain it?

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Wblock and saveas version
« Reply #22 on: September 24, 2009, 10:55:58 AM »
Yes,good reply.