Block Edit and Object table


(Darryl Menezes) #1

I am trying to monitor the change in Object Table, to find out if any new object is added or any object is deleted. But whenever a user enters block edit mode, the object table changes (The objects inside the blocks are added to the table). I want to ignore the change in object table due to a user entering block edit mode. Is this possible? Is there any event that is called when a user enters Block Edit mode? Thanks.


(Dale Fugier) #2

Hi @Darryl_Menezes,

To be notified when Rhino command begin and end, implement event handlers for Command.BeginCommand and Command.OnEndCommand.

using Rhino.Commands;
...
Command.BeginCommand += OnBeginCommand;
Command.EndCommand += OnEndCommand;

/// <summary>
/// Called when a command begins
/// </summary>
public static void OnBeginCommand(object sender, CommandEventArgs e)
{
  // TODO...
}

/// <summary>
/// Called when a command ends
/// </summary>
public static void OnEndCommand(object sender, CommandEventArgs e)
{
  // TODO...
}

When entering block edit mode, the BlockEdit command will both begin and end.

When exiting block edit mode, the hidden BlockEditApplyInPlaceEditItemChanges command will both begin and end.

Check the CommandEventArgs parameter for the name of these commands begin and end. With this, you should be able to ignore objects added or modified during block editing.

– Dale


(Darryl Menezes) #3

Thanks


(Darryl Menezes) #4

Hi @Dale,
Sorry for the late question. Is there any event to track when the ‘Block Edit dialog box’ is closed by clicking the ‘X’ on the form? Because I want to restrict certain operations during block edit mode and allow them only when the last run command is “BlockEditApplyInPlaceEditItemChanges”. But when the ‘X’ is clicked, the last run command is still “BlockEdit” even when the user is out of the block edit mode. Thanks.


(John Morse) #7

The BlockEdit command is currently in a plug-in and does not post any completion states to Rhino. If you are using a CRhinoEventWatcher to identify when the command is completing you might try looking at the provided result code and see if it is equal to cancel.


#8

Is there a solution for this now inbetween?
The suggestion to check the result code does unfortunately not work as the result of the command is success although the user pressed the X button…


#9

I found a hackaround for this now:
When entering the block edit mode the selected block is not a normal object any more. So search for the selected block inside the OnBeginCommand function and store it. then afterwards you can check if the status of this object is still not normal. If it is normal again you are outside of the blockedit again

 private bool isLoaded = false;
        public static bool isInBlocKEditMode = false;
        public static Guid blockEditCommandId, thisCommandId;
        private static RhinoDoc _doc;
        private static InstanceObject blockToEdit;

        public IcLoadEventHandlers()
        {
            // Rhino only creates one instance of each command class defined in a
            // plug-in, so it is safe to store a refence in a static property.
            Instance = this;
        }

        ///<summary>The only instance of this command.</summary>
        public static IcLoadEventHandlers Instance
        {
            get; private set;
        }

        ///<returns>The command name as it appears on the Rhino command line.</returns>
        public override string EnglishName
        {
            get { return "IcLoadEventHandlers"; }
        }

        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            if(isLoaded)
            {
                return Result.Cancel;
            }
            _doc = doc;
            Command.BeginCommand += OnBeginCommand;
            Command.EndCommand += OnEndCommand;

           
            return Result.Success;



        }


        /// <summary>
        /// Called when a command begins
        /// </summary>
        public static void OnBeginCommand(object sender, CommandEventArgs e)
        {
            // TODO...

            if (isInBlocKEditMode)
            {
                if(blockToEdit != null)
                {
                    if(blockToEdit.IsValid && !blockToEdit.IsDeleted)
                    {
                        if(blockToEdit.IsNormal   )
                        {
                            blockToEdit = null;
                            isInBlocKEditMode = false;
                            RhinoApp.WriteLine("BlockEdit was cancelled");
                        }
                    }
                    else
                    {
                        blockToEdit = null;
                        isInBlocKEditMode = false;
                        RhinoApp.WriteLine("BlockEdit was cancelled");
                    }
                }
                else
                {
                    blockToEdit = null;
                    isInBlocKEditMode = false;
                    RhinoApp.WriteLine("BlockEdit was cancelled");
                }
            }
            String commandName = e.CommandEnglishName;
            if (commandName.Contains("BlockEdit") && !commandName.Contains("BlockEditApplyInPlaceEditItemChanges"))
            {
                IEnumerable<RhinoObject> selectedObjects = _doc.Objects.GetSelectedObjects(false, false);
                InstanceObject instanceObject;
                foreach (RhinoObject thisRhObj in selectedObjects)
                {
                    if (thisRhObj.IsSelected(false) != 0)
                    {
                        instanceObject = thisRhObj as InstanceObject;
                        if (instanceObject != null)
                        {
                            blockToEdit = instanceObject;
                        }
                    } 
                }
            }

        }

        /// <summary>
        /// Called when a command ends
        /// </summary>
        public static void OnEndCommand(object sender, CommandEventArgs e)
        {
            // TODO...
            String commandName = e.CommandEnglishName;
            if (commandName.Contains("BlockEditApplyInPlaceEditItemChanges"))
            {
                isInBlocKEditMode = false;
                blockToEdit = null;
                RhinoApp.WriteLine("exiting BlockEdit");
            }
            else if (commandName.Contains("BlockEdit"))
            {
                isInBlocKEditMode = true;
                RhinoApp.WriteLine("entering BlockEdit");
                //RhinoApp.WriteLine("entering BlockEdit");
            }
        }