/***************************************************/ public static IBHoMObject FromRevit(this SpatialElement spatialElement, Discipline discipline, Transform transform = null, RevitSettings settings = null, Dictionary <string, List <IBHoMObject> > refObjects = null) { IElement2D result = null; switch (discipline) { case Discipline.Environmental: result = spatialElement.SpaceFromRevit(settings, refObjects); break; case Discipline.Facade: case Discipline.Architecture: case Discipline.Physical: result = spatialElement.RoomFromRevit(settings, refObjects); break; } if (result != null && transform?.IsIdentity == false) { TransformMatrix bHoMTransform = transform.FromRevit(); result = result.ITransform(bHoMTransform); } return(result as IBHoMObject); }
public static Point Centroid(this IElement2D element2D) { Point tmp = Geometry.Query.Centroid(element2D.OutlineCurve()); double area = Geometry.Query.Area(element2D.OutlineCurve()); double x = tmp.X * area; double y = tmp.Y * area; double z = tmp.Z * area; List <PolyCurve> openings = Geometry.Compute.BooleanUnion(element2D.InternalOutlineCurves()); foreach (ICurve o in openings) { Point oTmp = Geometry.Query.ICentroid(o); double oArea = o.IArea(); x -= oTmp.X * oArea; y -= oTmp.Y * oArea; z -= oTmp.Z * oArea; area -= oArea; } return(new Point { X = x / area, Y = y / area, Z = z / area }); }
/******************************************/ /**** IElement2D ****/ /******************************************/ public static double Area(this IElement2D element2D) { //TODO: make this work for PolyCurves (Booleans needed) double result = element2D.IOutlineCurve().Area(); List <Polyline> openings = new List <Polyline>(); foreach (PolyCurve o in element2D.IInternalOutlineCurves()) { Polyline p = o.ToPolyline(); if (p == null) { throw new NotImplementedException(); } openings.Add(p); } foreach (Polyline p in openings.BooleanUnion()) { result -= p.Area(); } return(result); }
public static List <ICurve> ElementCurves(this IElement2D element2D, bool recursive = true) { List <ICurve> result = new List <ICurve>(); PolyCurve outline = element2D.OutlineCurve(); foreach (ICurve curve in outline.Curves) { if (recursive) { result.AddRange(curve.ISubParts()); } else { result.Add(curve); } } foreach (IElement2D e in element2D.IInternalElements2D()) { result.AddRange(e.ElementCurves(recursive)); } return(result); }
public static IElement2D ITransform(this IElement2D element2D, TransformMatrix transform, double tolerance = Tolerance.Distance) { object result; if (!Reflection.Compute.TryRunExtensionMethod(element2D, "Transform", new object[] { transform, tolerance }, out result)) result = element2D.Transform(transform, tolerance); return result as IElement2D; }
public static string IPrimaryPropertyName(this IElement2D elem) { if (elem == null) { return(null); } return(PrimaryPropertyName(elem as dynamic)); }
public static IElement2D ISetInternalElements2D(this IElement2D element2D, List <IElement2D> newElements2D) { IElement2D result = Reflection.Compute.RunExtensionMethod(element2D, "SetInternalElements2D", new object[] { newElements2D }) as IElement2D; if (result == null && newElements2D.Count != 0) { Engine.Reflection.Compute.RecordError("Cannot set internal 2D elements to an " + element2D.GetType() + "."); } return(result ?? element2D.ShallowClone()); }
/******************************************/ /**** IElement2D ****/ /******************************************/ public static List <Point> ElementVertices(this IElement2D element2D) { List <Point> result = new List <Point>(); result.AddRange(element2D.IOutlineCurve().ElementVertices()); foreach (IElement2D e in element2D.IInternalElements2D()) { result.AddRange(e.ElementVertices()); } return(result); }
public static BH.oM.Geometry.Vector DominantVector(this IElement2D element2D, bool orthogonalPriority = true, double orthogonalLengthFactor = 0.5, double angleTolerance = BH.oM.Geometry.Tolerance.Angle) { if (element2D == null || orthogonalPriority == null || orthogonalLengthFactor == null || angleTolerance == null) { BH.Engine.Reflection.Compute.RecordError("One or more of the inputs is empty or null."); return(null); } IElement1D outline = BH.Engine.Geometry.Create.PolyCurve(element2D.IOutlineElements1D().Select(x => x.IGeometry())); return(DominantVector(outline, orthogonalPriority, orthogonalLengthFactor, angleTolerance)); }
public static double Area(this IElement2D element2D) { double result = element2D.OutlineCurve().IArea(); List <PolyCurve> openings = element2D.InternalOutlineCurves().BooleanUnion(); foreach (PolyCurve o in openings) { result -= o.Area(); } return(result); }
public static bool IsNearGrid(this IElement2D element2D, Grid grid, double maxDistance) { List <IElement1D> elements1D = element2D.IOutlineElements1D(); foreach (IElement1D e1D in elements1D) { if (IsNearGrid(e1D, grid, maxDistance)) { return(true); } } return(false); }
public static List <Point> ControlPoints(this IElement2D element2D, bool externalOnly = false) { List <Point> pts = Geometry.Query.ControlPoints(element2D.OutlineCurve()); if (!externalOnly) { foreach (IElement2D e in element2D.IInternalElements2D()) { pts.AddRange(e.ControlPoints()); } } return(pts); }
public static double Area(this IElement2D element2D) { double result = BH.Engine.Geometry.Query.Area(element2D.OutlineCurve()); List <PolyCurve> openings = element2D.InternalOutlineCurves().BooleanUnion(); foreach (PolyCurve o in openings) { result -= BH.Engine.Geometry.Query.Area(o); } return(result); }
public static Plane FitPlane(this IElement2D element2D, bool externalOnly = false, double tolerance = Tolerance.Distance) { List <Point> controlPoints = element2D.OutlineCurve().ControlPoints(); if (!externalOnly) { foreach (PolyCurve internalOutline in element2D.InternalOutlineCurves()) { controlPoints.AddRange(internalOutline.ControlPoints()); } } return(controlPoints.FitPlane(tolerance)); }
public static bool IsNearLevel(this IElement2D element2D, Level level, double maxDistance) { List <IElement1D> elements1D = element2D.IOutlineElements1D(); foreach (IElement1D e1D in elements1D) { if (IsNearLevel(e1D, level, maxDistance)) { return(true); } } return(false); }
public static bool IsSelfIntersecting(this IElement2D element2D, double tolerance = Tolerance.Distance) { if (Geometry.Query.IIsSelfIntersecting(element2D.OutlineCurve(), tolerance)) { return(true); } foreach (PolyCurve internalOutline in element2D.InternalOutlineCurves()) { if (Geometry.Query.IIsSelfIntersecting(internalOutline, tolerance)) { return(true); } } return(false); }
public static BoundingBox Bounds(this IElement2D element2D) { List <ICurve> elementCurves = element2D.ElementCurves(true); if (elementCurves.Count == 0) { return(null); } BoundingBox box = Geometry.Query.IBounds(elementCurves[0]); for (int i = 1; i < elementCurves.Count; i++) { box += Geometry.Query.IBounds(elementCurves[i]); } return(box); }
/***************************************************/ /**** Public Methods - IElements ****/ /***************************************************/ public static IElement2D Translate(this IElement2D element2D, Vector transform) //todo: move this to analytical along with other IElement methods { List <IElement1D> newOutline = new List <IElement1D>(); foreach (IElement1D element1D in element2D.IOutlineElements1D()) { newOutline.Add(element1D.Translate(transform)); } IElement2D result = element2D.ISetOutlineElements1D(newOutline); List <IElement2D> newInternalOutlines = new List <IElement2D>(); foreach (IElement2D internalElement2D in result.IInternalElements2D()) { newInternalOutlines.Add(internalElement2D.Translate(transform)); } result = result.ISetInternalElements2D(newInternalOutlines); return(result); }
public static IElement2D RoundCoordinates(this IElement2D element2d, int decimalPlaces = 6) { Vector normal = element2d.Normal().Normalise(); if (Math.Abs(Math.Abs(normal.X) - 1) < Tolerance.Angle || Math.Abs(Math.Abs(normal.Y) - 1) < Tolerance.Angle || Math.Abs(Math.Abs(normal.Z) - 1) < Tolerance.Angle) { Plane plane = new Plane() { Origin = Geometry.Modify.RoundCoordinates(element2d.OutlineCurve().StartPoint(), decimalPlaces), Normal = normal.RoundCoordinates(0) }; element2d = element2d.ISetOutlineElements1D(element2d.IOutlineElements1D().Select(x => x.ISetGeometry(Geometry.Modify.IRoundCoordinates(x.IGeometry().IProject(plane), decimalPlaces))).ToList()); return(element2d.ISetInternalElements2D(element2d.IInternalElements2D().Select(y => y.ISetOutlineElements1D(y.IOutlineElements1D().Select(x => x.ISetGeometry(Geometry.Modify.IRoundCoordinates(x.IGeometry().IProject(plane), decimalPlaces))).ToList())).ToList())); } Reflection.Compute.RecordWarning("Rounding the coordinates of a planar surface that is not aligned with the global coordinate system cannot be achieved without risk of losing planarity. No action has been taken."); return(element2d); }
public static IElement2D RoundCoordinates(this IElement2D element2d, int decimalPlaces = 6) { bool planar = element2d.IIsPlanar(); if (planar) { Vector normal = element2d.FitPlane().Normal.Normalise(); //If the element is planar AND aligned with one of the main coordinate system's planes then rounded element will get projected on this plane to keep it's planarity. if (Math.Abs(Math.Abs(normal.X) - 1) < Tolerance.Angle || Math.Abs(Math.Abs(normal.Y) - 1) < Tolerance.Angle || Math.Abs(Math.Abs(normal.Z) - 1) < Tolerance.Angle) { Plane plane = new Plane() { Origin = Geometry.Modify.RoundCoordinates(element2d.OutlineCurve().StartPoint(), decimalPlaces), Normal = normal.RoundCoordinates(0) }; element2d = element2d.ISetOutlineElements1D(element2d.IOutlineElements1D().Select(x => x.ISetGeometry(Geometry.Modify.IRoundCoordinates(x.IGeometry().IProject(plane), decimalPlaces))).ToList()); return(element2d.ISetInternalElements2D(element2d.IInternalElements2D().Select(y => y.ISetOutlineElements1D(y.IOutlineElements1D().Select(x => x.ISetGeometry(Geometry.Modify.IRoundCoordinates(x.IGeometry().IProject(plane), decimalPlaces))).ToList())).ToList())); } } //Here is the part with the default way of rounding element's coordinates: IElement2D newElement2d = element2d.ISetOutlineElements1D(element2d.IOutlineElements1D().Select(x => x.ISetGeometry(Geometry.Modify.IRoundCoordinates(x.IGeometry(), decimalPlaces))).ToList()); newElement2d = newElement2d.ISetInternalElements2D(newElement2d.IInternalElements2D().Select(y => y.ISetOutlineElements1D(y.IOutlineElements1D().Select(x => x.ISetGeometry(Geometry.Modify.IRoundCoordinates(x.IGeometry(), decimalPlaces))).ToList())).ToList()); if (planar && !newElement2d.IsPlanar()) //If the original element was planar we need to ensure that result is planar as well. { Reflection.Compute.RecordWarning("Rounding the coordinates of an IElement2D couldn't be achieved without losing planarity. No action has been taken."); return(element2d); } return(newElement2d); }
private static string PrimaryPropertyName(this IElement2D elem) { string nameConst = Reflection.Query.PropertyValue(elem, "Construction.Name").ToString(); string openNameConst = Reflection.Query.PropertyValue(elem, "OpeningConstruction.Name").ToString(); string nameProp = Reflection.Query.PropertyValue(elem, "Property.Name").ToString(); if (nameConst != null) { return(nameConst); } else if (openNameConst != null) { return(openNameConst); } else if (nameProp != null) { return(nameProp); } else { return(""); } }
/***************************************************/ /**** Private Methods - IElements ****/ /***************************************************/ private static IElement2D Transform(this IElement2D element2D, TransformMatrix transform, double tolerance) { if (!transform.IsRigidTransformation(tolerance)) { BH.Engine.Reflection.Compute.RecordError("Transformation failed: only rigid body transformations are currently supported."); return null; } List<IElement1D> newOutline = new List<IElement1D>(); foreach (IElement1D element1D in element2D.IOutlineElements1D()) { newOutline.Add(element1D.Transform(transform, tolerance)); } IElement2D result = element2D.ISetOutlineElements1D(newOutline); List<IElement2D> newInternalOutlines = new List<IElement2D>(); foreach (IElement2D internalElement2D in result.IInternalElements2D()) { newInternalOutlines.Add(internalElement2D.Transform(transform, tolerance)); } result = result.ISetInternalElements2D(newInternalOutlines); return result; }
public static List <IElement2D> AdjacentElements(this IElement2D element, IEnumerable <IElement2D> referenceElements) { List <IElement2D> adjacentElements = new List <IElement2D>(); if (element == null || referenceElements == null) { Reflection.Compute.RecordWarning("Can not get adjacencies of a null element."); return(null); } PolyCurve outline = element.OutlineCurve(); foreach (IElement2D refElem in referenceElements) { PolyCurve refOutline = refElem.OutlineCurve(); if (refOutline.IIsAdjacent(outline)) { adjacentElements.Add(refElem); } } return(adjacentElements); }
public static string AdjacencyID(this List <IElement1D> edges, List <IElement2D> elems) { string separator = "_"; List <string> adjIDs = new List <string>(); if (edges.Count != elems.Count) { Reflection.Compute.RecordWarning("Edge and element list lengths do not match. Each edge should have a corresponding element, please check your inputs."); return(null); } else { for (int i = 0; i < edges.Count; i++) { IElement1D edge = edges[i]; IElement2D elem = elems[i]; string adjID = "Elem:" + elem.IPrimaryPropertyName() + " " + "Edge:" + edge.IPrimaryPropertyName(); adjIDs.Add(adjID); } } adjIDs.Sort(); return(string.Join(separator, adjIDs)); }
public static IElement1D INewElement1D(this IElement2D element2D, ICurve curve) { return curve; }
/******************************************/ /**** IElement2D ****/ /******************************************/ public static IElement1D INewElement1D(this IElement2D element2D, ICurve curve) { return(Reflection.Compute.RunExtentionMethod(element2D, "NewElement1D", new object[] { curve }) as IElement1D); }
public static List <ICurve> InternalElementCurves(this IElement2D element2D, bool recursive = true) { return(element2D.IInternalElements2D().Where(x => x != null).SelectMany(x => x.ElementCurves(recursive)).ToList()); }
public static Point Centroid(this IElement2D element2D, double tolerance = Tolerance.Distance) { return(Geometry.Query.Centroid(new List <ICurve> { element2D.OutlineCurve() }, element2D.InternalOutlineCurves(), tolerance)); }
public static IElement2D INewInternalElement2D(this IElement2D element2D) { return(Reflection.Compute.RunExtensionMethod(element2D, "NewInternalElement2D") as IElement2D); }
public static List <PolyCurve> InternalOutlineCurves(this IElement2D element2D) { return(element2D.IInternalElements2D().Select(x => x.OutlineCurve()).ToList()); }