Exemplo n.º 1
0
        /// <summary>
        /// Report the custom data.
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="uiDoc"></param>
        /// <param name="setNewValues"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static Result ReportCustomData(Document doc, UIDocument uiDoc, bool setNewValues, ref string message)
        {
            FabricationPart          fabPart = null;
            FabricationConfiguration config  = null;

            try
            {
                // check for a load fabrication config
                config = FabricationConfiguration.GetFabricationConfiguration(doc);

                if (config == null)
                {
                    message = "No fabrication configuration loaded.";
                    return(Result.Failed);
                }

                // pick a fabrication part
                Reference refObj = uiDoc.Selection.PickObject(ObjectType.Element, "Pick a fabrication part to start.");
                fabPart = doc.GetElement(refObj) as FabricationPart;
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                return(Result.Cancelled);
            }

            if (fabPart == null)
            {
                message = "The selected element is not a fabrication part.";
                return(Result.Failed);
            }
            else
            {
                // get custom data from loaded fabrication config
                IList <int> customDataIds   = config.GetAllPartCustomData();
                int         customDataCount = customDataIds.Count;
                string      results         = string.Empty;

                // report custom data info
                if (customDataCount > 0)
                {
                    StringBuilder resultsBuilder = new StringBuilder();
                    resultsBuilder.AppendLine($"Fabrication config contains {customDataCount} custom data entries {Environment.NewLine}");

                    foreach (var customDataId in customDataIds)
                    {
                        FabricationCustomDataType customDataType = config.GetPartCustomDataType(customDataId);
                        string customDataName = config.GetPartCustomDataName(customDataId);

                        resultsBuilder.AppendLine($"Type: {customDataType.ToString()} Name: {customDataName}");
                        // check custom data exists on selected part
                        if (fabPart.HasCustomData(customDataId))
                        {
                            string fabPartCurrentValue = string.Empty;
                            string fabPartNewValue     = string.Empty;

                            switch (customDataType)
                            {
                            case FabricationCustomDataType.Text:

                                fabPartCurrentValue = $"\"{fabPart.GetPartCustomDataText(customDataId)}\"";

                                if (setNewValues)
                                {
                                    string installDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                                    fabPart.SetPartCustomDataText(customDataId, installDateTime);
                                    fabPartNewValue = installDateTime;
                                }

                                break;

                            case FabricationCustomDataType.Integer:

                                fabPartCurrentValue = fabPart.GetPartCustomDataInteger(customDataId).ToString();

                                if (setNewValues)
                                {
                                    int installHours = new Random().Next(1, 10);
                                    fabPart.SetPartCustomDataInteger(customDataId, installHours);
                                    fabPartNewValue = installHours.ToString();
                                }

                                break;

                            case FabricationCustomDataType.Real:

                                fabPartCurrentValue = $"{fabPart.GetPartCustomDataReal(customDataId):0.##}";

                                if (setNewValues)
                                {
                                    double installCost = new Random().NextDouble() * new Random().Next(100, 1000);
                                    fabPart.SetPartCustomDataReal(customDataId, installCost);
                                    fabPartNewValue = $"{installCost:0.##}";
                                }

                                break;
                            }

                            resultsBuilder.AppendLine("Current custom data entry value = "
                                                      + $"{fabPartCurrentValue} {Environment.NewLine}");

                            if (setNewValues)
                            {
                                resultsBuilder.AppendLine("New custom data entry value = "
                                                          + $"{fabPartNewValue} {Environment.NewLine}");
                            }
                        }
                        else
                        {
                            resultsBuilder.AppendLine($"Custom data entry is not set on the part {Environment.NewLine}");
                        }
                    }
                    results = resultsBuilder.ToString();
                }

                TaskDialog td = new TaskDialog("Custom Data")
                {
                    MainIcon        = TaskDialogIcon.TaskDialogIconInformation,
                    TitleAutoPrefix = false,
                    MainInstruction = $"{customDataCount} custom data entries found in the loaded fabrication config",
                    MainContent     = results
                };

                td.Show();
            }

            return(Result.Succeeded);
        }