RobotStudio event

How to Attach a Linesensor and a Wobj to a Mechanism

Hi,

I'm trying to attach a wobj and a Linesensor to a Mechanism. Unfortunatly it does not work. Since there is just happing nothing, I don't know where the problem is. Any ideas?

Wobj:

         public override void OnLoad(SmartComponent component)
        {
            base.OnLoad(component);
            Station stn = Project.ActiveProject as Station;
            if (stn != null)
            {
                IEnumerable<ProjectObject> stationobjects = stn.GetAllObjects();
                foreach (ProjectObject po in stationobjects)
                {
                    if (po.Name.Contains("wobj_1"))
                    {
                        Logger.AddMessage(new LogMessage("Wobj_1 found: " + po.TypeDisplayName));
                        IAttachableChild wobj = po as IAttachableChild;
                        for (int i = 0; i < component.GraphicComponents.Count; i++)
                        {
                            Mechanism mech = component.GraphicComponents[i] as Mechanism;
                            if (mech != null)
                            {
                                if (mech.Name == "XYZ")
                                {
                                    Flange[] flange = mech.GetFlanges();
                                    foreach (Flange fl in flange)
                                    {
                                        if (fl.Name == "XYZ_2") fl.Attach(wobj, false);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

LineSensor:

        public override void OnLoad(SmartComponent component)
        {
            base.OnLoad(component);
            
                foreach (GraphicComponent gc in component.GetGraphicComponents())
                {
                    if (gc.Name.Contains("LineSensor"))
                    {
                        Logger.AddMessage(new LogMessage("LineSensor found: " + gc.TypeDisplayName));
                        IAttachableChild ls = gc as IAttachableChild;
                        for (int i = 0; i < component.GraphicComponents.Count; i++)
                        {
                            Mechanism mech = component.GraphicComponents[i] as Mechanism;
                            if (mech != null)
                            {
                                if (mech.Name == "XYZ")
                                {
                                    Flange[] flange = mech.GetFlanges();
                                    foreach (Flange fl in flange)
                                    {
                                        if (fl.Name == "XYZ_2") fl.Attach(ls, false);
                                    }
                                }
                            }
                        }
                    }
                }
            }

         

Tagged:

Answers

  • Henry123
    Henry123
    edited June 2023
    I have found the reason why it does not work: The item where the Linesensor and the Wobj shall be attached to is a Library. Its necessary to disconnect it from the library first.

    Strange, because I haven't found any suggestion in the API documentation. Just trial and error.

    mech.DisconnectFromLibrary()


    @ABB: It would be fine to get some kind of message in the Logger if the child can't be attached due to the fact, that the parent is a Library.