コード例 #1
0
        public static ViewAnalogousVM GenerateNewAnalogousReactions(
            ReactionVM SelectedReaction,
            bool IsReactantsSelected,
            bool IsSolventsSelected,
            bool IsAgentsSelected,
            bool IsCatalystSelected,
            bool IspHSelected,
            bool IsTempSelected,
            bool IsTimeSelected,
            bool IsPressureSelected,
            bool IsRSNSelected,
            int ReactionsCountToCopy, int IndexToAdd
            )
        {
            ViewAnalogousVM vm            = new ViewAnalogousVM();
            var             mainViewModel = ((MainWindow)(App.Current.MainWindow)).DataContext as MainVM;

            mainViewModel.ProgressText = "Creating Analogous Reacions . .";
            try
            {
                vm.SelectedMasterReaction = SelectedReaction;
                List <ReactionParticipantVM> allParticipants = new List <ReactionParticipantVM>();
                List <RsnVM>      tanRsns      = new List <RsnVM>();
                List <ReactionVM> tanReactions = new List <ReactionVM>();
                var tanParticipants            = new List <ReactionParticipantVM>();
                if (IsReactantsSelected)
                {
                    var reactants = mainViewModel.TanVM.ReactionParticipants.OfType(ParticipantType.Reactant);
                    allParticipants.AddRange(reactants);
                }
                if (IsSolventsSelected)
                {
                    var solvents = mainViewModel.TanVM.ReactionParticipants.OfType(ParticipantType.Solvent);
                    allParticipants.AddRange(solvents);
                }

                if (IsAgentsSelected)
                {
                    var agents = mainViewModel.TanVM.ReactionParticipants.OfType(ParticipantType.Agent);
                    allParticipants.AddRange(agents);
                }

                if (IsCatalystSelected)
                {
                    var catalysts = mainViewModel.TanVM.ReactionParticipants.OfType(ParticipantType.Catalyst);
                    allParticipants.AddRange(catalysts);
                }
                for (int i = 0; i < ReactionsCountToCopy; i++)
                {
                    var reaction = new ReactionVM
                    {
                        DisplayOrder  = ++IndexToAdd,
                        Id            = Guid.NewGuid(),
                        TanVM         = SelectedReaction.TanVM,
                        AnalogousVMId = SelectedReaction.Id
                    };
                    #region Stages
                    var stages = new List <StageVM>();
                    foreach (var masterStage in SelectedReaction.Stages)
                    {
                        bool ValidStage     = true;
                        var  analogousStage = new StageVM {
                            Id = Guid.NewGuid(), ReactionVm = reaction
                        };
                        var Conditions = new List <StageConditionVM>();
                        if (masterStage.Conditions != null)
                        {
                            foreach (var selectedCondition in masterStage.Conditions)
                            {
                                var condition = new StageConditionVM
                                {
                                    DisplayOrder  = selectedCondition.DisplayOrder,
                                    Id            = Guid.NewGuid(),
                                    PH            = IspHSelected ? selectedCondition.PH : "",
                                    Pressure      = IsPressureSelected ? selectedCondition.Pressure : "",
                                    StageId       = analogousStage.Id,
                                    Temperature   = IsTempSelected ? selectedCondition.Temperature : "",
                                    Time          = IsTimeSelected ? selectedCondition.Time : "",
                                    TIME_TYPE     = IsTimeSelected ? selectedCondition.TIME_TYPE : "",
                                    PRESSURE_TYPE = IsPressureSelected ? selectedCondition.PRESSURE_TYPE : "",
                                    TEMP_TYPE     = IsTempSelected ? selectedCondition.TEMP_TYPE : "",
                                    PH_TYPE       = IspHSelected ? selectedCondition.PH_TYPE : "",
                                };
                                if ((IspHSelected && !string.IsNullOrEmpty(selectedCondition.PH)) || (IsPressureSelected && !string.IsNullOrEmpty(selectedCondition.Pressure)) ||
                                    (IsTempSelected && !string.IsNullOrEmpty(selectedCondition.Temperature)) || (IsTimeSelected && !string.IsNullOrEmpty(selectedCondition.Time)))
                                {
                                    Conditions.Add(condition);
                                }
                            }
                            analogousStage.SetConditions(Conditions);
                        }
                        var stageParticipants = (from sp in allParticipants where sp.StageVM.Id == masterStage.Id select sp).ToList();
                        foreach (var stageParticipant in stageParticipants)
                        {
                            var newParticipant = new ReactionParticipantVM
                            {
                                ChemicalType    = stageParticipant.ChemicalType,
                                DisplayOrder    = stageParticipant.DisplayOrder,
                                Name            = stageParticipant.Name,
                                Num             = stageParticipant.Num,
                                ParticipantType = stageParticipant.ParticipantType,
                                ReactionVM      = reaction,
                                Reg             = stageParticipant.Reg,
                                StageVM         = analogousStage,
                                TanChemicalId   = stageParticipant.TanChemicalId,
                                Yield           = stageParticipant.Yield,
                                Id = Guid.NewGuid()
                            };
                            tanParticipants.Add(newParticipant);
                        }
                        if (IsRSNSelected)
                        {
                            var stagersnList = (from rsn in mainViewModel.TanVM.Rsns where rsn.Reaction.Id == SelectedReaction.Id && rsn.Stage != null && rsn.Stage.Id == masterStage.Id select rsn).ToList();
                            foreach (var rsn in stagersnList)
                            {
                                var newRsn = new RsnVM
                                {
                                    CvtText  = rsn.CvtText,
                                    Reaction = reaction,
                                    IsRXN    = rsn.IsRXN,
                                    Stage    = analogousStage,
                                    FreeText = rsn.FreeText,
                                    Id       = Guid.NewGuid(),
                                    IsIgnorableInDelivery = rsn.IsIgnorableInDelivery,
                                    DisplayOrder          = rsn.DisplayOrder,
                                    ReactionParticipantId = rsn.ReactionParticipantId,
                                    SelectedChemical      = rsn.SelectedChemical
                                };
                                tanRsns.Add(newRsn);
                            }
                        }
                        if ((analogousStage.Conditions == null || !analogousStage.Conditions.Any()) &&
                            (tanParticipants == null || !tanParticipants.Where(tp => tp.StageVM != null && tp.StageVM.Id == analogousStage.Id).Any()) &&
                            (tanRsns == null || !tanRsns.Where(tp => tp.Stage != null && tp.Stage.Id == analogousStage.Id).Any()) && !mainViewModel.TanVM.ReactionParticipants.OfReactionAndStage(SelectedReaction.Id, masterStage.Id).OfType(ParticipantType.Reactant).Any())
                        {
                            ValidStage = false;
                        }
                        if (ValidStage)
                        {
                            stages.Add(analogousStage);
                        }
                    }
                    #endregion
                    reaction.SetStages(stages, 0, false, true);
                    tanReactions.Add(reaction);
                    var reactionParticipants = (from sp in allParticipants where sp.ReactionVM.Id == SelectedReaction.Id && sp.StageVM == null select sp).ToList();
                    foreach (var participant in reactionParticipants)
                    {
                        var newParticipant = new ReactionParticipantVM
                        {
                            ChemicalType    = participant.ChemicalType,
                            DisplayOrder    = participant.DisplayOrder,
                            Name            = participant.Name,
                            Num             = participant.Num,
                            ParticipantType = participant.ParticipantType,
                            ReactionVM      = reaction,
                            Reg             = participant.Reg,
                            StageVM         = null,
                            TanChemicalId   = participant.TanChemicalId,
                            Yield           = participant.Yield,
                            Id = Guid.NewGuid()
                        };
                        tanParticipants.Add(newParticipant);
                    }

                    if (IsRSNSelected)
                    {
                        var reationrsnList = (from rsn in mainViewModel.TanVM.Rsns where rsn.Reaction.Id == SelectedReaction.Id && rsn.Stage == null select rsn).ToList();
                        foreach (var rsn in reationrsnList)
                        {
                            var newRsn = new RsnVM
                            {
                                CvtText               = rsn.CvtText,
                                Reaction              = reaction,
                                FreeText              = rsn.FreeText,
                                IsRXN                 = rsn.IsRXN,
                                Stage                 = null,
                                Id                    = Guid.NewGuid(),
                                SelectedChemical      = rsn.SelectedChemical,
                                ReactionParticipantId = rsn.ReactionParticipantId,
                                DisplayOrder          = rsn.DisplayOrder,
                                IsIgnorableInDelivery = rsn.IsIgnorableInDelivery
                            };
                            tanRsns.Add(newRsn);
                        }
                    }
                }
                foreach (var participant in tanParticipants)
                {
                    vm.AllParticipants.Add(participant);
                    vm.ReactionParticipants.Add(participant);
                }
                vm.Rsns.Clear();
                foreach (var rsn in tanRsns)
                {
                    vm.Rsns.Add(rsn);
                }
                foreach (var reaction in tanReactions)
                {
                    vm.AnalogousReactions.Add(reaction);
                }
            }
            catch (Exception ex)
            {
                AppErrorBox.ShowErrorMessage("Can't Create Analogous Reactions", ex.Message);
            }
            return(vm);
        }
