public static List <Aperture> ToSAM_Apertures(this FamilyInstance familyInstance, ConvertSettings convertSettings) { if (familyInstance == null) { return(null); } List <Aperture> result = convertSettings?.GetObjects <Aperture>(familyInstance.Id); if (result != null) { return(result); } if (Core.Revit.Query.Simplified(familyInstance)) { result = Core.Revit.Query.IJSAMObjects <Aperture>(familyInstance); if (result != null) { convertSettings?.Add(familyInstance.Id, result); return(result); } } Point3D point3D_Location = Geometry.Revit.Query.LocationPoint3D(familyInstance); if (point3D_Location == null) { List <Solid> solids = Core.Revit.Query.Solids(familyInstance, new Options()); solids?.RemoveAll(x => x.Volume == 0); if (solids == null || solids.Count == 0) { return(null); } if (solids.Count > 1) { solids.Sort((x, y) => y.Volume.CompareTo(x.Volume)); } point3D_Location = solids[0].ComputeCentroid()?.ToSAM(); } if (point3D_Location == null) { return(null); } List <Face3D> face3Ds = null; PanelType panelType_Host = PanelType.Undefined; BuiltInCategory builtInCategory_Host = BuiltInCategory.INVALID; HostObject hostObject = null; if (familyInstance.Host != null) { hostObject = familyInstance.Host as HostObject; if (hostObject != null) { builtInCategory_Host = (BuiltInCategory)hostObject.Category.Id.IntegerValue; } } //Method 1 of extracting Geometry if (face3Ds == null || face3Ds.Count == 0) { Vector3D axisX = null; Vector3D normal = null; Vector3D axisY = null; if (builtInCategory_Host == BuiltInCategory.OST_Roofs) { axisX = familyInstance.HandOrientation.ToSAM_Vector3D(false); axisY = familyInstance.FacingOrientation.ToSAM_Vector3D(false); normal = Geometry.Spatial.Query.AxisY(axisY, axisX); } else { axisX = familyInstance.HandOrientation.ToSAM_Vector3D(false); normal = familyInstance.FacingOrientation.ToSAM_Vector3D(false); axisY = Geometry.Spatial.Query.AxisY(normal, axisX); } Geometry.Spatial.Plane plane = Geometry.Spatial.Create.Plane(point3D_Location, axisX, axisY); if (!plane.Normal.SameHalf(normal)) { plane.FlipZ(false); } List <Shell> shells = Geometry.Revit.Convert.ToSAM_Geometries <Shell>(familyInstance); if (shells == null || shells.Count == 0) { return(null); } List <Point2D> point2Ds = new List <Point2D>(); foreach (Shell shell in shells) { List <Face3D> face3Ds_Shell = shell?.Face3Ds; if (face3Ds_Shell == null || face3Ds_Shell.Count == 0) { continue; } foreach (Face3D face3D_Temp in face3Ds_Shell) { ISegmentable3D segmentable3D = face3D_Temp.GetExternalEdge3D() as ISegmentable3D; if (segmentable3D == null) { continue; } segmentable3D?.GetPoints()?.ForEach(x => point2Ds.Add(plane.Convert(x))); } } Face3D face3D = new Face3D(plane, Geometry.Planar.Create.Rectangle2D(point2Ds)); if (face3D != null && face3D.IsValid() && face3D.GetArea() > Core.Tolerance.MacroDistance) { face3Ds = new List <Face3D>() { face3D }; } } //Method 2 of extracting Geometry if (hostObject != null) { builtInCategory_Host = (BuiltInCategory)hostObject.Category.Id.IntegerValue; Geometry.Spatial.Plane plane_Host = null; if (hostObject is CurtainSystem && familyInstance is Autodesk.Revit.DB.Panel) { Autodesk.Revit.DB.Panel panel = (Autodesk.Revit.DB.Panel)familyInstance; ElementId uGridLineElementId = null; ElementId vGridLineElementId = null; panel.GetRefGridLines(ref uGridLineElementId, ref vGridLineElementId); CurtainSystem curtainSystem = (CurtainSystem)hostObject; List <Polygon3D> polygon3Ds = curtainSystem.CurtainCell(uGridLineElementId, vGridLineElementId)?.Polygon3Ds(); if (polygon3Ds != null && polygon3Ds.Count != 0) { polygon3Ds.Sort((x, y) => y.GetArea().CompareTo(x.GetArea())); plane_Host = polygon3Ds[0].GetPlane(); } } else { List <Face3D> face3Ds_Temp = hostObject.Profiles(); if (face3Ds_Temp != null && face3Ds_Temp.Count != 0) { plane_Host = face3Ds_Temp.Closest(point3D_Location)?.GetPlane(); } } if (plane_Host != null) { face3Ds = face3Ds?.ConvertAll(x => plane_Host.Project(x)); point3D_Location = plane_Host.Project(point3D_Location); } HostObjAttributes hostObjAttributes = familyInstance.Document.GetElement(hostObject.GetTypeId()) as HostObjAttributes; if (hostObjAttributes != null) { panelType_Host = hostObjAttributes.PanelType(); } if (panelType_Host == PanelType.Undefined) { panelType_Host = hostObject.PanelType(); } List <Face3D> face3Ds_Profiles = hostObject.Profiles(familyInstance.Id); face3Ds_Profiles?.RemoveAll(x => x == null || !x.IsValid()); if (face3Ds_Profiles != null && face3Ds_Profiles.Count > 0) { if (face3Ds == null || (face3Ds != null && face3Ds_Profiles.ConvertAll(x => x.GetArea()).Sum() <= face3Ds.ConvertAll(x => x.GetArea()).Sum())) { face3Ds = face3Ds_Profiles; } } } ApertureConstruction apertureConstruction = ToSAM_ApertureConstruction(familyInstance, convertSettings); if (apertureConstruction == null && panelType_Host != PanelType.Undefined) { apertureConstruction = Analytical.Query.DefaultApertureConstruction(panelType_Host, familyInstance.ApertureType()); //Default Aperture Construction } if (face3Ds == null || face3Ds.Count == 0) { return(result); } //TODO: Working on SAM Families (requested by Michal) string parameterName_Height = Core.Revit.Query.Name(ActiveSetting.Setting, typeof(Aperture), typeof(FamilyInstance), "GetHeight"); string parameterName_Width = Core.Revit.Query.Name(ActiveSetting.Setting, typeof(Aperture), typeof(FamilyInstance), "GetWidth"); if (!string.IsNullOrWhiteSpace(parameterName_Height) && !string.IsNullOrWhiteSpace(parameterName_Width)) { Parameter parameter_Height = familyInstance.LookupParameter(parameterName_Height); Parameter parameter_Width = familyInstance.LookupParameter(parameterName_Width); if (parameter_Height != null && parameter_Width != null && parameter_Height.HasValue && parameter_Width.HasValue && parameter_Height.StorageType == StorageType.Double && parameter_Width.StorageType == StorageType.Double) { #if Revit2017 || Revit2018 || Revit2019 || Revit2020 double height = UnitUtils.ConvertFromInternalUnits(parameter_Height.AsDouble(), DisplayUnitType.DUT_METERS); double width = UnitUtils.ConvertFromInternalUnits(parameter_Width.AsDouble(), DisplayUnitType.DUT_METERS); #else double height = UnitUtils.ConvertFromInternalUnits(parameter_Height.AsDouble(), UnitTypeId.Meters); double width = UnitUtils.ConvertFromInternalUnits(parameter_Width.AsDouble(), UnitTypeId.Meters); #endif } } result = new List <Aperture>(); foreach (Face3D face3D_Temp in face3Ds) { Aperture aperture = Analytical.Create.Aperture(apertureConstruction, face3D_Temp); if (aperture == null) { continue; } aperture.UpdateParameterSets(familyInstance, ActiveSetting.Setting.GetValue <Core.TypeMap>(Core.Revit.ActiveSetting.Name.ParameterMap)); result.Add(aperture); } convertSettings?.Add(familyInstance.Id, result); return(result); }
public static FamilyInstance ToRevit(this Aperture aperture, Document document, HostObject hostObject, Core.Revit.ConvertSettings convertSettings) { if (aperture == null || document == null) { return(null); } FamilyInstance result = convertSettings?.GetObject <FamilyInstance>(aperture.Guid); if (result != null) { return(result); } ApertureConstruction apertureConstruction = aperture.ApertureConstruction; if (apertureConstruction == null) { return(null); } FamilySymbol familySymbol = apertureConstruction.ToRevit(document, convertSettings); if (familySymbol == null) { familySymbol = Analytical.Query.DefaultApertureConstruction(hostObject.PanelType(), apertureConstruction.ApertureType).ToRevit(document, convertSettings); //Default Aperture Construction } if (familySymbol == null) { return(null); } Point3D point3D_Location = aperture.PlanarBoundary3D?.Plane?.Origin; if (point3D_Location == null) { return(null); } Level level = Geometry.Revit.Query.LowLevel(document, point3D_Location.Z); if (level == null) { return(null); } XYZ referenceDirection = new XYZ(0, 0, 0); if (hostObject is RoofBase) { Face3D face3D = aperture.GetFace3D(); Geometry.Spatial.Plane plane = face3D.GetPlane(); bool coplanar = plane.Coplanar(Geometry.Spatial.Plane.WorldXY); //if(coplanar) //{ // referenceDirection = new XYZ(0, 0, 1); //} result = document.Create.NewFamilyInstance(point3D_Location.ToRevit(), familySymbol, referenceDirection, hostObject, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); if (result == null) { return(null); } //List<Geometry.Planar.Point2D> point2Ds = new List<Geometry.Planar.Point2D>(); //IClosedPlanar3D closedPlanar3D = face3D.GetExternalEdge3D(); //if (closedPlanar3D is ICurvable3D) //{ // List<ICurve3D> curve3Ds = ((ICurvable3D)closedPlanar3D).GetCurves(); // foreach (ICurve3D curve3D in curve3Ds) // { // ICurve3D curve3D_Temp = plane.Project(curve3D); // point2Ds.Add(plane.Convert(curve3D_Temp.GetStart())); // } //} //Geometry.Planar.Rectangle2D rectangle2D = Geometry.Planar.Create.Rectangle2D(point2Ds); Geometry.Planar.Rectangle2D rectangle2D = Analytical.Create.Rectangle2D(aperture.PlanarBoundary3D); if (rectangle2D == null) { return(null); } document.Regenerate(); result = document.GetElement(result.Id) as FamilyInstance; Vector3D handOrientation_FamilyInstance = result.HandOrientation.ToSAM_Vector3D(false); Vector3D facingOrientation_FamilyInstance = result.FacingOrientation.ToSAM_Vector3D(false); double factor = 0; Geometry.Planar.Vector2D direction = rectangle2D.WidthDirection; if (!coplanar && Core.Query.Round(direction.Y) < 0) { factor = System.Math.PI / 2; } Vector3D handOrienation_Aperture = plane.Convert(direction); Geometry.Spatial.Plane plane_FamilyInstance = new Geometry.Spatial.Plane(point3D_Location, handOrientation_FamilyInstance, facingOrientation_FamilyInstance); handOrienation_Aperture = plane_FamilyInstance.Project(handOrienation_Aperture); double angle = Geometry.Spatial.Query.SignedAngle(handOrientation_FamilyInstance, handOrienation_Aperture, plane.Normal); result.Location.Rotate(Line.CreateUnbound(point3D_Location.ToRevit(), plane.Normal.ToRevit(false)), angle + factor); //document.Regenerate(); //BoundingBox3D boundingBox3D_familyInstance = familyInstance.BoundingBox3D(); //BoundingBox3D boundingBox3D_Aperture = aperture.GetBoundingBox(); //if(boundingBox3D_familyInstance.Min.Distance(boundingBox3D_Aperture.Min) > SAM.Core.Tolerance.MacroDistance) // familyInstance.Location.Rotate(Line.CreateUnbound(point3D_Location.ToRevit(), plane.Normal.ToRevit(false)), System.Math.PI / 2); //Geometry.Planar.Rectangle2D rectangle2D = Geometry.Planar.Create.Rectangle2D(point2Ds); //Geometry.Planar.Vector2D direction = null; //if (rectangle2D.Height > rectangle2D.Width) // direction = rectangle2D.HeightDirection; //else // direction = rectangle2D.WidthDirection; //double angle = plane.Convert(direction).ToRevit(false).AngleTo(new XYZ(0, 1, 0)); //angle = System.Math.PI - angle; ////if (angle > System.Math.PI) //// angle = -(angle - System.Math.PI); //if (direction.X < 0) // angle = -angle; //familyInstance.Location.Rotate(Line.CreateUnbound(point3D_Location.ToRevit(), plane.Normal.ToRevit(false)), angle); } else { result = document.Create.NewFamilyInstance(point3D_Location.ToRevit(), familySymbol, hostObject, level, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); } if (result == null) { return(null); } if (result.CanFlipHand) { document.Regenerate(); //This is needed to get flip correctly pushed to revit Vector3D axisX = result.HandOrientation.ToSAM_Vector3D(false); if (!axisX.SameHalf(aperture.Plane.AxisX)) { result.flipHand(); } } if (result.CanFlipFacing) { document.Regenerate(); //This is needed to get flip correctly pushed to revit Vector3D normal = result.FacingOrientation.ToSAM_Vector3D(false); if (!normal.SameHalf(aperture.Plane.Normal)) { result.flipFacing(); } } if (convertSettings.ConvertParameters) { Core.Revit.Modify.SetValues(result, aperture, new BuiltInParameter[] { BuiltInParameter.INSTANCE_SILL_HEIGHT_PARAM, BuiltInParameter.INSTANCE_HEAD_HEIGHT_PARAM, BuiltInParameter.FAMILY_LEVEL_PARAM, BuiltInParameter.SCHEDULE_LEVEL_PARAM }); Core.Revit.Modify.SetValues(result, aperture, ActiveSetting.Setting); bool simplified = false; //Check if geometry is simplified if (!Geometry.Planar.Query.Rectangular(aperture.PlanarBoundary3D?.ExternalEdge2DLoop?.GetClosed2D(), Core.Tolerance.MacroDistance)) { simplified = true; } if (!simplified && result.Host is Autodesk.Revit.DB.Wall) { Face3D face3D = aperture.GetFace3D(); Geometry.Spatial.Plane plane = face3D.GetPlane(); Geometry.Planar.Rectangle2D rectangle2D = Analytical.Create.Rectangle2D(aperture.PlanarBoundary3D); if (rectangle2D != null) { Vector3D widthDirection = plane.Convert(rectangle2D.WidthDirection); Vector3D heightDirection = plane.Convert(rectangle2D.HeightDirection); //TODO: Implement code for Tilted Walls Vector3D vector3D_Z = Vector3D.WorldZ; if (!widthDirection.AlmostSimilar(vector3D_Z) && !heightDirection.AlmostSimilar(vector3D_Z)) { simplified = true; } } } Core.Revit.Modify.SetSimplified(result, simplified); Core.Revit.Modify.SetJson(result, aperture.ToJObject()?.ToString()); } convertSettings?.Add(aperture.Guid, result); return(result); }
public static Aperture ToSAM_Aperture(this FamilyInstance familyInstance, Core.Revit.ConvertSettings convertSettings) { if (familyInstance == null) { return(null); } Aperture result = convertSettings?.GetObject <Aperture>(familyInstance.Id); if (result != null) { return(result); } if (Core.Revit.Query.Simplified(familyInstance)) { result = Core.Revit.Query.IJSAMObject <Aperture>(familyInstance); if (result != null) { convertSettings?.Add(familyInstance.Id, result); return(result); } } Point3D point3D_Location = Geometry.Revit.Query.Location(familyInstance); if (point3D_Location == null) { return(null); } PanelType panelType_Host = PanelType.Undefined; BuiltInCategory builtInCategory_Host = BuiltInCategory.INVALID; if (familyInstance.Host != null) { HostObject hostObject = familyInstance.Host as HostObject; if (hostObject != null) { builtInCategory_Host = (BuiltInCategory)hostObject.Category.Id.IntegerValue; List <Face3D> face3Ds_Temp = hostObject.Profiles(); if (face3Ds_Temp != null && face3Ds_Temp.Count != 0) { Geometry.Spatial.Plane plane_Host = face3Ds_Temp.Closest(point3D_Location)?.GetPlane(); if (plane_Host != null) { point3D_Location = plane_Host.Project(point3D_Location); } } HostObjAttributes hostObjAttributes = familyInstance.Document.GetElement(hostObject.GetTypeId()) as HostObjAttributes; if (hostObjAttributes != null) { panelType_Host = hostObjAttributes.PanelType(); } if (panelType_Host == PanelType.Undefined) { panelType_Host = hostObject.PanelType(); } } } ApertureConstruction apertureConstruction = ToSAM_ApertureConstruction(familyInstance, convertSettings); if (apertureConstruction == null && panelType_Host != PanelType.Undefined) { apertureConstruction = Analytical.Query.DefaultApertureConstruction(panelType_Host, familyInstance.ApertureType()); //Default Aperture Construction } Vector3D axisX = null; Vector3D normal = null; Vector3D axisY = null; if (builtInCategory_Host == BuiltInCategory.OST_Roofs) { axisX = familyInstance.HandOrientation.ToSAM_Vector3D(false); axisY = familyInstance.FacingOrientation.ToSAM_Vector3D(false); normal = Geometry.Spatial.Query.AxisY(axisY, axisX); } else { axisX = familyInstance.HandOrientation.ToSAM_Vector3D(false); normal = familyInstance.FacingOrientation.ToSAM_Vector3D(false); axisY = Geometry.Spatial.Query.AxisY(normal, axisX); } Geometry.Spatial.Plane plane = Geometry.Spatial.Create.Plane(point3D_Location, axisX, axisY); if (!plane.Normal.SameHalf(normal)) { plane.FlipZ(false); } List <Face3D> face3Ds = Geometry.Revit.Convert.ToSAM_Face3Ds(familyInstance); if (face3Ds == null || face3Ds.Count == 0) { return(null); } List <Point2D> point2Ds = new List <Point2D>(); foreach (Face3D face3D_Temp in face3Ds) { IClosedPlanar3D closedPlanar3D = face3D_Temp.GetExternalEdge3D(); if (closedPlanar3D is ICurvable3D) { List <ICurve3D> curve3Ds = ((ICurvable3D)closedPlanar3D).GetCurves(); foreach (ICurve3D curve3D in curve3Ds) { ICurve3D curve3D_Temp = plane.Project(curve3D); point2Ds.Add(plane.Convert(curve3D_Temp.GetStart())); } } } if (point2Ds == null || point2Ds.Count == 0) { return(null); } //TODO: Working on SAM Families (requested by Michal) string parameterName_Height = Core.Revit.Query.Name(ActiveSetting.Setting, typeof(Aperture), typeof(FamilyInstance), "GetHeight"); string parameterName_Width = Core.Revit.Query.Name(ActiveSetting.Setting, typeof(Aperture), typeof(FamilyInstance), "GetWidth"); if (!string.IsNullOrWhiteSpace(parameterName_Height) && !string.IsNullOrWhiteSpace(parameterName_Width)) { Parameter parameter_Height = familyInstance.LookupParameter(parameterName_Height); Parameter parameter_Width = familyInstance.LookupParameter(parameterName_Width); if (parameter_Height != null && parameter_Width != null && parameter_Height.HasValue && parameter_Width.HasValue && parameter_Height.StorageType == StorageType.Double && parameter_Width.StorageType == StorageType.Double) { double height = UnitUtils.ConvertFromInternalUnits(parameter_Height.AsDouble(), DisplayUnitType.DUT_METERS); double width = UnitUtils.ConvertFromInternalUnits(parameter_Width.AsDouble(), DisplayUnitType.DUT_METERS); BoundingBox2D boundingBox2D = new BoundingBox2D(point2Ds); double factor_Height = height / boundingBox2D.Height; double factor_Width = width / boundingBox2D.Width; point2Ds = point2Ds.ConvertAll(x => new Point2D(x.X * factor_Width, x.Y * factor_Height)); } } Rectangle2D rectangle2D = Geometry.Planar.Create.Rectangle2D(point2Ds); result = new Aperture(apertureConstruction, new Face3D(plane, rectangle2D)); result.UpdateParameterSets(familyInstance, ActiveSetting.Setting.GetValue <Core.TypeMap>(Core.Revit.ActiveSetting.Name.ParameterMap)); convertSettings?.Add(familyInstance.Id, result); return(result); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="dataAccess"> /// The DA object is used to retrieve from inputs and store in outputs. /// </param> protected override void SolveInstance(IGH_DataAccess dataAccess) { bool run = false; if (!dataAccess.GetData(2, ref run) || !run) { return; } bool merge = true; if (!dataAccess.GetData(1, ref merge)) { return; } GH_ObjectWrapper objectWrapper = null; if (!dataAccess.GetData(0, ref objectWrapper) || objectWrapper.Value == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid data"); return; } dynamic obj = objectWrapper.Value; ElementId aId = obj.Id as ElementId; string message = null; Document document = obj.Document as Document; Autodesk.Revit.DB.Mechanical.Space space = document.GetElement(aId) as Autodesk.Revit.DB.Mechanical.Space; if (space == null) { message = "Invalid Element"; AddRuntimeMessage(GH_RuntimeMessageLevel.Error, message); return; } if (space.Location == null) { message = string.Format("Cannot generate Panels. Space {0} [ElementId: {1}] not enclosed", space.Name, space.Id.IntegerValue); AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, message); return; } if (space.Volume < Core.Tolerance.MacroDistance) { message = string.Format("Space cannot be converted because it has no volume. Space {0} [ElementId: {1}] not enclosed", space.Name, space.Id.IntegerValue); AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, message); return; } ConvertSettings convertSettings = new ConvertSettings(true, true, true); List <Panel> panels = Analytical.Revit.Create.Panels(space, convertSettings); if (panels == null || panels.Count == 0) { message = "Panels ould not be generated"; AddRuntimeMessage(GH_RuntimeMessageLevel.Error, message); return; } panels.RemoveAll(x => x == null); List <Panel> panels_Temp = new List <Panel>(); foreach (Panel panel in panels) { PanelGroup panelGroup = Analytical.Query.PanelGroup(panel.PanelType); if (panelGroup != PanelGroup.Floor && panelGroup != PanelGroup.Roof) { continue; } ElementId elementId = panel.ElementId(); if (elementId == null || elementId == ElementId.InvalidElementId) { panels_Temp.Add(panel); continue; } HostObject hostObject = document.GetElement(elementId) as HostObject; if (hostObject == null) { panels_Temp.Add(panel); continue; } Geometry.Spatial.Plane plane = panel.PlanarBoundary3D.Plane; List <Geometry.Spatial.Face3D> face3Ds = Geometry.Revit.Query.Profiles(hostObject); if (face3Ds == null || face3Ds.Count == 0) { panels_Temp.Add(panel); continue; } Geometry.Spatial.Plane plane_Face3D = face3Ds.Find(x => plane.Coplanar(x.GetPlane()))?.GetPlane(); if (plane_Face3D == null) { panels_Temp.Add(panel); continue; } Geometry.Spatial.Point3D point3D_Projected = plane_Face3D.Project(plane.Origin); panel.Move(new Geometry.Spatial.Vector3D(plane.Origin, point3D_Projected)); panels_Temp.Add(panel); } List <Panel> redundantPanels = new List <Panel>(); if (merge) { panels_Temp = Analytical.Query.MergeCoplanarPanels(panels_Temp, Core.Tolerance.MacroDistance, ref redundantPanels, true, Core.Tolerance.MacroDistance); } dataAccess.SetDataList(0, panels_Temp.FindAll(x => Analytical.Query.PanelGroup(x.PanelType) == PanelGroup.Floor)); dataAccess.SetDataList(1, panels_Temp.FindAll(x => Analytical.Query.PanelGroup(x.PanelType) == PanelGroup.Roof)); dataAccess.SetDataList(2, redundantPanels); }
public static HostObject ToRevit(this Panel panel, Document document, Core.Revit.ConvertSettings convertSettings) { Geometry.Spatial.Face3D face3D = panel?.GetFace3D(); if (face3D == null) { return(null); } HostObject result = convertSettings?.GetObject <HostObject>(panel.Guid); if (result != null) { return(result); } PanelType panelType = panel.PanelType; Geometry.Spatial.Vector3D normal = panel.Normal; HostObjAttributes hostObjAttributes = panel.Construction.ToRevit(document, panelType, normal, convertSettings); if (hostObjAttributes == null) { hostObjAttributes = Analytical.Query.DefaultConstruction(panelType)?.ToRevit(document, panelType, normal, convertSettings); //Default Construction } BuiltInParameter[] builtInParameters = null; if (hostObjAttributes is WallType) { List <Curve> curveList = new List <Curve>(); foreach (Geometry.Spatial.IClosedPlanar3D closedPlanar3D in face3D.GetEdge3Ds()) { List <Line> lines = closedPlanar3D.ToRevit(); if (lines == null) { continue; } curveList.AddRange(lines); } if (curveList == null || curveList.Count == 0) { return(null); } double lowElevation = panel.LowElevation(); Level level = document.LowLevel(lowElevation); if (level == null) { return(null); } Wall wall = Wall.Create(document, curveList, hostObjAttributes.Id, level.Id, false, panel.Normal.ToRevit(false)); Parameter parameter = null; parameter = wall.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE); if (parameter != null) { parameter.Set(ElementId.InvalidElementId); } parameter = wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM); if (parameter != null) { double height = UnitUtils.ConvertToInternalUnits((panel.HighElevation() - lowElevation), DisplayUnitType.DUT_METERS); parameter.Set(height); } double levelElevation = UnitUtils.ConvertFromInternalUnits(level.Elevation, DisplayUnitType.DUT_METERS); if (Math.Abs(lowElevation - levelElevation) > Core.Tolerance.MacroDistance) { parameter = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET); if (parameter != null) { parameter.Set(UnitUtils.ConvertToInternalUnits(lowElevation - levelElevation, DisplayUnitType.DUT_METERS)); } } builtInParameters = new BuiltInParameter[] { BuiltInParameter.WALL_BASE_CONSTRAINT, BuiltInParameter.WALL_BASE_OFFSET, BuiltInParameter.WALL_HEIGHT_TYPE, BuiltInParameter.WALL_USER_HEIGHT_PARAM, BuiltInParameter.WALL_KEY_REF_PARAM }; result = wall; } else if (hostObjAttributes is FloorType) { Geometry.Spatial.IClosedPlanar3D closedPlanar3D_External = face3D.GetExternalEdge3D(); if (!(closedPlanar3D_External is Geometry.Spatial.ICurvable3D)) { return(null); } double elevation = panel.LowElevation(); Level level = document.HighLevel(elevation); Geometry.Spatial.Plane plane = new Geometry.Spatial.Plane(new Geometry.Spatial.Point3D(0, 0, elevation), Geometry.Spatial.Vector3D.WorldZ); CurveArray curveArray_Sloped = new CurveArray(); CurveArray curveArray_Plane = new CurveArray(); Geometry.Spatial.IClosedPlanar3D closedPlanar3D = face3D.GetExternalEdge3D(); if (!(closedPlanar3D is Geometry.Spatial.ICurvable3D)) { return(null); } List <Geometry.Spatial.Segment3D> segment3Ds = Geometry.Revit.Query.Segment3Ds(closedPlanar3D); if (segment3Ds == null || segment3Ds.Count == 0) { return(null); } foreach (Geometry.Spatial.Segment3D segment3D in segment3Ds) { curveArray_Sloped.Append(segment3D.ToRevit_Line()); Geometry.Spatial.Segment3D segment3D_Temp = plane.Project(segment3D); if (segment3D_Temp == null) { continue; } curveArray_Plane.Append(segment3D_Temp.ToRevit_Line()); } Floor floor = document.Create.NewFloor(curveArray_Plane, hostObjAttributes as FloorType, level, false); if (floor != null) { floor.ChangeTypeId(hostObjAttributes.Id); List <Geometry.Spatial.IClosedPlanar3D> closedPlanar3Ds_Internal = face3D.GetInternalEdge3Ds(); if (closedPlanar3Ds_Internal != null && closedPlanar3Ds_Internal.Count > 0) { //Requires to be regenerated before inserting openings //https://thebuildingcoder.typepad.com/blog/2013/07/create-a-floor-with-an-opening-or-complex-boundary.html document.Regenerate(); foreach (Geometry.Spatial.IClosedPlanar3D closedPlanar3D_Internal in face3D.GetInternalEdge3Ds()) { List <Geometry.Spatial.Segment3D> segment3Ds_Internal = Geometry.Revit.Query.Segment3Ds(closedPlanar3D_Internal); if (segment3Ds_Internal == null || segment3Ds_Internal.Count == 0) { continue; } curveArray_Plane = new CurveArray(); foreach (Geometry.Spatial.Segment3D segment3D in segment3Ds) { curveArray_Sloped.Append(segment3D.ToRevit_Line()); Geometry.Spatial.Segment3D segment3D_Temp = plane.Project(segment3D); if (segment3D_Temp == null) { continue; } curveArray_Plane.Append(segment3D_Temp.ToRevit_Line()); } Opening opening = document.Create.NewOpening(floor, curveArray_Plane, true); } } } if (floor != null) { document.Regenerate(); SlabShapeEditor slabShapeEditor = floor.SlabShapeEditor; if (slabShapeEditor != null) { slabShapeEditor.ResetSlabShape(); foreach (Curve curve in curveArray_Sloped) { XYZ xYZ = curve.GetEndPoint(0); slabShapeEditor.DrawPoint(xYZ); } } } builtInParameters = new BuiltInParameter[] { BuiltInParameter.LEVEL_PARAM }; result = floor; } else if (hostObjAttributes is RoofType) { CurveArray curveArray = new CurveArray(); foreach (Geometry.Spatial.IClosedPlanar3D closedPlanar3D in face3D.GetEdge3Ds()) { List <Geometry.Spatial.Segment3D> segment3Ds = Geometry.Revit.Query.Segment3Ds(closedPlanar3D); if (segment3Ds == null || segment3Ds.Count == 0) { return(null); } segment3Ds.ForEach(x => curveArray.Append(x.ToRevit_Line())); } Level level = document.HighLevel(panel.LowElevation()); double levelElevation = level.Elevation; ModelCurveArray modelCurveArray = new ModelCurveArray(); RoofBase roofBase = document.Create.NewFootPrintRoof(curveArray, level, hostObjAttributes as RoofType, out modelCurveArray); Parameter parameter = roofBase.get_Parameter(BuiltInParameter.ROOF_UPTO_LEVEL_PARAM); if (parameter != null) { parameter.Set(ElementId.InvalidElementId); } SlabShapeEditor slabShapeEditor = roofBase.SlabShapeEditor; if (slabShapeEditor != null) { slabShapeEditor.ResetSlabShape(); foreach (Curve curve in curveArray) { XYZ xYZ = curve.GetEndPoint(0); //if (Math.Abs(xYZ.Z - levelElevation) > Core.Tolerance.MicroDistance) slabShapeEditor.DrawPoint(xYZ); } } builtInParameters = new BuiltInParameter[] { BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM, BuiltInParameter.ROOF_BASE_LEVEL_PARAM, BuiltInParameter.ROOF_UPTO_LEVEL_PARAM }; result = roofBase; } if (result == null) { return(null); } List <Aperture> apertures = panel.Apertures; if (apertures != null) { Geometry.Spatial.Plane plane_Panel = panel.PlanarBoundary3D?.Plane; foreach (Aperture aperture in apertures) { Geometry.Spatial.Plane plane_Aperture = aperture?.PlanarBoundary3D?.Plane; if (plane_Aperture == null) { continue; } //bool flipHand = !plane_Panel.AxisX.SameHalf(plane_Aperture.AxisX); //bool flipFacing = !plane_Panel.Normal.SameHalf(plane_Aperture.Normal); FamilyInstance failyInstance_Aperture = aperture.ToRevit(document, result, convertSettings); } } if (convertSettings.ConvertParameters) { Core.Revit.Modify.SetValues(result, panel, builtInParameters); Core.Revit.Modify.SetValues(result, panel, ActiveSetting.Setting); Core.Revit.Modify.SetJson(result, panel.ToJObject()?.ToString()); } //TODO: Implement proper log //System.IO.File.AppendAllText(@"C:\Users\DengusiakM\Desktop\SAM\2020-04-16 floorbug\LOG.txt", string.Format("{0}\t{1}\t{2}\n", DateTime.Now.ToString(), panel.Guid, panel.Name)); convertSettings?.Add(panel.Guid, result); return(result); }
public static IOpening ToSAM_Opening(this FamilyInstance familyInstance, ConvertSettings convertSettings) { if (familyInstance == null) { return(null); } IOpening result = convertSettings?.GetObject <IOpening>(familyInstance.Id); if (result != null) { return(result); } if (Core.Revit.Query.Simplified(familyInstance)) { result = Core.Revit.Query.IJSAMObjects <IOpening>(familyInstance)?.FirstOrDefault(); if (result != null) { convertSettings?.Add(familyInstance.Id, result); return(result); } } Point3D point3D_Location = Geometry.Revit.Query.LocationPoint3D(familyInstance); if (point3D_Location == null) { List <Solid> solids = Core.Revit.Query.Solids(familyInstance, new Options()); solids?.RemoveAll(x => x.Volume == 0); if (solids == null || solids.Count == 0) { return(null); } if (solids.Count > 1) { solids.Sort((x, y) => y.Volume.CompareTo(x.Volume)); } point3D_Location = solids[0].ComputeCentroid()?.ToSAM(); } if (point3D_Location == null) { return(null); } OpeningType openingType = ToSAM_OpeningType(familyInstance.Symbol, convertSettings); if (openingType == null) { return(null); } BuiltInCategory builtInCategory_Host = BuiltInCategory.INVALID; HostObject hostObject = familyInstance.Host as HostObject; if (hostObject != null) { builtInCategory_Host = (BuiltInCategory)hostObject.Category.Id.IntegerValue; } Vector3D axisX = null; Vector3D normal = null; Vector3D axisY = null; if (builtInCategory_Host == BuiltInCategory.OST_Roofs) { axisX = familyInstance.HandOrientation.ToSAM_Vector3D(false); axisY = familyInstance.FacingOrientation.ToSAM_Vector3D(false); normal = Geometry.Spatial.Query.AxisY(axisY, axisX); } else { axisX = familyInstance.HandOrientation.ToSAM_Vector3D(false); normal = familyInstance.FacingOrientation.ToSAM_Vector3D(false); axisY = Geometry.Spatial.Query.AxisY(normal, axisX); } Geometry.Spatial.Plane plane = Geometry.Spatial.Create.Plane(point3D_Location, axisX, axisY); if (!plane.Normal.SameHalf(normal)) { plane.FlipZ(false); } List <Face3D> face3Ds = Geometry.Revit.Convert.ToSAM_Geometries <Face3D>(familyInstance); if (face3Ds == null || face3Ds.Count == 0) { return(null); } List <Point2D> point2Ds = new List <Point2D>(); foreach (Face3D face3D_Temp in face3Ds) { IClosedPlanar3D closedPlanar3D = face3D_Temp.GetExternalEdge3D(); if (closedPlanar3D is ICurvable3D) { List <ICurve3D> curve3Ds = ((ICurvable3D)closedPlanar3D).GetCurves(); foreach (ICurve3D curve3D in curve3Ds) { ICurve3D curve3D_Temp = plane.Project(curve3D); point2Ds.Add(plane.Convert(curve3D_Temp.GetStart())); } } } if (point2Ds == null || point2Ds.Count == 0) { return(null); } Rectangle2D rectangle2D = Geometry.Planar.Create.Rectangle2D(point2Ds); result = Analytical.Create.Opening(openingType, new Face3D(plane, rectangle2D)); result.UpdateParameterSets(familyInstance); convertSettings?.Add(familyInstance.Id, result); return(result); }
protected override void TrySolveInstance(IGH_DataAccess dataAccess) { int index = -1; bool run = false; index = Params.IndexOfInputParam("_run"); if (index == -1 || !dataAccess.GetData(index, ref run) || !run) { return; } double maxDistance = 0.2; index = Params.IndexOfInputParam("_maxDistance"); if (index == -1 || !dataAccess.GetData(index, ref maxDistance)) { return; } RhinoInside.Revit.GH.Types.Level level_GH = null; index = Params.IndexOfInputParam("_level"); if (index == -1 || !dataAccess.GetData(index, ref level_GH)) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid data"); return; } RhinoInside.Revit.GH.Types.Level referenceLevel_GH = null; index = Params.IndexOfInputParam("_referenceLevel"); if (index == -1 || !dataAccess.GetData(index, ref referenceLevel_GH)) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid data"); return; } Level level = level_GH.Value; if (level == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid data"); return; } Level referenceLevel = referenceLevel_GH.Value; if (level == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid data"); return; } #if Revit2017 || Revit2018 || Revit2019 || Revit2020 double elevation = UnitUtils.ConvertFromInternalUnits(level.Elevation, DisplayUnitType.DUT_METERS); double referenceElevation = UnitUtils.ConvertFromInternalUnits(referenceLevel.Elevation, DisplayUnitType.DUT_METERS); #else double elevation = UnitUtils.ConvertFromInternalUnits(level.Elevation, UnitTypeId.Meters); double referenceElevation = UnitUtils.ConvertFromInternalUnits(referenceLevel.Elevation, UnitTypeId.Meters); #endif Document document = level.Document; IEnumerable <Autodesk.Revit.DB.Wall> walls_All = new FilteredElementCollector(document).OfClass(typeof(Autodesk.Revit.DB.Wall)).Cast <Autodesk.Revit.DB.Wall>(); if (walls_All == null || walls_All.Count() == 0) { return; } StartTransaction(document); ConvertSettings convertSettings = new ConvertSettings(true, true, true); List <Panel> panels = new List <Panel>(); List <Panel> panels_Reference = new List <Panel>(); foreach (Autodesk.Revit.DB.Wall wall in walls_All) { List <Panel> panels_Temp = Analytical.Revit.Convert.ToSAM(wall, convertSettings); foreach (Panel panel in panels_Temp) { double max = panel.MaxElevation(); double min = panel.MinElevation(); if (Math.Abs(min - elevation) < Core.Tolerance.Distance || (min - Core.Tolerance.Distance < elevation && max - Core.Tolerance.Distance > elevation)) { panels.Add(panel); } if (Math.Abs(min - referenceElevation) < Core.Tolerance.Distance || (min - Core.Tolerance.Distance < referenceElevation && max - Core.Tolerance.Distance > referenceElevation)) { panels_Reference.Add(panel); } } } IEnumerable <ElementId> elementIds = panels.ConvertAll(x => x.ElementId()).Distinct(); IEnumerable <ElementId> elementIds_Reference = panels_Reference.ConvertAll(x => x.ElementId()).Distinct(); Geometry.Spatial.Plane plane = new Geometry.Spatial.Plane(new Point3D(0, 0, elevation), Vector3D.WorldZ); Dictionary <Segment2D, HostObjAttributes> dictionary_Reference = new Dictionary <Segment2D, HostObjAttributes>(); foreach (ElementId elementId in elementIds_Reference) { Element element = document.GetElement(elementId); if (element == null) { continue; } HostObjAttributes hostObjAttributes = document.GetElement(element.GetTypeId()) as HostObjAttributes; if (hostObjAttributes == null) { continue; } LocationCurve locationCurve = element.Location as LocationCurve; ISegmentable3D segmentable3D = locationCurve.ToSAM() as ISegmentable3D; if (segmentable3D == null) { continue; } List <Segment3D> segment3Ds = segmentable3D.GetSegments(); if (segment3Ds == null || segment3Ds.Count == 0) { continue; } segment3Ds.ForEach(x => dictionary_Reference[plane.Convert(x)] = hostObjAttributes); } Dictionary <Segment2D, ElementId> dictionary = new Dictionary <Segment2D, ElementId>(); foreach (ElementId elementId in elementIds) { LocationCurve locationCurve = document.GetElement(elementId).Location as LocationCurve; Segment3D segment3D = locationCurve.ToSAM() as Segment3D; if (segment3D == null) { continue; } dictionary[plane.Convert(plane.Project(segment3D))] = elementId; } Dictionary <Segment2D, ElementId> dictionary_Result = new Dictionary <Segment2D, ElementId>(); foreach (KeyValuePair <Segment2D, ElementId> keyValuePair in dictionary) { Segment2D segment2D = keyValuePair.Key; List <Segment2D> segment2Ds_Temp = dictionary_Reference.Keys.ToList().FindAll(x => x.Collinear(segment2D) && x.Distance(segment2D) <= maxDistance + Core.Tolerance.MacroDistance && x.Distance(segment2D) > Core.Tolerance.MacroDistance); if (segment2Ds_Temp == null || segment2Ds_Temp.Count == 0) { continue; } Element element = document.GetElement(keyValuePair.Value); if (element == null) { continue; } HostObjAttributes hostObjAttributes = document.GetElement(element.GetTypeId()) as HostObjAttributes; if (hostObjAttributes == null) { continue; } segment2Ds_Temp.Sort((x, y) => segment2D.Distance(x).CompareTo(segment2D.Distance(y))); Segment2D segment2D_Reference = null; foreach (Segment2D segment2D_Temp in segment2Ds_Temp) { HostObjAttributes hostObjAttributes_Temp = dictionary_Reference[segment2D_Temp]; if (hostObjAttributes.Name.Equals(hostObjAttributes_Temp.Name)) { segment2D_Reference = segment2D_Temp; break; } } if (segment2D_Reference == null) { HashSet <PanelType> panelTypes = new HashSet <PanelType>(); panelTypes.Add(Analytical.Revit.Query.PanelType(hostObjAttributes)); switch (panelTypes.First()) { case PanelType.CurtainWall: panelTypes.Add(PanelType.WallExternal); break; case PanelType.UndergroundWall: panelTypes.Add(PanelType.WallExternal); break; case PanelType.Undefined: panelTypes.Add(PanelType.WallInternal); break; } foreach (Segment2D segment2D_Temp in segment2Ds_Temp) { HostObjAttributes hostObjAttributes_Temp = dictionary_Reference[segment2D_Temp]; PanelType panelType_Temp = Analytical.Revit.Query.PanelType(hostObjAttributes_Temp); if (panelTypes.Contains(panelType_Temp)) { segment2D_Reference = segment2D_Temp; break; } } } if (segment2D_Reference == null) { segment2D_Reference = segment2Ds_Temp.First(); } Segment2D segment2D_Project = segment2D_Reference.Project(segment2D); if (segment2D_Project == null) { continue; } dictionary_Result[segment2D_Project] = dictionary[segment2D]; } List <HostObject> result = new List <HostObject>(); foreach (KeyValuePair <Segment2D, ElementId> keyValuePair in dictionary_Result) { Autodesk.Revit.DB.Wall wall = document.GetElement(keyValuePair.Value) as Autodesk.Revit.DB.Wall; if (wall == null || !wall.IsValidObject) { continue; } Segment2D segment2D = keyValuePair.Key; bool pinned = wall.Pinned; if (wall.Pinned) { using (SubTransaction subTransaction = new SubTransaction(document)) { subTransaction.Start(); wall.Pinned = false; subTransaction.Commit(); } } Segment3D segment3D = plane.Convert(segment2D); LocationCurve locationCurve = wall.Location as LocationCurve; using (SubTransaction subTransaction = new SubTransaction(document)) { subTransaction.Start(); document.Regenerate(); locationCurve.Curve = Geometry.Revit.Convert.ToRevit(segment3D); subTransaction.Commit(); } if (wall.Pinned != pinned) { using (SubTransaction subTransaction = new SubTransaction(document)) { subTransaction.Start(); wall.Pinned = pinned; subTransaction.Commit(); } } result.Add(wall); } index = Params.IndexOfOutputParam("walls"); if (index != -1) { dataAccess.SetDataList(index, result); } }