public void UpdateMajorationSet(PLMPackEntities db, CardboardProfile cp, Dictionary <string, double> majorations) { // create majoration set if it does not exists if (db.MajorationSets.Count(mjs => (mjs.ComponentGuid == this.Guid) && (mjs.CardboardProfileId == cp.Id)) == 0) { db.MajorationSets.Add(new MajorationSet() { Id = System.Guid.NewGuid().ToString(), ComponentGuid = this.Guid, CardboardProfileId = cp.Id }); db.SaveChanges(); } // retrieve majoration set MajorationSet majoSet = db.MajorationSets.Single(mjs => (mjs.ComponentGuid == this.Guid) && (mjs.CardboardProfileId == cp.Id)); // delete any existing majorations foreach (Majoration majo in db.Majorations.Where(m => m.MajorationSetId == majoSet.Id)) { db.Majorations.Remove(majo); } db.SaveChanges(); // (re)create majorations foreach (KeyValuePair <string, double> entry in majorations) { majoSet.Majorations.Add(new Majoration() { Name = entry.Key, Value = entry.Value }); } db.SaveChanges(); }
public Dictionary <string, double> GetMajorationSet(PLMPackEntities db, CardboardProfile cp) { if (db.MajorationSets.Count( mjs => (mjs.ComponentGuid == Guid) && (mjs.CardboardProfile.GroupId == cp.GroupId)) == 0) { try { // dict majo db.MajorationSets.Add(new MajorationSet() { ComponentGuid = Guid, CardboardProfileId = cp.Id } ); db.SaveChanges(); // build list of majo var majoSets = db.MajorationSets.Where(mjs => (mjs.ComponentGuid == this.Guid) && (mjs.CardboardProfile.GroupId == this.Document.GroupId)); MajorationSet mjsNearest = null; double diffMax = double.MaxValue; foreach (MajorationSet mjset in majoSets) { double thickness = mjset.CardboardProfile.Thickness; if (Math.Abs(thickness - cp.Thickness) < diffMax) { mjsNearest = mjset; diffMax = Math.Abs(thickness - cp.Thickness); } } MajorationSet mjsCurrent = MajorationSet.Single(mjs => (mjs.ComponentGuid == this.Guid) && (mjs.CardboardProfileId == cp.Id)); double thicknessNearest = mjsNearest.CardboardProfile.Thickness; foreach (Majoration mj in mjsNearest.Majorations) { db.Majorations.Add( new Majoration() { MajorationSetId = mjsCurrent.Id, Name = mj.Name, Value = mj.Value * cp.Thickness / thicknessNearest } ); } db.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } var majorationSets = db.MajorationSets.Where(mjs => (mjs.ComponentGuid == this.Guid) && (mjs.CardboardProfile.GroupId == this.Document.GroupId)); // find nearest set MajorationSet nearestSet = null; foreach (MajorationSet majoSet in majorationSets) { if (null == nearestSet || (Math.Abs(majoSet.CardboardProfile.Thickness - cp.Thickness) < Math.Abs(nearestSet.CardboardProfile.Thickness - cp.Thickness)) ) { nearestSet = majoSet; } } // build dictionnary Dictionary <string, double> dictMajo = new Dictionary <string, double>(); if (null != nearestSet) { double coef = (double)(cp.Thickness / nearestSet.CardboardProfile.Thickness); dictMajo.Add("th1", cp.Thickness); dictMajo.Add("ep1", cp.Thickness); foreach (Majoration maj in nearestSet.Majorations) { double valueMaj = maj.Value * coef; if (Math.Abs(coef - 1.0) > 1.0e-3) { MajoRounding rounding = MajoRounding.ROUDING_FIRSTDECIMALNEAREST; switch (rounding) { case MajoRounding.ROUDING_FIRSTDECIMALNEAREST: valueMaj = Math.Round(valueMaj * 10) / 10.0; break; case MajoRounding.ROUNDING_HALFNEAREST: valueMaj = Math.Round(valueMaj * 2) / 2.0; break; case MajoRounding.ROUNDING_HALFTOP: valueMaj = Math.Ceiling(valueMaj * 2) / 2; break; case MajoRounding.ROUDING_INT: valueMaj = Math.Round(valueMaj); break; default: break; // no rounding } } dictMajo.Add(maj.Name, valueMaj); } } return(dictMajo); }