Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions D2PComponentsCSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
34 changes: 15 additions & 19 deletions D2P_Core/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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;
Expand All @@ -58,7 +55,6 @@ public Plane Plane
{
get => Label.Plane;
set => (GeometryCollection[ID] as TextEntity).Plane = value;

}
public GeometryCollection GeometryCollection
{
Expand All @@ -78,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);
Expand All @@ -89,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);
Expand Down Expand Up @@ -175,12 +171,12 @@ IList<Guid> 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);
Expand Down
34 changes: 15 additions & 19 deletions D2P_Core/ComponentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,16 @@ 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; } = 50;
public Color LayerColor { get; set; } = Color.Black;
public Settings Settings { get; set; } = Settings.Default;

public ComponentType(string typeID, string typeName)
{
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;
}

public ComponentType(Layer layer, Settings settings)
Expand All @@ -41,13 +35,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;
}
}
}
2 changes: 1 addition & 1 deletion D2P_Core/Interfaces/IComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace D2P_Core.Interfaces
{
public interface IComponent : IComponentType
{
RhinoDoc ActiveDoc { get; set; }
RhinoDoc ActiveDoc { get; }
ComponentType ComponentType { get; }

// IDs
Expand Down
12 changes: 10 additions & 2 deletions D2P_Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; } = ':';
Expand All @@ -24,6 +24,14 @@ public class Settings
public char CountDelimiter { get; set; } = '#';
public char JointDelimiter { get; set; } = '+';

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()
{
return (Settings)MemberwiseClone();
Expand Down
2 changes: 1 addition & 1 deletion D2P_Core/Utility/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static bool RemoveObjectsFromAllGroups(IEnumerable<Guid> 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;
Expand Down
24 changes: 24 additions & 0 deletions D2P_CoreTests/D2P_CoreTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="MSTest" Version="3.6.4" />
<PackageReference Include="Rhino.Inside" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\D2P_Core\D2P_Core.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

</Project>
77 changes: 77 additions & 0 deletions D2P_CoreTests/MSTestSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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 = string.Empty;

[AssemblyInitialize]
public static void AssemblyInitialize(TestContext context)
{
if (_initialized)
{
throw new InvalidOperationException("Rhino.Inside should only be initialized once.");
}

_rhinoDir = GetRhinoInstallationDirectory();
AssertRhinoDirectoryExists(_rhinoDir);
context.WriteLine("Current Rhino 7 installation: " + _rhinoDir);

EnsureRunningIn64Bit();
UpdateEnvironmentPath(_rhinoDir);

RhinoInside.Resolver.Initialize();
_initialized = true;
context.WriteLine("Rhino.Inside initialization started.");

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()
{
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);
}
}
}
28 changes: 28 additions & 0 deletions D2P_CoreTests/Unit/ComponentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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.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));
}
}
}
2 changes: 1 addition & 1 deletion D2P_GrasshopperTools/GH/Components/GHComponentSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions D2P_GrasshopperTools/GH/Components/GHCreateComponentType.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using D2P_Core;
using Grasshopper.Kernel;
using Rhino;
using System;
using System.Drawing;

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion D2P_GrasshopperTools/GH/Stream/GHStreamComponentTypes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using D2P_Core;
using Grasshopper.Kernel;
using Rhino;
using System;
using System.Linq;

Expand Down Expand Up @@ -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);

Expand Down
Loading