Ejemplo n.º 1
0
        private void SetDefaultParameterValues(ElementType loadedElementType, IEnumerable <CADProperty> properties, CADMetadata cadMetadata)
        {
            using (var monitoredExecutionBlock = monitoredExecutionContext
                                                 .MonitorMethod <RevitHost>(nameof(SetDefaultParameterValues))
                                                 .WithParameter(nameof(loadedElementType), loadedElementType)
                                                 .WithParameter(nameof(properties), properties)
                                                 .WithParameter(nameof(cadMetadata), cadMetadata)
                                                 .WithTiming())
            {
                if (!string.IsNullOrEmpty(cadMetadata?.ParameterInfo))
                {
                    var parameterInfoList = JsonConvert.DeserializeObject <List <CADObjectParameter> >(cadMetadata.ParameterInfo);

                    var parameters = loadedElementType.GetOrderedParameters().Where(p => p.Definition.Name.StartsWith("EC_EF") || p.Definition.Name.StartsWith("MC_EF") || p.Definition.Name.StartsWith("uob_prod_"));

                    foreach (Parameter parameter in loadedElementType.Parameters)
                    {
                        if (parameter != null && parameter.HasValue && (parameter.Definition.Name.StartsWith("EC_EF") || parameter.Definition.Name.StartsWith("MC_EF") || parameter.Definition.Name.StartsWith("uob_prod_")))
                        {
                            var cadObjectParameter = parameterInfoList.FirstOrDefault(parameterInfo => parameterInfo.Name.Equals(parameter.Definition.Name));
                            if (cadObjectParameter != null && !string.IsNullOrEmpty(cadObjectParameter.Value))
                            {
                                switch (cadObjectParameter.ValueType)
                                {
                                case "N":
                                    if (GetDisplayUnitTypeDictionary().ContainsKey(cadObjectParameter.Unit))
                                    {
                                        var displayUnitType = GetDisplayUnitTypeDictionary()[cadObjectParameter.Unit];
                                        var valueString     = cadObjectParameter.Value.Replace(".", ",");

                                        double.TryParse(valueString, System.Globalization.NumberStyles.Any, new System.Globalization.CultureInfo("nl-NL"), out var doubleValue);
                                        var value = parameterHelper.ConvertToAPI(doubleValue, displayUnitType);

                                        parameterHelper.SetParameterValue(parameter, value);
                                    }
                                    else
                                    {
                                        parameterHelper.SetParameterValue(parameter, string.Empty);
                                    }

                                    break;

                                default:
                                    parameterHelper.SetParameterValue(parameter, cadObjectParameter.Value);
                                    break;
                                }
                            }
                            else
                            {
                                if (!parameter.Definition.Name.ToLower().Contains("uob_prod_") ||
                                    (parameter.Definition.Name.StartsWith("uob_prod_", StringComparison.OrdinalIgnoreCase) &&
                                     properties.FirstOrDefault(cadProperty => parameter.Definition.Name.StartsWith(cadProperty.Code, StringComparison.OrdinalIgnoreCase)) != null))
                                {
                                    parameterHelper.SetParameterValue(parameter, string.Empty);
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
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
                });
            }
        }