Ejemplo n.º 1
0
        private Dictionary <string, ParameterInfo> ConvertProperties(IEnumerable <CADProperty> properties)
        {
            using (var monitoredExecutionBlock = monitoredExecutionContext
                                                 .MonitorMethod <RevitHost>(nameof(ConvertProperties))
                                                 .WithParameter(nameof(properties), properties)
                                                 .WithTiming())
            {
                var parameters = new Dictionary <string, ParameterInfo>();
                var type       = Models.ParameterType.Type;
                var valueType  = ParameterValueType.String;

                foreach (var property in properties)
                {
                    if (property.Info.TryGetValue("InjectionType", out var typeValue))
                    {
                        type = typeValue.ToEnum(Models.ParameterType.Type);
                    }

                    object value = null;
                    if (property.Selected && (property.Value.LogicalValue != null || property.Value.NumericValue != null || property.Value.Description != null))
                    {
                        switch (property.Type)
                        {
                        case "N":
                            valueType = ParameterValueType.Numeric;
                            var etimDisplayUnitType = UOLAddInUtilities.ConvertEUCode(property.UnitCode);
                            value = parameterHelper.ConvertToAPI((double)property.Value.NumericValue, etimDisplayUnitType);
                            break;

                        case "L":
                            valueType = ParameterValueType.Boolean;
                            value     = property.Value.LogicalValue;
                            break;

                        default:
                            value     = property.Value.Code ?? property.Value.Description;
                            valueType = ParameterValueType.String;
                            break;
                        }

                        var propertyName = $"{property.Code}_{property.PortCode}_";

                        if (!parameters.ContainsKey(propertyName))
                        {
                            parameters.Add(propertyName, new ParameterInfo()
                            {
                                Name = propertyName, Type = type, Value = value, ValueType = valueType
                            });
                        }
                    }
                }

                return(parameters);
            }
        }
        /// <summary>
        /// Converts a Revit element to a <see cref="CADProduct"/>.
        /// </summary>
        /// <param name="elements">The Revit elements to convert.</param>
        /// <param name="progressBar">The progress bar uesd for showing progress.</param>
        /// <returns>A <see cref="CADProduct"/>.</returns>
        public CADProduct ConvertToCADProduct(Element element)
        {
            if (element is FamilyInstance familyInstance && !string.IsNullOrEmpty(element.Document.PathName))
            {
                var cadFilterResultString = File.ReadAllText(element.Document.PathName.Replace(".rvt", $"_{familyInstance.Symbol.Id.IntegerValue.ToString()}.met"));
                if (!string.IsNullOrEmpty(cadFilterResultString))
                {
                    var cadFilterResult = JsonConvert.DeserializeObject <CADFilterResult>(cadFilterResultString);

                    var cadProduct = cadFilterResult.CADProducts.FirstOrDefault();

                    UOLAddInUtilities.UpdatePropertiesWithValuesFromFamily(familyInstance.Symbol, cadProduct.Properties, false, true);
                    UOLAddInUtilities.UpdatePropertiesWithValuesFromFamily(familyInstance, cadProduct.Properties, false, true);

                    cadProduct.Id        = familyInstance.Id.ToString();
                    cadProduct.ProductId = cadProduct.ProductId ?? 0;
                    return(cadProduct);
                }
            }

            return(null);
        }
        public static bool UpdatePropertiesWithValuesFromFamily(Element loadedElementOrType, IEnumerable <CADProperty> properties, bool setUserDefinedValues = false, bool deleteUnknownProperties = false)
        {
            var hasUserDefinedValues      = false;
            var monitoredExecutionContext = ApplicationGlobals.ApplicationContext.GetService <IMonitoredExecutionContext>();
            var parameterHelper           = new ParameterHelper();

            using (var monitoredExecutionBlock = monitoredExecutionContext
                                                 .MonitorMethod <UOLAddInUtilities>(nameof(UpdatePropertiesWithValuesFromFamily))
                                                 .WithParameter(nameof(loadedElementOrType), loadedElementOrType)
                                                 .WithParameter(nameof(properties), properties)
                                                 .WithParameter(nameof(setUserDefinedValues), setUserDefinedValues)
                                                 .WithTiming())
            {
                var propertiesToDelete = new List <CADProperty>();

                foreach (var cadProperty in properties)
                {
                    if (setUserDefinedValues || (!setUserDefinedValues && !cadProperty.Selected) || (!setUserDefinedValues && deleteUnknownProperties))
                    {
                        var propertyName   = cadProperty.Code.StartsWith("uob_prod_", StringComparison.OrdinalIgnoreCase) ? $"{cadProperty.Code}" : $"{cadProperty.Code}_{cadProperty.PortCode}_";
                        var revitParameter = parameterHelper.GetParameterBySearchString(loadedElementOrType, propertyName, true);

                        if (revitParameter == null || !revitParameter.HasValue)
                        {
                            if (revitParameter == null && deleteUnknownProperties)
                            {
                                propertiesToDelete.Add(cadProperty);
                            }

                            continue;
                        }

                        switch (revitParameter.StorageType)
                        {
                        case StorageType.Double:
                            var    etimDisplayUnitType  = UOLAddInUtilities.ConvertEUCode(cadProperty.UnitCode);
                            double?parameterDoubleValue = Math.Round(parameterHelper.ConvertFromAPI(etimDisplayUnitType, revitParameter.AsDouble()), 6);

                            if (parameterDoubleValue == 0 && cadProperty.Value.NumericValue == null)
                            {
                                parameterDoubleValue = cadProperty.Value.NumericValue;
                            }

                            if (setUserDefinedValues && !parameterDoubleValue.Equals(cadProperty.Value.NumericValue))
                            {
                                cadProperty.UserDefinedValue = new CADPropertyValue {
                                    NumericValue = parameterDoubleValue
                                };
                                cadProperty.Selected = true;
                                hasUserDefinedValues = true;
                            }
                            else if (!setUserDefinedValues)
                            {
                                cadProperty.Value.NumericValue = parameterDoubleValue;
                            }

                            break;

                        case StorageType.Integer:
                            if (cadProperty.Type == "L")
                            {
                                var parameterBooleanValue = Convert.ToBoolean(revitParameter.AsInteger());
                                if (setUserDefinedValues && !parameterBooleanValue.Equals(cadProperty.Value.LogicalValue))
                                {
                                    cadProperty.UserDefinedValue = new CADPropertyValue {
                                        LogicalValue = parameterBooleanValue
                                    };
                                    cadProperty.Selected = true;
                                    hasUserDefinedValues = true;
                                }
                                else if (!setUserDefinedValues)
                                {
                                    cadProperty.Value.LogicalValue = parameterBooleanValue;
                                }
                            }
                            else
                            {
                                double?parameterIntegerValue = revitParameter.AsInteger();
                                if (parameterIntegerValue == 0 && cadProperty.Value.NumericValue == null)
                                {
                                    parameterIntegerValue = cadProperty.Value.NumericValue;
                                }

                                if (setUserDefinedValues && !parameterIntegerValue.Equals(cadProperty.Value.NumericValue))
                                {
                                    cadProperty.UserDefinedValue = new CADPropertyValue {
                                        NumericValue = parameterIntegerValue
                                    };
                                    cadProperty.Selected = true;
                                    hasUserDefinedValues = true;
                                }
                                else if (!setUserDefinedValues)
                                {
                                    cadProperty.Value.NumericValue = parameterIntegerValue;
                                }
                            }

                            break;

                        case StorageType.String:
                            var parameterStringValue = revitParameter.AsString();
                            if (parameterStringValue.StartsWith("EV", StringComparison.OrdinalIgnoreCase))
                            {
                                if (setUserDefinedValues && !parameterStringValue.Equals(cadProperty.Value.Code))
                                {
                                    cadProperty.UserDefinedValue = new CADPropertyValue {
                                        Code = parameterStringValue, Description = null
                                    };
                                    cadProperty.Selected = true;
                                    hasUserDefinedValues = true;
                                }
                                else if (!setUserDefinedValues && (string.IsNullOrEmpty(cadProperty.Value.Code) || !cadProperty.Value.Code.Equals(parameterStringValue, StringComparison.OrdinalIgnoreCase)))
                                {
                                    cadProperty.Value.Code        = parameterStringValue;
                                    cadProperty.Value.Description = null;
                                }
                            }
                            else
                            {
                                if (setUserDefinedValues && !(parameterStringValue == string.Empty && cadProperty.Value.Description == null) && !parameterStringValue.Equals(cadProperty.Value.Description))
                                {
                                    cadProperty.UserDefinedValue = new CADPropertyValue {
                                        Code = null, Description = string.IsNullOrEmpty(parameterStringValue) ? null : parameterStringValue
                                    };
                                    cadProperty.Selected = true;
                                    hasUserDefinedValues = true;
                                }
                                else if (!setUserDefinedValues)
                                {
                                    cadProperty.Value.Code        = null;
                                    cadProperty.Value.Description = string.IsNullOrEmpty(parameterStringValue) ? null : parameterStringValue;
                                }
                            }

                            break;

                        case StorageType.ElementId:
                            var parameterElementIdIntegerValue = revitParameter.AsElementId().IntegerValue;
                            if (setUserDefinedValues && !parameterElementIdIntegerValue.Equals(cadProperty.Value.NumericValue))
                            {
                                cadProperty.UserDefinedValue = new CADPropertyValue {
                                    NumericValue = parameterElementIdIntegerValue
                                };
                                cadProperty.Selected = true;
                                hasUserDefinedValues = true;
                            }
                            else if (!setUserDefinedValues)
                            {
                                cadProperty.Value.NumericValue = parameterElementIdIntegerValue;
                            }

                            break;
                        }
                    }
                }

                if (propertiesToDelete.Count > 0)
                {
                    var filteredProperties = new List <CADProperty>(properties);

                    foreach (var property in propertiesToDelete)
                    {
                        filteredProperties.Remove(property);
                    }

                    properties = filteredProperties;
                }
            }

            return(hasUserDefinedValues);
        }
        public void CreateRibbon(UIControlledApplication uiControlledApplication)
        {
            // Creates the Ribbon with two panels and buttons on it.
            var tabName = Properties.Resources.RibbonName;

            uiControlledApplication.CreateRibbonTab(tabName);

            var searchPanel     = uiControlledApplication.CreateRibbonPanel(tabName, Properties.Resources.RibbonSearchPanel);
            var validationPanel = uiControlledApplication.CreateRibbonPanel(tabName, Properties.Resources.RibbonPanel_Validate);

            searchPanel.AddItem(CreatePushButton("BtnSearch", Properties.Resources.RibbonButtonSearchDescription, "UOL.Revit.SampleAddin.Commands.SearchUOLExternalCommand", UOLAddInUtilities.ImageToByte(Properties.Resources.Add), UOLAddInUtilities.ImageToByte(Properties.Resources.Add_64x64)));
            searchPanel.AddItem(CreatePushButton("BtnEditInstances", Properties.Resources.RibbonButtonEditInstancesDescription, "UOL.Revit.SampleAddin.Commands.EditInstancesExternalCommand", UOLAddInUtilities.ImageToByte(Properties.Resources.Edit), UOLAddInUtilities.ImageToByte(Properties.Resources.Edit_64x64)));
            validationPanel.AddItem(CreatePushButton("btnRequestValidation", Properties.Resources.RibbonButton_ValidateSubRequestValidationDescription, "UOL.Revit.SampleAddin.Commands.AddValidationRequestExternalCommand", UOLAddInUtilities.ImageToByte(Properties.Resources.iconV2), UOLAddInUtilities.ImageToByte(Properties.Resources.iconV2)));
            validationPanel.AddItem(CreatePushButton("btnProcessValidation", Properties.Resources.RibbonButton_ValidateSubProcessValidationDescription, "UOL.Revit.SampleAddin.Commands.ProcessValidationRequestExternalCommand", UOLAddInUtilities.ImageToByte(Properties.Resources.iconV3), UOLAddInUtilities.ImageToByte(Properties.Resources.iconV3)));
        }
Ejemplo n.º 5
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
                });
            }
        }