From 3befeec2319989e93046f6ce11e67ce8e2d31740 Mon Sep 17 00:00:00 2001
From: Mathias Chouet <mathias.chouet@irstea.fr>
Date: Tue, 14 Apr 2020 17:56:22 +0200
Subject: [PATCH] Fix #386 - generate SectionParametree from RegimeUniforme

---
 .../calculator.component.html                 |  5 ++
 .../calculator.component.ts                   | 75 ++++++++++++++++---
 src/locale/messages.en.json                   |  3 +-
 src/locale/messages.fr.json                   |  3 +-
 4 files changed, 72 insertions(+), 14 deletions(-)

diff --git a/src/app/components/generic-calculator/calculator.component.html b/src/app/components/generic-calculator/calculator.component.html
index 2767474fd..aacbb2d19 100644
--- a/src/app/components/generic-calculator/calculator.component.html
+++ b/src/app/components/generic-calculator/calculator.component.html
@@ -115,6 +115,11 @@
                         </button>
                     </div>
 
+                    <button mat-raised-button color="accent" id="generate-ru-sp" *ngIf="isRegimeUniforme" (click)="generateRuSp()"
+                        [disabled]="! generateRuSpEnabled" [title]="uitextGenerateRuSpTitle">
+                        {{ uitextGenerateRuSp }}
+                    </button>
+
                     <mat-card-content>
                         <calc-results id="resultsComp" (afterViewChecked)="onCalcResultsViewChecked()"></calc-results>
                     </mat-card-content>
diff --git a/src/app/components/generic-calculator/calculator.component.ts b/src/app/components/generic-calculator/calculator.component.ts
index 28ed8e336..a4873f788 100644
--- a/src/app/components/generic-calculator/calculator.component.ts
+++ b/src/app/components/generic-calculator/calculator.component.ts
@@ -14,7 +14,8 @@ import {
     acSection,
     ParamDefinition,
     round,
-    Nub
+    Nub,
+    RegimeUniforme
 } from "jalhyd";
 
 import { generateValuesCombination } from "../../util";
@@ -211,6 +212,10 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
         return this.intlService.localizeText("INFO_CALCULATOR_RESULTS_GENERATE_SP_AVAL");
     }
 
+    public get uitextGenerateRuSp() {
+        return this.intlService.localizeText("INFO_CALCULATOR_RESULTS_GENERATE_RU_SP");
+    }
+
     public get uitextOpenHelp() {
         return this.intlService.localizeText("INFO_CALCULATOR_OPEN_HELP");
     }
@@ -584,6 +589,11 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
         return this.is(CalculatorType.Bief);
     }
 
+    // true if current Nub is RegimeUniforme
+    public get isRegimeUniforme() {
+        return this.is(CalculatorType.RegimeUniforme);
+    }
+
     /**
      * Returns true if no parameter is varying
      */
@@ -613,8 +623,8 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
         if (! this.hasResults) {
             return this.intlService.localizeText("INFO_CALCULER_D_ABORD");
         }
-        if (! this.allParamsAreFixed) {
-            return this.intlService.localizeText("INFO_PAB_PARAMETRES_FIXES");
+        if (! this.allParamsAreFixed()) {
+            return this.intlService.localizeText("INFO_PARAMETRES_FIXES");
         }
         return "";
     }
