Exemple #1
0
        /***************************************************/
        /****              Public methods               ****/
        /***************************************************/

        public static Floor ToRevitFloor(this oM.Physical.Elements.Floor floor, Document document, RevitSettings settings = null, Dictionary <Guid, List <int> > refObjects = null)
        {
            if (floor == null || floor.Construction == null || document == null)
            {
                return(null);
            }

            Floor revitFloor = refObjects.GetValue <Floor>(document, floor.BHoM_Guid);

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

            PlanarSurface planarSurface = floor.Location as PlanarSurface;

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

            settings = settings.DefaultIfNull();

            FloorType floorType = floor.Construction?.ToRevitElementType(document, new List <BuiltInCategory> {
                BuiltInCategory.OST_Floors
            }, settings, refObjects) as FloorType;

            if (floorType == null)
            {
                floorType = floor.ElementType(document, settings);
            }

            if (floorType == null)
            {
                Compute.ElementTypeNotFoundWarning(floor);
                return(null);
            }

            double bottomElevation = floor.Location.IBounds().Min.Z;
            Level  level           = document.LevelBelow(bottomElevation.FromSI(UnitType.UT_Length), settings);

            oM.Geometry.Plane sketchPlane = new oM.Geometry.Plane {
                Origin = new BH.oM.Geometry.Point {
                    Z = bottomElevation
                }, Normal = Vector.ZAxis
            };
            ICurve     curve      = planarSurface.ExternalBoundary.IProject(sketchPlane);
            CurveArray curveArray = Create.CurveArray(curve.IToRevitCurves());

            BH.oM.Geometry.Plane slabPlane = planarSurface.FitPlane();
            if (1 - Math.Abs(Vector.ZAxis.DotProduct(slabPlane.Normal)) <= settings.AngleTolerance)
            {
                revitFloor = document.Create.NewFloor(curveArray, floorType, level, true);
            }
            else
            {
                Vector normal = slabPlane.Normal;
                if (normal.Z < 0)
                {
                    normal = -slabPlane.Normal;
                }

                double angle = normal.Angle(Vector.ZAxis);
                double tan   = Math.Tan(angle);

                XYZ dir = normal.Project(oM.Geometry.Plane.XY).ToRevit().Normalize();
                BH.oM.Geometry.Line ln = slabPlane.PlaneIntersection(sketchPlane);
                XYZ start = ln.ClosestPoint(curveArray.get_Item(0).GetEndPoint(0).PointFromRevit(), true).ToRevit();
                Autodesk.Revit.DB.Line line = Autodesk.Revit.DB.Line.CreateBound(start, start + dir);

                revitFloor = document.Create.NewSlab(curveArray, level, line, -tan, true);
                revitFloor.SetParameter(BuiltInParameter.ELEM_TYPE_PARAM, floorType.Id);
            }

            revitFloor.CheckIfNullPush(floor);
            if (revitFloor == null)
            {
                return(null);
            }

            document.Regenerate();

            if (planarSurface.InternalBoundaries != null)
            {
                foreach (ICurve hole in planarSurface.InternalBoundaries)
                {
                    document.Create.NewOpening(revitFloor, Create.CurveArray(hole.IProject(slabPlane).IToRevitCurves()), true);
                }
            }

            foreach (BH.oM.Physical.Elements.IOpening opening in floor.Openings)
            {
                PlanarSurface openingLocation = opening.Location as PlanarSurface;
                if (openingLocation == null)
                {
                    BH.Engine.Reflection.Compute.RecordWarning(String.Format("Conversion of a floor opening to Revit failed because its location is not a planar surface. Floor BHoM_Guid: {0}, Opening BHoM_Guid: {1}", floor.BHoM_Guid, opening.BHoM_Guid));
                    continue;
                }

                document.Create.NewOpening(revitFloor, Create.CurveArray(openingLocation.ExternalBoundary.IToRevitCurves()), true);

                if (!(opening is BH.oM.Physical.Elements.Void))
                {
                    BH.Engine.Reflection.Compute.RecordWarning(String.Format("Revit allows only void openings in floors, therefore the BHoM opening of type {0} has been converted to a void opening. Floor BHoM_Guid: {1}, Opening BHoM_Guid: {2}", opening.GetType().Name, floor.BHoM_Guid, opening.BHoM_Guid));
                }
            }

            double offset = revitFloor.LookupParameterDouble(BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM);

            // Copy parameters from BHoM object to Revit element
            revitFloor.CopyParameters(floor, settings);

            // Update the offset in case the level had been overwritten.
            if (revitFloor.LevelId.IntegerValue != level.Id.IntegerValue)
            {
                Level newLevel = document.GetElement(revitFloor.LevelId) as Level;
                offset += (level.ProjectElevation - newLevel.ProjectElevation).ToSI(UnitType.UT_Length);
            }

            revitFloor.SetParameter(BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM, offset);

            refObjects.AddOrReplace(floor, revitFloor);
            return(revitFloor);
        }
