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

            IList <Rhino.Geometry.Point3d> points,
            DB.FamilySymbol type
        )
        {
            var adaptivePoints = points.ConvertAll(GeometryEncoder.ToXYZ);

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

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

            if (element is DB.FamilyInstance instance && DB.AdaptiveComponentInstanceUtils.IsAdaptiveComponentInstance(instance))
            {
                var adaptivePointIds = DB.AdaptiveComponentInstanceUtils.GetInstancePlacementPointElementRefIds(instance);
                if (adaptivePointIds.Count == adaptivePoints.Length)
                {
                    int index = 0;
                    foreach (var vertex in adaptivePointIds.Select(id => doc.GetElement(id)).Cast <DB.ReferencePoint>())
                    {
                        vertex.Position = adaptivePoints[index++];
                    }

                    return;
                }
            }

            {
                var creationData = new List <Autodesk.Revit.Creation.FamilyInstanceCreationData>
                {
                    Revit.ActiveUIApplication.Application.Create.NewFamilyInstanceCreationData(type, adaptivePoints)
                };

                var newElementIds = doc.IsFamilyDocument ?
                                    doc.FamilyCreate.NewFamilyInstances2(creationData) :
                                    doc.Create.NewFamilyInstances2(creationData);

                if (newElementIds.Count != 1)
                {
                    doc.Delete(newElementIds);
                    throw new InvalidOperationException();
                }

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

                ReplaceElement(ref element, doc.GetElement(newElementIds.First()), parametersMask);
            }
        }
        protected void PipeHostParameter(IGH_DataAccess DA, DB.Element srcElement, DB.BuiltInParameter srcParam, string paramName)
        {
            var param = srcElement.get_Parameter(srcParam);

            if (param != null)
            {
                switch (param.StorageType)
                {
                case DB.StorageType.None: break;

                case DB.StorageType.String:
                    DA.SetData(paramName, param.AsString());
                    break;

                case DB.StorageType.Integer:
                    DA.SetData(paramName, param.AsInteger());
                    break;

                case DB.StorageType.Double:
                    DA.SetData(paramName, param.AsDoubleInRhinoUnits());
                    break;

                case DB.StorageType.ElementId:
                    DA.SetData(
                        paramName,
                        Types.Element.FromElementId(doc: srcElement.Document, Id: param.AsElementId())
                        );
                    break;
                }
            }
        }
Пример #3
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);
            }
        }
        protected static bool TryGetFilterIntegerParam(DB.BuiltInParameter paramId, int pattern, out DB.ElementFilter filter)
        {
            var rule = new DB.FilterIntegerRule
                       (
                new DB.ParameterValueProvider(new DB.ElementId(paramId)),
                new DB.FilterNumericEquals(),
                pattern
                       );

            filter = new DB.ElementParameterFilter(rule, false);
            return(true);
        }
        protected static bool TryGetFilterLengthParam(DB.BuiltInParameter paramId, double pattern, out DB.ElementFilter filter)
        {
            var rule = new DB.FilterDoubleRule
                       (
                new DB.ParameterValueProvider(new DB.ElementId(paramId)),
                new DB.FilterNumericEquals(),
                pattern,
                Revit.VertexTolerance
                       );

            filter = new DB.ElementParameterFilter(rule, false);
            return(true);
        }
Пример #6
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);
            }
        }
Пример #7
0
        SetOutputFromPropertySetElementParam(IGH_DataAccess DA, DB.PropertySetElement srcElement,
                                             DB.BuiltInParameter srcParam, string paramName)
        {
            if (srcElement is null)
            {
                DA.SetData(paramName, null);
                return(false);
            }

            bool valueFound = false;
            var  param      = srcElement.get_Parameter(srcParam);

            if (param != null)
            {
                valueFound = true;
                switch (param.StorageType)
                {
                case DB.StorageType.None: break;

                case DB.StorageType.String:
                    DA.SetData(paramName, param.AsString());
                    break;

                case DB.StorageType.Integer:
                    if (param.Definition.ParameterType == DB.ParameterType.YesNo)
                    {
                        DA.SetData(paramName, param.AsInteger() != 0);
                    }
                    else
                    {
                        DA.SetData(paramName, param.AsInteger());
                    }
                    break;

                case DB.StorageType.Double:
                    DA.SetData(paramName, param.AsDoubleInRhinoUnits());
                    break;

                case DB.StorageType.ElementId:
                    DA.SetData(
                        paramName,
                        Types.Element.FromElementId(srcElement.Document, param.AsElementId())
                        );
                    break;
                }
            }

            return(valueFound);
        }
