Example #1
0
        /// <summary>
        /// The implementation for IExternalCommand.Execute()
        /// </summary>
        /// <param name="commandData">The Revit command data.</param>
        /// <param name="message">The error message (ignored).</param>
        /// <param name="elements">The elements to display in the failure dialog (ignored).</param>
        /// <returns>Result.Succeeded</returns>
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document    doc   = commandData.Application.ActiveUIDocument.Document;
            Transaction trans = new Transaction(doc, "Modify a naming rule");

            trans.Start();

            try
            {
                ExportPDFSettings settings = ExportPDFSettings.FindByName(doc, "sample");
                if (settings == null)
                {
                    message = "Cannot find sample settings";
                    trans.RollBack();
                    return(Result.Failed);
                }

                PDFExportOptions options = settings.GetOptions();

                // Naming rule remains the same in silence if exporting is combined
                if (options.Combine)
                {
                    message = "Exporting is combined. To change naming rule you need to set exporting not combined.";
                    trans.RollBack();
                    return(Result.Failed);
                }

                // Get naming rule
                IList <TableCellCombinedParameterData> namingRule = options.GetNamingRule();

                // Find SHEET_APPROVED_BY rule
                BuiltInParameter param              = BuiltInParameter.SHEET_APPROVED_BY;
                ElementId        categoryId         = Category.GetCategory(doc, BuiltInCategory.OST_Sheets).Id;
                ElementId        paramId            = new ElementId(param);
                TableCellCombinedParameterData rule = namingRule.SingleOrDefault(r => (r.CategoryId == categoryId && r.ParamId == paramId));
                if (rule == null)
                {
                    message = "No such rule in naming rule";
                    trans.RollBack();
                    return(Result.Failed);
                }

                // Mofidy rule
                rule.SampleValue = "Modify my sample value";
                namingRule       = namingRule.OrderBy(data => data.SampleValue).ToList(); // The order of rules is defined by the naming rule list
                options.SetNamingRule(namingRule);
                // Note that naming rule won't be changed if exporting is combined, see the comments of PDFExportOptions.SetOptions
                settings.SetOptions(options);
            }
            catch (Exception ex)
            {
                message = ex.ToString();
                trans.RollBack();
                return(Result.Failed);
            }

            trans.Commit();
            return(Result.Succeeded);
        }
Example #2
0
        /// <summary>
        /// The implementation for IExternalCommand.Execute()
        /// </summary>
        /// <param name="commandData">The Revit command data.</param>
        /// <param name="message">The error message (ignored).</param>
        /// <param name="elements">The elements to display in the failure dialog (ignored).</param>
        /// <returns>Result.Succeeded</returns>
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document    doc   = commandData.Application.ActiveUIDocument.Document;
            Transaction trans = new Transaction(doc, "Add a naming rule");

            trans.Start();

            try
            {
                ExportPDFSettings settings = ExportPDFSettings.FindByName(doc, "sample");
                if (settings == null)
                {
                    message = "Cannot find sample settings";
                    trans.RollBack();
                    return(Result.Failed);
                }

                PDFExportOptions options = settings.GetOptions();

                // Naming rule remains the same in silence if exporting is combined
                if (options.Combine)
                {
                    message = "Exporting is combined. To change naming rule you need to set exporting not combined.";
                    trans.RollBack();
                    return(Result.Failed);
                }

                // Get naming rule
                IList <TableCellCombinedParameterData> namingRule = options.GetNamingRule();

                // Add naming parameter Sheets-Approved-By to naming rule
                BuiltInParameter param      = BuiltInParameter.SHEET_APPROVED_BY;
                ElementId        categoryId = Category.GetCategory(doc, BuiltInCategory.OST_Sheets).Id;
                ElementId        paramId    = new ElementId(param);
                TableCellCombinedParameterData itemSheetApprovedBy = TableCellCombinedParameterData.Create();
                itemSheetApprovedBy.CategoryId  = categoryId;
                itemSheetApprovedBy.ParamId     = paramId;
                itemSheetApprovedBy.Prefix      = "-"; // You can also add prefix/suffix
                itemSheetApprovedBy.Separator   = "-";
                itemSheetApprovedBy.SampleValue = param.ToString();
                namingRule.Add(itemSheetApprovedBy);
                // Don't forget to set naming rule for options
                options.SetNamingRule(namingRule);
                // And save the options for settings
                // Note that naming rule won't be changed if exporting is combined, see the comments of PDFExportOptions.SetOptions
                settings.SetOptions(options);
            }
            catch (Exception ex)
            {
                message = ex.ToString();
                trans.RollBack();
                return(Result.Failed);
            }

            trans.Commit();
            return(Result.Succeeded);
        }
        public static string GetExportNameFromNamingRule(PDFExportOptions opts, ExportSheet vs)
        {
            var    segs         = opts.GetNamingRule();
            string filenameTest = string.Empty;

            foreach (var seg in segs)
            {
                filenameTest += seg.Prefix;
                var pid       = seg.ParamId;
                var cid       = seg.CategoryId;
                var cidString = cid.ToString();
                if (cid.IntegerValue == (int)BuiltInCategory.OST_ProjectInformation)
                {
                    var param = vs.Sheet.Document.ProjectInformation.Parameters.Cast <Parameter>().Where(p => p.Id == pid);
                    if (param.Count() > 0)
                    {
                        var paramValue = param.First().AsValueString();
                        filenameTest += paramValue;
                    }
                }
                else
                {
                    var param = vs.Sheet.Parameters.Cast <Parameter>().Where(p => p.Id == pid);
                    if (param.Count() > 0)
                    {
                        // if (param.First().Definition.Name == "Current Revision" && vs.ForceDate)
                        // {
                        //     filenameTest += "Current Revision";
                        // }
                        // else
                        // {
                        var paramValue = param.First().AsValueString();
                        if (paramValue.Length < 1)
                        {
                            filenameTest += "Current Revision";
                        }
                        else
                        {
                            filenameTest += paramValue;
                        }
                        //// }
                    }
                }
                filenameTest += seg.Suffix;
                filenameTest += seg.Separator;
            }
            return(filenameTest);
        }