Пример #1
0
        public static PanelType PanelType(this Geometry.Spatial.Vector3D normal)
        {
            if (normal == null)
            {
                return(Analytical.PanelType.Undefined);
            }

            double value;

            value = normal.Unit.DotProduct(Geometry.Spatial.Vector3D.WorldZ);
            if (System.Math.Abs(value) <= Core.Revit.Tolerance.Tilt)
            {
                return(Analytical.PanelType.Wall);
            }

            if (value < 0)
            {
                value = normal.Unit.DotProduct(Geometry.Spatial.Vector3D.WorldY);
                if (System.Math.Abs(value) <= Core.Revit.Tolerance.Tilt)
                {
                    return(Analytical.PanelType.Floor);
                }
            }

            return(Analytical.PanelType.Roof);
        }
        protected override void TrySolveInstance(IGH_DataAccess dataAccess)
        {
            Document document = RhinoInside.Revit.Revit.ActiveDBDocument;

            if (document == null)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid data");
                return;
            }

            Panel panel = null;

            if (!dataAccess.GetData(0, ref panel))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid data");
                return;
            }

            Geometry.Spatial.Vector3D normal = panel.Normal;

            PanelType panelType = panel.PanelType;

            if (panelType == PanelType.Air || panelType == PanelType.Undefined)
            {
                dataAccess.SetData(0, new GooPanel(new Panel(panel)));
                dataAccess.SetData(1, Analytical.Revit.Convert.ToRevit_HostObjAttributes(panel, document, new Core.Revit.ConvertSettings(false, true, false)));
                return;
            }

            PanelType panelType_Normal = Analytical.Revit.Query.PanelType(normal);

            if (panelType_Normal == PanelType.Undefined || panelType.PanelGroup() == panelType_Normal.PanelGroup())
            {
                dataAccess.SetData(0, new GooPanel(new Panel(panel)));
                dataAccess.SetData(1, Analytical.Revit.Convert.ToRevit_HostObjAttributes(panel, document, new Core.Revit.ConvertSettings(false, true, false)));
                return;
            }

            if (panelType.PanelGroup() == PanelGroup.Floor || panelType.PanelGroup() == PanelGroup.Roof)
            {
                double value = normal.Unit.DotProduct(Geometry.Spatial.Vector3D.WorldY);
                if (Math.Abs(value) <= Core.Revit.Tolerance.Tilt)
                {
                    dataAccess.SetData(0, new GooPanel(new Panel(panel)));
                    dataAccess.SetData(1, Analytical.Revit.Convert.ToRevit_HostObjAttributes(panel, document, new Core.Revit.ConvertSettings(false, true, false)));
                    return;
                }
            }

            HostObjAttributes hostObjAttributes = Analytical.Revit.Modify.DuplicateByType(document, panelType_Normal, panel.Construction) as HostObjAttributes;

            if (hostObjAttributes == null)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, string.Format("Skipped - Could not duplicate construction for {0} panel (Guid: {1}).", panel.Name, panel.Guid));
                return;
            }

            dataAccess.SetData(0, new Panel(panel, panelType_Normal));
            dataAccess.SetData(1, hostObjAttributes);
        }
Пример #3
0
        public static BuiltInCategory BuiltInCategory(this Geometry.Spatial.Vector3D normal)
        {
            PanelType panelType = Analytical.Query.PanelType(normal, Core.Revit.Tolerance.Tilt);

            if (panelType == Analytical.PanelType.Undefined)
            {
                return(Autodesk.Revit.DB.BuiltInCategory.INVALID);
            }

            return(BuiltInCategory(panelType));
        }
