Exemplo n.º 1
0
        void ReconstructSketchPlaneByPlane
        (
            Document doc,
            ref Autodesk.Revit.DB.Element element,

            Rhino.Geometry.Plane plane
        )
        {
            if (!plane.IsValid)
            {
                ThrowArgumentException(nameof(plane), "Plane is not valid.");
            }

            var scaleFactor = 1.0 / Revit.ModelUnits;

            plane = plane.ChangeUnits(scaleFactor);

            if (element is SketchPlane sketchPlane)
            {
                bool pinned = element.Pinned;
                element.Pinned = false;

                var plane0 = sketchPlane.GetPlane();
                using (var plane1 = plane.ToHost())
                {
                    if (!plane0.Normal.IsParallelTo(plane1.Normal))
                    {
                        var    axisDirection = plane0.Normal.CrossProduct(plane1.Normal);
                        double angle         = plane0.Normal.AngleTo(plane1.Normal);

                        using (var axis = Line.CreateUnbound(plane0.Origin, axisDirection))
                            ElementTransformUtils.RotateElement(doc, element.Id, axis, angle);

                        plane0 = sketchPlane.GetPlane();
                    }

                    {
                        double angle = plane0.XVec.AngleOnPlaneTo(plane1.XVec, plane1.Normal);
                        if (angle != 0.0)
                        {
                            using (var axis = Line.CreateUnbound(plane0.Origin, plane1.Normal))
                                ElementTransformUtils.RotateElement(doc, element.Id, axis, angle);
                        }
                    }

                    var trans = plane1.Origin - plane0.Origin;
                    if (!trans.IsZeroLength())
                    {
                        ElementTransformUtils.MoveElement(doc, element.Id, trans);
                    }
                }

                element.Pinned = pinned;
            }
            else
            {
                ReplaceElement(ref element, SketchPlane.Create(doc, plane.ToHost()));
            }
        }
Exemplo n.º 2
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;
            }
        }
Exemplo n.º 3
0
        void ReconstructView3DByPlane
        (
            DB.Document doc,
            ref DB.View3D view,

            Rhino.Geometry.Plane plane,
            Optional <DB.ElementType> type,
            Optional <string> name,
            Optional <bool> perspective
        )
        {
            var scaleFactor = 1.0 / Revit.ModelUnits;

            plane = plane.ChangeUnits(scaleFactor);

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

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

            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}", ". ")}");
                }
            }
        }