Esempio n. 1
0
        public static List <Polygon3D> Polygon3Ds(this CurtainCell curtainCell)
        {
            CurveArrArray curveArrArray = curtainCell?.PlanarizedCurveLoops;

            if (curveArrArray == null)
            {
                return(null);
            }

            List <Polygon3D> result = new List <Polygon3D>();

            foreach (CurveArray curveArray in curtainCell.PlanarizedCurveLoops)
            {
                Polygon3D polygon3D = curveArray?.ToSAM_Polygon3D();
                if (polygon3D == null && !polygon3D.IsValid())
                {
                    continue;
                }


                Geometry.Spatial.Plane plane = polygon3D.GetPlane();
                if (plane == null)
                {
                    continue;
                }

                result.Add(polygon3D);
            }

            return(result);
        }
Esempio n. 2
0
        public static Panel SimilarPanel(this Geometry.Spatial.Face3D face3D, Dictionary <Panel, Geometry.Spatial.Face3D> panelsDictionary, double distanceTolerance = Core.Tolerance.MacroDistance, double coplanarTolerance = Core.Tolerance.MacroDistance)
        {
            if (face3D == null || panelsDictionary == null)
            {
                return(null);
            }

            Geometry.Spatial.Plane plane = face3D.GetPlane();
            double area = face3D.GetArea();

            Geometry.Planar.IClosed2D closed2D_1       = Geometry.Spatial.Query.Convert(plane, face3D.GetExternalEdge3D());
            Geometry.Planar.Point2D   point2D_Internal = closed2D_1.GetInternalPoint2D();

            double areaDifferece_Min = double.MaxValue;
            Panel  result            = null;

            foreach (KeyValuePair <Panel, Geometry.Spatial.Face3D> keyValuePair in panelsDictionary)
            {
                if (keyValuePair.Value == null)
                {
                    continue;
                }

                double areaDifference = System.Math.Abs(keyValuePair.Value.GetArea() - area);

                if (areaDifferece_Min < areaDifference)
                {
                    continue;
                }

                Geometry.Spatial.Face3D face3D_Temp = keyValuePair.Value;
                Geometry.Spatial.Plane  plane_Temp  = face3D_Temp.GetPlane();

                if (!plane.Coplanar(plane_Temp, coplanarTolerance))
                {
                    continue;
                }

                Geometry.Spatial.Point3D point3D_Origin  = plane_Temp.Origin;
                Geometry.Spatial.Point3D point3D_Project = Geometry.Spatial.Query.Project(plane, point3D_Origin);
                if (point3D_Origin.Distance(point3D_Project) > distanceTolerance)
                {
                    continue;
                }

                Geometry.Planar.IClosed2D closed2D_2 = Geometry.Spatial.Query.Convert(plane, face3D_Temp.GetExternalEdge3D());
                if (closed2D_2.Inside(point2D_Internal))
                {
                    result            = keyValuePair.Key;
                    areaDifferece_Min = areaDifference;
                }
            }

            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// Gets Wall Panels from Spatial Element base on its Boundary
        /// </summary>
        /// <param name="spatialElement">Revit Spatial Element such as Space or Room</param>
        /// <param name="elevation_Bottom">Bottom Elevation</param>
        /// <param name="elevation_Top">Top Elevation</param>
        /// <param name="convertSettings">Convert Settings</param>
        /// <param name="spatialElementBoundaryOptions">Revit SpactialElementBoundaryOptions. Center Spatial Element Boundary Location will be used when set to null</param>
        /// <returns>Wall Panels</returns>
        public static List <Panel> Panels(this SpatialElement spatialElement, double elevation_Bottom, double elevation_Top, Core.Revit.ConvertSettings convertSettings, SpatialElementBoundaryOptions spatialElementBoundaryOptions = null)
        {
            if (spatialElement == null || double.IsNaN(elevation_Top) || double.IsNaN(elevation_Bottom))
            {
                return(null);
            }

            if (spatialElementBoundaryOptions == null)
            {
                spatialElementBoundaryOptions = new SpatialElementBoundaryOptions();
                spatialElementBoundaryOptions.SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Center;
            }

            Document document = spatialElement.Document;

            Geometry.Spatial.Plane plane        = Geometry.Spatial.Plane.WorldXY;
            Geometry.Spatial.Plane plane_Top    = plane.GetMoved(new Geometry.Spatial.Vector3D(0, 0, elevation_Top)) as Geometry.Spatial.Plane;
            Geometry.Spatial.Plane plane_Bottom = plane.GetMoved(new Geometry.Spatial.Vector3D(0, 0, elevation_Bottom)) as Geometry.Spatial.Plane;

            List <Panel> panels = new List <Panel>();

            IList <IList <BoundarySegment> > boundaries = spatialElement.GetBoundarySegments(spatialElementBoundaryOptions);

            foreach (IList <BoundarySegment> boundarySegments in boundaries)
            {
                if (boundarySegments == null)
                {
                    continue;
                }

                foreach (BoundarySegment boundarySegment in boundarySegments)
                {
                    if (boundarySegment == null)
                    {
                        continue;
                    }

                    Curve curve = boundarySegment.GetCurve();
                    if (curve == null)
                    {
                        continue;
                    }

                    HostObject hostObject = null;
                    if (boundarySegment.ElementId != null && boundarySegment.ElementId != ElementId.InvalidElementId)
                    {
                        if (boundarySegment.LinkElementId == null || boundarySegment.LinkElementId == ElementId.InvalidElementId)
                        {
                            hostObject = document.GetElement(boundarySegment.ElementId) as HostObject;
                        }
                        else
                        {
                            RevitLinkInstance revitLinkInstance = document.GetElement(boundarySegment.LinkElementId) as RevitLinkInstance;
                            if (revitLinkInstance != null)
                            {
                                Document document_Linked = revitLinkInstance.GetLinkDocument();
                                if (document_Linked != null)
                                {
                                    hostObject = document_Linked.GetElement(boundarySegment.ElementId) as HostObject;
                                }
                            }
                        }
                    }

                    Panel panel_Temp = null;

                    if (hostObject != null)
                    {
                        List <Panel> panels_Temp = hostObject.ToSAM(convertSettings);
                        if (panels_Temp != null && panels_Temp.Count > 0)
                        {
                            panel_Temp = panels_Temp[0];
                        }
                    }

                    List <Geometry.Spatial.Segment3D> segment3Ds = Geometry.Revit.Convert.ToSAM_Segment3Ds(curve);
                    if (segment3Ds == null || segment3Ds.Count == 0)
                    {
                        continue;
                    }

                    foreach (Geometry.Spatial.Segment3D segment3D in segment3Ds)
                    {
                        Geometry.Spatial.Segment3D segment3D_Top    = Geometry.Spatial.Query.Project(plane_Top, segment3D);
                        Geometry.Spatial.Segment3D segment3D_Bottom = Geometry.Spatial.Query.Project(plane_Bottom, segment3D);

                        Geometry.Spatial.Polygon3D polygon3D = Geometry.Spatial.Create.Polygon3D(new Geometry.Spatial.Point3D[] { segment3D_Top[0], segment3D_Top[1], segment3D_Bottom[1], segment3D_Bottom[0] });
                        if (polygon3D == null)
                        {
                            continue;
                        }

                        Geometry.Spatial.Face3D face3D = new Geometry.Spatial.Face3D(polygon3D);

                        Panel panel = null;
                        if (panel_Temp != null)
                        {
                            panel = Analytical.Create.Panel(Guid.NewGuid(), panel_Temp, face3D);
                        }

                        if (panel_Temp != null)
                        {
                            Geometry.Spatial.Vector3D normal = polygon3D.GetPlane()?.Normal;
                            PanelType panelType = Analytical.Query.PanelType(normal);

                            Construction construction = Query.DefaultAirConstruction(panelType.PanelGroup());
                            panel = Analytical.Create.Panel(construction, panelType, face3D);
                        }

                        if (panel != null)
                        {
                            panels.Add(panel);
                        }
                    }
                }
            }

            return(panels);
        }
Esempio n. 4
0
        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 Autodesk.Revit.DB.WallType)
            {
                double lowElevation = panel.LowElevation();

                Level level = document.LowLevel(lowElevation);

                Autodesk.Revit.DB.Wall wall = ToRevit_Wall(face3D, document, (Autodesk.Revit.DB.WallType)hostObjAttributes, level);
                if (wall == null)
                {
                    return(result);
                }

                //List<Curve> curveList = new List<Curve>();
                //foreach (Geometry.Spatial.IClosedPlanar3D closedPlanar3D in face3D.GetEdge3Ds())
                //{
                //    if (Geometry.Spatial.Query.Clockwise(closedPlanar3D))
                //        closedPlanar3D.Reverse();

                //    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)
                {
#if Revit2017 || Revit2018 || Revit2019 || Revit2020
                    double height = UnitUtils.ConvertToInternalUnits((panel.HighElevation() - lowElevation), DisplayUnitType.DUT_METERS);
#else
                    double height = UnitUtils.ConvertToInternalUnits((panel.HighElevation() - lowElevation), UnitTypeId.Meters);
#endif


                    parameter.Set(height);
                }

#if Revit2017 || Revit2018 || Revit2019 || Revit2020
                double levelElevation = UnitUtils.ConvertFromInternalUnits(level.Elevation, DisplayUnitType.DUT_METERS);
#else
                double levelElevation = UnitUtils.ConvertFromInternalUnits(level.Elevation, UnitTypeId.Meters);
#endif

                if (Math.Abs(lowElevation - levelElevation) > Core.Tolerance.MacroDistance)
                {
                    parameter = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);
                    if (parameter != null)
                    {
#if Revit2017 || Revit2018 || Revit2019 || Revit2020
                        parameter.Set(UnitUtils.ConvertToInternalUnits(lowElevation - levelElevation, DisplayUnitType.DUT_METERS));
#else
                        parameter.Set(UnitUtils.ConvertToInternalUnits(lowElevation - levelElevation, UnitTypeId.Meters));
#endif
                    }
                }

                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 Autodesk.Revit.DB.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 = Geometry.Spatial.Query.Project(plane, segment3D);
                    if (segment3D_Temp == null)
                    {
                        continue;
                    }

                    curveArray_Plane.Append(segment3D_Temp.ToRevit_Line());
                }