Пример #8
0
        void ReconstructRoofByOutline
        (
            DB.Document doc,
            ref DB.FootPrintRoof element,

            Rhino.Geometry.Curve boundary,
            Optional <DB.RoofType> type,
            Optional <DB.Level> level
        )
        {
            if
            (
                boundary.IsShort(Revit.ShortCurveTolerance * Revit.ModelUnits) ||
                !boundary.IsClosed ||
                !boundary.TryGetPlane(out var boundaryPlane, Revit.VertexTolerance * Revit.ModelUnits) ||
                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));

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

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

            using (var curveArray = boundary.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(bbox.Min.Z / Revit.ModelUnits - level.Value.GetHeight());
            }
        }
Пример #9
0
        protected void PipeHostParameter(IGH_DataAccess DA, DB.Element srcElement, DB.BuiltInParameter srcParam, string paramName)
        {
            if (srcElement is null)
            {
                DA.SetData(paramName, null);
                return;
            }

            var param = srcElement.get_Parameter(srcParam);

            if (param != null)
            {
                switch (param.StorageType)
                {
                case DB.StorageType.None: break;

                case DB.StorageType.String:
                    DA.SetData(paramName, param.AsString());
                    break;

                case DB.StorageType.Integer:
                    if (param.Definition.ParameterType == DB.ParameterType.YesNo)
                    {
                        DA.SetData(paramName, param.AsInteger() != 0);
                    }
                    else
                    {
                        DA.SetData(paramName, param.AsInteger());
                    }
                    break;

                case DB.StorageType.Double:
                    DA.SetData(paramName, param.AsDoubleInRhinoUnits());
                    break;

                case DB.StorageType.ElementId:
                    DA.SetData(
                        paramName,
                        Types.Element.FromElementId(srcElement.Document, param.AsElementId())
                        );
                    break;
                }
            }
        }
        internal static bool TryGetFilterStringParam(DB.BuiltInParameter paramId, ref string pattern, out DB.ElementFilter filter)
        {
            if (pattern is string subPattern)
            {
                var inverted = false;
                var method   = Operator.CompareMethodFromPattern(ref subPattern, ref inverted);
                if (Operator.CompareMethod.Nothing < method && method < Operator.CompareMethod.Wildcard)
                {
                    var evaluator = default(DB.FilterStringRuleEvaluator);
                    switch (method)
                    {
                    case Operator.CompareMethod.Equals: evaluator = new DB.FilterStringEquals(); break;

                    case Operator.CompareMethod.StartsWith: evaluator = new DB.FilterStringBeginsWith(); break;

                    case Operator.CompareMethod.EndsWith: evaluator = new DB.FilterStringEndsWith(); break;

                    case Operator.CompareMethod.Contains: evaluator = new DB.FilterStringContains(); break;
                    }

                    var rule = new DB.FilterStringRule
                               (
                        new DB.ParameterValueProvider(new DB.ElementId(paramId)),
                        evaluator,
                        subPattern,
                        true
                               );

                    filter  = new DB.ElementParameterFilter(rule, inverted);
                    pattern = default;
                    return(true);
                }
            }

            filter = default;
            return(false);
        }
Пример #11
0
        void ReconstructBeamByCurve
        (
            DB.Document doc,
            ref DB.FamilyInstance element,

            Rhino.Geometry.Curve curve,
            Optional <DB.FamilySymbol> type,
            Optional <DB.Level> level
        )
        {
            if
            (
                curve.IsClosed ||
                !curve.IsPlanar(Revit.VertexTolerance * Revit.ModelUnits) ||
                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.ToCurve();

            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);
            }
        }
 protected void PipeHostParameter(IGH_DataAccess DA, DB.Element srcElement, DB.BuiltInParameter srcParam, string paramName)
 {
     DA.SetData(paramName, srcElement?.get_Parameter(srcParam).AsGoo());
 }
