Пример #1
0
        public CADHostResult InsertCadContent(string contentPath, string name, IEnumerable <CADProperty> properties, bool placeInstance, CADMetadata cadMetadata)
        {
            using (var monitoredExecutionBlock = monitoredExecutionContext
                                                 .MonitorMethod <RevitHost>(nameof(InsertCadContent))
                                                 .WithParameter(nameof(contentPath), contentPath)
                                                 .WithParameter(nameof(name), name)
                                                 .WithParameter(nameof(properties), properties)
                                                 .WithParameter(nameof(placeInstance), placeInstance)
                                                 .WithTiming())
            {
                var parameters = new Dictionary <string, ParameterInfo>();
                try
                {
                    parameters = ConvertProperties(properties);
                }
                catch
                {
                    MessageBox.Show(Properties.Resources.MessageBoxCadPropertiesIncomplete_Text, Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(new CADHostResult {
                        State = CADHostResultState.Failed
                    });
                }

                ElementType loadedElementType;
                using (var transaction = new Transaction(ApplicationGlobals.ExternalCommandData.Application.ActiveUIDocument.Document, "Insert Family"))
                {
                    transaction.Start();

                    var typeDataList = new List <TypeData>()
                    {
                        new TypeData()
                        {
                            Path       = contentPath,
                            Parameters = new Dictionary <string, ParameterInfo>(),
                            TypeName   = name
                        }
                    };

                    loadedElementType = familyHelper.LoadTypes(ApplicationGlobals.ExternalCommandData.Application.ActiveUIDocument.Document, typeDataList);

                    SetDefaultParameterValues(loadedElementType, properties, cadMetadata);

                    foreach (var parameter in parameters)
                    {
                        var revitParameter = parameterHelper.GetParameterBySearchString(loadedElementType, parameter.Value.Name);

                        if (revitParameter == null)
                        {
                            revitParameter = parameterHelper.GetParameterBySearchString(loadedElementType, parameter.Value.Name.Replace("_0_", "_"));
                        }

                        parameterHelper.SetParameterValue(revitParameter, parameter.Value.Value);
                    }

                    transaction.Commit();
                }

                if (!loadedElementType.IsValidObject)
                {
                    return(new CADHostResult {
                        State = CADHostResultState.Failed
                    });
                }

                UOLAddInUtilities.UpdatePropertiesWithValuesFromFamily(loadedElementType, properties, false);

                if (placeInstance)
                {
                    try
                    {
                        if ((FamilySymbol)loadedElementType != null && ApplicationGlobals.ExternalCommandData.Application.ActiveUIDocument.ActiveView.ViewType != ViewType.Elevation && ApplicationGlobals.ExternalCommandData.Application.ActiveUIDocument.ActiveView.ViewType != ViewType.Section)
                        {
                            new ElementHelper().PlaceFamilyInstance(ApplicationGlobals.ExternalCommandData.Application.ActiveUIDocument, (FamilySymbol)loadedElementType);
                        }
                        else
                        {
                            MessageBox.Show(Properties.Resources.MessageBoxCadContentLoadedCantPlace_Text, Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    catch (Autodesk.Revit.Exceptions.InvalidOperationException invalidOperationException)
                    {
                        monitoredExecutionBlock.LogException(invalidOperationException);
                        MessageBox.Show(Properties.Resources.MessageBoxCadContentLoadedCantPlace_Text, Properties.Resources.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return(new CADHostResult {
                            CADObject = loadedElementType, State = CADHostResultState.Succeeded
                        });
                    }
                }

                return(new CADHostResult {
                    CADObject = loadedElementType, State = CADHostResultState.Succeeded
                });
            }
        }