From a02f44d14993ce2a328882df7c5ddeac7a328ac5 Mon Sep 17 00:00:00 2001 From: "francois.grand" <francois.grand@irstea.fr> Date: Tue, 17 Apr 2018 12:18:34 +0200 Subject: [PATCH] #46 ajout de NubFactory.replaceSessionNub() pour remplacer un nub par un autre --- src/nub_factory.ts | 53 +++++++++++++++++++++++++++-- src/structure/parallel_structure.ts | 13 +++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/nub_factory.ts b/src/nub_factory.ts index 7a8409d8..69ac91a6 100644 --- a/src/nub_factory.ts +++ b/src/nub_factory.ts @@ -41,13 +41,17 @@ export class NubFactory { this._defaultPrecision = p; } + private newSessionNub(p: Props | {}): SessionNub { + const params = p instanceof Props ? p : new Props(p); + const nub = this.createNub(params); + return new SessionNub(nub, params); + } + /** * créé un Nub et l'ajoute à la session */ public createSessionNub(p: Props | {}): SessionNub { - const params = p instanceof Props ? p : new Props(p); - const nub = this.createNub(params); - const res = new SessionNub(nub, params); + const res = this.newSessionNub(p); this._session.push(res); return res; } @@ -59,6 +63,49 @@ export class NubFactory { return undefined; } + private replaceStructureNub(oldNub: Nub, newNub: Nub) { + const b1 = oldNub instanceof Structure; + const b2 = newNub instanceof Structure; + if ((b1 && !b2) || (!b1 && b2)) + throw new Error(`NubFactory.replaceStructureNub() : arguments incorrects (${typeof (oldNub)}/${typeof (newNub)}`); + if (b1 && b2) { + for (const sn of this._session) + if (sn.nub instanceof ParallelStructure) { + const psn = sn.nub as ParallelStructure; + let i = 0; + for (const st of psn.structures) { + if (st.uid == oldNub.uid) { + psn.replaceStructure(i, newNub as Structure); + return; + } + i++; + } + } + throw new Error(`NubFactory.replaceStructureNub() : la structure (uid ${oldNub.uid}) à remplacer n'a pas été trouvée`); + } + } + + /** + * remplace un SessionNub par un nouveau + * @param sn SessionNub à remplacer + * @param params propriété du nouveau SessionNub + */ + public replaceSessionNub(sn: SessionNub, params: Props): SessionNub { + let i = 0; + for (const n of this._session) { + if (n.uid == sn.uid) { + const newNub = this.newSessionNub(params); + this._session[i] = newNub; + this.replaceStructureNub(sn.nub, newNub.nub); + return newNub; + } + i++; + } + + // pas trouvé + return undefined; + } + /** * créé un Nub * @param calcType type de Nub diff --git a/src/structure/parallel_structure.ts b/src/structure/parallel_structure.ts index b0f3f5ec..f06396c2 100644 --- a/src/structure/parallel_structure.ts +++ b/src/structure/parallel_structure.ts @@ -63,6 +63,19 @@ export class ParallelStructure extends Nub { this.structures.push(structure); } + /** + * remplace une structure hydraulique + * @param index indice de la structure dans le tableau + * @param structure nouvelle structure + */ + public replaceStructure(index: number, structure: Structure) { + if (index > -1 && index < this.structures.length) { + this.structures[index] = structure; + } else { + throw new Error(`ParallelStructure.replaceStructure invalid index ${index}`); + } + } + /** * Supprime une structure hydraulique * @param index numéro de la structure dans le tableau -- GitLab