Author Topic: Event when Group is copied  (Read 2458 times)

0 Members and 1 Guest are viewing this topic.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Event when Group is copied
« on: August 15, 2014, 02:22:43 PM »
I'm trying to control a particular anonymous Group's Description property so I can count them after the drawing is finished.  When I create the Group I set the Description but if the user copies the Group the property is set to null on the new Group.  I've tried creating an ObjectOverrule for Groups but only the Open event fires when a Group is copied. 

I also tried Database.BeginDeepClone and Database.DeepCloneEnded but in both situations the IdMapping is empty. So, like Database.ObjectAppended there is no way to tell if the new Group came from one of the Groups I'm counting.

Any ideas on how to know when a group has been copied?  I know blocks are a better tool for this but this is MEP and if you put pipes and fittings in blocks the connectors will not connect to piping and fittings outside the block.
Revit 2019, AMEP 2019 64bit Win 10

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Event when Group is copied
« Reply #1 on: August 15, 2014, 03:41:41 PM »
Hi MC,


You have probably already tried this but thought I would suggest it.  You mention that you tried overrules but the only event that was firing was the open event.  Have you tried overruling the close method as outlined in this post?  http://www.theswamp.org/index.php?topic=43356.msg486228#msg486228.

Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

n.yuan

  • Bull Frog
  • Posts: 348
Re: Event when Group is copied
« Reply #2 on: August 15, 2014, 05:36:28 PM »
When you say " I've tried creating an ObjectOverrule for Groups but only the Open event fires when a Group is copied", are you saying that in your overridden Open() and Close() methods, only code in the Open() method runs and code in Close() is not triggered? Or, you did not override Close() method?

I wrote a simple ObjectOverrule class and did a quick test:

Code: [Select]
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace GroupObjectOverrule
{
    public class MyGroupObjectOverrule : ObjectOverrule
    {
        private static MyGroupObjectOverrule _instance = null;

        public static MyGroupObjectOverrule Instance
        {
            get
            {
                if (_instance == null) _instance = new MyGroupObjectOverrule();
                return _instance;
            }
        }

        public void StartOverrule()
        {
            AddOverrule(RXClass.GetClass(typeof(Group)), this, false);
            this.SetNoFilter();
            Overruling = true;
        }

        public void StopOverrule()
        {
            RemoveOverrule(RXClass.GetClass(typeof(Group)), this);
            Overruling = false;
        }

        public override void Open(DBObject dbObject, OpenMode mode)
        {
            base.Open(dbObject, mode);

            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //ed.WriteMessage("\nDBObject is opened: {0}", dbObject.GetType().Name);
        }

        public override void Close(DBObject dbObject)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //ed.WriteMessage("\nDBObject is to be closed: {0}", dbObject.GetType().Name);

            if (dbObject.IsNewObject)
            {
                Group g = dbObject as Group;
                if (g != null)
                {
                    ed.WriteMessage(
                        "\nGroup name: {0}; group description: {1}\n",
                        g.Name, g.Description);
                }
            }

            base.Close(dbObject);
        }
    }
}

and

Code: [Select]
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(GroupObjectOverrule.MyCadCommands))]

namespace GroupObjectOverrule
{
    public class MyCadCommands
    {
        private static bool _overruled = false;

        [CommandMethod("GroupOR")]
        public static void ToggleGroupOverrule()
        {
            Document dwg = CadApp.DocumentManager.MdiActiveDocument;
            if (dwg == null) return;

            Editor ed = dwg.Editor;

            try
            {
                if (!_overruled)
                {
                    MyGroupObjectOverrule.Instance.StartOverrule();
                    _overruled = true;
                    ed.WriteMessage("\nMyGroupOverrule enabled.");
                }
                else
                {
                    MyGroupObjectOverrule.Instance.StopOverrule();
                    _overruled = false;
                    ed.WriteMessage("\nMyGroupOverrule disabled.");
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: {0}\n{1}", ex.Message, ex.StackTrace);
            }
            finally
            {
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }
    }
}

In testing, I start AutoCAD, NETLOAD the code, run the command GROUPOR.

Then I create a few entities, run command GROUP to put the entities in a anonymous group. Then I select the group, and copy the group to different location. I can clearly see the command line list the new anonymous group name (*A2) printed by my code in Close() method.

By the way, the original anonymous group is named *A1, if I use "ClassicGroup" command to examine the groups in drawing.

So, yes, you can use ObjectOverrule to catch the newly created group. However, you may need to find out a way to determine if the newly created group is your targeting group you want to assign description to it.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Event when Group is copied
« Reply #3 on: August 18, 2014, 08:33:40 AM »
Thanks guys!  I did not overrule the close method.  There is no way to know within the closing method that this new group was created from one of the anonymous groups I'm counting.   So although I could see it was an anonymous group I couldn't be sure if it was one I was counting.

This weekend I thought it might be easiest to include a block within the group. Then I could quantify the blocks and even test them to make sure they belonged to an anonymous group.
Revit 2019, AMEP 2019 64bit Win 10

BlackBox

  • King Gator
  • Posts: 3770
Re: Event when Group is copied
« Reply #4 on: August 18, 2014, 09:04:34 AM »
This weekend I thought it might be easiest to include a block within the group. Then I could quantify the blocks and even test them to make sure they belonged to an anonymous group.

Couldn't you just write XData to the entities within the Group(s) you're tracking instead, and filter for that?
"How we think determines what we do, and what we do determines what we get."

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Event when Group is copied
« Reply #5 on: August 19, 2014, 07:45:46 AM »
This weekend I thought it might be easiest to include a block within the group. Then I could quantify the blocks and even test them to make sure they belonged to an anonymous group.

Couldn't you just write XData to the entities within the Group(s) you're tracking instead, and filter for that?

Yes, that might be a better solution.
Revit 2019, AMEP 2019 64bit Win 10

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Event when Group is copied
« Reply #6 on: August 19, 2014, 04:11:23 PM »
saving the complexity of the overrule you could also catch this when the Database.BeginDeepCloneTranslation event fires

JONTHEPOPE

  • Guest
Re: Event when Group is copied
« Reply #7 on: August 20, 2014, 12:56:45 AM »
what is a deep clone?
how is it different from just a regular clone?

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Event when Group is copied
« Reply #8 on: August 20, 2014, 08:54:50 AM »
Do a search for Advanced deepclone API in AutoCAD and you will find your answer.



Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013