#if Revit2017 || Revit2018 || Revit2019 || Revit2020 || Revit2021
                Autodesk.Revit.DB.Floor floor = document.Create.NewFloor(curveArray_Plane, hostObjAttributes as Autodesk.Revit.DB.FloorType, level, false);
#else
                CurveLoop curveLoop = new CurveLoop();
                foreach (Curve curve in curveArray_Plane)
                {
                    curveLoop.Append(curve);
                }

                Autodesk.Revit.DB.Floor floor = Autodesk.Revit.DB.Floor.Create(document, new CurveLoop[] { curveLoop }, hostObjAttributes.Id, level.Id);
#endif

                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)
                            foreach (Geometry.Spatial.Segment3D segment3D in segment3Ds_Internal)
                            {
                                curveArray_Sloped.Append(segment3D.ToRevit_Line());

                                Geometry.Spatial.Segment3D segment3D_Temp = Geometry.Spatial.Query.Project(plane, 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 Autodesk.Revit.DB.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 Autodesk.Revit.DB.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)
            {
                if (result is Autodesk.Revit.DB.Wall && ((Autodesk.Revit.DB.Wall)result).WallType.Kind == WallKind.Curtain)
                {
                }
                else
                {
                    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);
        }
Esempio n. 5
0
        public static List <Shell> Shells(this Document document, IEnumerable <Autodesk.Revit.DB.Mechanical.Space> spaces = null, double offset = 0.1, double snapTolerance = Core.Tolerance.MacroDistance, double tolerance = Core.Tolerance.Distance)
        {
            if (document == null)
            {
                return(null);
            }

            //Collecting Space list
            List <Autodesk.Revit.DB.Mechanical.Space> spaces_Temp = new FilteredElementCollector(document).OfCategory(BuiltInCategory.OST_MEPSpaces).Cast <Autodesk.Revit.DB.Mechanical.Space>().ToList();

            if (spaces != null)
            {
                List <Autodesk.Revit.DB.Mechanical.Space> spaces_New = new List <Autodesk.Revit.DB.Mechanical.Space>();
                foreach (Autodesk.Revit.DB.Mechanical.Space space in spaces)
                {
                    int index = spaces_Temp.FindIndex(x => x.Id.IntegerValue == space.Id.IntegerValue);
                    if (index != -1)
                    {
                        spaces_New.Add(spaces_Temp[index]);
                    }
                }
                spaces_Temp = spaces_New;
            }

            if (spaces_Temp == null || spaces_Temp.Count == 0)
            {
                return(null);
            }

            //Dictionary of bottom elevations and tuples (top elevation, location 2D, Space) (Metric Units)
            Dictionary <double, List <Tuple <double, Geometry.Planar.Point2D, Autodesk.Revit.DB.Mechanical.Space> > > dictionary = new Dictionary <double, List <Tuple <double, Geometry.Planar.Point2D, Autodesk.Revit.DB.Mechanical.Space> > >();

            //Space cut elevations (Imperial Units)
            HashSet <double> cutElevations = new HashSet <double>();

            //Collecting Spaces data
            foreach (Autodesk.Revit.DB.Mechanical.Space space in spaces_Temp)
            {
                XYZ xyz = (space.Location as LocationPoint)?.Point;
                if (xyz == null)
                {
                    continue;
                }

                double elevation_Top = double.NaN;

                Parameter parameter = space.get_Parameter(BuiltInParameter.ROOM_UPPER_LEVEL);
                if (parameter != null && parameter.HasValue)
                {
                    ElementId elementId = parameter.AsElementId();
                    if (elementId != null && elementId != ElementId.InvalidElementId)
                    {
                        Level level = document.GetElement(elementId) as Level;
                        if (level != null)
                        {
#if Revit2017 || Revit2018 || Revit2019 || Revit2020
                            elevation_Top = UnitUtils.ConvertFromInternalUnits(level.Elevation, DisplayUnitType.DUT_METERS);
#else
                            elevation_Top = UnitUtils.ConvertFromInternalUnits(level.Elevation, UnitTypeId.Meters);
#endif
                        }
                    }
                }

                BoundingBoxXYZ boundingBoxXYZ = space.get_BoundingBox(null);

                if (double.IsNaN(elevation_Top) && boundingBoxXYZ != null)
                {
#if Revit2017 || Revit2018 || Revit2019 || Revit2020
                    elevation_Top = UnitUtils.ConvertFromInternalUnits(boundingBoxXYZ.Max.Z, DisplayUnitType.DUT_METERS);
#else
                    elevation_Top = UnitUtils.ConvertFromInternalUnits(boundingBoxXYZ.Max.Z, UnitTypeId.Meters);
#endif
                }

                double elevation_Bottom = double.NaN;

                if (boundingBoxXYZ != null)
                {
#if Revit2017 || Revit2018 || Revit2019 || Revit2020
                    elevation_Bottom = UnitUtils.ConvertFromInternalUnits(boundingBoxXYZ.Min.Z, DisplayUnitType.DUT_METERS);
#else
                    elevation_Bottom = UnitUtils.ConvertFromInternalUnits(boundingBoxXYZ.Min.Z, UnitTypeId.Meters);
#endif
                }


                if (double.IsNaN(elevation_Bottom))
                {
                    ElementId elementId = space.LevelId;
                    if (elementId != null && elementId != ElementId.InvalidElementId)
                    {
                        Level level = document.GetElement(elementId) as Level;
#if Revit2017 || Revit2018 || Revit2019 || Revit2020
                        elevation_Bottom = UnitUtils.ConvertFromInternalUnits(level.Elevation, DisplayUnitType.DUT_METERS);
#else
                        elevation_Bottom = UnitUtils.ConvertFromInternalUnits(level.Elevation, UnitTypeId.Meters);
#endif
                    }
                }

                Point3D point3D = Geometry.Revit.Convert.ToSAM(xyz);
                if (point3D == null)
                {
                    continue;
                }

                Geometry.Planar.Point2D point2D = Geometry.Spatial.Plane.WorldXY.Convert(point3D);
                if (point2D == null)
                {
                    continue;
                }

#if Revit2017 || Revit2018 || Revit2019 || Revit2020
                cutElevations.Add(UnitUtils.ConvertToInternalUnits(elevation_Bottom + offset, DisplayUnitType.DUT_METERS));
#else
                cutElevations.Add(UnitUtils.ConvertToInternalUnits(elevation_Bottom + offset, UnitTypeId.Meters));
#endif

                if (!dictionary.TryGetValue(elevation_Bottom, out List <Tuple <double, Geometry.Planar.Point2D, Autodesk.Revit.DB.Mechanical.Space> > tuples))
                {
                    tuples = new List <Tuple <double, Geometry.Planar.Point2D, Autodesk.Revit.DB.Mechanical.Space> >();
                    dictionary[elevation_Bottom] = tuples;
                }
                tuples.Add(new Tuple <double, Geometry.Planar.Point2D, Autodesk.Revit.DB.Mechanical.Space>(elevation_Top, point2D, space));
            }

            //Collecting Revit Walls
            List <Autodesk.Revit.DB.Wall> walls = new FilteredElementCollector(document).OfClass(typeof(Autodesk.Revit.DB.Wall)).Cast <Autodesk.Revit.DB.Wall>().ToList();
            if (walls == null || walls.Count == 0)
            {
                return(null);
            }

            //Converting Revit Walls to SAM Panels
            List <Panel> panels = new List <Panel>();
            foreach (Autodesk.Revit.DB.Wall wall in walls)
            {
                BoundingBoxXYZ boundingBoxXYZ = wall?.get_BoundingBox(null);
                if (boundingBoxXYZ == null)
                {
                    continue;
                }

                bool valid = false;
                foreach (double cutElevation in cutElevations)
                {
                    if (boundingBoxXYZ.Max.Z >= cutElevation && boundingBoxXYZ.Min.Z <= cutElevation)
                    {
                        valid = true;
                        break;
                    }
                }

                if (!valid)
                {
                    continue;
                }

                List <Panel> panels_Wall = wall.ToSAM(new Core.Revit.ConvertSettings(true, false, false));
                if (panels_Wall == null || panels_Wall.Count == 0)
                {
                    continue;
                }

                panels.AddRange(panels_Wall);
            }

            if (panels == null || panels.Count == 0)
            {
                return(null);
            }

            List <Shell> result = new List <Shell>();

            //Inerating through elevations and Spaces data
            foreach (KeyValuePair <double, List <Tuple <double, Geometry.Planar.Point2D, Autodesk.Revit.DB.Mechanical.Space> > > keyValuePair in dictionary)
            {
                double elevation_Bottom = keyValuePair.Key;
                double elevation_Cut    = elevation_Bottom + offset;

                Geometry.Spatial.Plane plane_Cut = Geometry.Spatial.Plane.WorldXY.GetMoved(new Vector3D(0, 0, elevation_Cut)) as Geometry.Spatial.Plane;

                List <Geometry.Planar.Segment2D> segment2Ds = new List <Geometry.Planar.Segment2D>();
                foreach (Panel panel in panels)
                {
                    IClosedPlanar3D closedPlanar3D = panel?.GetFace3D()?.GetExternalEdge3D();
                    if (closedPlanar3D == null)
                    {
                        continue;
                    }

                    PlanarIntersectionResult planarIntersectionResult = plane_Cut.PlanarIntersectionResult(closedPlanar3D);
                    if (planarIntersectionResult == null || !planarIntersectionResult.Intersecting)
                    {
                        continue;
                    }

                    List <Geometry.Planar.ISegmentable2D> segmentable2Ds_Temp = planarIntersectionResult.GetGeometry2Ds <Geometry.Planar.ISegmentable2D>();
                    if (segmentable2Ds_Temp == null || segmentable2Ds_Temp.Count == 0)
                    {
                        continue;
                    }

                    segmentable2Ds_Temp?.ForEach(x => segment2Ds.AddRange(x.GetSegments()));
                }

                if (panels == null || panels.Count == 0)
                {
                    continue;
                }

                segment2Ds = Geometry.Planar.Query.Split(segment2Ds, tolerance);

                segment2Ds = Geometry.Planar.Query.Snap(segment2Ds, true, snapTolerance);

                List <Tuple <Geometry.Planar.BoundingBox2D, Geometry.Planar.Polygon2D> > tuples_Polygon2D = Geometry.Planar.Create.Polygon2Ds(segment2Ds)?.ConvertAll(x => new Tuple <Geometry.Planar.BoundingBox2D, Geometry.Planar.Polygon2D>(x.GetBoundingBox(tolerance), x));
                if (tuples_Polygon2D == null || tuples_Polygon2D.Count == 0)
                {
                    continue;
                }

                Geometry.Spatial.Plane plane_Bottom = Geometry.Spatial.Plane.WorldXY.GetMoved(new Vector3D(0, 0, elevation_Bottom)) as Geometry.Spatial.Plane;

                List <Tuple <double, Geometry.Planar.Point2D, Autodesk.Revit.DB.Mechanical.Space> > tuples_Space = keyValuePair.Value;
                while (tuples_Space.Count > 0)
                {
                    Tuple <double, Geometry.Planar.Point2D, Autodesk.Revit.DB.Mechanical.Space> tuple = tuples_Space[0];
                    tuples_Space.RemoveAt(0);

                    Geometry.Spatial.Plane plane_Top = Geometry.Spatial.Plane.WorldXY.GetMoved(new Vector3D(0, 0, tuple.Item1)) as Geometry.Spatial.Plane;

                    Geometry.Planar.Point2D point2D = tuple.Item2;

                    List <Tuple <Geometry.Planar.BoundingBox2D, Geometry.Planar.Polygon2D> > tuples_Polygon2D_External = tuples_Polygon2D.FindAll(x => x.Item1.Inside(point2D, tolerance)).FindAll(x => x.Item2.Inside(point2D, tolerance));
                    tuples_Polygon2D_External.Sort((x, y) => x.Item1.GetArea().CompareTo(y.Item1.GetArea()));

                    Tuple <Geometry.Planar.BoundingBox2D, Geometry.Planar.Polygon2D> tuple_Polygon2D_External = tuples_Polygon2D_External.FirstOrDefault();
                    if (tuple_Polygon2D_External == null)
                    {
                        continue;
                    }

                    List <Tuple <Geometry.Planar.BoundingBox2D, Geometry.Planar.Polygon2D> > tuples_Polygon2D_Internal = new List <Tuple <Geometry.Planar.BoundingBox2D, Geometry.Planar.Polygon2D> >();
                    foreach (Tuple <Geometry.Planar.BoundingBox2D, Geometry.Planar.Polygon2D> tuple_Polygon2D in tuples_Polygon2D)
                    {
                        if (tuple_Polygon2D == tuple_Polygon2D_External)
                        {
                            continue;
                        }

                        if (tuple_Polygon2D_External.Item1.Inside(tuple_Polygon2D.Item1, tolerance) && tuple_Polygon2D_External.Item2.Inside(tuple_Polygon2D.Item2, tolerance))
                        {
                            tuples_Polygon2D_Internal.Add(tuple_Polygon2D);
                        }
                    }

                    List <Geometry.Planar.Face2D> face2Ds = Geometry.Planar.Query.Difference(new Geometry.Planar.Face2D(tuple_Polygon2D_External.Item2), tuples_Polygon2D_Internal.ConvertAll(x => new Geometry.Planar.Face2D(x.Item2)), tolerance);
                    if (face2Ds == null || face2Ds.Count == 0)
                    {
                        continue;
                    }


                    foreach (Geometry.Planar.Face2D face2D in face2Ds)
                    {
                        tuples_Space.RemoveAll(x => face2D.Inside(x.Item2, tolerance));
                    }

                    List <Face3D> face3Ds = new List <Face3D>();
                    face3Ds.AddRange(face2Ds.ConvertAll(x => new Face3D(plane_Bottom, x)));
                    face3Ds.AddRange(face2Ds.ConvertAll(x => new Face3D(plane_Top, x)));

                    List <Shell> shells_Temp = Geometry.Spatial.Create.Shells(face3Ds, new double[] { elevation_Bottom }, offset, snapTolerance: snapTolerance, tolerance_Distance: tolerance);
                    if (shells_Temp == null || shells_Temp.Count == 0)
                    {
                        continue;
                    }

                    result.AddRange(shells_Temp);
                }
            }

            return(result);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        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);
        }
