From 22a711aec726bc8840a2a1b80e6f12005ecc6ae1 Mon Sep 17 00:00:00 2001 From: Tom Svilans Date: Thu, 19 Nov 2020 16:46:43 +0100 Subject: [PATCH 1/3] feat(goo): add GH_Element and GH_Structure classes --- GluLamb.GH/GluLambGoo.cs | 193 ++++++++++++++++++------------ GluLamb/Structure/Element.cs | 6 + GluLamb/Structure/ElementGroup.cs | 7 ++ GluLamb/Structure/Structure.cs | 6 +- 4 files changed, 132 insertions(+), 80 deletions(-) diff --git a/GluLamb.GH/GluLambGoo.cs b/GluLamb.GH/GluLambGoo.cs index c4752ed..fc87590 100644 --- a/GluLamb.GH/GluLambGoo.cs +++ b/GluLamb.GH/GluLambGoo.cs @@ -280,123 +280,158 @@ public override bool Read(GH_IReader reader) } #endregion } - /* - public class GH_GlulamWorkpiece : GH_Goo, IGH_PreviewData + + public class GH_Element : GH_Goo { - public GH_GlulamWorkpiece() { this.Value = null; } - public GH_GlulamWorkpiece(GH_GlulamWorkpiece goo) { this.Value = goo.Value; } - public GH_GlulamWorkpiece(GlulamWorkpiece native) { this.Value = native; } - public override IGH_Goo Duplicate() => new GH_GlulamWorkpiece(this); - public override bool IsValid => true; - public override string TypeName => "GluLamb Workpiece"; - public override string TypeDescription => "GluLamb Workpiece"; - public override string ToString() => Value.ToString(); //this.Value.ToString(); + #region Constructors + public GH_Element() : this(null) { } + public GH_Element(Element native) { this.Value = native; } + + public override IGH_Goo Duplicate() + { + if (Value == null) + return new GH_Element(); + else + return new GH_Element(Value.Duplicate()); + } + #endregion + + public static Element ParseGlulam(object obj) + { + if (obj is GH_Element) + return (obj as GH_Element).Value; + else + return obj as Element; + } + public override string ToString() + { + if (Value == null) return "Null glulam element"; + return Value.ToString(); + } + + public override string TypeName => "GlulamElementGoo"; + public override string TypeDescription => "GlulamElementGoo"; public override object ScriptVariable() => Value; - public override bool CastFrom(object source) + public override bool IsValid { - if (source is GlulamWorkpiece) + get { - Value = source as GlulamWorkpiece; + if (Value == null) return false; return true; } - if (source is GH_GlulamWorkpiece) + } + public override string IsValidWhyNot + { + get { - Value = (source as GH_GlulamWorkpiece).Value; - return true; + if (Value == null) return "No data"; + return string.Empty; } - if (source is Glulam) + } + + #region Casting + public override bool CastFrom(object source) + { + if (source == null) return false; + if (source is Element glulam) { - Value = new GlulamWorkpiece(new BasicAssembly(source as Glulam)); + Value = glulam; return true; } - if (source is GH_Glulam) + if (source is GH_Element ghGlulam) { - Value = new GlulamWorkpiece(new BasicAssembly((source as GH_Glulam).Value)); + Value = ghGlulam.Value; return true; } - return false; } public override bool CastTo(ref Q target) { - if (typeof(Q).IsAssignableFrom(typeof(GH_Mesh))) - { - Mesh[] meshes = Value.GetMesh(); - Mesh m = new Mesh(); - for (int i = 0; i < meshes.Length; ++i) - { - m.Append(meshes[i]); - } - object mesh = new GH_Mesh(m); + if (Value == null) return false; - target = (Q)mesh; - return true; - } - if (typeof(Q).IsAssignableFrom(typeof(GlulamWorkpiece))) + return false; + } + + #endregion + + } + + public class GH_Structure : GH_Goo + { + #region Constructors + public GH_Structure() : this(null) { } + public GH_Structure(Structure native) { this.Value = native; } + + public override IGH_Goo Duplicate() + { + if (Value == null) + return new GH_Structure(); + else + return new GH_Structure(Value.Duplicate()); + } + #endregion + + public static Structure ParseStructure(object obj) + { + if (obj is GH_Structure) + return (obj as GH_Structure).Value; + else + return obj as Structure; + } + public override string ToString() + { + if (Value == null) return "Null glulam structure"; + return Value.ToString(); + } + + public override string TypeName => "GlulamStructureGoo"; + public override string TypeDescription => "GlulamStructureGoo"; + public override object ScriptVariable() => Value; + + public override bool IsValid + { + get { - object blank = Value; - target = (Q)blank; + if (Value == null) return false; return true; } - if (typeof(Q).IsAssignableFrom(typeof(GH_Brep))) + } + public override string IsValidWhyNot + { + get { - Brep[] breps = Value.GetBrep(); - Brep b = new Brep(); - for (int i = 0; i < breps.Length; ++i) - { - b.Append(breps[i]); - } - object brep = new GH_Brep(b); - target = (Q)brep; - return true; + if (Value == null) return "No data"; + return string.Empty; } - //if (typeof(Q).IsAssignableFrom(typeof(GH_))) - if (typeof(Q).IsAssignableFrom(typeof(GH_Curve))) + } + + #region Casting + public override bool CastFrom(object source) + { + if (source == null) return false; + if (source is Structure structure) { - Curve[] crvs = Value.Blank.GetAllGlulams().Select(x => x.Centreline).ToArray(); - //target = crvs.Select(x => new GH_Curve(x)).ToList() as Q; - object crv = new GH_Curve(crvs.FirstOrDefault()); - target = (Q)(crv); + Value = structure; return true; } - if (typeof(Q).IsAssignableFrom(typeof(List))) + if (source is GH_Structure ghStructure) { - Curve[] crvs = Value.Blank.GetAllGlulams().Select(x => x.Centreline).ToArray(); - //target = crvs.Select(x => new GH_Curve(x)).ToList() as Q; - object crv = crvs.Select(x => new GH_Curve(x)).ToList(); - target = (Q)(crv); + Value = ghStructure.Value; return true; } - - return base.CastTo(ref target); + return false; } - BoundingBox IGH_PreviewData.ClippingBox + public override bool CastTo(ref Q target) { - get - { - BoundingBox box = BoundingBox.Empty; - - Mesh[] meshes = Value.GetMesh(); + if (Value == null) return false; - for (int i = 0; i < meshes.Length; ++i) - { - box.Union(meshes[i].GetBoundingBox(true)); - } - return box; - } + return false; } - public void DrawViewportMeshes(GH_PreviewMeshArgs args) - { - //args.Pipeline.DrawMeshShaded(Value.GetBoundingMesh(), args.Material); - } + #endregion - public void DrawViewportWires(GH_PreviewWireArgs args) - { - } } - */ } \ No newline at end of file diff --git a/GluLamb/Structure/Element.cs b/GluLamb/Structure/Element.cs index eebf6da..65c6f5c 100644 --- a/GluLamb/Structure/Element.cs +++ b/GluLamb/Structure/Element.cs @@ -29,6 +29,12 @@ public Element(Plane handle, string name = "") m_plane = handle; } + public Element Duplicate() + { + // TODO + return this; + } + public Plane Handle { get diff --git a/GluLamb/Structure/ElementGroup.cs b/GluLamb/Structure/ElementGroup.cs index 420f1c8..5c756fb 100644 --- a/GluLamb/Structure/ElementGroup.cs +++ b/GluLamb/Structure/ElementGroup.cs @@ -16,6 +16,13 @@ public ElementGroup(string name = "ElementGroup") Name = name; //m_elements = new List(); } + + public ElementGroup Duplicate() + { + // TODO + return this; + } + /* public int Count { diff --git a/GluLamb/Structure/Structure.cs b/GluLamb/Structure/Structure.cs index baabe72..a2f88c3 100644 --- a/GluLamb/Structure/Structure.cs +++ b/GluLamb/Structure/Structure.cs @@ -18,7 +18,11 @@ public Structure() Elements = new List(); Groups = new Dictionary>(); } - + public Structure Duplicate() + { + // TODO + return this; + } public static Structure FromBeamElements(List elements, double searchDistance, double overlapDistance) { int counter = 0; From bba59ddafa32e36b6717ed460f993a0b31ff2031 Mon Sep 17 00:00:00 2001 From: Tom Svilans Date: Thu, 19 Nov 2020 17:04:14 +0100 Subject: [PATCH 2/3] feat(element): add Transform() --- GluLamb/Structure/Element.cs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/GluLamb/Structure/Element.cs b/GluLamb/Structure/Element.cs index 65c6f5c..faf0428 100644 --- a/GluLamb/Structure/Element.cs +++ b/GluLamb/Structure/Element.cs @@ -11,12 +11,15 @@ public class Element { public string Name; public List Connections; + public List Handles; + protected Plane m_plane; public GeometryBase Geometry; public Element(string name = "") { Connections = new List(); + Handles = new List(); Name = name; m_plane = Plane.WorldXY; Geometry = null; @@ -75,18 +78,34 @@ public Element GetConnected(int index) return conn.ElementB; return conn.ElementA; } + + public virtual void Transform(Rhino.Geometry.Transform xform) + { + Geometry.Transform(xform); + m_plane.Transform(xform); + + for (int i = 0; i < Handles.Count; ++i) + { + Handles[i].Transform(xform); + } + } } public class BeamElement : Element { public BeamBase Beam; - public BeamElement(BeamBase beam) + public BeamElement(BeamBase beam, string name="BeamElement") : base(name) { Beam = beam; m_plane = Beam.GetPlane(Beam.Centreline.Domain.Mid); } + public BeamElement(BeamBase beam, Plane handle, string name = "BeamElement") : base(handle, name) + { + Beam = beam; + } + public override GeometryBase Discretize(double length) { var t = Beam.Centreline.DivideByLength(length, false).ToList(); @@ -111,6 +130,11 @@ public override Point3d GetConnectionPoint(double t) { return Beam.Centreline.PointAt(t); } + public override void Transform(Rhino.Geometry.Transform xform) + { + base.Transform(xform); + Beam.Transform(xform); + } } } From 17abba41cc08bdb04e1399e54772b1763c11ff79 Mon Sep 17 00:00:00 2001 From: Tom Svilans Date: Fri, 20 Nov 2020 11:26:05 +0100 Subject: [PATCH 3/3] feat(element): add GlulamBeamElement --- .../Create/Cmpt_CreateGlulamBeamElement.cs | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 GluLamb.GH/Create/Cmpt_CreateGlulamBeamElement.cs diff --git a/GluLamb.GH/Create/Cmpt_CreateGlulamBeamElement.cs b/GluLamb.GH/Create/Cmpt_CreateGlulamBeamElement.cs new file mode 100644 index 0000000..d598f58 --- /dev/null +++ b/GluLamb.GH/Create/Cmpt_CreateGlulamBeamElement.cs @@ -0,0 +1,89 @@ +/* + * GluLamb + * A constrained glulam modelling toolkit. + * Copyright 2020 Tom Svilans + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using Grasshopper.Kernel; +using Grasshopper.Kernel.Types; +using Rhino.Geometry; + +namespace GluLamb.GH.Components +{ + public class Cmpt_CreateGlulamBeamElement : GH_Component + { + public Cmpt_CreateGlulamBeamElement() + : base("Glulam Beam", "GBeam", + "Create glulam beam element.", + "GluLamb", "Create") + { + } + + + protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager) + { + pManager.AddTextParameter("Name", "N", "Name of glulam beam.", GH_ParamAccess.item, "GlulamBeamElement"); + pManager.AddGenericParameter("Glulam", "G", "Glulam blank.", GH_ParamAccess.item); + pManager.AddPlaneParameter("Plane", "P", "Handle for element (baseplane).", GH_ParamAccess.item); + + pManager[0].Optional = true; + pManager[2].Optional = true; + } + + protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager) + { + pManager.AddGenericParameter("Element", "E", "Glulam beam element.", GH_ParamAccess.item); + } + + protected override void SolveInstance(IGH_DataAccess DA) + { + Glulam glulam = null; + + if (!DA.GetData("Glulam", ref glulam)) + return; + + string name = "GlulamBeamElement"; + DA.GetData("Name", ref name); + + Plane handle = Plane.Unset; + DA.GetData("Plane", ref handle); + + BeamElement beam_element; + if (handle == Plane.Unset) + beam_element = new BeamElement(glulam, name); + else + beam_element = new BeamElement(glulam, handle, name); + + DA.SetData("Element", new GH_Element(beam_element)); + } + + protected override System.Drawing.Bitmap Icon + { + get + { + return Properties.Resources.glulamb_FreeformGlulam_24x24; + } + } + + public override Guid ComponentGuid + { + get { return new Guid("950B8230-422B-45D0-A5EA-01FE2E446E8A"); } + } + } +} \ No newline at end of file