Moving components to sublayouts

Hi,

I am trying to automatically move certain components to new or existing sublayouts when they are added to the simulation world. For example when a fence compoent is added to the layout it is moved to a “Fences“ sublayout. What I tried looks something like this (a simplified example):

[Export(typeof(IPlugin))]
public class TestPlugin : IPlugin
{
    [Import]
    IApplication _application = null;

    void IPlugin.Exit()
    {
        _application.World.ComponentAdded -= WorldComponentAdded;
    }

    void IPlugin.Initialize()
    {
        _application.World.ComponentAdded += WorldComponentAdded;
    }

    void WorldComponentAdded(object sender, ComponentAddedEventArgs e)
    {
        var root = _application.World;
        var existing_layouts = root.Layouts.Select(x => x.Name);
        ISimLayout layout = null;
        if (!existing_layouts.Contains("TestLayout")) 
        { 
            layout = root.CreateLayout("TestLayout", root.RootNode); 
        }
        else 
        { 
            layout = root.Layouts.Where(x => x.Name == "TestLayout").FirstOrDefault(); 
        }
        e.Component.RootNode.AttachTo(layout.RootNode);
    }
}

It all works good until I try to either add a component that has another component attached or is attached to another component. I don’t know which one of these relationships is the culprit. The problem also appears when I load a layout that has components connected with interfaces (processor, path…). What happens is the component/layout loading gets stuck at “Initializing Simulation“.

Is this not the correct way of doing this? I haven’t found anything specific in the documentation that would suggest I am doing something wrong.

If anyone wants to test this behaviour you can try for example adding the “2-Axis Servo Positioner“ from the Fanuc section of the eCatalog which has two blocks without geometry attached for some reason. It should be at the top of the Public Models folder because it starts with a number.

I tried disconnecting first and reconnecting after moving the components to a sublayout but had no luck in fixing the problem. Does anyone have any idea what is causing this problem or how to fix it?

Thanks,

Jure