Пример #4
0
        public static HostObjAttributes ToRevit(this Construction construction, Document document, PanelType panelType, Geometry.Spatial.Vector3D normal, Core.Revit.ConvertSettings convertSettings)
        {
            if (construction == null && panelType == PanelType.Air && normal != null)
            {
                construction = Query.DefaultAirConstruction(normal.PanelType().PanelGroup());
            }

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

            HostObjAttributes result = null;

            if (panelType != PanelType.Shade)
            {
                result = convertSettings?.GetObject <HostObjAttributes>(construction.Guid);
            }
            else
            {
                List <HostObjAttributes> hostObjAttributes = convertSettings?.GetObjects <HostObjAttributes>(construction.Guid);
                if (hostObjAttributes != null && hostObjAttributes.Count != 0)
                {
                    if (normal.PanelType().PanelGroup() == PanelGroup.Wall)
                    {
                        result = hostObjAttributes.Find(x => x is Autodesk.Revit.DB.WallType);
                    }
                    else
                    {
                        result = hostObjAttributes.Find(x => !(x is Autodesk.Revit.DB.WallType));
                    }
                }
            }

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

            FilteredElementCollector filteredElementCollector = Query.FilteredElementCollector(document, panelType)?.OfClass(typeof(HostObjAttributes));

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

            if (normal != null)
            {
                if (panelType == PanelType.Air || panelType == PanelType.Shade)
                {
                    BuiltInCategory builtInCategory = normal.BuiltInCategory();
                    if (builtInCategory == BuiltInCategory.OST_Walls)
                    {
                        filteredElementCollector.OfCategory(builtInCategory);
                    }
                }
            }

            //FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document).OfClass(typeof(HostObjAttributes));

            //BuiltInCategory builtInCategory = panelType.BuiltInCategory();
            //if (normal != null && (builtInCategory == BuiltInCategory.INVALID || panelType == PanelType.Shade))
            //    builtInCategory = normal.BuiltInCategory();

            //if (builtInCategory != BuiltInCategory.INVALID)
            //    filteredElementCollector.OfCategory(builtInCategory);


            string familyName_Source = null;
            string typeName_Source   = null;

            if (!Core.Revit.Query.TryGetFamilyNameAndTypeName(construction.Name, out familyName_Source, out typeName_Source))
            {
                return(null);
            }

            foreach (HostObjAttributes hostObjAttributes in filteredElementCollector)
            {
                string fullName = Core.Revit.Query.FullName(hostObjAttributes);

                string familyName = null;
                string typeName   = null;
                if (!Core.Revit.Query.TryGetFamilyNameAndTypeName(fullName, out familyName, out typeName))
                {
                    continue;
                }

                if (fullName != null && fullName.Equals(construction.Name))
                {
                    return(hostObjAttributes);
                }

                if (!string.IsNullOrWhiteSpace(familyName) && !string.IsNullOrWhiteSpace(familyName_Source))
                {
                    continue;
                }

                if (typeName.Equals(typeName_Source))
                {
                    result = hostObjAttributes;
                }
            }

            convertSettings?.Add(construction.Guid, result);

            return(result);
        }
Пример #5
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);
        }
Пример #6
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);
        }
