Esempio n. 1
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. 2
0
        protected override void TrySolveInstance(IGH_DataAccess dataAccess)
        {
            bool run = false;

            if (!dataAccess.GetData(3, ref run) || !run)
            {
                return;
            }

            double maxDistance = 0.5;

            if (!dataAccess.GetData(2, ref maxDistance))
            {
                return;
            }

            RhinoInside.Revit.GH.Types.Level level_GH = null;
            if (!dataAccess.GetData(0, ref level_GH))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid data");
                return;
            }

            RhinoInside.Revit.GH.Types.Level referenceLevel_GH = null;
            if (!dataAccess.GetData(1, 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;
            }

            double elevation          = UnitUtils.ConvertFromInternalUnits(level.Elevation, DisplayUnitType.DUT_METERS);
            double referenceElevation = UnitUtils.ConvertFromInternalUnits(referenceLevel.Elevation, DisplayUnitType.DUT_METERS);

            Document document = level.Document;

            IEnumerable <Wall> walls_All = new FilteredElementCollector(document).OfClass(typeof(Wall)).Cast <Wall>();

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

            ConvertSettings convertSettings = new ConvertSettings(true, true, true);

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

            foreach (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)
            {
                Wall wall = document.GetElement(keyValuePair.Value) as 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);
            }

            dataAccess.SetDataList(0, result);
        }