Esempio n. 9
0
        /// <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);
        }
Esempio n. 10
0
        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);
        }
Esempio n. 11
0
        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);
            }
        }
Esempio n. 12
0
        public static Panel ToSAM(this EnergyAnalysisSurface energyAnalysisSurface, Core.Revit.ConvertSettings convertSettings, Shell shell = null, double silverSpacing = Core.Tolerance.MacroDistance, double tolerance = Core.Tolerance.Distance)
        {
            if (energyAnalysisSurface == null)
            {
                return(null);
            }

            Panel result = convertSettings?.GetObject <Panel>(energyAnalysisSurface.Id);

            if (result != null)
            {
                return(result);
            }

            Document document = energyAnalysisSurface.Document;

            Polygon3D polygon3D = energyAnalysisSurface.GetPolyloop().ToSAM();

            if (polygon3D == null)
            {
                return(null);
            }

            if (shell != null)
            {
                Geometry.Spatial.Plane plane = polygon3D.GetPlane();

                if (plane != null)
                {
                    Point3D point3D = polygon3D.InternalPoint3D();
                    if (point3D != null)
                    {
                        Vector3D normal = shell.Normal(polygon3D.InternalPoint3D(), true, silverSpacing, tolerance);
                        if (!normal.SameHalf(plane.AxisZ))
                        {
                            plane.FlipZ();
                            polygon3D = new Polygon3D(plane, polygon3D.GetPoints().ConvertAll(x => plane.Convert(x)));
                        }
                    }
                }
            }

            HostObject hostObject = Core.Revit.Query.Element(document, energyAnalysisSurface.CADObjectUniqueId, energyAnalysisSurface.CADLinkUniqueId) as HostObject;

            if (hostObject == null)
            {
                return(new Panel(null, PanelType.Air, new Face3D(polygon3D)));
            }

            ElementId elementId_Type = hostObject.GetTypeId();

            if (elementId_Type == null || elementId_Type == ElementId.InvalidElementId)
            {
                return(null);
            }

            PanelType    panelType    = Query.PanelType(hostObject);
            Construction construction = ((HostObjAttributes)hostObject.Document.GetElement(elementId_Type)).ToSAM(convertSettings);

            if (construction == null)
            {
                construction = Analytical.Query.DefaultConstruction(panelType); //Default Construction
            }
            PanelType panelType_Temp = Query.PanelType(construction);

            if (panelType_Temp != PanelType.Undefined)
            {
                panelType = panelType_Temp;
            }

            Face3D face3D = new Face3D(polygon3D);

            result = new Panel(construction, panelType, face3D);

            IEnumerable <EnergyAnalysisOpening> energyAnalysisOpenings = energyAnalysisSurface.GetAnalyticalOpenings();

            if (energyAnalysisOpenings != null && energyAnalysisOpenings.Count() != 0)
            {
                foreach (EnergyAnalysisOpening energyAnalysisOpening in energyAnalysisOpenings)
                {
                    Aperture aperture = energyAnalysisOpening.ToSAM(convertSettings);
                    if (aperture != null)
                    {
                        result.AddAperture(aperture);
                    }
                }
            }

            result.UpdateParameterSets(energyAnalysisSurface, ActiveSetting.Setting.GetValue <Core.TypeMap>(Core.Revit.ActiveSetting.Name.ParameterMap));

            convertSettings?.Add(energyAnalysisSurface.Id, result);

            return(result);
        }
