예제 #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);
        }
예제 #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);
        }
예제 #3
0
        public static List <TableCellCombinedParameterData> CreateNamingRuleFromFormatString(string filenameScheme, Document doc)
        {
            var fec = new FilteredElementCollector(doc);

            fec.OfClass(typeof(ViewSheet));
            var sheetParam = fec.First() as ViewSheet;

            string prefix = string.Empty;

            ////string suffix = string.Empty;

            string[] slib =
            {
                "$height",
                "$width",
                "$fullExportName",
                "$fullExportPath",
                "$exportDir",
                "$pageSize",
                "$projectNumber",
                "$sheetDescription",
                "$sheetNumber",
                "$sheetRevisionDescription",
                "$sheetRevisionDate",
                "$sheetRevision"
            };

            var scheme = new List <TableCellCombinedParameterData>();

            string s = filenameScheme;

            char[] c = s.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                prefix += c[i];

                if (i == c.Length - 1)
                {
                    if (scheme.Count > 0)
                    {
                        scheme[scheme.Count - 1].Suffix = prefix;
                    }
                }

                if (i > 0 && c[i] == '_' && c[i - 1] == '_')
                {
                    var n = s.Substring(i);
                    if (n.Contains(@"__"))
                    {
                        var ni = n.IndexOf(@"__");
                        i += ni;
                        i += 1;
                        var customParamName = n.Substring(1, ni - 1);
                        var seg             = TableCellCombinedParameterData.Create();
                        var p = sheetParam.GetParameters(customParamName);
                        if (p.Count > 0)
                        {
                            seg.ParamId = p[0].Id;
                            seg.Prefix  = prefix.Replace("_", string.Empty);
                            prefix      = string.Empty;
                            scheme.Add(seg);
                        }
                    }
                    else
                    {
                        var seg = TableCellCombinedParameterData.Create();
                        prefix += c[i].ToString();
                        scheme.Add(seg);
                    }
                }

                if (c[i] == '$')
                {
                    foreach (string t in slib)
                    {
                        if (t.Length + i <= s.Length)
                        {
                            if (s.Substring(i, t.Length) == t)
                            {
                                var seg = TableCellCombinedParameterData.Create();
                                switch (t)
                                {
                                case "$height":
                                    break;

                                case "$width":
                                    break;

                                case "$fullExportName":
                                    break;

                                case "$fullExportPath":
                                    break;

                                case "$exportDir":
                                    break;

                                case "$pageSize":
                                    break;

                                case "$projectNumber":
                                    i             += t.Length - 1;
                                    seg.ParamId    = new ElementId(BuiltInParameter.PROJECT_NUMBER);
                                    seg.CategoryId = new ElementId(BuiltInCategory.OST_ProjectInformation);
                                    seg.Prefix     = prefix.Replace("$", string.Empty);
                                    prefix         = string.Empty;
                                    scheme.Add(seg);
                                    break;

                                case "$sheetDescription":
                                    i          += t.Length - 1;
                                    seg.ParamId = new ElementId(BuiltInParameter.SHEET_NAME);
                                    seg.Prefix  = prefix.Replace("$", string.Empty);
                                    prefix      = string.Empty;
                                    scheme.Add(seg);
                                    break;

                                case "$sheetNumber":
                                    i          += t.Length - 1;
                                    seg.ParamId = new ElementId(BuiltInParameter.SHEET_NUMBER);
                                    seg.Prefix  = prefix.Replace("$", string.Empty);
                                    prefix      = string.Empty;
                                    scheme.Add(seg);
                                    break;

                                case "$sheetRevision":
                                    i          += t.Length - 1;
                                    seg.ParamId = new ElementId(BuiltInParameter.SHEET_CURRENT_REVISION);
                                    seg.Prefix  = prefix.Replace("$", string.Empty);
                                    prefix      = string.Empty;
                                    scheme.Add(seg);
                                    break;

                                case "$sheetRevisionDate":
                                    i             += t.Length - 1;
                                    seg.ParamId    = new ElementId(BuiltInParameter.SHEET_CURRENT_REVISION_DATE);
                                    seg.CategoryId = new ElementId(BuiltInCategory.OST_Revisions);
                                    if (Settings1.Default.ForceDateRevision || Settings1.Default.UseDateForEmptyRevisions)
                                    {
                                        seg.SampleValue = Common.MiscUtilities.GetDateString;
                                    }
                                    else
                                    {
                                        seg.SampleValue = string.Empty;
                                    }
                                    seg.Prefix = prefix.Replace("$", string.Empty);
                                    prefix     = string.Empty;
                                    scheme.Add(seg);
                                    break;

                                case "$sheetRevisionDescription":
                                    i          += t.Length - 1;
                                    seg.ParamId = new ElementId(BuiltInParameter.SHEET_CURRENT_REVISION_DESCRIPTION);
                                    seg.Prefix  = prefix.Replace("$", string.Empty);
                                    prefix      = string.Empty;
                                    scheme.Add(seg);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            return(scheme);
        }