public void OnStopEditLayer(ESRI.ArcGIS.Schematic.ISchematicLayer schLayer)
 {
     if (schLayer != null && this.State == ESRI.ArcGIS.Desktop.AddIns.ExtensionState.Enabled)
     {
         Updatecontainers(schLayer);
     }
 }
        private void Updatecontainers(ESRI.ArcGIS.Schematic.ISchematicLayer schLayer)
        {
            if (schLayer == null)
            {
                return;
            }

            // get the inMemorydiagram if any
            ISchematicInMemoryDiagram inMemoryDiagram;

            inMemoryDiagram = schLayer.SchematicInMemoryDiagram;
            if (inMemoryDiagram == null)
            {
                return;
            }

            bool bCreate = false;

            // create or remove relations between containers and their contents
            if (this.State == ESRI.ArcGIS.Desktop.AddIns.ExtensionState.Enabled)
            {
                bCreate = true;
            }

            ISchematicElementClass        schemContainerClass = null;
            ISchematicElementClass        schemElementClass;
            ISchematicElementClass        schemStationClass = null;
            IEnumSchematicInMemoryFeature enumElementsInContainer;
            IEnumSchematicInMemoryFeature enumContainerElements;
            ISchematicInMemoryFeature     schemFeature          = null;
            ISchematicInMemoryFeature     schemContainerFeature = null;
            object feederOID;
            string containerNameID;
            IEnumSchematicElementClass   enumElementClass;
            ISchematicAttributeContainer schemAttributeContainer;
            ISchematicAttribute          schemAttributeRelatedFeeder;
            //ISchematicElement schemElement ;
            ISchematicRelationController     schemRelationController     = null;
            ISchematicRelationControllerEdit schemRelationControllerEdit = null;
            Collection colContElem = new Collection();

            // Getting SchematicFeature Class Stations and Containers
            enumElementClass = inMemoryDiagram.SchematicDiagramClass.AssociatedSchematicElementClasses;
            enumElementClass.Reset();
            schemElementClass = enumElementClass.Next();

            while (schemElementClass != null)
            {
                if (schemElementClass.Name == "Stations")
                {
                    schemStationClass = schemElementClass;
                }
                if (schemElementClass.Name == "Containers")
                {
                    schemContainerClass = schemElementClass;
                }
                if (schemStationClass != null && schemContainerClass != null)
                {
                    break;
                }

                schemElementClass = enumElementClass.Next();
            }
            // go out if schemStationClass or schemContainerClass are null
            if (schemStationClass == null || schemContainerClass == null)
            {
                return;
            }

            // Getting the Stations elements that will be displayed in the containers
            enumElementsInContainer = inMemoryDiagram.GetSchematicInMemoryFeaturesByClass(schemStationClass);
            if (enumElementsInContainer == null)
            {
                return;
            }

            // Creating the Schematic Container Manager
            schemRelationController = new SchematicRelationController();

            // Creating the Schematic Container Editor that will be used to define the relation between the stations and their container
            schemRelationControllerEdit = (ISchematicRelationControllerEdit)schemRelationController;

            // Defining each Container element as a schematic container
            enumContainerElements = inMemoryDiagram.GetSchematicInMemoryFeaturesByClass(schemContainerClass);

            // Add Container Element to a collection
            enumContainerElements.Reset();
            schemContainerFeature = enumContainerElements.Next();
            while (schemContainerFeature != null)
            {
                colContElem.Add(schemContainerFeature, schemContainerFeature.Name, null, null);
                schemContainerFeature = enumContainerElements.Next();
            }

            // Setting the relation between each station and its related container
            enumElementsInContainer.Reset();
            schemFeature = enumElementsInContainer.Next();

            while (schemFeature != null)
            {
                // The relation is specified by the RelatedFeeder attribute value defined for each station
                schemAttributeContainer     = (ISchematicAttributeContainer)schemFeature.SchematicElementClass;
                schemAttributeRelatedFeeder = schemAttributeContainer.GetSchematicAttribute("RelatedFeeder", false);

                feederOID = schemAttributeRelatedFeeder.GetValue((ISchematicObject)schemFeature);

                if (feederOID != null)
                {
                    containerNameID = "Container-" + feederOID.ToString();

                    try
                    {
                        // Retrieve Container Element in the collection
                        schemContainerFeature = (ISchematicInMemoryFeature)colContElem[containerNameID];

                        if (bCreate)
                        {
                            schemRelationControllerEdit.CreateRelation(schemFeature, schemContainerFeature); // Create relation
                        }
                        else
                        {
                            schemRelationControllerEdit.DeleteRelation(schemFeature); // delete child relation
                        }
                    }
                    catch { }
                }

                schemContainerFeature = null;
                schemFeature          = enumElementsInContainer.Next();
            }

            if (!bCreate)
            {
                // Force container geometry
                enumContainerElements.Reset();
                while ((schemContainerFeature = enumContainerElements.Next()) != null)
                {
                    try
                    {
                        // set an empty geometry
                        // container does not have content at this stage
                        Polygon   emptyRectangle    = new Polygon();
                        IGeometry ContainerGeometry = (IGeometry)emptyRectangle;
                        schemContainerFeature.Shape = ContainerGeometry;
                    }
                    catch { }
                }
            }

            IActiveView activeView = (IActiveView)ArcMap.Document.FocusMap;

            if (activeView != null)
            {
                activeView.ContentsChanged();
                activeView.Refresh();
            }
        }