Пример #1
0
        void ReconstructModelLineByCurve
        (
            Document doc,
            ref Autodesk.Revit.DB.Element element,

            Rhino.Geometry.Curve curve,
            Autodesk.Revit.DB.SketchPlane sketchPlane
        )
        {
            var scaleFactor = 1.0 / Revit.ModelUnits;

            var plane = sketchPlane.GetPlane().ToRhino().ChangeUnits(scaleFactor);

            if
            (
                ((curve = curve.ChangeUnits(scaleFactor)) is null) ||
                ((curve = Rhino.Geometry.Curve.ProjectToPlane(curve, plane)) == null)
            )
            {
                ThrowArgumentException(nameof(curve), "Failed to project curve in the sketchPlane.");
            }

            var centerLine = curve.ToHost();

            if (curve.IsClosed == centerLine.IsBound)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Unable to keep curve closed.");
            }

            if (element is ModelCurve modelCurve && centerLine.IsSameKindAs(modelCurve.GeometryCurve))
            {
                modelCurve.SetSketchPlaneAndCurve(sketchPlane, centerLine);
            }
Пример #2
0
        void ReconstructFloorByOutline
        (
            DB.Document doc,
            ref DB.Floor element,

            Rhino.Geometry.Curve boundary,
            Optional <DB.FloorType> type,
            Optional <DB.Level> level,
            [Optional] bool structural
        )
        {
            var scaleFactor = 1.0 / Revit.ModelUnits;

            if
            (
                ((boundary = boundary.ChangeUnits(scaleFactor)) is null) ||
                !boundary.IsClosed ||
                !boundary.TryGetPlane(out var boundaryPlane, Revit.VertexTolerance) ||
                boundaryPlane.ZAxis.IsParallelTo(Rhino.Geometry.Vector3d.ZAxis) == 0
            )
            {
                ThrowArgumentException(nameof(boundary), "Boundary must be an horizontal planar closed curve.");
            }

            SolveOptionalType(ref type, doc, DB.ElementTypeGroup.FloorType, nameof(type));

            SolveOptionalLevel(doc, boundary, ref level, out var bbox);

            var curveArray = boundary.ToHostMultiple().ToCurveArray();

            var parametersMask = new DB.BuiltInParameter[]
            {
                DB.BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM,
                DB.BuiltInParameter.ELEM_FAMILY_PARAM,
                DB.BuiltInParameter.ELEM_TYPE_PARAM,
                DB.BuiltInParameter.LEVEL_PARAM,
                DB.BuiltInParameter.FLOOR_PARAM_IS_STRUCTURAL
            };

            if (type.Value.IsFoundationSlab)
            {
                ReplaceElement(ref element, doc.Create.NewFoundationSlab(curveArray, type.Value, level.Value, structural, DB.XYZ.BasisZ), parametersMask);
            }
            else
            {
                ReplaceElement(ref element, doc.Create.NewFloor(curveArray, type.Value, level.Value, structural, DB.XYZ.BasisZ), parametersMask);
            }

            if (element != null)
            {
                var boundaryBBox = boundary.GetBoundingBox(true);
                element.get_Parameter(DB.BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM).Set(boundaryBBox.Min.Z - level.Value.Elevation);
            }
        }
Пример #3
0
        void ReconstructRoofByOutline
        (
            DB.Document doc,
            ref DB.FootPrintRoof element,

            Rhino.Geometry.Curve boundary,
            Optional <DB.RoofType> type,
            Optional <DB.Level> level
        )
        {
            var scaleFactor = 1.0 / Revit.ModelUnits;

            if
            (
                ((boundary = boundary.ChangeUnits(scaleFactor)) is null) ||
                boundary.IsShort(Revit.ShortCurveTolerance) ||
                !boundary.IsClosed ||
                !boundary.TryGetPlane(out var boundaryPlane, Revit.VertexTolerance) ||
                boundaryPlane.ZAxis.IsParallelTo(Rhino.Geometry.Vector3d.ZAxis) == 0
            )
            {
                ThrowArgumentException(nameof(boundary), "Boundary should be an horizontal planar closed curve.");
            }

            SolveOptionalType(ref type, doc, DB.ElementTypeGroup.RoofType, nameof(type));

            double minZ = boundary.GetBoundingBox(true).Min.Z;

            SolveOptionalLevel(ref level, doc, minZ);

            var parametersMask = new DB.BuiltInParameter[]
            {
                DB.BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM,
                DB.BuiltInParameter.ELEM_FAMILY_PARAM,
                DB.BuiltInParameter.ELEM_TYPE_PARAM,
                DB.BuiltInParameter.LEVEL_PARAM,
                DB.BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM
            };

            using (var curveArray = boundary.ToHostMultiple().ToCurveArray())
            {
                var footPrintToModelCurvesMapping = new DB.ModelCurveArray();
                ReplaceElement(ref element, doc.Create.NewFootPrintRoof(curveArray, level.Value, type.Value, out footPrintToModelCurvesMapping), parametersMask);
            }

            if (element != null)
            {
                element.get_Parameter(DB.BuiltInParameter.ROOF_LEVEL_OFFSET_PARAM).Set(minZ - level.Value.Elevation);
            }
        }