Exemple #2
0
        /***************************************************/

        public static FamilyInstance ToRevitFamilyInstance(this IFramingElement framingElement, Document document, RevitSettings settings = null, Dictionary <Guid, List <int> > refObjects = null)
        {
            if (framingElement == null || document == null)
            {
                return(null);
            }

            FamilyInstance familyInstance = refObjects.GetValue <FamilyInstance>(document, framingElement.BHoM_Guid);

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

            settings = settings.DefaultIfNull();

            if (framingElement.Location == null)
            {
                BH.Engine.Reflection.Compute.RecordError(String.Format("Revit element could not be created because the driving curve of a BHoM object is null. BHoM_Guid: {0}", framingElement.BHoM_Guid));
                return(null);
            }

            if (!framingElement.Location.IIsPlanar())
            {
                BH.Engine.Reflection.Compute.RecordError(string.Format("Revit framing does only support planar curves, element could not be created. BHoM_Guid: {0}", framingElement.BHoM_Guid));
                return(null);
            }

            Curve revitCurve = framingElement.Location.IToRevit();

            if (revitCurve == null)
            {
                BH.Engine.Reflection.Compute.RecordError(string.Format("Revit element could not be created because of curve conversion issues. BHoM_Guid: {0}", framingElement.BHoM_Guid));
                return(null);
            }

            Level level = document.LevelBelow(framingElement.Location, settings);

            FamilySymbol familySymbol = framingElement.Property.ToRevitElementType(document, framingElement.BuiltInCategories(document), settings, refObjects);

            if (familySymbol == null)
            {
                familySymbol = framingElement.ElementType(document, settings) as FamilySymbol;
            }

            if (familySymbol == null)
            {
                Compute.ElementTypeNotFoundWarning(framingElement);
                return(null);
            }

            FamilyPlacementType familyPlacementType = familySymbol.Family.FamilyPlacementType;

            if (familyPlacementType != FamilyPlacementType.CurveBased && familyPlacementType != FamilyPlacementType.CurveBasedDetail && familyPlacementType != FamilyPlacementType.CurveDrivenStructural && familyPlacementType != FamilyPlacementType.TwoLevelsBased)
            {
                Compute.InvalidFamilyPlacementTypeError(framingElement, familySymbol);
                return(null);
            }

            if (framingElement is Beam)
            {
                familyInstance = document.Create.NewFamilyInstance(revitCurve, familySymbol, level, Autodesk.Revit.DB.Structure.StructuralType.Beam);
            }
            else if (framingElement is Bracing || framingElement is Cable)
            {
                familyInstance = document.Create.NewFamilyInstance(revitCurve, familySymbol, level, Autodesk.Revit.DB.Structure.StructuralType.Brace);
            }
            else
            {
                familyInstance = document.Create.NewFamilyInstance(revitCurve, familySymbol, level, Autodesk.Revit.DB.Structure.StructuralType.UnknownFraming);
            }

            document.Regenerate();

            familyInstance.CheckIfNullPush(framingElement);
            if (familyInstance == null)
            {
                return(null);
            }

            oM.Physical.FramingProperties.ConstantFramingProperty barProperty = framingElement.Property as oM.Physical.FramingProperties.ConstantFramingProperty;
            if (barProperty != null)
            {
                //TODO: if the material does not get assigned an error should be thrown?
                if (barProperty.Material != null)
                {
                    Material material = document.GetElement(new ElementId(BH.Engine.Adapters.Revit.Query.ElementId(barProperty.Material))) as Material;
                    if (material != null)
                    {
                        Parameter param = familyInstance.get_Parameter(BuiltInParameter.STRUCTURAL_MATERIAL_PARAM);
                        if (param != null && param.HasValue && !param.IsReadOnly)
                        {
                            familyInstance.StructuralMaterialId = material.Id;
                        }
                        else
                        {
                            BH.Engine.Reflection.Compute.RecordWarning(string.Format("The BHoM material has been correctly converted, but the property could not be assigned to the Revit element. ElementId: {0}", familyInstance.Id));
                        }
                    }
                }
            }

            //Set the insertion point to centroid.
            Parameter zJustification = familyInstance.get_Parameter(BuiltInParameter.Z_JUSTIFICATION);

            if (zJustification != null && !zJustification.IsReadOnly)
            {
                zJustification.Set((int)Autodesk.Revit.DB.Structure.ZJustification.Origin);
            }

            familyInstance.CopyParameters(framingElement, settings);
            familyInstance.SetLocation(framingElement, settings);

            if (familyInstance.StructuralMaterialType != StructuralMaterialType.Concrete && familyInstance.StructuralMaterialType != StructuralMaterialType.PrecastConcrete)
            {
                StructuralFramingUtils.DisallowJoinAtEnd(familyInstance, 0);
                StructuralFramingUtils.DisallowJoinAtEnd(familyInstance, 1);
            }

            refObjects.AddOrReplace(framingElement, familyInstance);
            return(familyInstance);
        }