Пример #7
0
        protected override void TrySolveInstance(IGH_DataAccess dataAccess)
        {
            int index = -1;

            Document document = RhinoInside.Revit.Revit.ActiveDBDocument;

            if (document == null)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid data");
                return;
            }

            Core.SAMObject sAMObject = null;
            index = Params.IndexOfInputParam("_analytical");
            if (index == -1 || !dataAccess.GetData(index, ref sAMObject))
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid data");
                return;
            }

            List <Panel> panels = null;

            if (sAMObject is Panel)
            {
                panels = new List <Panel>()
                {
                    (Panel)sAMObject
                };
            }
            else if (sAMObject is AdjacencyCluster)
            {
                panels = ((AdjacencyCluster)sAMObject).GetPanels();
            }
            else if (sAMObject is AnalyticalModel)
            {
                panels = ((AnalyticalModel)sAMObject).GetPanels();
            }

            if (panels == null || panels.Count == 0)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid data");
                return;
            }

            StartTransaction(document);

            List <ElementType> elementTypes = new List <ElementType>();

            for (int i = 0; i < panels.Count; i++)
            {
                Panel panel = panels[i];
                if (panel == null)
                {
                    continue;
                }

                Geometry.Spatial.Vector3D normal = panel.Normal;
                PanelType panelType = panel.PanelType;

                if (panelType == PanelType.Air || panelType == PanelType.Undefined)
                {
                    panels[i] = Create.Panel(panel);
                    ElementType elementType = Analytical.Revit.Convert.ToRevit_HostObjAttributes(panel, document, new Core.Revit.ConvertSettings(false, true, false));
                    if (elementType != null && elementTypes.Find(x => x.Id == elementType.Id) == null)
                    {
                        elementTypes.Add(elementType);
                    }

                    continue;
                }

                PanelType panelType_Normal = Analytical.Revit.Query.PanelType(normal);
                if (panelType_Normal == PanelType.Undefined || panelType.PanelGroup() == panelType_Normal.PanelGroup())
                {
                    panels[i] = Create.Panel(panel);
                    ElementType elementType = Analytical.Revit.Convert.ToRevit_HostObjAttributes(panel, document, new Core.Revit.ConvertSettings(false, true, false));
                    if (elementType != null && elementTypes.Find(x => x.Id == elementType.Id) == null)
                    {
                        elementTypes.Add(elementType);
                    }

                    continue;
                }

                if (panelType.PanelGroup() == PanelGroup.Floor || panelType.PanelGroup() == PanelGroup.Roof)
                {
                    double value = normal.Unit.DotProduct(Geometry.Spatial.Vector3D.WorldY);
                    if (Math.Abs(value) <= Core.Revit.Tolerance.Tilt)
                    {
                        panels[i] = Create.Panel(panel);
                        ElementType elementType = Analytical.Revit.Convert.ToRevit_HostObjAttributes(panel, document, new Core.Revit.ConvertSettings(false, true, false));
                        if (elementType != null && elementTypes.Find(x => x.Id == elementType.Id) == null)
                        {
                            elementTypes.Add(elementType);
                        }

                        continue;
                    }
                }

                HostObjAttributes hostObjAttributes = Analytical.Revit.Modify.DuplicateByType(document, panelType_Normal, panel.Construction) as HostObjAttributes;
                if (hostObjAttributes == null)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, string.Format("Skipped - Could not duplicate construction for {0} panel (Guid: {1}).", panel.Name, panel.Guid));
                    continue;
                }

                panels[i] = Create.Panel(panel, panelType_Normal);

                if (elementTypes.Find(x => x.Id == hostObjAttributes.Id) == null)
                {
                    elementTypes.Add(hostObjAttributes);
                }
            }


            int index_Analytical = Params.IndexOfOutputParam("analytical");

            if (index_Analytical != -1)
            {
                if (sAMObject is Panel)
                {
                    dataAccess.SetData(index_Analytical, panels?.FirstOrDefault());
                }
                else if (sAMObject is AnalyticalModel)
                {
                    AnalyticalModel analyticalModel = new AnalyticalModel((AnalyticalModel)sAMObject);
                    panels.ForEach(x => analyticalModel.AddPanel(x));
                    dataAccess.SetData(index_Analytical, analyticalModel);
                }
                else if (sAMObject is AdjacencyCluster)
                {
                    AdjacencyCluster adjacencyCluster = new AdjacencyCluster((AdjacencyCluster)sAMObject);
                    panels.ForEach(x => adjacencyCluster.AddObject(x));
                    dataAccess.SetData(index_Analytical, adjacencyCluster);
                }
            }

            int index_ElementType = Params.IndexOfOutputParam("elementType");

            if (index_ElementType != -1)
            {
                dataAccess.SetDataList(index_ElementType, elementTypes);
            }
        }