@@ -696,9 +706,9 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
      */
     public generateSPAmont() {
         const bief = (this._formulaire.currentNub as Bief);
-        this.generateSP(
-            this.generateYValuesForSP(bief.prms.Z1, bief.prms.ZF1),
-            this.generateIfValuesForSP()
+        this.generateBiefSP(
+            this.generateBiefYValuesForSP(bief.prms.Z1, bief.prms.ZF1),
+            this.generateBiefIfValuesForSP()
         );
     }
 
@@ -707,9 +717,9 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
      */
     public generateSPAval() {
         const bief = (this._formulaire.currentNub as Bief);
-        this.generateSP(
-            this.generateYValuesForSP(bief.prms.Z2, bief.prms.ZF2),
-            this.generateIfValuesForSP()
+        this.generateBiefSP(
+            this.generateBiefYValuesForSP(bief.prms.Z2, bief.prms.ZF2),
+            this.generateBiefIfValuesForSP()
         );
     }
 
@@ -718,7 +728,7 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
      * @param Z cote de leau (Z1 ou Z2)
      * @param ZF cote de fond (ZF1 ou ZF2)
      */
-    private generateYValuesForSP(Z: ParamDefinition, ZF: ParamDefinition): number | number[] {
+    private generateBiefYValuesForSP(Z: ParamDefinition, ZF: ParamDefinition): number | number[] {
         const bief = (this._formulaire.currentNub as Bief);
         return generateValuesCombination(
             bief,
@@ -732,7 +742,7 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
     /**
      * Génère une liste de valeurs de pente en fonction de ZF1, ZF2 et Long
      */
-    private generateIfValuesForSP(): number | number[] {
+    private generateBiefIfValuesForSP(): number | number[] {
         const bief = (this._formulaire.currentNub as Bief);
         return generateValuesCombination(
             bief,
@@ -748,7 +758,7 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
      * @param Ys tirant(s) d'eau
      * @param Ifs pente(s)
      */
-    private generateSP(Ys: number | number[], Ifs: number | number[]) {
+    private generateBiefSP(Ys: number | number[], Ifs: number | number[]) {
         const bief = (this._formulaire.currentNub as Bief);
         const serialisedSection = bief.section.serialise();
         const sectionCopy = Session.getInstance().unserialiseSingleNub(serialisedSection, false).nub;
@@ -779,6 +789,47 @@ export class GenericCalculatorComponent implements OnInit, DoCheck, AfterViewChe
         );
     }
 
+    public get generateRuSpEnabled(): boolean {
+        return this.hasResults && ! this._formulaire.currentNub.result.hasErrorMessages();
+    }
+
+    public get uitextGenerateRuSpTitle(): string {
+        if (! this.generateRuSpEnabled) {
+            return this.intlService.localizeText("INFO_CALCULER_D_ABORD");
+        } else {
+            return "";
+        }
+    }
+
+    /**
+     * Génère une SectionParametree à partir du module RegimeUniforme en cours
+     */
+    public generateRuSp() {
+        const ru = (this._formulaire.currentNub as RegimeUniforme);
+        // copy section
+        const serialisedSection = ru.section.serialise();
+        const sectionCopy = Session.getInstance().unserialiseSingleNub(serialisedSection, false).nub;
+        const secParam = new SectionParametree(sectionCopy as acSection);
+        // copy value of calculated param
+        const cp = ru.calculatedParam;
+        const scp = secParam.section.getParameter(cp.symbol);
+        if (cp.hasMultipleValues) {
+            scp.setValues(ru.result.getCalculatedValues().map(v => round(v, this.appSetupService.displayPrecision)));
+        } else {
+            scp.singleValue = ru.result.vCalc;
+        }
+        Session.getInstance().registerNub(secParam);
+
+        this.formulaireService.createFormulaire(CalculatorType.SectionParametree, secParam)
+            .then((f: FormulaireDefinition) => {
+                // calculate
+                f.doCompute();
+                // go to new SP
+                this.router.navigate(["/calculator", f.uid]).then();
+            }
+        );
+    }
+
     public saveCalculator() {
         this.formulaireService.saveForm(this._formulaire);
     }
diff --git a/src/locale/messages.en.json b/src/locale/messages.en.json
index 3a4e3f25f..e133d5c21 100644
--- a/src/locale/messages.en.json
+++ b/src/locale/messages.en.json
@@ -69,6 +69,7 @@
     "INFO_CALCULATOR_OPEN_HELP": "Help",
     "INFO_CALCULATOR_PARAMFIXES": "Fixed parameters",
     "INFO_CALCULATOR_RESULTS_GENERATE_PAB": "Generate a fish ladder",
+    "INFO_CALCULATOR_RESULTS_GENERATE_RU_SP": "Hydraulic details of section",
     "INFO_CALCULATOR_RESULTS_GENERATE_SP_AMONT": "Hydraulic details of upstream section",
     "INFO_CALCULATOR_RESULTS_GENERATE_SP_AVAL": "Hydraulic details of downstream section",
     "INFO_CALCULATOR_RESULTS_TITLE": "Results",
@@ -360,7 +361,7 @@
     "INFO_PAB_HEADER_PARAMETERS": "Parameters",
     "INFO_PAB_HEADER_VALUES": "Values",
     "INFO_PAB_CLOISON_OUVRAGE_N": "Wall : device #%s",
-    "INFO_PAB_PARAMETRES_FIXES": "All parameters must be fixed",
+    "INFO_PARAMETRES_FIXES": "All parameters must be fixed",
     "INFO_PAB_TITRE_COURT": "Fish ladder",
     "INFO_PAB_TITRE": "Fish ladder",
     "INFO_PAB_TITRE_PROFIL": "Fish ladder longitudinal profile",
diff --git a/src/locale/messages.fr.json b/src/locale/messages.fr.json
index 444dde1d3..e846222d0 100644
--- a/src/locale/messages.fr.json
+++ b/src/locale/messages.fr.json
@@ -69,6 +69,7 @@
     "INFO_CALCULATOR_OPEN_HELP": "Aide",
     "INFO_CALCULATOR_PARAMFIXES": "Paramètres fixés",
     "INFO_CALCULATOR_RESULTS_GENERATE_PAB": "Générer une passe à bassins",
+    "INFO_CALCULATOR_RESULTS_GENERATE_RU_SP": "Détails hydrauliques de la section",
     "INFO_CALCULATOR_RESULTS_GENERATE_SP_AMONT": "Détails hydrauliques de la section amont",
     "INFO_CALCULATOR_RESULTS_GENERATE_SP_AVAL": "Détails hydrauliques de la section aval",
     "INFO_CALCULATOR_RESULTS_TITLE": "Résultats",
@@ -359,7 +360,7 @@
     "INFO_PAB_HEADER_PARAMETERS": "Paramètres",
     "INFO_PAB_HEADER_VALUES": "Valeurs",
     "INFO_PAB_CLOISON_OUVRAGE_N": "Cloison : ouvrage n° %s",
-    "INFO_PAB_PARAMETRES_FIXES": "Tous les paramètres doivent être fixés",
+    "INFO_PARAMETRES_FIXES": "Tous les paramètres doivent être fixés",
     "INFO_PAB_TITRE_COURT": "PAB",
     "INFO_PAB_TITRE": "Passe à bassins",
     "INFO_PAB_TITRE_PROFIL": "Profil en long de la passe",
-- 
GitLab