Exemple #3
0
        /***************************************************/
        /****              Public methods               ****/
        /***************************************************/

        public static FamilyInstance ToRevitFamilyInstance(this Column framingElement, Document document, RevitSettings settings = null, Dictionary <Guid, List <int> > refObjects = null)
        {
            if (framingElement == null || document == null)
            {
                return(null);
            }

            FamilyInstance familyInstance = refObjects.GetValue <FamilyInstance>(document, framingElement.BHoM_Guid);

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

            settings = settings.DefaultIfNull();

            if (framingElement.Location == null)
            {
                BH.Engine.Reflection.Compute.RecordError(String.Format("Revit element could not be created because the driving curve of a BHoM object is null. BHoM_Guid: {0}", framingElement.BHoM_Guid));
                return(null);
            }

            if (framingElement.Location as BH.oM.Geometry.Line == null)
            {
                BH.Engine.Reflection.Compute.RecordError(string.Format("Revit does only support line-based columns. Try pushing your element as a beam instead. BHoM_Guid: {0}", framingElement.BHoM_Guid));
                return(null);
            }

            if (((BH.oM.Geometry.Line)framingElement.Location).Start.Z >= ((BH.oM.Geometry.Line)framingElement.Location).End.Z)
            {
                BH.Engine.Reflection.Compute.RecordError(string.Format("Start point of Revit columns need to have lower elevation than the end point. Have a look at flipping your location curves. BHoM_Guid: {0}", framingElement.BHoM_Guid));
                return(null);
            }

            Level level = document.LevelBelow(framingElement.Location, settings);

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

            Line columnLine = framingElement.Location.IToRevit() as Line;

            FamilySymbol familySymbol = framingElement.Property.ToRevitElementType(document, framingElement.BuiltInCategories(document), settings, refObjects);

            if (familySymbol == null)
            {
                familySymbol = framingElement.ElementType(document, settings) as FamilySymbol;
            }

            if (familySymbol == null)
            {
                Compute.ElementTypeNotFoundWarning(framingElement);
                return(null);
            }

            FamilyPlacementType familyPlacementType = familySymbol.Family.FamilyPlacementType;

            if (familyPlacementType != FamilyPlacementType.CurveBased && familyPlacementType != FamilyPlacementType.CurveBasedDetail && familyPlacementType != FamilyPlacementType.CurveDrivenStructural && familyPlacementType != FamilyPlacementType.TwoLevelsBased)
            {
                Compute.InvalidFamilyPlacementTypeError(framingElement, familySymbol);
                return(null);
            }

            familyInstance = document.Create.NewFamilyInstance(columnLine, familySymbol, level, Autodesk.Revit.DB.Structure.StructuralType.Column);
            document.Regenerate();

            familyInstance.CheckIfNullPush(framingElement);
            if (familyInstance == null)
            {
                return(null);
            }

            oM.Physical.FramingProperties.ConstantFramingProperty barProperty = framingElement.Property as oM.Physical.FramingProperties.ConstantFramingProperty;
            if (barProperty != null)
            {
                //TODO: if the material does not get assigned an error should be thrown?
                if (barProperty.Material != null)
                {
                    Autodesk.Revit.DB.Material material = document.GetElement(new ElementId(BH.Engine.Adapters.Revit.Query.ElementId(barProperty.Material))) as Autodesk.Revit.DB.Material;
                    if (material != null)
                    {
                        Parameter param = familyInstance.get_Parameter(BuiltInParameter.STRUCTURAL_MATERIAL_PARAM);
                        if (param != null && param.HasValue && !param.IsReadOnly)
                        {
                            familyInstance.StructuralMaterialId = material.Id;
                        }
                        else
                        {
                            BH.Engine.Reflection.Compute.RecordWarning(string.Format("The BHoM material has been correctly converted, but the property could not be assigned to the Revit element. ElementId: {0}", familyInstance.Id));
                        }
                    }
                }
            }

            // Make sure the top is above base, otherwise Revit will complain for no reason.
            familyInstance.get_Parameter((BuiltInParameter.FAMILY_BASE_LEVEL_OFFSET_PARAM)).Set(-1e+3);
            familyInstance.get_Parameter((BuiltInParameter.FAMILY_TOP_LEVEL_OFFSET_PARAM)).Set(1e+3);

            familyInstance.CopyParameters(framingElement, settings);
            familyInstance.SetLocation(framingElement, settings);

            refObjects.AddOrReplace(framingElement, familyInstance);
            return(familyInstance);
        }
        /***************************************************/
        /****               Public Methods              ****/
        /***************************************************/

        public static IMaterialFragment MaterialFragmentFromRevit(this Material material, string grade = null, RevitSettings settings = null, Dictionary <string, List <IBHoMObject> > refObjects = null)
        {
            if (material == null)
            {
                return(null);
            }

            string            refId            = material.Id.ReferenceIdentifier(grade);
            IMaterialFragment materialFragment = refObjects.GetValue <IMaterialFragment>(refId);

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

            settings = settings.DefaultIfNull();

            StructuralMaterialType structuralMaterialType = material.MaterialClass.StructuralMaterialType();

            materialFragment = structuralMaterialType.LibraryMaterial(grade);
            if (materialFragment != null)
            {
                return(materialFragment);
            }

            Compute.MaterialNotInLibraryNote(material);

            switch (structuralMaterialType)
            {
            case StructuralMaterialType.Concrete:
            case StructuralMaterialType.PrecastConcrete:
                materialFragment = new Concrete();
                break;

            case StructuralMaterialType.Aluminum:
                materialFragment = new Aluminium();
                break;

            case StructuralMaterialType.Steel:
                materialFragment = new Steel();
                break;

            case StructuralMaterialType.Wood:
                materialFragment = new Timber();
                break;

            default:
                BH.Engine.Reflection.Compute.RecordWarning(String.Format("Revit material of structural type {0} is currently not supported, the material was converted to a generic isotropic BHoM material. Revit ElementId: {1}", structuralMaterialType, material.Id.IntegerValue));
                materialFragment = new GenericIsotropicMaterial();
                break;
            }

            materialFragment.CopyCharacteristics(material);

            string name = material.Name;

            if (!string.IsNullOrWhiteSpace(grade))
            {
                name += " grade " + grade;
            }

            materialFragment.Name = name;

            refObjects.AddOrReplace(refId, materialFragment);
            return(materialFragment);
        }