From bce475583cc994390d7bfc052abf1a3e16d1388d Mon Sep 17 00:00:00 2001 From: Bastian Nispel Date: Tue, 18 Mar 2025 15:53:24 +0100 Subject: [PATCH 1/3] implement basic core-test --- D2PComponentsCSharp.sln | 6 +++++ D2P_Core/Component.cs | 1 - D2P_Core/ComponentType.cs | 33 ++++++++++++++-------------- D2P_Core/Settings.cs | 2 ++ D2P_CoreTests/D2P_CoreTests.csproj | 24 ++++++++++++++++++++ D2P_CoreTests/MSTestSettings.cs | 1 + D2P_CoreTests/Unit/ComponentTests.cs | 18 +++++++++++++++ 7 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 D2P_CoreTests/D2P_CoreTests.csproj create mode 100644 D2P_CoreTests/MSTestSettings.cs create mode 100644 D2P_CoreTests/Unit/ComponentTests.cs diff --git a/D2PComponentsCSharp.sln b/D2PComponentsCSharp.sln index cedfe71..e6f8be8 100644 --- a/D2PComponentsCSharp.sln +++ b/D2PComponentsCSharp.sln @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D2P_GrasshopperTools", "D2P EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D2P_Core", "D2P_Core\D2P_Core.csproj", "{173BFE75-42C2-4E14-B0EC-EF0ECC1BDDF3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D2P_CoreTests", "D2P_CoreTests\D2P_CoreTests.csproj", "{97ECF6D3-0083-4E79-8F7B-5EFCACC66432}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -31,6 +33,10 @@ Global {173BFE75-42C2-4E14-B0EC-EF0ECC1BDDF3}.Debug|Any CPU.Build.0 = Debug|Any CPU {173BFE75-42C2-4E14-B0EC-EF0ECC1BDDF3}.Release|Any CPU.ActiveCfg = Release|Any CPU {173BFE75-42C2-4E14-B0EC-EF0ECC1BDDF3}.Release|Any CPU.Build.0 = Release|Any CPU + {97ECF6D3-0083-4E79-8F7B-5EFCACC66432}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {97ECF6D3-0083-4E79-8F7B-5EFCACC66432}.Debug|Any CPU.Build.0 = Debug|Any CPU + {97ECF6D3-0083-4E79-8F7B-5EFCACC66432}.Release|Any CPU.ActiveCfg = Release|Any CPU + {97ECF6D3-0083-4E79-8F7B-5EFCACC66432}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/D2P_Core/Component.cs b/D2P_Core/Component.cs index e683262..e1c494f 100644 --- a/D2P_Core/Component.cs +++ b/D2P_Core/Component.cs @@ -58,7 +58,6 @@ public Plane Plane { get => Label.Plane; set => (GeometryCollection[ID] as TextEntity).Plane = value; - } public GeometryCollection GeometryCollection { diff --git a/D2P_Core/ComponentType.cs b/D2P_Core/ComponentType.cs index 6f61c1b..1bead4c 100644 --- a/D2P_Core/ComponentType.cs +++ b/D2P_Core/ComponentType.cs @@ -7,22 +7,19 @@ namespace D2P_Core { public class ComponentType : IComponentType { - public ComponentType(string typeID, string typeName, Settings settings, double? labelSize, Color? layerColor) + public string TypeID { get; set; } + public string TypeName { get; set; } + public double LabelSize { get; set; } + public Color LayerColor { get; set; } + public Settings Settings { get; set; } + + public ComponentType(string typeID, string typeName, Settings settings = null, double? labelSize = null, Color? layerColor = null) { TypeID = typeID; TypeName = typeName; LabelSize = labelSize ?? Rhino.RhinoDoc.ActiveDoc.DimStyles.Current.TextHeight; LayerColor = layerColor ?? Color.Black; - Settings = settings; - } - - public ComponentType(IComponent component) - { - TypeID = component.TypeID; - TypeName = component.TypeName; - LabelSize = component.LabelSize; - LayerColor = component.LayerColor; - Settings = component.Settings; + Settings = settings ?? Settings.Default; } public ComponentType(Layer layer, Settings settings) @@ -41,13 +38,15 @@ public ComponentType(TextObject textObj, Settings settings) LabelSize = textObj.TextGeometry.TextHeight; LayerColor = Objects.ComponentTypeLayerColorFromObject(textObj, settings); Settings = Layers.GetComponentTypeSettings(textObj, settings); - } - public string TypeID { get; set; } - public string TypeName { get; set; } - public double LabelSize { get; set; } - public Color LayerColor { get; set; } - public Settings Settings { get; set; } + public ComponentType(IComponent component) + { + TypeID = component.TypeID; + TypeName = component.TypeName; + LabelSize = component.LabelSize; + LayerColor = component.LayerColor; + Settings = component.Settings; + } } } diff --git a/D2P_Core/Settings.cs b/D2P_Core/Settings.cs index a543c01..8b40e39 100644 --- a/D2P_Core/Settings.cs +++ b/D2P_Core/Settings.cs @@ -24,6 +24,8 @@ public class Settings public char CountDelimiter { get; set; } = '#'; public char JointDelimiter { get; set; } = '+'; + public static Settings Default => new Settings(); + public Settings ShallowCopy() { return (Settings)MemberwiseClone(); diff --git a/D2P_CoreTests/D2P_CoreTests.csproj b/D2P_CoreTests/D2P_CoreTests.csproj new file mode 100644 index 0000000..6b6c0c8 --- /dev/null +++ b/D2P_CoreTests/D2P_CoreTests.csproj @@ -0,0 +1,24 @@ + + + + net48 + latest + enable + enable + + + + + + + + + + + + + + + + + diff --git a/D2P_CoreTests/MSTestSettings.cs b/D2P_CoreTests/MSTestSettings.cs new file mode 100644 index 0000000..aaf278c --- /dev/null +++ b/D2P_CoreTests/MSTestSettings.cs @@ -0,0 +1 @@ +[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)] diff --git a/D2P_CoreTests/Unit/ComponentTests.cs b/D2P_CoreTests/Unit/ComponentTests.cs new file mode 100644 index 0000000..276f9ca --- /dev/null +++ b/D2P_CoreTests/Unit/ComponentTests.cs @@ -0,0 +1,18 @@ +using D2P_Core; +using Rhino.Geometry; + +namespace D2P_CoreTests.Unit +{ + [TestClass] + public sealed class ComponentTests + { + [TestMethod] + public void Component_Constructor() + { + var componentType = new ComponentType("TEST", "Testing Type"); + var component = new Component(componentType, "199", Plane.WorldXY); + + Assert.IsTrue(component.ID != Guid.Empty); + } + } +} From d7bfb102b2350fb1cb233321f56c0d25fda30934 Mon Sep 17 00:00:00 2001 From: Bastian Nispel Date: Tue, 18 Mar 2025 17:38:05 +0100 Subject: [PATCH 2/3] refactoring rhino doc dependencies and add rhino.inside --- D2P_Core/Component.cs | 33 +++++---- D2P_Core/ComponentType.cs | 11 ++- D2P_Core/Interfaces/IComponent.cs | 2 +- D2P_Core/Settings.cs | 12 +++- D2P_Core/Utility/Group.cs | 2 +- D2P_CoreTests/D2P_CoreTests.csproj | 2 +- D2P_CoreTests/MSTestSettings.cs | 67 ++++++++++++++++++- .../GH/Components/GHComponentSettings.cs | 2 +- .../GH/Components/GHCreateComponentType.cs | 10 ++- .../GH/Stream/GHStreamComponentTypes.cs | 3 +- .../Utility/DefaultSettings.cs | 2 +- 11 files changed, 109 insertions(+), 37 deletions(-) diff --git a/D2P_Core/Component.cs b/D2P_Core/Component.cs index e1c494f..a4988bd 100644 --- a/D2P_Core/Component.cs +++ b/D2P_Core/Component.cs @@ -16,15 +16,12 @@ namespace D2P_Core public class Component : IComponent { - GeometryCollection _geometryCollection = new GeometryCollection(); AttributeCollection _attributeCollection = new AttributeCollection(); LayerCollection _stagingLayerCollection = new LayerCollection(); - protected ComponentType _componentType; - // Doc - public RhinoDoc ActiveDoc { get; set; } = RhinoDoc.ActiveDoc; + public RhinoDoc ActiveDoc { get; } = null; // IDs public Guid ID { get; set; } @@ -33,16 +30,16 @@ public class Component : IComponent public string ShortName => Label.PlainText; // State - public bool IsInitialized => ActiveDoc.Layers.FindName(Layers.ComposeComponentTypeLayerName(this)) != null; - public bool IsVirtual => ActiveDoc.Objects.FindId(ID) == null; + public bool IsInitialized => ActiveDoc == null || ActiveDoc.Layers.FindName(Layers.ComposeComponentTypeLayerName(this)) != null; + public bool IsVirtual => ActiveDoc == null || ActiveDoc.Objects.FindId(ID) == null; public bool IsVirtualClone { get; private set; } // Type - public ComponentType ComponentType => _componentType; - public string TypeID => _componentType.TypeID; - public string TypeName => _componentType.TypeName; - public Color LayerColor => _componentType.LayerColor; - public Settings Settings => _componentType.Settings; + public ComponentType ComponentType { get; } + public string TypeID => ComponentType.TypeID; + public string TypeName => ComponentType.TypeName; + public Color LayerColor => ComponentType.LayerColor; + public Settings Settings => ComponentType.Settings; // Geometry public double LabelSize => Label.TextHeight; @@ -77,7 +74,7 @@ public AttributeCollection AttributeCollection public Component(ComponentType componentType, string name, Plane plane) { ID = Guid.NewGuid(); - _componentType = componentType; + ComponentType = componentType; var label = TextEntity.Create(name, plane, Settings.DimensionStyle, false, 0, 0); label.TextHeight = componentType.LabelSize; GeometryCollection.Add(ID, label); @@ -88,16 +85,16 @@ public Component(ComponentType componentType, string name, Plane plane) public Component(ComponentType componentType, Guid id) { ID = id; - _componentType = componentType; + ComponentType = componentType; } - protected Component(IComponent component) + private Component(IComponent component) { ID = component.ID; - _componentType = new ComponentType(component); + ComponentType = new ComponentType(component); GeometryCollection = new GeometryCollection(component.GeometryCollection); var label = TextEntity.Create(ShortName, Plane, Settings.DimensionStyle, false, 0, 0); - label.TextHeight = _componentType.LabelSize; + label.TextHeight = ComponentType.LabelSize; GeometryCollection[ID] = label; AttributeCollection = new AttributeCollection(component.AttributeCollection); StagingLayerCollection = new LayerCollection(component.StagingLayerCollection); @@ -174,12 +171,12 @@ IList AddObjectToCollections(ComponentMember member) return ids; } - void CacheCollections() + private void CacheCollections() { _geometryCollection = GeometryCollection; _attributeCollection = AttributeCollection; } - void RemoveObjectFromCollections(Guid objID) + private void RemoveObjectFromCollections(Guid objID) { _geometryCollection.Remove(objID); _attributeCollection.Remove(objID); diff --git a/D2P_Core/ComponentType.cs b/D2P_Core/ComponentType.cs index 1bead4c..45dfbd2 100644 --- a/D2P_Core/ComponentType.cs +++ b/D2P_Core/ComponentType.cs @@ -9,17 +9,14 @@ public class ComponentType : IComponentType { public string TypeID { get; set; } public string TypeName { get; set; } - public double LabelSize { get; set; } - public Color LayerColor { get; set; } - public Settings Settings { get; set; } + public double LabelSize { get; set; } = 50; + public Color LayerColor { get; set; } = Color.Black; + public Settings Settings { get; set; } = Settings.Default; - public ComponentType(string typeID, string typeName, Settings settings = null, double? labelSize = null, Color? layerColor = null) + public ComponentType(string typeID, string typeName) { TypeID = typeID; TypeName = typeName; - LabelSize = labelSize ?? Rhino.RhinoDoc.ActiveDoc.DimStyles.Current.TextHeight; - LayerColor = layerColor ?? Color.Black; - Settings = settings ?? Settings.Default; } public ComponentType(Layer layer, Settings settings) diff --git a/D2P_Core/Interfaces/IComponent.cs b/D2P_Core/Interfaces/IComponent.cs index 4fc5e62..672d67f 100644 --- a/D2P_Core/Interfaces/IComponent.cs +++ b/D2P_Core/Interfaces/IComponent.cs @@ -8,7 +8,7 @@ namespace D2P_Core.Interfaces { public interface IComponent : IComponentType { - RhinoDoc ActiveDoc { get; set; } + RhinoDoc ActiveDoc { get; } ComponentType ComponentType { get; } // IDs diff --git a/D2P_Core/Settings.cs b/D2P_Core/Settings.cs index 8b40e39..3c3e134 100644 --- a/D2P_Core/Settings.cs +++ b/D2P_Core/Settings.cs @@ -12,8 +12,8 @@ public class Settings public Color RootLayerColor { get; set; } = Color.FromArgb(220, 75, 58); // Style - public string DimensionStyleName { get; set; } = RhinoDoc.ActiveDoc.DimStyles.Current.Name; - public DimensionStyle DimensionStyle => RhinoDoc.ActiveDoc.DimStyles.FindName(DimensionStyleName) ?? RhinoDoc.ActiveDoc.DimStyles.Current; + public string DimensionStyleName { get; set; } + public DimensionStyle DimensionStyle { get; } // Delimiter public char TypeDelimiter { get; set; } = ':'; @@ -24,7 +24,13 @@ public class Settings public char CountDelimiter { get; set; } = '#'; public char JointDelimiter { get; set; } = '+'; - public static Settings Default => new Settings(); + public Settings(RhinoDoc doc) + { + DimensionStyleName = doc?.DimStyles.Current.Name ?? string.Empty; + DimensionStyle = doc?.DimStyles.FindName(DimensionStyleName) ?? doc?.DimStyles.Current ?? new DimensionStyle(); + } + + public static Settings Default => new Settings(null); public Settings ShallowCopy() { diff --git a/D2P_Core/Utility/Group.cs b/D2P_Core/Utility/Group.cs index eabd235..83a54ad 100644 --- a/D2P_Core/Utility/Group.cs +++ b/D2P_Core/Utility/Group.cs @@ -41,7 +41,7 @@ public static bool RemoveObjectsFromAllGroups(IEnumerable objectIDs, Rhino public static int GetGroupIndex(Guid componentID, RhinoDoc doc) { - if (componentID.Equals(Guid.Empty)) return -1; + if (doc == null || componentID.Equals(Guid.Empty)) return -1; var rhObj = doc.Objects.FindId(componentID); if (rhObj?.GroupCount != 1) return -1; diff --git a/D2P_CoreTests/D2P_CoreTests.csproj b/D2P_CoreTests/D2P_CoreTests.csproj index 6b6c0c8..c865d14 100644 --- a/D2P_CoreTests/D2P_CoreTests.csproj +++ b/D2P_CoreTests/D2P_CoreTests.csproj @@ -10,7 +10,7 @@ - + diff --git a/D2P_CoreTests/MSTestSettings.cs b/D2P_CoreTests/MSTestSettings.cs index aaf278c..c3ee4eb 100644 --- a/D2P_CoreTests/MSTestSettings.cs +++ b/D2P_CoreTests/MSTestSettings.cs @@ -1 +1,66 @@ -[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)] +using Microsoft.Win32; +using System.Reflection; + +[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)] + +namespace D2P_CoreTests +{ + [TestClass] + public static class TestInit + { + private static bool initialized = false; + private static string rhinoDir; + + [AssemblyInitialize] + public static void AssemblyInitialize(TestContext context) + { + //get the correct rhino 7 installation directory + rhinoDir = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\7.0\Install", "Path", null) as string ?? string.Empty; + Assert.IsTrue(System.IO.Directory.Exists(rhinoDir), "Rhino system dir not found: {0}", rhinoDir); + context.WriteLine(" The current Rhino 7 installation is " + rhinoDir); + + if (initialized) + { + throw new InvalidOperationException("Initialize Rhino.Inside once"); + } + else + { + RhinoInside.Resolver.Initialize(); + initialized = true; + context.WriteLine("Rhino.Inside init has started"); + } + + // Ensure we are running the tests in x64 + Assert.IsTrue(Environment.Is64BitProcess, "Tests must be run as x64"); + + // Set path to rhino system directory + string envPath = Environment.GetEnvironmentVariable("path"); + Environment.SetEnvironmentVariable("path", envPath + ";" + rhinoDir); + + // Start a headless rhino instance using Rhino.Inside + StartRhino(); + + // We have to load grasshopper.dll on the current AppDomain manually for some reason + AppDomain.CurrentDomain.AssemblyResolve += ResolveGrasshopper; + } + + [STAThread] + public static void StartRhino() + { + var rhinoCore = new Rhino.Runtime.InProcess.RhinoCore(null, Rhino.Runtime.InProcess.WindowStyle.NoWindow); + } + + private static Assembly ResolveGrasshopper(object sender, ResolveEventArgs args) + { + var name = args.Name; + + if (!name.StartsWith("Grasshopper")) + { + return null; + } + + var path = Path.Combine(Path.GetFullPath(Path.Combine(rhinoDir, @"..\")), "Plug-ins\\Grasshopper\\Grasshopper.dll"); + return Assembly.LoadFrom(path); + } + } +} diff --git a/D2P_GrasshopperTools/GH/Components/GHComponentSettings.cs b/D2P_GrasshopperTools/GH/Components/GHComponentSettings.cs index cefb5bf..daa1cb6 100644 --- a/D2P_GrasshopperTools/GH/Components/GHComponentSettings.cs +++ b/D2P_GrasshopperTools/GH/Components/GHComponentSettings.cs @@ -85,7 +85,7 @@ protected override void SolveInstance(IGH_DataAccess DA) DA.GetData(8, ref countDelimiter); DA.GetData(9, ref jointDelimiter); - var settings = new Settings() + var settings = new Settings(null) { RootLayerName = !string.IsNullOrEmpty(rootLayerName) ? rootLayerName : Properties.Settings.Default.DefaultRootLayerName, RootLayerColor = rootLayerColor != Color.Empty ? rootLayerColor : Properties.Settings.Default.DefaultRootLayerColor, diff --git a/D2P_GrasshopperTools/GH/Components/GHCreateComponentType.cs b/D2P_GrasshopperTools/GH/Components/GHCreateComponentType.cs index c18cfec..3dbfc07 100644 --- a/D2P_GrasshopperTools/GH/Components/GHCreateComponentType.cs +++ b/D2P_GrasshopperTools/GH/Components/GHCreateComponentType.cs @@ -1,5 +1,6 @@ using D2P_Core; using Grasshopper.Kernel; +using Rhino; using System; using System.Drawing; @@ -60,7 +61,7 @@ protected override void SolveInstance(IGH_DataAccess DA) DA.GetData(4, ref settings); if (settings == null) - settings = new Settings(); + settings = new Settings(RhinoDoc.ActiveDoc); if (string.IsNullOrEmpty(typeName)) { var componentLayer = D2P_Core.Utility.Layers.FindComponentLayerByType(typeID, settings.RootLayerName); @@ -76,7 +77,12 @@ protected override void SolveInstance(IGH_DataAccess DA) if (labelSize <= 0) labelSize = Rhino.RhinoDoc.ActiveDoc.DimStyles.Current.TextHeight; - var cls = new ComponentType(typeID, typeName, settings, labelSize, layerColor); + var cls = new ComponentType(typeID, typeName) + { + Settings = settings, + LayerColor = layerColor, + LabelSize = labelSize, + }; DA.SetData(0, cls); } diff --git a/D2P_GrasshopperTools/GH/Stream/GHStreamComponentTypes.cs b/D2P_GrasshopperTools/GH/Stream/GHStreamComponentTypes.cs index 8970d00..1329b2d 100644 --- a/D2P_GrasshopperTools/GH/Stream/GHStreamComponentTypes.cs +++ b/D2P_GrasshopperTools/GH/Stream/GHStreamComponentTypes.cs @@ -1,5 +1,6 @@ using D2P_Core; using Grasshopper.Kernel; +using Rhino; using System; using System.Linq; @@ -41,7 +42,7 @@ protected override void SolveInstance(IGH_DataAccess DA) { Settings settings = null; DA.GetData(0, ref settings); - settings = settings ?? new Settings(); + settings = settings ?? new Settings(RhinoDoc.ActiveDoc); var componentTypes = D2P_Core.Utility.Instantiation.GetComponentTypes(settings); diff --git a/D2P_GrasshopperTools/Utility/DefaultSettings.cs b/D2P_GrasshopperTools/Utility/DefaultSettings.cs index 783eac8..0a6a2ad 100644 --- a/D2P_GrasshopperTools/Utility/DefaultSettings.cs +++ b/D2P_GrasshopperTools/Utility/DefaultSettings.cs @@ -4,7 +4,7 @@ internal static class DefaultSettings { internal static D2P_Core.Settings Create() { - return new D2P_Core.Settings() + return new D2P_Core.Settings(null) { RootLayerName = Properties.Settings.Default.DefaultRootLayerName, RootLayerColor = Properties.Settings.Default.DefaultRootLayerColor, From 8fbb77203ba75bd91b4fb679ed613e36469801cc Mon Sep 17 00:00:00 2001 From: Bastian Nispel Date: Tue, 18 Mar 2025 18:03:59 +0100 Subject: [PATCH 3/3] refactor test-init and implement component-constructor test --- D2P_Core/Component.cs | 2 +- D2P_CoreTests/MSTestSettings.cs | 59 +++++++++++++++++----------- D2P_CoreTests/Unit/ComponentTests.cs | 12 +++++- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/D2P_Core/Component.cs b/D2P_Core/Component.cs index a4988bd..8b837ed 100644 --- a/D2P_Core/Component.cs +++ b/D2P_Core/Component.cs @@ -30,7 +30,7 @@ public class Component : IComponent public string ShortName => Label.PlainText; // State - public bool IsInitialized => ActiveDoc == null || ActiveDoc.Layers.FindName(Layers.ComposeComponentTypeLayerName(this)) != null; + public bool IsInitialized => ActiveDoc != null && ActiveDoc.Layers.FindName(Layers.ComposeComponentTypeLayerName(this)) != null; public bool IsVirtual => ActiveDoc == null || ActiveDoc.Objects.FindId(ID) == null; public bool IsVirtualClone { get; private set; } diff --git a/D2P_CoreTests/MSTestSettings.cs b/D2P_CoreTests/MSTestSettings.cs index c3ee4eb..1aca86f 100644 --- a/D2P_CoreTests/MSTestSettings.cs +++ b/D2P_CoreTests/MSTestSettings.cs @@ -8,42 +8,53 @@ namespace D2P_CoreTests [TestClass] public static class TestInit { - private static bool initialized = false; - private static string rhinoDir; + private static bool _initialized = false; + private static string _rhinoDir = string.Empty; [AssemblyInitialize] public static void AssemblyInitialize(TestContext context) { - //get the correct rhino 7 installation directory - rhinoDir = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\7.0\Install", "Path", null) as string ?? string.Empty; - Assert.IsTrue(System.IO.Directory.Exists(rhinoDir), "Rhino system dir not found: {0}", rhinoDir); - context.WriteLine(" The current Rhino 7 installation is " + rhinoDir); - - if (initialized) - { - throw new InvalidOperationException("Initialize Rhino.Inside once"); - } - else + if (_initialized) { - RhinoInside.Resolver.Initialize(); - initialized = true; - context.WriteLine("Rhino.Inside init has started"); + throw new InvalidOperationException("Rhino.Inside should only be initialized once."); } - // Ensure we are running the tests in x64 - Assert.IsTrue(Environment.Is64BitProcess, "Tests must be run as x64"); + _rhinoDir = GetRhinoInstallationDirectory(); + AssertRhinoDirectoryExists(_rhinoDir); + context.WriteLine("Current Rhino 7 installation: " + _rhinoDir); - // Set path to rhino system directory - string envPath = Environment.GetEnvironmentVariable("path"); - Environment.SetEnvironmentVariable("path", envPath + ";" + rhinoDir); + EnsureRunningIn64Bit(); + UpdateEnvironmentPath(_rhinoDir); - // Start a headless rhino instance using Rhino.Inside - StartRhino(); + RhinoInside.Resolver.Initialize(); + _initialized = true; + context.WriteLine("Rhino.Inside initialization started."); - // We have to load grasshopper.dll on the current AppDomain manually for some reason + StartRhino(); AppDomain.CurrentDomain.AssemblyResolve += ResolveGrasshopper; } + private static string GetRhinoInstallationDirectory() + { + return Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\7.0\Install", "Path", null) as string ?? string.Empty; + } + + private static void AssertRhinoDirectoryExists(string rhinoDir) + { + Assert.IsTrue(Directory.Exists(rhinoDir), $"Rhino system directory not found: {rhinoDir}"); + } + + private static void EnsureRunningIn64Bit() + { + Assert.IsTrue(Environment.Is64BitProcess, "Tests must be run as x64."); + } + + private static void UpdateEnvironmentPath(string rhinoDir) + { + string currentPath = Environment.GetEnvironmentVariable("PATH") ?? string.Empty; + Environment.SetEnvironmentVariable("PATH", currentPath + ";" + rhinoDir); + } + [STAThread] public static void StartRhino() { @@ -59,7 +70,7 @@ private static Assembly ResolveGrasshopper(object sender, ResolveEventArgs args) return null; } - var path = Path.Combine(Path.GetFullPath(Path.Combine(rhinoDir, @"..\")), "Plug-ins\\Grasshopper\\Grasshopper.dll"); + var path = Path.Combine(Path.GetFullPath(Path.Combine(_rhinoDir, @"..\")), "Plug-ins\\Grasshopper\\Grasshopper.dll"); return Assembly.LoadFrom(path); } } diff --git a/D2P_CoreTests/Unit/ComponentTests.cs b/D2P_CoreTests/Unit/ComponentTests.cs index 276f9ca..878aab3 100644 --- a/D2P_CoreTests/Unit/ComponentTests.cs +++ b/D2P_CoreTests/Unit/ComponentTests.cs @@ -12,7 +12,17 @@ public void Component_Constructor() var componentType = new ComponentType("TEST", "Testing Type"); var component = new Component(componentType, "199", Plane.WorldXY); - Assert.IsTrue(component.ID != Guid.Empty); + Assert.AreNotEqual(Guid.Empty, component.ID); + Assert.AreEqual(-1, component.GroupIdx); + Assert.AreEqual("TEST:199", component.Name); + Assert.AreEqual("199", component.ShortName); + Assert.IsFalse(component.IsInitialized); + Assert.IsTrue(component.IsVirtual); + Assert.IsFalse(component.IsVirtualClone); + Assert.AreEqual(componentType, component.ComponentType); + Assert.AreEqual(Plane.WorldXY, component.Plane); + Assert.IsTrue(component.GeometryCollection.ContainsKey(component.ID)); + Assert.IsTrue(component.AttributeCollection.ContainsKey(component.ID)); } } }