コード例 #2
0
        private static ReactionParticipantViewVM GetParticipantView(ReactionParticipantVM participant)
        {
            try
            {
                var mainViewModel   = (App.Current.MainWindow as MainWindow).DataContext as MainVM;
                var participantView = new ReactionParticipantViewVM();
                participantView.ShortName     = participant.Num.ToString();
                participantView.TooltipText   = participant.Reg + " - " + participant.Name;
                participantView.Name          = participant.Name + "(" + participant.Num + ")";
                participantView.Formula       = participant.Formula;
                participantView.Num           = participant.Num;
                participantView.StageVM       = participant.StageVM;
                participantView.Reg           = participant.Reg;
                participantView.NextImagePath = PLUS_ICON;

                #region Color
                if (participant.ParticipantType == ParticipantType.Product)
                {
                    participantView.BorderBrush = StyleConstants.ProductBrush;
                    participantView.BgColor     = StyleConstants.ProductColor.Name;
                }
                else if (participant.ParticipantType == ParticipantType.Reactant)
                {
                    participantView.BorderBrush = StyleConstants.ReactantBrush;
                    participantView.BgColor     = StyleConstants.ReactantColor.Name;
                }
                else if (participant.ParticipantType == ParticipantType.Agent)
                {
                    participantView.BorderBrush = StyleConstants.AgentBrush;
                }
                else if (participant.ParticipantType == ParticipantType.Catalyst)
                {
                    participantView.BorderBrush = StyleConstants.CatalystBrush;
                }
                else if (participant.ParticipantType == ParticipantType.Solvent)
                {
                    participantView.BorderBrush = StyleConstants.SolventBrush;
                }
                #endregion

                #region Chemical Name
                TanChemicalVM chemicalName = null;
                if (participant.ChemicalType == ChemicalType.S8500 || participant.ChemicalType == ChemicalType.S9000)
                {
                    chemicalName = S.Find(participant.Reg);
                }
                else if (participant.ChemicalType == ChemicalType.NUM || participant.ChemicalType == ChemicalType.S8000)
                {
                    var tanChemical = (from p in mainViewModel.TanVM.TanChemicals where p.NUM == participant.Num select p).FirstOrDefault();
                    if (tanChemical != null)
                    {
                        chemicalName = new Models.TanChemicalVM
                        {
                            RegNumber       = tanChemical.RegNumber,
                            NUM             = tanChemical.NUM,
                            ChemicalType    = tanChemical.ChemicalType,
                            Name            = tanChemical.Name,
                            ImagePath       = tanChemical.ImagePath,
                            MolString       = tanChemical.MolString,
                            AllImagePaths   = tanChemical.Substancepaths.Select(s => s.ImagePath).Distinct().ToList(),
                            StereoChemisrty = tanChemical.ABSSterio
                        };
                    }
                }
                participantView.ChemicalName = chemicalName;
                #endregion
                return(participantView);
            }
            catch (Exception ex)
            {
                Log.This(ex);
                throw;
            }
        }