Пример #13
0
        void ReconstructBuildingPadByOutline
        (
            DB.Document doc,
            ref DB.Architecture.BuildingPad element,

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

            boundaries = boundaries.Select(x => x.ChangeUnits(scaleFactor)).ToArray();

            var boundaryBBox = Rhino.Geometry.BoundingBox.Empty;

            foreach (var boundary in boundaries)
            {
                boundaryBBox.Union(boundary.GetBoundingBox(true));
            }

            var curveLoops = boundaries.Select(region => DB.CurveLoop.Create(region.ToHostMultiple().SelectMany(x => x.ToBoundedCurves()).ToList()));

            SolveOptionalLevel(ref level, doc, boundaryBBox.Min.Z, nameof(level));

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

            if (element is DB.Architecture.BuildingPad buildingPad)
            {
                element.get_Parameter(DB.BuiltInParameter.LEVEL_PARAM).Set(level.Value.Id);

                buildingPad.SetBoundary(curveLoops.ToList());
            }
            else
            {
                SolveOptionalType(ref type, doc, DB.ElementTypeGroup.BuildingPadType, (document, param) => DB.BuildingPadType.CreateDefault(document), nameof(type));

                var newPad = DB.Architecture.BuildingPad.Create
                             (
                    doc,
                    type.Value.Id,
                    level.Value.Id,
                    curveLoops.ToList()
                             );

                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.BUILDINGPAD_HEIGHTABOVELEVEL_PARAM
                };

                ReplaceElement(ref element, newPad, parametersMask);
            }

            element?.get_Parameter(DB.BuiltInParameter.BUILDINGPAD_HEIGHTABOVELEVEL_PARAM).Set(boundaryBBox.Min.Z - level.Value.Elevation);
        }
Пример #14
0
        protected void PipeHostParameter <T>(IGH_DataAccess DA, DB.Element srcElement, DB.BuiltInParameter srcParam, string paramName) where T : Types.GH_Enumerate, new()
        {
            if (srcElement is null)
            {
                DA.SetData(paramName, null);
                return;
            }

            var param = srcElement.get_Parameter(srcParam);

            if (param != null && param.StorageType == DB.StorageType.Integer)
            {
                var enumType = new T();
                enumType.Value = param.AsInteger();
                DA.SetData(paramName, enumType);
            }
        }
Пример #15
0
        void ReconstructFamilyInstanceByLocation
        (
            DB.Document doc,
            ref DB.FamilyInstance element,

            [Description("Location where to place the element. Point or plane is accepted.")]
            Plane location,
            DB.FamilySymbol type,
            Optional <DB.Level> level,
            [Optional] DB.Element host
        )
        {
            if (!location.IsValid)
            {
                ThrowArgumentException(nameof(location), "Location is not valid.");
            }

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

            {
                FamilyInstanceCreationData creationData;
                switch (type.Family.FamilyPlacementType)
                {
                case DB.FamilyPlacementType.OneLevelBased:
                    creationData = CreateOneLevelBased(doc, location, type, level, host);
                    break;

                case DB.FamilyPlacementType.OneLevelBasedHosted:
                    creationData = CreateOneLevelBasedHosted(doc, location, type, level, host);
                    break;

                case DB.FamilyPlacementType.TwoLevelsBased:
                    creationData = CreateTwoLevelsBased(doc, location, type, level, host);
                    break;

                case DB.FamilyPlacementType.WorkPlaneBased:
                    creationData = CreateWorkPlaneBased(doc, location, type, level, host);
                    break;

                default:
                    creationData = CreateDefault(doc, location, type, level, host);
                    break;
                }

                var dataList = new List <FamilyInstanceCreationData>()
                {
                    creationData
                };
                var newElementIds = doc.IsFamilyDocument ?
                                    doc.FamilyCreate.NewFamilyInstances2(dataList) :
                                    doc.Create.NewFamilyInstances2(dataList);

                if (newElementIds.Count != 1)
                {
                    doc.Delete(newElementIds);
                    throw new InvalidOperationException();
                }

                var parametersMask = new DB.BuiltInParameter[]
                {
                    DB.BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM,
                    DB.BuiltInParameter.ELEM_FAMILY_PARAM,
                    DB.BuiltInParameter.ELEM_TYPE_PARAM,
                    DB.BuiltInParameter.FAMILY_LEVEL_PARAM,
                    DB.BuiltInParameter.FAMILY_BASE_LEVEL_PARAM,
                    DB.BuiltInParameter.FAMILY_BASE_LEVEL_OFFSET_PARAM,
                    DB.BuiltInParameter.FAMILY_TOP_LEVEL_PARAM,
                    DB.BuiltInParameter.FAMILY_TOP_LEVEL_OFFSET_PARAM,
                    DB.BuiltInParameter.SCHEDULE_LEVEL_PARAM,
                    DB.BuiltInParameter.SCHEDULE_BASE_LEVEL_PARAM,
                    DB.BuiltInParameter.SCHEDULE_BASE_LEVEL_OFFSET_PARAM,
                    DB.BuiltInParameter.SCHEDULE_TOP_LEVEL_PARAM,
                    DB.BuiltInParameter.SCHEDULE_TOP_LEVEL_OFFSET_PARAM,
                    DB.BuiltInParameter.INSTANCE_SCHEDULE_ONLY_LEVEL_PARAM,
                    DB.BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM
                };

                ReplaceElement(ref element, doc.GetElement(newElementIds.First()) as DB.FamilyInstance, parametersMask);
                doc.Regenerate();

                if (element.Pinned)
                {
                    try { element.Pinned = false; }
                    catch (Autodesk.Revit.Exceptions.InvalidOperationException) { }
                }
            }

            element?.SetLocation(location.Origin.ToXYZ(), location.XAxis.ToXYZ(), location.YAxis.ToXYZ());
        }
