Exemplo n.º 1
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());
        }