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);
        }
Esempio n. 2
0
        public static List <ComponentMessage> AssembleUnrelatedComponentMessages(List <Component> _comps, ComponentFactory _factory)
        {
            List <ComponentMessage> messages = new List <ComponentMessage>();

            if (_comps == null)
            {
                return(messages);
            }

            // get the parent ids
            List <long> parent_ids = new List <long>();

            if (_factory != null)
            {
                foreach (Component c in _comps)
                {
                    long             c_parent_id  = -1L;
                    List <Component> parent_chain = _factory.GetParentComponentChain(c);
                    if (parent_chain.Count > 1)
                    {
                        Component direct_parent = parent_chain[parent_chain.Count - 2];
                        if (direct_parent != null)
                        {
                            c_parent_id = direct_parent.ID;
                        }
                    }
                    parent_ids.Add(c_parent_id);
                }
            }
            else
            {
                parent_ids = Enumerable.Repeat(-1L, _comps.Count).ToList();
            }

            // assemble the messages
            int nrM = _comps.Count;

            if (nrM == 1)
            {
                messages.Add(ComponentMessageTranslator.AssembleComponentMessage(_comps[0], MessagePositionInSeq.SINGLE_MESSAGE, parent_ids[0]));
            }
            else
            {
                for (int i = 0; i < nrM; i++)
                {
                    MessagePositionInSeq pos = MessagePositionInSeq.UNKNOWN;
                    if (i == 0)
                    {
                        pos = MessagePositionInSeq.SEQUENCE_START_MESSAGE;
                    }
                    else if (i == nrM - 1)
                    {
                        pos = MessagePositionInSeq.SEQUENCE_END_MESSAGE;
                    }
                    else
                    {
                        pos = MessagePositionInSeq.MESSAGE_INSIDE_SEQUENCE;
                    }

                    messages.Add(ComponentMessageTranslator.AssembleComponentMessage(_comps[i], pos, parent_ids[i]));
                }
            }

            return(messages);
        }