Esempio n. 1
0
        public static void TranslateIntoComponents(List <ComponentMessage> _messages, ComponentFactory _comp_factory,
                                                   out List <ParameterStructure.Component.Component> new_comps_created)
        {
            new_comps_created = new List <ParameterStructure.Component.Component>();
            if (_messages == null)
            {
                return;
            }
            if (_messages.Count == 0)
            {
                return;
            }
            if (_comp_factory == null)
            {
                return;
            }

            // 1. Modify the components
            Dictionary <Component, ComponentAction> updated = new Dictionary <Component, ComponentAction>();

            foreach (ComponentMessage msg in _messages)
            {
                ParameterStructure.Component.Component comp = null;
                ComponentAction action_to_take = ComponentAction.NONE;
                if (msg.CompID > -1)
                {
                    // handling of existing components
                    comp           = _comp_factory.GetByID(msg.CompID);
                    action_to_take = (msg.ActionToTake == MessageAction.DELETE) ? ComponentAction.DELETE : ComponentAction.UPDATE;
                }
                else
                {
                    Debug.WriteLine("CB: creating new component from {0}", msg.CompDescr);
                    // create a new component (adds it to the top level of the component record of the Factory)
                    // NOTE: should happen only for components describing a space
                    comp = _comp_factory.CreateEmptyComponent(false);
                    new_comps_created.Add(comp);
                    ////comp.Name = GeometryUtils.Relationship2GeometryToCompNameDE(msg.GeomType);
                    comp.Name        = msg.CompDescr;
                    comp.Description = "Representation";
                    action_to_take   = ComponentAction.CREATE;
                }
                if (comp != null)
                {
                    Debug.WriteLine("CB: calling 'UpdateComponentFromMessage' with {0} for {1}", action_to_take, msg.CompDescr);
                    ComponentMessageTranslator.UpdateComponentFromMessage(comp, msg, _comp_factory.Caller, true);
                    _comp_factory.UpdateConnectivity(comp); // for Relation2Geometry Type CONTAINED_IN -> propagates realization
                    if (!(updated.ContainsKey(comp)))
                    {
                        updated.Add(comp, action_to_take);
                    }
                }
            }

            // 2. Adjust the relationships btw the components
            // NOTE: includes deletion of all automatically generated sub-components and
            // replacing them with the new automatically created components

            List <long> parent_ids = new List <long>();

            foreach (ComponentMessage msg in _messages)
            {
                if (msg.CompParentID < 0 && msg.CompRepParentID > -1)
                {
                    // the parent was just generated...
                    int  index         = _messages.FindIndex(x => x.CompRepID == msg.CompRepParentID);
                    long new_parent_id = updated.ElementAt(index).Key.ID;
                    parent_ids.Add(new_parent_id);
                }
                else
                {
                    // the parent existed already before the call to this method OR
                    // there is no parent
                    parent_ids.Add(msg.CompParentID);
                }
            }

            List <List <long> > ref_comp_ids = _messages.Select(x => new List <long>(x.CompRefIds)).ToList();

            // happens independent of the current user (references have a higher priority than writing access)
            _comp_factory.AdjustSubAndRefComponentDependenciesAfterAutomaticGeneration(updated, parent_ids, ref_comp_ids);
        }