Пример #16
0
        void ReconstructRailingByCurve
        (
            DB.Document doc,
            ref DB.Architecture.Railing element,

            Rhino.Geometry.Curve curve,
            Optional <DB.Architecture.RailingType> type,
            Optional <DB.Level> level,
            [Optional] DB.Element host,
            [Optional] bool flipped
        )
        {
            SolveOptionalType(ref type, doc, DB.ElementTypeGroup.StairsRailingType, nameof(type));
            SolveOptionalLevel(doc, curve, ref level, out var bbox);

            // Axis
            var levelPlane = new Rhino.Geometry.Plane(new Rhino.Geometry.Point3d(0.0, 0.0, level.Value.Elevation * Revit.ModelUnits), Rhino.Geometry.Vector3d.ZAxis);

            curve = Rhino.Geometry.Curve.ProjectToPlane(curve, levelPlane);
            curve = curve.Simplify(Rhino.Geometry.CurveSimplifyOptions.All, Revit.VertexTolerance * Revit.ModelUnits, Revit.AngleTolerance) ?? curve;

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

            DB.Architecture.Railing newRail = null;
            if (element is DB.Architecture.Railing previousRail)
            {
                newRail = previousRail;

                newRail.SetPath(curve.ToCurveLoop());
            }
            else
            {
                newRail = DB.Architecture.Railing.Create
                          (
                    doc,
                    curve.ToCurveLoop(),
                    type.Value.Id,
                    level.Value.Id
                          );

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

                ReplaceElement(ref element, newRail, parametersMask);
            }

            if (newRail is object)
            {
                using (var baseLevel = newRail.get_Parameter(DB.BuiltInParameter.STAIRS_RAILING_BASE_LEVEL_PARAM))
                {
                    if (!baseLevel.IsReadOnly)
                    {
                        baseLevel.Set(level.Value.Id);
                    }
                }
                using (var heightOffset = newRail.get_Parameter(DB.BuiltInParameter.STAIRS_RAILING_HEIGHT_OFFSET))
                {
                    if (!heightOffset.IsReadOnly)
                    {
                        heightOffset.Set(bbox.Min.Z / Revit.ModelUnits - level.Value.Elevation);
                    }
                }

                newRail.HostId = host?.Id ?? DB.ElementId.InvalidElementId;

                if (newRail.Flipped != flipped)
                {
                    newRail.Flip();
                }
            }
        }
Пример #17
0
        private bool ConvertToBool(Rvt.BuiltInParameter bip)
        {
            var parameter = family.get_Parameter(bip);

            return(Convert.ToBoolean(parameter.AsInteger()));
        }
