ISelectionManager and '.RootNode.Delete();' do not work

Hi everyone,
I am currently working on a C# plugin that deletes dynamic components on command. I have provided two ways to do this.
The first one includes an additional panel where all components are displayed in a list and can be selected by the user. If the user presses a button, a function is executed, which leads via detours to the fact that the component is deleted from the 3D world.
This is the code for this:
int _id = Convert.ToInt32(someStringContainingTheId); // _id is the _ISimComponent.GetHashCode()
ISimComponent _component = null;
foreach (var _comp in App.World.Components) // find component by the ID
{
if (_comp.GetHashCode() == _id)
{
if (_component == null)
{
_component = _comp;
}
}
}
if (_component != null)
{
_component.RootNode.Delete(); // delete the component as recommended in other topic
App.Simulation.Update();
}

Under normal circumstances this works perfectly fine.
As a second way I want to make it possible to select a ISimComponent in the 3D world an than press another button. The effect should be the same.
this is the code:
ISimComponent _simComponent = null;
foreach (var _comp in App.World.Components)
{
if (_comp != null)
{
if (_comp.IsHighlighted)
{
_simComponent = _comp;
}
}
}
if(_simComponent != null)
{
_simComponent.RootNode.Delete();
App.Simulation.Update();
}

This approach does not wor at all. The Plugin seems to be stuck in a Loop or somewere it should not be, since VC done not a single thing after calling ‘_simComponent.RootNode.Delete();’. It never makes it to the ’ App.Simulation.Update();'.

I tested a few things and the selection of the component in 3D seems to be the root of my problem, since the first approach also does not work if I select the component in 3D, even though I would not need to.

After that realization I tryed to unselect the component in the code befor deleting it:
if (_component.IsHighlighted)
{
ISelectionManager _sm = App.SelectionManager;
_sm.Clear();
}

Now the problem occurs at the line ‘_sm.Clear();’, it dose not make it to the delete()-function.Therefore I think that the ISelectionManager is not working properly.
Can someone please tell me, what I am doing wrong and how to fix this.
Best Regards

Did you try using ISimWorld.DeleteComponent(…) instead?

Also Object.GetHashCode() is generally not guaranteed to give unique ids, just “unique enough” for purposes such as key index organization in Dictionary class etc.

Thanks for the fast reply.
I tryed, didn´t work. The result was the same.
I think the problem is with the ISelectionManager. If I can unselect the component before deleting it, the problem will be solved.

Thanks for the hint, for now that is OK. Later on I will use a unique property I created my self.

This works fine in VC 4.6, both for static and dynamic components. The method is in a view model and called when a button is clicked.

        public void OnButtonClick()
        {
            ISimComponent[] selectedComponents = IoC.Get<ISelectionManager>().GetSelection<ISimComponent>();

            foreach (ISimComponent component in selectedComponents)
            {
                _application.World.DeleteComponent(component);
            }

            IoC.Get<IRenderService>().RequestRender();
        }

Your problem is probably due to some context where you are trying to delete the components, like from within simulation or selection manager event handlers which don’t expect the objects to get deleted there. You might also have some other code in such event handler which interferes with the selection change / deletion.