diff --git a/I3DShapesTool.Lib/Model/I3D/I3D.cs b/I3DShapesTool.Lib/Model/I3D/I3D.cs index 26f2e3e..9167563 100644 --- a/I3DShapesTool.Lib/Model/I3D/I3D.cs +++ b/I3DShapesTool.Lib/Model/I3D/I3D.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace I3DShapesTool.Lib.Model.I3D { @@ -7,5 +8,41 @@ public class I3D public string? Name { get; set; } public string? Version { get; set; } public TransformGroup SceneRoot { get; } = new TransformGroup("root", 0, I3DVector.Zero, I3DVector.Zero, I3DVector.One); + + private IDictionary? _shapesMap; + + private void MapShapesRecurse(TransformGroup parent) + { + if (_shapesMap == null) + throw new InvalidOperationException(); + + if (parent is Shape shape && shape.ShapeId != null) + { + _shapesMap.Add((int)shape.ShapeId, shape); + } + + foreach (var child in parent.Children) + { + MapShapesRecurse(child); + } + } + + private void MapShapes() + { + _shapesMap = new Dictionary(); + MapShapesRecurse(SceneRoot); + } + + public void Setup() + { + MapShapes(); + } + + public Shape? GetShape(int id) + { + if (_shapesMap == null || !_shapesMap.ContainsKey(id)) + return null; + return _shapesMap[id]; + } } } diff --git a/I3DShapesTool.Lib/Model/I3D/I3DXMLReader.cs b/I3DShapesTool.Lib/Model/I3D/I3DXMLReader.cs index ac3ec80..0381b78 100644 --- a/I3DShapesTool.Lib/Model/I3D/I3DXMLReader.cs +++ b/I3DShapesTool.Lib/Model/I3D/I3DXMLReader.cs @@ -130,6 +130,8 @@ public static I3D ParseXML(string filePath) } } + result.Setup(); + return result; } }