Пример #18
0
        void ReconstructWallByProfile
        (
            DB.Document doc,
            ref DB.Wall element,

            IList <Rhino.Geometry.Curve> profile,
            Optional <DB.WallType> type,
            Optional <DB.Level> level,
            [Optional] DB.WallLocationLine locationLine,
            [Optional] bool flipped,
            [Optional, NickName("J")] bool allowJoins,
            [Optional] DB.Structure.StructuralWallUsage structuralUsage
        )
        {
            foreach (var boundary in profile)
            {
                if
                (
                    !boundary.IsClosed ||
                    !boundary.TryGetPlane(out var boundaryPlane, Revit.VertexTolerance) ||
                    !boundaryPlane.ZAxis.IsPerpendicularTo(Rhino.Geometry.Vector3d.ZAxis)
                )
                {
                    ThrowArgumentException(nameof(boundary), "Boundary must be a vertical planar closed curve.");
                }
            }

            SolveOptionalType(ref type, doc, DB.ElementTypeGroup.WallType, nameof(type));
            SolveOptionalLevel(doc, profile, ref level, out var bbox);

            foreach (var curve in profile)
            {
                curve.RemoveShortSegments(Revit.ShortCurveTolerance * Revit.ModelUnits);
            }
            var boundaries = profile.SelectMany(x => GeometryEncoder.ToCurveMany(x)).SelectMany(CurveExtension.ToBoundedCurves).ToList();

            // 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)
                {
                    profile[0].TryGetPlane(out var plane);
                    var translation = DB.Transform.CreateTranslation((plane.Normal * (flipped ? -offsetDist : offsetDist)).ToXYZ());
                    for (int b = 0; b < boundaries.Count; ++b)
                    {
                        boundaries[b] = boundaries[b].CreateTransformed(translation);
                    }
                }
            }

            var newWall = DB.Wall.Create
                          (
                doc,
                boundaries,
                type.Value.Id,
                level.Value.Id,
                structuralUsage != DB.Structure.StructuralWallUsage.NonBearing
                          );

            // Walls are created with the last LocationLine used in the Revit editor!!
            //newWall.get_Parameter(BuiltInParameter.WALL_KEY_REF_PARAM).Set((int) WallLocationLine.WallCenterline);

            var parametersMask = new DB.BuiltInParameter[]
            {
                DB.BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM,
                DB.BuiltInParameter.ELEM_FAMILY_PARAM,
                DB.BuiltInParameter.ELEM_TYPE_PARAM,
                DB.BuiltInParameter.WALL_KEY_REF_PARAM,
                DB.BuiltInParameter.WALL_USER_HEIGHT_PARAM,
                DB.BuiltInParameter.WALL_BASE_CONSTRAINT,
                DB.BuiltInParameter.WALL_BASE_OFFSET,
                DB.BuiltInParameter.WALL_STRUCTURAL_SIGNIFICANT,
                DB.BuiltInParameter.WALL_STRUCTURAL_USAGE_PARAM
            };

            ReplaceElement(ref element, newWall, parametersMask);

            if (newWall != null)
            {
                newWall.get_Parameter(DB.BuiltInParameter.WALL_BASE_CONSTRAINT).Set(level.Value.Id);
                newWall.get_Parameter(DB.BuiltInParameter.WALL_BASE_OFFSET).Set(bbox.Min.Z / Revit.ModelUnits - level.Value.Elevation);
                newWall.get_Parameter(DB.BuiltInParameter.WALL_KEY_REF_PARAM).Set((int)locationLine);
                if (structuralUsage == DB.Structure.StructuralWallUsage.NonBearing)
                {
                    newWall.get_Parameter(DB.BuiltInParameter.WALL_STRUCTURAL_SIGNIFICANT).Set(0);
                }
                else
                {
                    newWall.get_Parameter(DB.BuiltInParameter.WALL_STRUCTURAL_SIGNIFICANT).Set(1);
                    newWall.get_Parameter(DB.BuiltInParameter.WALL_STRUCTURAL_USAGE_PARAM).Set((int)structuralUsage);
                }

                if (newWall.Flipped != flipped)
                {
                    newWall.Flip();
                }

                // Setup joins in a last step
                if (allowJoins)
                {
                    joinedWalls.Add(newWall);
                }
                else
                {
                    DB.WallUtils.DisallowWallJoinAtEnd(newWall, 0);
                    DB.WallUtils.DisallowWallJoinAtEnd(newWall, 1);
                }
            }
        }
