internal Dyn.Geometry EnsureUnit(Dyn.Geometry geo, bool disposeInput = false) { Dyn.Geometry scgeo = null; Dyn.Plane xy = Dyn.Plane.XY(); if (targetUnit == baseUnit) { scgeo = geo.Scale(xy, 1, 1, 1); } else { if (baseUnit == "Meters" && targetUnit == "Feet") { double factor = 3.280841666667; scgeo = geo.Scale(xy, factor, factor, factor); } else if (baseUnit == "Feet" && targetUnit == "Meters") { double factor = 0.304800164592; scgeo = geo.Scale(xy, factor, factor, factor); } else { } } if (disposeInput) { geo.Dispose(); } xy.Dispose(); return(scgeo); }
public static Dictionary <string, object> LocationData(global::Revit.Elements.Element viewport) { //obtain the element id from the sheet Autodesk.Revit.DB.Viewport internalViewport = (Autodesk.Revit.DB.Viewport)viewport.InternalElement; //obtain the box center of the viewport var boxCenterInternal = internalViewport.GetBoxCenter().ToPoint(); //Construct new point at sheet elevation of 0 Autodesk.DesignScript.Geometry.Point boxCenter = Autodesk.DesignScript.Geometry.Point.ByCoordinates(boxCenterInternal.X, boxCenterInternal.Y, 0); //this obtains the box outline var boxOutline = internalViewport.GetBoxOutline(); //temporary geometry var bBox = BoundingBox.ByCorners(boxOutline.MaximumPoint.ToPoint(), boxOutline.MinimumPoint.ToPoint()); var boxCuboid = Cuboid.ByCorners(boxOutline.MaximumPoint.ToPoint(), boxOutline.MinimumPoint.ToPoint()); //create plane that corresponds to sheet plane Plane boxPlane = Plane.ByOriginNormal(boxOutline.MaximumPoint.ToPoint(), Vector.ZAxis()); var boxSurface = boxCuboid.Intersect(boxPlane); List <Autodesk.DesignScript.Geometry.Curve[]> boxCurves = new List <Autodesk.DesignScript.Geometry.Curve[]>(); foreach (var geometry in boxSurface) { var surf = (Surface)geometry; boxCurves.Add(surf.PerimeterCurves()); surf.Dispose(); } List <Autodesk.DesignScript.Geometry.Curve> boxSheetCurves = new List <Autodesk.DesignScript.Geometry.Curve>(); //pull the curves onto a plane at 0,0,0 foreach (Autodesk.DesignScript.Geometry.Curve[] curve in boxCurves) { foreach (Autodesk.DesignScript.Geometry.Curve c in curve) { boxSheetCurves.Add(c.PullOntoPlane(Plane.XY())); c.Dispose(); } } //dispose of temporary geometries boxCuboid.Dispose(); boxPlane.Dispose(); //returns the outputs var outInfo = new Dictionary <string, object> { { "bBox", bBox }, { "boxCenter", boxCenter }, { "boxOutline", boxSheetCurves } }; return(outInfo); }
/// <summary> /// Gets the rectangular openings. /// </summary> /// <returns></returns> /// <remarks>This method uses walls, doors, windows and generic models bounding boxes to determine the rectangles. /// These objects can be in the host file or in linked Revit files.</remarks> public static IList <Autodesk.DesignScript.Geometry.Rectangle> GetRectangularOpenings() { Utils.Log(string.Format("OpeningUtils.GetRectangularOpenings started...", "")); Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument; //look for Walls, Doors, Windows, Generic Models in the current document and in the linked documents ElementCategoryFilter wallFilter = new ElementCategoryFilter(BuiltInCategory.OST_Walls); ElementCategoryFilter doorFilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors); ElementCategoryFilter windowFilter = new ElementCategoryFilter(BuiltInCategory.OST_Windows); ElementCategoryFilter genericFilter = new ElementCategoryFilter(BuiltInCategory.OST_GenericModel); IList <ElementFilter> filterList = new List <ElementFilter>() { wallFilter, doorFilter, windowFilter, genericFilter }; LogicalOrFilter orFilter = new LogicalOrFilter(filterList); IList <Autodesk.DesignScript.Geometry.Solid> solids = new List <Autodesk.DesignScript.Geometry.Solid>(); IList <Autodesk.DesignScript.Geometry.Rectangle> output = new List <Autodesk.DesignScript.Geometry.Rectangle>(); foreach (Autodesk.Revit.DB.Element e in new FilteredElementCollector(doc) .WherePasses(orFilter) .WhereElementIsNotElementType() .Where(x => x.Parameters.Cast <Autodesk.Revit.DB.Parameter>() .First(p => p.Id.IntegerValue == (int)BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).HasValue)) { string comments = e.Parameters.Cast <Autodesk.Revit.DB.Parameter>().First(p => p.Id.IntegerValue == (int)BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).AsString(); if (comments.ToLower() != "opening") { continue; } Transform tr = Transform.Identity; if (e is Instance) { Instance instance = e as Instance; tr = instance.GetTotalTransform(); } IList <Autodesk.Revit.DB.Solid> temp = new List <Autodesk.Revit.DB.Solid>(); foreach (GeometryObject go in e.get_Geometry(new Options())) { if (go is GeometryInstance) { GeometryInstance geoInstance = go as GeometryInstance; foreach (var gi in geoInstance.SymbolGeometry) { if (gi is Autodesk.Revit.DB.Solid) { Autodesk.Revit.DB.Solid s = gi as Autodesk.Revit.DB.Solid; s = SolidUtils.CreateTransformed(s, tr); temp.Add(s); } } } else { if (go is Autodesk.Revit.DB.Solid) { Autodesk.Revit.DB.Solid s = go as Autodesk.Revit.DB.Solid; s = SolidUtils.CreateTransformed(s, tr); temp.Add(s); } } } if (temp.Count > 0) { Autodesk.Revit.DB.Solid s0 = temp[0]; for (int i = 1; i < temp.Count; ++i) { s0 = BooleanOperationsUtils.ExecuteBooleanOperation(s0, temp[i], BooleanOperationsType.Union); } solids.Add(s0.ToProtoType()); } } foreach (RevitLinkInstance rli in new FilteredElementCollector(doc).OfClass(typeof(RevitLinkInstance)).WhereElementIsNotElementType()) { Autodesk.Revit.DB.Document link = rli.GetLinkDocument(); foreach (Autodesk.Revit.DB.Element e in new FilteredElementCollector(link) .WherePasses(orFilter) .WhereElementIsNotElementType() .Where(x => x.Parameters.Cast <Autodesk.Revit.DB.Parameter>() .First(p => p.Id.IntegerValue == (int)BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).HasValue)) { Transform tr = rli.GetTotalTransform(); string comments = e.Parameters.Cast <Autodesk.Revit.DB.Parameter>().First(p => p.Id.IntegerValue == (int)BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).AsString(); if (comments.ToLower() != "opening") { continue; } if (e is Instance) { Instance instance = e as Instance; tr = tr.Multiply(instance.GetTotalTransform()); } IList <Autodesk.Revit.DB.Solid> temp = new List <Autodesk.Revit.DB.Solid>(); foreach (var go in e.get_Geometry(new Options())) { if (go is GeometryInstance) { GeometryInstance geoInstance = go as GeometryInstance; foreach (var gi in geoInstance.SymbolGeometry) { if (gi is Autodesk.Revit.DB.Solid) { Autodesk.Revit.DB.Solid s = gi as Autodesk.Revit.DB.Solid; s = SolidUtils.CreateTransformed(s, tr); temp.Add(s); } } } else { if (go is Autodesk.Revit.DB.Solid) { Autodesk.Revit.DB.Solid s = go as Autodesk.Revit.DB.Solid; s = SolidUtils.CreateTransformed(s, tr); temp.Add(s); } } } if (temp.Count > 0) { Autodesk.Revit.DB.Solid s0 = temp[0]; for (int i = 1; i < temp.Count; ++i) { s0 = BooleanOperationsUtils.ExecuteBooleanOperation(s0, temp[i], BooleanOperationsType.Union); } solids.Add(s0.ToProtoType()); } } } foreach (var s in solids) { IList <Autodesk.DesignScript.Geometry.Point> points = new List <Autodesk.DesignScript.Geometry.Point>(); foreach (var v in s.Vertices) { points.Add(v.PointGeometry); } points = Autodesk.DesignScript.Geometry.Point.PruneDuplicates(points); Autodesk.DesignScript.Geometry.Plane plane = Autodesk.DesignScript.Geometry.Plane.ByBestFitThroughPoints(points); plane = Autodesk.DesignScript.Geometry.Plane.ByOriginNormal(points.Last(), plane.Normal); IList <Autodesk.DesignScript.Geometry.Point> temp = new List <Autodesk.DesignScript.Geometry.Point>(); foreach (var p in points) { foreach (var q in p.Project(plane, plane.Normal)) { temp.Add(q as Autodesk.DesignScript.Geometry.Point); } foreach (var q in p.Project(plane, plane.Normal.Reverse())) { temp.Add(q as Autodesk.DesignScript.Geometry.Point); } } temp = Autodesk.DesignScript.Geometry.Point.PruneDuplicates(temp); CoordinateSystem cs = CoordinateSystem.ByPlane(plane); IList <Autodesk.DesignScript.Geometry.Point> relative = new List <Autodesk.DesignScript.Geometry.Point>(); foreach (var p in temp) { relative.Add(p.Transform(cs.Inverse()) as Autodesk.DesignScript.Geometry.Point); } var min = Autodesk.DesignScript.Geometry.Point.ByCoordinates(relative.Min(p => p.X), relative.Min(p => p.Y), relative.Min(p => p.Z)); var max = Autodesk.DesignScript.Geometry.Point.ByCoordinates(relative.Max(p => p.X), relative.Max(p => p.Y), relative.Max(p => p.Z)); double width = max.X - min.X; double height = max.Y - min.Y; min = min.Transform(cs) as Autodesk.DesignScript.Geometry.Point; max = max.Transform(cs) as Autodesk.DesignScript.Geometry.Point; plane = Autodesk.DesignScript.Geometry.Plane.ByOriginNormal(Autodesk.DesignScript.Geometry.Line.ByStartPointEndPoint(min, max).PointAtParameter(0.5), plane.Normal); Autodesk.DesignScript.Geometry.Rectangle rectangle = Autodesk.DesignScript.Geometry.Rectangle.ByWidthLength(plane, width, height); output.Add(rectangle); plane.Dispose(); cs.Dispose(); min.Dispose(); max.Dispose(); } Utils.Log(string.Format("OpeningUtils.GetRectangularOpenings completed.", "")); return(output); }
/// <summary> /// Create a Revit Floor given it's curve outline and Level /// </summary> /// <param name="outline">The outline.</param> /// <param name="floorType">Type of the floor.</param> /// <param name="level">The level.</param> /// <param name="structural">if set to <c>true</c> [structural].</param> /// <returns> /// The floor /// </returns> public static SlopedFloor ByOutlineTypeAndLevel(Autodesk.DesignScript.Geometry.PolyCurve outline, Revit.Elements.FloorType floorType, Revit.Elements.Level level, bool structural) { Utils.Log(string.Format("SlopedFloor.ByOutlineTypeAndLevel started...", "")); try { var profile = new CurveArray(); Autodesk.DesignScript.Geometry.Plane plane = Autodesk.DesignScript.Geometry.Plane.ByBestFitThroughPoints( outline.Curves().Cast <Autodesk.DesignScript.Geometry.Curve>().Select(x => x.StartPoint)); Vector normal = plane.Normal; if (normal.Dot(Vector.ZAxis()) <= 0) { normal = normal.Reverse(); } Autodesk.DesignScript.Geometry.Point origin = plane.Origin; Autodesk.DesignScript.Geometry.Point end = origin.Add(normal); Autodesk.DesignScript.Geometry.Point projection = Autodesk.DesignScript.Geometry.Point.ByCoordinates(end.X, end.Y, -1000); end = Autodesk.DesignScript.Geometry.Point.ByCoordinates(end.X, end.Y, end.Z + 1000); Autodesk.DesignScript.Geometry.Point intersection = null; var result = plane.Intersect(Autodesk.DesignScript.Geometry .Line.ByStartPointEndPoint(end, projection)); if (result.Length > 0) { intersection = result[0] as Autodesk.DesignScript.Geometry.Point; } else { var message = "Couldn't find intersection"; Utils.Log(string.Format("ERROR: SlopedFloor.ByOutlineTypeAndLevel {0}", message)); throw new Exception(message); } Autodesk.DesignScript.Geometry.Curve temp = Autodesk.DesignScript.Geometry.Line.ByBestFitThroughPoints(new Autodesk.DesignScript.Geometry.Point[] { origin, intersection }); PolyCurve flat = PolyCurve.ByJoinedCurves(outline.PullOntoPlane(Autodesk.DesignScript.Geometry.Plane.XY() .Offset(temp.StartPoint.Z)).Explode().Cast <Autodesk.DesignScript.Geometry.Curve>().ToList()); Autodesk.DesignScript.Geometry.Curve flatLine = temp.PullOntoPlane(Autodesk.DesignScript.Geometry.Plane.XY().Offset(temp.StartPoint.Z)); if (Math.Abs(Math.Abs(plane.Normal.Dot(Vector.ZAxis())) - 1) < 0.00001) { var f = Revit.Elements.Floor.ByOutlineTypeAndLevel(flat, floorType, level); f.InternalElement.Parameters.Cast <Autodesk.Revit.DB.Parameter>() .First(x => x.Id.IntegerValue.Equals(Autodesk.Revit.DB.BuiltInParameter.FLOOR_PARAM_IS_STRUCTURAL)) .Set(structural ? 1 : 0); plane.Dispose(); flatLine.Dispose(); flat.Dispose(); origin.Dispose(); end.Dispose(); projection.Dispose(); intersection.Dispose(); temp.Dispose(); return(new SlopedFloor(f.InternalElement as Autodesk.Revit.DB.Floor)); } double slope = (temp.EndPoint.Z - temp.StartPoint.Z) / flatLine.Length; foreach (Autodesk.DesignScript.Geometry.Curve c in flat.Curves()) { profile.Append(c.ToRevitType()); } Autodesk.Revit.DB.Line slopeArrow = flatLine.ToRevitType() as Autodesk.Revit.DB.Line; var ft = floorType.InternalElement as Autodesk.Revit.DB.FloorType; var lvl = level.InternalElement as Autodesk.Revit.DB.Level; var floor = new SlopedFloor(profile, slopeArrow, slope, ft, lvl, structural); floor.Level = level; floor.Floortype = floorType; floor.Structural = structural; //DocumentManager.Regenerate(); plane.Dispose(); flatLine.Dispose(); flat.Dispose(); origin.Dispose(); end.Dispose(); projection.Dispose(); intersection.Dispose(); temp.Dispose(); Utils.Log(string.Format("SlopedFloor.ByOutlineTypeAndLevel completed.", "")); return(floor); } catch (Exception ex) { Utils.Log(string.Format("ERROR: SlopedFloor.ByOutlineTypeAndLevel {0}", ex.Message)); throw ex; } }