Esempio n. 13
0
        public static List <Panel> SimilarPanels(this Geometry.Spatial.Face3D face3D, Dictionary <Panel, Geometry.Spatial.Face3D> panelsDictionary, double distanceTolerance = Tolerance.MacroDistance, double coplanarTolerance = Tolerance.MacroDistance)
        {
            if (face3D == null || panelsDictionary == null)
            {
                return(null);
            }

            Geometry.Spatial.Plane plane = face3D.GetPlane();

            Geometry.Planar.IClosed2D closed2D_1       = Geometry.Spatial.Query.Convert(plane, face3D.GetExternalEdge3D());
            Geometry.Planar.Point2D   point2D_Internal = closed2D_1.GetInternalPoint2D();

            double area = face3D.GetArea();

            List <Tuple <Panel, double> > tuples = new List <Tuple <Panel, double> >();

            foreach (KeyValuePair <Panel, Geometry.Spatial.Face3D> keyValuePair in panelsDictionary)
            {
                if (keyValuePair.Value == null)
                {
                    continue;
                }

                Geometry.Spatial.Face3D face3D_Temp = keyValuePair.Value;
                Geometry.Spatial.Plane  plane_Temp  = face3D_Temp.GetPlane();

                if (!plane.Coplanar(plane_Temp, coplanarTolerance))
                {
                    continue;
                }

                Geometry.Spatial.Point3D point3D_Origin  = plane_Temp.Origin;
                Geometry.Spatial.Point3D point3D_Project = Geometry.Spatial.Query.Project(plane, point3D_Origin);
                if (point3D_Origin.Distance(point3D_Project) > distanceTolerance)
                {
                    continue;
                }

                Geometry.Planar.IClosed2D closed2D_2 = Geometry.Spatial.Query.Convert(plane, face3D_Temp.GetExternalEdge3D());
                if (closed2D_2.Inside(point2D_Internal))
                {
                    tuples.Add(new Tuple <Panel, double>(keyValuePair.Key, Math.Abs(keyValuePair.Value.GetArea() - area)));
                }
            }

            if (tuples == null || tuples.Count == 0)
            {
                return(null);
            }

            if (tuples.Count == 1)
            {
                return new List <Panel>()
                       {
                           tuples.First().Item1
                       }
            }
            ;

            tuples.Sort((x, y) => x.Item2.CompareTo(y.Item2));

            double       areatolerance = distanceTolerance * 10;
            List <Panel> result        = tuples.FindAll(x => x.Item2 < areatolerance).ConvertAll(x => x.Item1);

            if (result != null && result.Count > 0)
            {
                return(result);
            }

            return(new List <Panel>()
            {
                tuples.First().Item1
            });
        }
    }