Пример #19
0
        void ReconstructFamilyInstanceByLocation
        (
            DB.Document doc,
            ref DB.FamilyInstance element,

            [Description("Location where to place the element. Point or plane is accepted.")]
            Rhino.Geometry.Plane location,
            DB.FamilySymbol type,
            Optional <DB.Level> level,
            [Optional] DB.Element host
        )
        {
            var scaleFactor = 1.0 / Revit.ModelUnits;

            location = location.ChangeUnits(scaleFactor);

            if (!location.IsValid)
            {
                ThrowArgumentException(nameof(location), "Should be a valid point or plane.");
            }

            SolveOptionalLevel(doc, location.Origin, ref level, out var bbox);

            if (host == null && type.Family.FamilyPlacementType == DB.FamilyPlacementType.OneLevelBasedHosted)
            {
                ThrowArgumentException(nameof(host), $"This family requires a host.");
            }

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

            ChangeElementTypeId(ref element, type.Id);

            bool hasSameHost = false;

            if (element is DB.FamilyInstance)
            {
                hasSameHost = (element.Host?.Id ?? DB.ElementId.InvalidElementId) == (host?.Id ?? DB.ElementId.InvalidElementId);
                if (element.Host == null)
                {
                    if (element?.get_Parameter(DB.BuiltInParameter.INSTANCE_FREE_HOST_PARAM) is DB.Parameter freeHostParam)
                    {
                        var freeHostName = freeHostParam.AsString();
                        hasSameHost = freeHostName.EndsWith(host?.Name ?? level.Value.Name);
                    }
                }
            }

            if
            (
                hasSameHost &&
                element is DB.FamilyInstance &&
                element.Location is DB.LocationPoint locationPoint
            )
            {
                using (var levelParam = element.get_Parameter(DB.BuiltInParameter.FAMILY_LEVEL_PARAM))
                {
                    if (levelParam.AsElementId() != level.Value.Id)
                    {
                        levelParam.Set(level.Value.Id);
                        doc.Regenerate();
                    }
                }

                if (host is object)
                {
                    var newOrigin = location.Origin.ToHost();
                    if (!newOrigin.IsAlmostEqualTo(locationPoint.Point))
                    {
                        element.Pinned      = false;
                        locationPoint.Point = newOrigin;
                        element.Pinned      = true;
                    }
                }
            }
            else
            {
                var creationData = new List <Autodesk.Revit.Creation.FamilyInstanceCreationData>()
                {
                    new Autodesk.Revit.Creation.FamilyInstanceCreationData(location.Origin.ToHost(), type, host, level.Value, DB.Structure.StructuralType.NonStructural)
                };

                var newElementIds = doc.IsFamilyDocument ?
                                    doc.FamilyCreate.NewFamilyInstances2(creationData) :
                                    doc.Create.NewFamilyInstances2(creationData);

                if (newElementIds.Count != 1)
                {
                    doc.Delete(newElementIds);
                    throw new InvalidOperationException();
                }

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

                ReplaceElement(ref element, doc.GetElement(newElementIds.First()) as DB.FamilyInstance, parametersMask);
                doc.Regenerate();
            }

            if (element is object && element.Host is null)
            {
                element.Pinned = false;
                element.SetTransform(location.Origin.ToHost(), location.XAxis.ToHost(), location.YAxis.ToHost());
                element.Pinned = true;
            }
        }
 public APIAssetBuiltInPropAttribute(DB.BuiltInParameter paramId, Type type, bool exclusive = false)
 {
     ParamId   = paramId;
     DataType  = type;
     Exclusive = exclusive;
 }