Пример #4
0
        void ReconstructBeamByCurve
        (
            DB.Document doc,
            ref DB.FamilyInstance element,

            Rhino.Geometry.Curve curve,
            Optional <DB.FamilySymbol> type,
            Optional <DB.Level> level
        )
        {
            var scaleFactor = 1.0 / Revit.ModelUnits;

            if
            (
                ((curve = curve.ChangeUnits(scaleFactor)) is null) ||
                curve.IsClosed ||
                !curve.TryGetPlane(out var axisPlane, Revit.VertexTolerance) ||
                curve.GetNextDiscontinuity(Rhino.Geometry.Continuity.C2_continuous, curve.Domain.Min, curve.Domain.Max, out var _)
            )
            {
                ThrowArgumentException(nameof(curve), "Curve must be a C2 continuous planar non closed curve.");
            }

            SolveOptionalType(ref type, doc, DB.BuiltInCategory.OST_StructuralFraming, nameof(type));

            if (!type.Value.IsActive)
            {
                type.Value.Activate();
            }

            SolveOptionalLevel(ref level, doc, curve, nameof(level));

            var centerLine = curve.ToHost();

            // Type
            ChangeElementTypeId(ref element, type.Value.Id);

            DB.FamilyInstance newBeam = null;
            if (element is DB.FamilyInstance previousBeam && element.Location is DB.LocationCurve locationCurve && centerLine.IsSameKindAs(locationCurve.Curve))
            {
                newBeam = previousBeam;

                locationCurve.Curve = centerLine;
            }
Пример #5
0
        void ReconstructBeamByCurve
        (
            DB.Document doc,
            ref DB.FamilyInstance element,

            Rhino.Geometry.Curve curve,
            Optional <DB.FamilySymbol> type,
            Optional <DB.Level> level
        )
        {
            var scaleFactor = 1.0 / Revit.ModelUnits;

            if
            (
                ((curve = curve.ChangeUnits(scaleFactor)) is null) ||
                curve.IsClosed ||
                !curve.TryGetPlane(out var axisPlane, Revit.VertexTolerance) ||
                curve.GetNextDiscontinuity(Rhino.Geometry.Continuity.C2_continuous, curve.Domain.Min, curve.Domain.Max, out var _)
            )
            {
                ThrowArgumentException(nameof(curve), "Curve must be a C2 continuous planar non closed curve.");
            }

            SolveOptionalLevel(ref level, doc, curve, nameof(level));

            var centerLine = curve.ToHost();

            if (type.HasValue)
            {
                ChangeElementTypeId(ref element, type.Value.Id);
            }

            if (element is object && element.Location is DB.LocationCurve locationCurve && centerLine.IsSameKindAs(locationCurve.Curve))
            {
                element.get_Parameter(DB.BuiltInParameter.LEVEL_PARAM).Set(level.Value.Id);

                locationCurve.Curve = centerLine;
            }
Пример #6
0
        void ReconstructWallByCurve
        (
            DB.Document doc,
            ref DB.Wall element,

            Rhino.Geometry.Curve curve,
            Optional <DB.WallType> type,
            Optional <DB.Level> level,
            [Optional] double height,
            [Optional] DB.WallLocationLine locationLine,
            [Optional] bool flipped,
            [Optional, NickName("J")] bool allowJoins,
            [Optional] DB.Structure.StructuralWallUsage structuralUsage
        )
        {
            var scaleFactor = 1.0 / Revit.ModelUnits;

#if REVIT_2020
            if
            (
                ((curve = curve.ChangeUnits(scaleFactor)) is null) ||
                !(curve.IsLinear(Revit.VertexTolerance) || curve.IsArc(Revit.VertexTolerance) || curve.IsEllipse(Revit.VertexTolerance)) ||
                !curve.TryGetPlane(out var axisPlane, Revit.VertexTolerance) ||
                axisPlane.ZAxis.IsParallelTo(Rhino.Geometry.Vector3d.ZAxis) == 0
            )
            {
                ThrowArgumentException(nameof(curve), "Curve must be a horizontal line, arc or ellipse curve.");
            }
#else
            if
            (
                ((curve = curve.ChangeUnits(scaleFactor)) is null) ||
                !(curve.IsLinear(Revit.VertexTolerance) || curve.IsArc(Revit.VertexTolerance)) ||
                !curve.TryGetPlane(out var axisPlane, Revit.VertexTolerance) ||
                axisPlane.ZAxis.IsParallelTo(Rhino.Geometry.Vector3d.ZAxis) == 0
            )
            {
                ThrowArgumentException(nameof(curve), "Curve must be a horizontal line or arc curve.");
            }
#endif

            SolveOptionalType(ref type, doc, DB.ElementTypeGroup.WallType, nameof(type));

            bool levelIsEmpty = SolveOptionalLevel(doc, curve, ref level, out var bbox);

            height *= scaleFactor;
            if (height < Revit.VertexTolerance)
            {
                height = type.GetValueOrDefault()?.GetCompoundStructure()?.SampleHeight ?? LiteralLengthValue(6.0) / Revit.ModelUnits;
            }

            // Axis
            var levelPlane = new Rhino.Geometry.Plane(new Rhino.Geometry.Point3d(0.0, 0.0, level.Value.Elevation), Rhino.Geometry.Vector3d.ZAxis);
            curve = Rhino.Geometry.Curve.ProjectToPlane(curve, levelPlane);

            var centerLine = curve.ToHost();

            // LocationLine
            if (locationLine != DB.WallLocationLine.WallCenterline)
            {
                double offsetDist        = 0.0;
                var    compoundStructure = type.Value.GetCompoundStructure();
                if (compoundStructure == null)
                {
                    switch (locationLine)
                    {
                    case DB.WallLocationLine.WallCenterline:
                    case DB.WallLocationLine.CoreCenterline:
                        break;

                    case DB.WallLocationLine.FinishFaceExterior:
                    case DB.WallLocationLine.CoreExterior:
                        offsetDist = type.Value.Width / +2.0;
                        break;

                    case DB.WallLocationLine.FinishFaceInterior:
                    case DB.WallLocationLine.CoreInterior:
                        offsetDist = type.Value.Width / -2.0;
                        break;
                    }
                }
                else
                {
                    if (!compoundStructure.IsVerticallyHomogeneous())
                    {
                        compoundStructure = DB.CompoundStructure.CreateSimpleCompoundStructure(compoundStructure.GetLayers());
                    }

                    offsetDist = compoundStructure.GetOffsetForLocationLine(locationLine);
                }

                if (offsetDist != 0.0)
                {
                    centerLine = centerLine.CreateOffset(flipped ? -offsetDist : offsetDist, DB.XYZ.BasisZ);
                }
            }

            // Type
            ChangeElementTypeId(ref element, type.Value.Id);

            DB.Wall newWall = null;
            if (element is DB.Wall previousWall && previousWall.Location is DB.LocationCurve locationCurve && centerLine.IsSameKindAs(locationCurve.Curve))
            {
                newWall = previousWall;

                locationCurve.Curve = centerLine;
            }
Пример #7
0
        void ReconstructGridByCurve
        (
            Document doc,
            ref Autodesk.Revit.DB.Element element,

            Rhino.Geometry.Curve curve,
            Optional <Autodesk.Revit.DB.GridType> type,
            Optional <string> name
        )
        {
            var scaleFactor = 1.0 / Revit.ModelUnits;

            curve = curve.ChangeUnits(scaleFactor);

            SolveOptionalType(ref type, doc, ElementTypeGroup.GridType, nameof(type));

            var parametersMask = name == Optional.Nothig ?
                                 new BuiltInParameter[]
            {
                BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM,
                BuiltInParameter.ELEM_FAMILY_PARAM,
                BuiltInParameter.ELEM_TYPE_PARAM
            } :
            new BuiltInParameter[]
            {
                BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM,
                BuiltInParameter.ELEM_FAMILY_PARAM,
                BuiltInParameter.ELEM_TYPE_PARAM,
                BuiltInParameter.DATUM_TEXT
            };

            if (curve.TryGetLine(out var line, Revit.VertexTolerance))
            {
                ReplaceElement(ref element, Grid.Create(doc, line.ToHost()), parametersMask);
                ChangeElementTypeId(ref element, type.Value.Id);
            }
            else if (curve.TryGetArc(out var arc, Revit.VertexTolerance))
            {
                ReplaceElement(ref element, Grid.Create(doc, arc.ToHost()), parametersMask);
                ChangeElementTypeId(ref element, type.Value.Id);
            }
            else
            {
                using (var curveLoop = new CurveLoop())
                    using (var polyline = curve.ToArcsAndLines(Revit.VertexTolerance, Revit.AngleTolerance, Revit.ShortCurveTolerance, double.PositiveInfinity))
                    {
                        int count = polyline.SegmentCount;
                        for (int s = 0; s < count; ++s)
                        {
                            var segment = polyline.SegmentCurve(s);

                            if (segment is Rhino.Geometry.LineCurve l)
                            {
                                curveLoop.Append(l.ToHost());
                            }
                            else if (segment is Rhino.Geometry.ArcCurve a)
                            {
                                curveLoop.Append(a.ToHost());
                            }
                            else
                            {
                                ThrowArgumentException(nameof(curve), "Invalid curve type.");
                            }
                        }

                        curve.TryGetPlane(out var plane);
                        var sketchPlane = SketchPlane.Create(doc, plane.ToHost());

                        ReplaceElement(ref element, doc.GetElement(MultiSegmentGrid.Create(doc, type.Value.Id, curveLoop, sketchPlane.Id)), parametersMask);
                    }
            }

            if (name != Optional.Nothig && element != null)
            {
                try { element.Name = name.Value; }
                catch (Autodesk.Revit.Exceptions.ArgumentException e)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, $"{e.Message.Replace($".{Environment.NewLine}", ". ")}");
                }
            }
        }
Пример #8
0
        void ReconstructBeamByCurve
        (
            DB.Document doc,
            ref DB.FamilyInstance element,

            Rhino.Geometry.Curve curve,
            Optional <DB.FamilySymbol> type,
            Optional <DB.Level> level
        )
        {
            var scaleFactor = 1.0 / Revit.ModelUnits;

            if
            (
                ((curve = curve.ChangeUnits(scaleFactor)) is null) ||
                curve.IsClosed ||
                !curve.TryGetPlane(out var axisPlane, Revit.VertexTolerance) ||
                curve.GetNextDiscontinuity(Rhino.Geometry.Continuity.C2_continuous, curve.Domain.Min, curve.Domain.Max, out var _)
            )
            {
                ThrowArgumentException(nameof(curve), "Curve must be a C2 continuous planar non closed curve.");
            }

            SolveOptionalLevel(doc, curve, ref level, out var bbox);

            var centerLine = curve.ToHost();

            if (type.HasValue)
            {
                ChangeElementTypeId(ref element, type.Value.Id);
            }

            // Try to update Beam
            if (element is object && element.Location is DB.LocationCurve locationCurve && centerLine.IsSameKindAs(locationCurve.Curve))
            {
                var referenceLevel = element.get_Parameter(DB.BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM);
                var updateLevel    = referenceLevel.AsElementId() != level.Value.Id;

                if (!updateLevel || !referenceLevel.IsReadOnly)
                {
                    if (updateLevel)
                    {
                        referenceLevel.Set(level.Value.Id);
                    }

                    locationCurve.Curve = centerLine;
                    return;
                }
            }

            // Reconstruct Beam
            {
                SolveOptionalType(ref type, doc, DB.BuiltInCategory.OST_StructuralFraming, nameof(type));

                var newBeam = doc.Create.NewFamilyInstance
                              (
                    centerLine,
                    type.Value,
                    level.Value,
                    DB.Structure.StructuralType.Beam
                              );

                newBeam.get_Parameter(DB.BuiltInParameter.Y_JUSTIFICATION).Set((int)DB.Structure.YJustification.Origin);
                newBeam.get_Parameter(DB.BuiltInParameter.Z_JUSTIFICATION).Set((int)DB.Structure.ZJustification.Origin);

                if (element is object && DB.Structure.StructuralFramingUtils.IsJoinAllowedAtEnd(element, 0))
                {
                    DB.Structure.StructuralFramingUtils.AllowJoinAtEnd(newBeam, 0);
                }
                else
                {
                    DB.Structure.StructuralFramingUtils.DisallowJoinAtEnd(newBeam, 0);
                }

                if (element is object && DB.Structure.StructuralFramingUtils.IsJoinAllowedAtEnd(element, 1))
                {
                    DB.Structure.StructuralFramingUtils.AllowJoinAtEnd(newBeam, 1);
                }
                else
                {
                    DB.Structure.StructuralFramingUtils.DisallowJoinAtEnd(newBeam, 1);
                }

                var parametersMask = new DB.BuiltInParameter[]
                {
                    DB.BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM,
                    DB.BuiltInParameter.ELEM_FAMILY_PARAM,
                    DB.BuiltInParameter.ELEM_TYPE_PARAM,
                    DB.BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM
                };

                ReplaceElement(ref element, newBeam, parametersMask);
            }
        }