diff --git a/SchematicDrawing/IDrawingItem.cs b/SchematicDrawing/IDrawingItem.cs index 3e8bfd185..40b6a885d 100644 --- a/SchematicDrawing/IDrawingItem.cs +++ b/SchematicDrawing/IDrawingItem.cs @@ -11,6 +11,7 @@ namespace SchematicDrawing /// /// Schematic drawing parameters /// + int Id { get; } string Name { get; } /// Unique name string ClassName { get; } diff --git a/SchematicDrawing/Pipe.cs b/SchematicDrawing/Pipe.cs index 68e80cc06..802d1e8f2 100644 --- a/SchematicDrawing/Pipe.cs +++ b/SchematicDrawing/Pipe.cs @@ -1,5 +1,5 @@ /// -/// Copyright (c) 2020 Sensus Slovensko a.s. +/// Copyright (c) 2020-2021 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; @@ -31,9 +31,9 @@ namespace SchematicDrawing HasIntersection = false; } - public Pipe(string pipeStr, Sz sz, Dictionary name2Item) + public Pipe(string pipeStr, Sz sz, Dictionary id2Item) { - String2Items(pipeStr, name2Item, out StartItem, out StartNodeId, out EndItem, out EndNodeId); + String2Items(pipeStr, id2Item, out StartItem, out StartNodeId, out EndItem, out EndNodeId); Sz = sz; CoordinatesAreValid = false; HasIntersection = false; @@ -41,7 +41,7 @@ namespace SchematicDrawing public override string ToString() { - return (StartItem != null && EndItem != null) ? string.Format("{0}~{1}~{2}~{3}", StartItem.Name, StartNodeId, EndItem.Name, EndNodeId) : string.Empty; + return (StartItem != null && EndItem != null) ? string.Format("{0}~{1}~{2}~{3}", StartItem.Id, StartNodeId, EndItem.Id, EndNodeId) : string.Empty; } @@ -85,16 +85,17 @@ namespace SchematicDrawing /// Start item /// End item /// true when conversion successful - public static bool String2Items(string pipeStr, Dictionary name2Item, out IDrawingItem startItem, out int startNid, out IDrawingItem endItem, out int endNid) + public static bool String2Items(string pipeStr, Dictionary id2Item, out IDrawingItem startItem, out int startNid, out IDrawingItem endItem, out int endNid) { string[] fields = pipeStr.Split(new char[] { '~' }); + int startId, endId; IDrawingItem start, end; DrawingShape dshapeFrom, dshapeTo; - if (fields.Length == 4 && name2Item.TryGetValue(fields[0], out start) + if (fields.Length == 4 && int.TryParse(fields[0], out startId) && id2Item.TryGetValue(startId, out start) && (null != (dshapeFrom = DrawingShape.Shapes[DrawingShape.DrShIx(start.Shape, start.Sz)])) && int.TryParse(fields[1], out startNid) && (startNid >= 0) && (startNid < dshapeFrom.Nodes.Length) - && name2Item.TryGetValue(fields[2], out end) + && int.TryParse(fields[2], out endId) && id2Item.TryGetValue(endId, out end) && (null != (dshapeTo = DrawingShape.Shapes[DrawingShape.DrShIx(end.Shape, end.Sz)])) && int.TryParse(fields[3], out endNid) && (endNid >= 0) && (endNid < dshapeTo.Nodes.Length)) { diff --git a/SchematicDrawing/SchematicDrawingCtrl.cs b/SchematicDrawing/SchematicDrawingCtrl.cs index 2d36bc318..1634cb4e0 100644 --- a/SchematicDrawing/SchematicDrawingCtrl.cs +++ b/SchematicDrawing/SchematicDrawingCtrl.cs @@ -131,7 +131,7 @@ namespace SchematicDrawing IList pipesXL; IList allPipes; /// - public string[] PipesS + public string[] PipesS { get { string[] retv = new string[pipesS.Count]; @@ -144,7 +144,7 @@ namespace SchematicDrawing { foreach (var pStr in value) { - pipesS.Add(new Pipe(pStr, Sz.S, Name2Item)); + pipesS.Add(new Pipe(pStr, Sz.S, Id2Item)); } } @@ -171,7 +171,7 @@ namespace SchematicDrawing { foreach (var pStr in value) { - pipesM.Add(new Pipe(pStr, Sz.M, Name2Item)); + pipesM.Add(new Pipe(pStr, Sz.M, Id2Item)); } } @@ -196,7 +196,7 @@ namespace SchematicDrawing { foreach (var pStr in value) { - pipesL.Add(new Pipe(pStr, Sz.L, Name2Item)); + pipesL.Add(new Pipe(pStr, Sz.L, Id2Item)); } } @@ -221,7 +221,7 @@ namespace SchematicDrawing { foreach (var pStr in value) { - pipesXL.Add(new Pipe(pStr, Sz.XL, Name2Item)); + pipesXL.Add(new Pipe(pStr, Sz.XL, Id2Item)); } } @@ -283,6 +283,7 @@ namespace SchematicDrawing /// Dictionary that speeds up drawing edges between nodes /// public Dictionary Name2Item; + public Dictionary Id2Item; /// @@ -343,6 +344,7 @@ namespace SchematicDrawing items = new List(); Name2Item = new Dictionary(); + Id2Item = new Dictionary(); SelectedItem = null; route = 0x55AA; /// Invalid value to be overwritten in the first write to Route @@ -366,6 +368,7 @@ namespace SchematicDrawing { items.Add(item); Name2Item.Add(item.Name, item); + Id2Item.Add(item.Id, item); if (!EditMode) { @@ -397,6 +400,7 @@ namespace SchematicDrawing { items.Clear(); Name2Item.Clear(); + Id2Item.Clear(); graph.Clear(); Redraw(); } @@ -580,7 +584,7 @@ namespace SchematicDrawing for (int j = 0; j < allPipes.Count; j++) { Pipe pipe = allPipes[j]; - DrawPipe(e, pipe, pipe.StartItem.GNodes[pipe.StartNodeId].Dist); + DrawPipe(e, pipe, pipe.StartItem != null ? pipe.StartItem.GNodes[pipe.StartNodeId].Dist : int.MaxValue); } } diff --git a/TBF/Rig/ComponentBase.cs b/TBF/Rig/ComponentBase.cs index d933acdc7..3aaad5d64 100644 --- a/TBF/Rig/ComponentBase.cs +++ b/TBF/Rig/ComponentBase.cs @@ -13,6 +13,7 @@ namespace TBF.Rig readonly Generic.IComponentCfg cfg; public Generic.IComponentCfg Cfg { get { return cfg; } } + public int Id { get { return cfg.Id; } } public string Name { get { return cfg.Name; } } public string ClassName { get { return cfg.Factory.ClassName; } } public string ParentName { get { return cfg.ParentName; } } diff --git a/TBF/Rig/ComponentCfgBase.cs b/TBF/Rig/ComponentCfgBase.cs index a8021678f..20a7c7d6b 100644 --- a/TBF/Rig/ComponentCfgBase.cs +++ b/TBF/Rig/ComponentCfgBase.cs @@ -12,6 +12,9 @@ namespace TBF.Rig { public class ComponentCfgBase { + [XmlIgnore] + public int Id { get; set; } + [XmlIgnore] public string Name { @@ -127,6 +130,7 @@ namespace TBF.Rig IComponentCfg config = s.Deserialize(reader) as IComponentCfg; if (config != null) { + config.Id = cmpntEntity.Id; config.Name = cmpntEntity.Name; config.ParentName = cmpntEntity.Parent; config.ItemNr = cmpntEntity.ItemNr; diff --git a/TBF/Rig/Generic/IComponentCfg.cs b/TBF/Rig/Generic/IComponentCfg.cs index aca9288db..99bb3b4d9 100644 --- a/TBF/Rig/Generic/IComponentCfg.cs +++ b/TBF/Rig/Generic/IComponentCfg.cs @@ -8,6 +8,8 @@ namespace TBF.Rig.Generic { public interface IComponentCfg { + int Id { get; set; } + /// /// Unique component # (0 .. nr.of components-1) /// diff --git a/TBF/UI/Bench/EditSchDrawing/EditSchDrawingDlg.cs b/TBF/UI/Bench/EditSchDrawing/EditSchDrawingDlg.cs index a3b0157c2..026c2b905 100644 --- a/TBF/UI/Bench/EditSchDrawing/EditSchDrawingDlg.cs +++ b/TBF/UI/Bench/EditSchDrawing/EditSchDrawingDlg.cs @@ -104,9 +104,9 @@ namespace TBF.UI.Bench.EditSchDrawing var eCfg = cmpCfg as TBF.Rig.Various.Drawing.Pipes.Configuration; switch (eCfg.Sz) { - case Sz.S: if (pipesScfg == null) pipesScfg = eCfg; break; - case Sz.M: if (pipesMcfg == null) pipesMcfg = eCfg; break; - case Sz.L: if (pipesLcfg == null) pipesLcfg = eCfg; break; + case Sz.S: if (pipesScfg == null) pipesScfg = eCfg; break; + case Sz.M: if (pipesMcfg == null) pipesMcfg = eCfg; break; + case Sz.L: if (pipesLcfg == null) pipesLcfg = eCfg; break; case Sz.XL: if (pipesXLcfg == null) pipesXLcfg = eCfg; break; } } @@ -236,6 +236,29 @@ namespace TBF.UI.Bench.EditSchDrawing IComponentFactory factory = selectComponentTypeDlg.SlctdCmpntFactory; IComponentCfg cfg = factory.DefaultConfig(); + /// Find a uniqueue component name with a nummber + int i = 1; + do + { + string newName = string.Format("{0}{1}", cfg.Name, i); + bool alreadyExists = false; + foreach (var ce in cmpntEntities) + { + if (ce.Name == newName) + { + alreadyExists = true; + break; + } + } + if (!alreadyExists) + { + cfg.Name = newName; + break; + } + i++; + } + while (i < 100); + IComponentCfgCtrl cfgControl = cfg.GetControl(cmpntEntities); cfgControl.Config = cfg; cfgControl.Config.ItemNr = (cmpntEntities.Count > 0) ? (cmpntEntities[cmpntEntities.Count - 1].ItemNr + 1) : 1; @@ -249,6 +272,8 @@ namespace TBF.UI.Bench.EditSchDrawing { Config.Entities.Component newComponent = cfgForm.Config.CreateDbEntity(); cmpntEntities.Add(newComponent); + session.SaveOrUpdate(newComponent); + cfgForm.Config.Id = newComponent.Id; IDrawingItem item = cfgForm.Config as IDrawingItem; if (item != null)