Пример #21
0
        void ReconstructView3DByPlane
        (
            DB.Document doc,
            ref DB.View3D view,

            Rhino.Geometry.Plane plane,
            Optional <DB.ElementType> type,
            Optional <string> name,
            Optional <bool> perspective
        )
        {
            SolveOptionalType(ref type, doc, DB.ElementTypeGroup.ViewType3D, nameof(type));

            var orientation = new DB.ViewOrientation3D
                              (
                plane.Origin.ToXYZ(),
                plane.YAxis.ToXYZ(),
                plane.ZAxis.ToXYZ()
                              );

            if (view is null)
            {
                var newView = perspective.IsNullOrMissing ?
                              DB.View3D.CreatePerspective
                              (
                    doc,
                    type.Value.Id
                              ) :
                              DB.View3D.CreateIsometric
                              (
                    doc,
                    type.Value.Id
                              );

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

                newView.SetOrientation(orientation);
                view.get_Parameter(DB.BuiltInParameter.VIEWER_CROP_REGION).Set(0);
                ReplaceElement(ref view, newView, parametersMask);
            }
            else
            {
                view.SetOrientation(orientation);

                if (perspective.HasValue)
                {
                    view.get_Parameter(DB.BuiltInParameter.VIEWER_PERSPECTIVE).Set(perspective.Value ? 1 : 0);
                }

                ChangeElementTypeId(ref view, type.Value.Id);
            }

            if (name.HasValue && view is object)
            {
                try { view.Name = name.Value; }
                catch (Autodesk.Revit.Exceptions.ArgumentException e)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, $"{e.Message.Replace($".{Environment.NewLine}", ". ")}");
                }
            }
        }
Пример #22
0
        public static IDictionary MergeElementTypes(DynaElem FromType, DynaElem ToType)
        {
            //  Name of Transaction
            string transactionName = "Merge Element Type";

            // Get the Revit elements from the Dynamo Elements
            RevitDB.ElementType rFromType = (RevitDB.ElementType)FromType.InternalElement;
            RevitDB.ElementType rToType   = (RevitDB.ElementType)ToType.InternalElement;

            RevitDoc document = rToType.Document;

            // Collect all instances of FromType
            RevitDB.FilteredElementCollector collector        = new RevitDB.FilteredElementCollector(document);
            RevitDB.BuiltInParameter         parameterId      = RevitDB.BuiltInParameter.ELEM_TYPE_PARAM;
            RevitDB.FilterNumericEquals      filterNumberRule = new RevitDB.FilterNumericEquals();
            RevitDB.ParameterValueProvider   provider         = new RevitDB.ParameterValueProvider(new RevitDB.ElementId(parameterId));
            RevitDB.FilterRule    filterRule      = new RevitDB.FilterElementIdRule(provider, filterNumberRule, rFromType.Id);
            RevitDB.ElementFilter filterParameter = new RevitDB.ElementParameterFilter(filterRule, false);

            Type instanceType = Select.InstanceClassFromTypeClass(rFromType.GetType());

            if (instanceType != null)
            {
                collector.OfClass(instanceType);
            }

            IEnumerable <RevitDB.Element> instances = collector
                                                      .WhereElementIsNotElementType()
                                                      .WherePasses(filterParameter)
                                                      .ToElements();

            // Intialize list for elements that are successfully merged and failed to merge.
            List <DynaElem> elements       = new List <DynaElem>();
            List <DynaElem> elementsFailed = new List <DynaElem>();

            // Define Function to change instances types.
            Action <IEnumerable <RevitDB.Element> > _SetType = (isntances) =>
            {
                foreach (RevitDB.Element elem in instances)
                {
                    // If Element is in a group, put the element in the failed list
                    int groupId = elem.GroupId.IntegerValue;
                    if (groupId == -1)
                    {
                        //elem.TextNoteType = rToType;
                        RevitDB.Parameter param = elem.get_Parameter(RevitDB.BuiltInParameter.ELEM_TYPE_PARAM);
                        param.Set(rToType.Id);
                        DynaElem dElem = elem.ToDSType(true);
                        elements.Add(dElem);
                    }
                    else
                    {
                        DynaElem dElem = elem.ToDSType(true);
                        elementsFailed.Add(dElem);
                    }
                }

                // Check if there are any instances of FromType left
                int count = collector.Count();
                if (count == 0)
                {
                    document.Delete(rFromType.Id);
                }
            };

            if (document.IsModifiable)
            {
                TransactionManager.Instance.EnsureInTransaction(document);
                _SetType(instances);
                TransactionManager.Instance.TransactionTaskDone();
            }
            else
            {
                using (Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(document))
                {
                    trans.Start(transactionName);
                    _SetType(instances);
                    trans.Commit();
                }
            }

            return(new Dictionary <string, object>
            {
                { "Merged", elements },
                { "Failed", elementsFailed }
            });
        }