Exemplo n.º 1
0
        public static void UpdateComponentFromMessage(Component _comp, ComponentMessage _cmsg,
                                                      ComponentManagerType _user, bool _add_missing_params)
        {
            if (_comp == null || _cmsg == null)
            {
                return;
            }
            if (_cmsg.CompID > -1 && _comp.ID != _cmsg.CompID)
            {
                return;
            }

            // 1. TRANSFER THE GEOMETRIC RELATIONSHIPS
            // extract the main geometric relationship type (even for multiple relationships, there should only be ONE type)
            List <InterProcCommunication.Specific.GeometricRelationship> geom_rels = new List <InterProcCommunication.Specific.GeometricRelationship>(_cmsg.GeomRelationships);

            if (geom_rels.Count == 0)
            {
                // ... Houston, we have a problem!!!
                // This should not happen!
            }
            else
            {
                // combine the incoming relationships with processing info
                Dictionary <InterProcCommunication.Specific.GeometricRelationship, bool> geom_rels_processing = new Dictionary <GeometricRelationship, bool>();
                foreach (InterProcCommunication.Specific.GeometricRelationship gr in geom_rels)
                {
                    geom_rels_processing.Add(gr, false);
                }
                // combine the old relationships with updating info (is used to remove obsolete relationships)
                List <bool> old_geom_rels_updating = Enumerable.Repeat(false, _comp.R2GInstances.Count).ToList();

                // update the existing geometric relationship(s)
                for (int i = 0; i < _comp.R2GInstances.Count; i++)
                {
                    InterProcCommunication.Specific.GeometricRelationship gr_corresponding = geom_rels.FirstOrDefault(x => x.GrID == _comp.R2GInstances[i].ID);
                    if (gr_corresponding != null)
                    {
                        ParameterStructure.Geometry.Relation2GeomType gr_type = ParameterStructure.Geometry.GeometryUtils.StringToRelationship2Geometry(InterProcCommunication.Specific.GeometryUtils.Relationship2GeometryToString(gr_corresponding.GrState.Type));
                        _comp.R2GInstances[i].Name  = gr_corresponding.GrName;
                        _comp.R2GInstances[i].State = new ParameterStructure.Geometry.Relation2GeomState {
                            IsRealized = gr_corresponding.GrState.IsRealized, Type = gr_type
                        };
                        _comp.R2GInstances[i].GeomIDs   = gr_corresponding.GrIds;
                        _comp.R2GInstances[i].GeomCS    = gr_corresponding.GrUCS;
                        _comp.R2GInstances[i].TRm_WC2LC = gr_corresponding.GrTrWC2LC;
                        _comp.R2GInstances[i].TRm_LC2WC = gr_corresponding.GrTrLC2WC;

                        // instance info cannot change, except for the path
                        _comp.R2GInstances[i].InstancePath = new List <System.Windows.Media.Media3D.Point3D>(gr_corresponding.InstPath);

                        geom_rels_processing[gr_corresponding] = true;
                        old_geom_rels_updating[i] = true;
                    }
                }

                // add new relationships
                List <ParameterStructure.Geometry.GeometricRelationship> to_be_added = new List <ParameterStructure.Geometry.GeometricRelationship>();
                foreach (var entry in geom_rels_processing)
                {
                    if (entry.Value)
                    {
                        continue; // skip processed
                    }
                    ParameterStructure.Geometry.Relation2GeomType  type  = ParameterStructure.Geometry.GeometryUtils.StringToRelationship2Geometry(InterProcCommunication.Specific.GeometryUtils.Relationship2GeometryToString(entry.Key.GrState.Type));
                    ParameterStructure.Geometry.Relation2GeomState state = new ParameterStructure.Geometry.Relation2GeomState {
                        Type = type, IsRealized = entry.Key.GrState.IsRealized
                    };
                    ParameterStructure.Geometry.GeometricRelationship new_gr = new ParameterStructure.Geometry.GeometricRelationship(entry.Key.GrName, state,
                                                                                                                                     entry.Key.GrIds, entry.Key.GrUCS, entry.Key.GrTrWC2LC, entry.Key.GrTrLC2WC);
                    new_gr.InstancePath = new List <System.Windows.Media.Media3D.Point3D>(entry.Key.InstPath); // added 06.09.2017
                    to_be_added.Add(new_gr);
                }

                // communicate to component (performs the deleletion of the relationships that were not updated)
                _comp.UpdateGeometricRelationships(old_geom_rels_updating, to_be_added, _user);
            }

            // 2. TRANSFER THE PARAMETERS
            Dictionary <string, double> p_dict = Comp2GeomCommunication.GetReservedParamDictionary(geom_rels[0].GrState.Type);

            foreach (var entry in p_dict)
            {
                // retrieve value
                double val = _cmsg[entry.Key];
                if (double.IsNaN(val))
                {
                    continue;
                }

                // transfer to component
                _comp.SetParameterValue(entry.Key, val, _user, _add_missing_params);
            }
        }