예제 #1
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;
            }
        }