Пример #8
0
        /// <summary>
        /// Converts Face3D to Wall
        /// </summary>
        /// <param name="face3D">SAM Geometry Face3D</param>
        /// <param name="document">Revit Document</param>
        /// <param name="wallType">Revit WallType. Default WallType will be used if null</param>
        /// <param name="level">Revit Level. Low Level for Face3D will be used if null</param>
        /// <returns>Revit Wall</returns>
        public static Autodesk.Revit.DB.Wall ToRevit_Wall(this Geometry.Spatial.Face3D face3D, Document document, Autodesk.Revit.DB.WallType wallType = null, Level level = null)
        {
            if (face3D == null || document == null)
            {
                return(null);
            }

            if (face3D.GetArea() < Core.Tolerance.MacroDistance)
            {
                return(null);
            }

            Geometry.Spatial.Vector3D normal = face3D.GetPlane().Normal;
            if (normal == null)
            {
                return(null);
            }

            if (wallType == null)
            {
                ElementId elementId = document.Settings.Categories.get_Item(BuiltInCategory.OST_Walls).Id;
                if (elementId != null && elementId != ElementId.InvalidElementId)
                {
                    elementId = document.GetDefaultFamilyTypeId(elementId);
                    if (elementId != null && elementId != ElementId.InvalidElementId)
                    {
                        wallType = document.GetElement(elementId) as Autodesk.Revit.DB.WallType;
                    }
                }
            }

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

            if (level == null)
            {
                double elevation = Geometry.Revit.Convert.ToRevit(face3D.GetBoundingBox().Min).Z;
                level = Core.Revit.Query.LowLevel(document, elevation);
            }

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

            List <CurveLoop> curveLoops = Geometry.Revit.Convert.ToRevit(face3D);

            curveLoops?.RemoveAll(x => x == null);
            if (curveLoops == null || curveLoops.Count == 0)
            {
                return(null);
            }

            XYZ xyz_Normal = Geometry.Revit.Convert.ToRevit(normal, false);

            if (curveLoops.Count == 1)
            {
                return(Autodesk.Revit.DB.Wall.Create(document, curveLoops[0].ToList(), wallType.Id, level.Id, false, xyz_Normal));
            }

            //The Wall.Create method requires the curveLoops to be in either all counter-clockwise direction or all clockwise direction.
            for (int i = 0; i < curveLoops.Count; i++)
            {
                if (curveLoops[i].IsCounterclockwise(xyz_Normal))
                {
                    curveLoops[i].Flip();
                }
            }

            List <Curve> curves = new List <Curve>();


            //According to https://forums.autodesk.com/t5/revit-api-forum/wall-create-by-profile/td-p/8961085
            //CurveLoops have to be organised in correct order. Hypothesis :
            //If the curveLoop contains 1 (or more) vertical Line(s), the vertical line should be the first to add to the List.
            foreach (CurveLoop curveLoop in curveLoops)
            {
                List <Curve> curves_Temp = curveLoop?.ToList();
                if (curveLoop == null)
                {
                    continue;
                }

                List <Curve> curves_Postponed = new List <Curve>();

                int startindex = 0;
                while (startindex < curves_Temp.Count)
                {
                    Curve curve = curves_Temp[startindex];
                    if (!(curve is Line))
                    {
                        curves_Postponed.Add(curve);
                        startindex++;
                        continue;
                    }
                    XYZ dir = curve.GetEndPoint(1).Subtract(curve.GetEndPoint(0)).Normalize();
                    if (!dir.IsAlmostEqualTo(XYZ.BasisZ) && !dir.IsAlmostEqualTo(XYZ.BasisZ.Negate()))
                    {
                        curves_Postponed.Add(curve);
                        startindex++;
                    }
                    else
                    {
                        break;
                    }
                }

                for (int i = startindex; i < curves_Temp.Count; i++)
                {
                    Curve curve = curves_Temp[i];
                    curves.Add(curve);
                }

                if (curves_Postponed.Count > 0)
                {
                    curves.AddRange(curves_Postponed);
                }
            }

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

            return(Autodesk.Revit.DB.Wall.Create(document, curves, wallType.Id, level.Id, false, xyz_Normal));
        }