Commit a02f44d1 authored by Grand Francois's avatar Grand Francois
Browse files

#46 ajout de NubFactory.replaceSessionNub() pour remplacer un nub par un autre

Showing with 63 additions and 3 deletions
+63 -3
......@@ -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
......
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment