Editing document on DocumentSaving/DocumentPrinting

Editing document on DocumentSaving/DocumentPrinting

wouter_vandebergh
Contributor Contributor
1,618 Views
5 Replies
Message 1 of 6

Editing document on DocumentSaving/DocumentPrinting

wouter_vandebergh
Contributor
Contributor

Just before printing/saving I would like to save the smallest view scale on all sheets. This is done to circumvent the "As indicated" scale text in the titleblock once you add a legend to a sheet. We just want to display the scale of the view, not the legend. In case of multiple views the smallest scale.

 

I'm trying to modify all sheets on catching these events by overriding a shared parameter. I can read the existing value, but not set a new one.

I tried with/without a transaction with no difference. Do these events allow modifications of the document?

 

 [TransactionAttribute(TransactionMode.Manual)]
    [RegenerationAttribute(RegenerationOption.Manual)]
    public class SheetScaleManager : IExternalApplication
    {
        public Result OnStartup(UIControlledApplication uiApp)
        {
            uiApp.ControlledApplication.DocumentPrinting += new EventHandler<DocumentPrintingEventArgs>(AppDocumentPrinting);
            uiApp.ControlledApplication.DocumentSaving += new EventHandler<DocumentSavingEventArgs>(AppDocumentSaving);

            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            application.ControlledApplication.DocumentPrinting -= new EventHandler<DocumentPrintingEventArgs>(AppDocumentPrinting);
            application.ControlledApplication.DocumentSaving-= new EventHandler<DocumentSavingEventArgs>(AppDocumentSaving);

            Autodesk.Revit.UI.Result retResult = Result.Succeeded;
            return retResult;
        }

        public void AppDocumentSaving(object sender, Autodesk.Revit.DB.Events.DocumentSavingEventArgs args)
        {
            ManageSheetScale(sender, args.Document);
        }

        public void AppDocumentPrinting(object sender, Autodesk.Revit.DB.Events.DocumentPrintingEventArgs args)
        {
            ManageSheetScale(sender, args.Document);
        }

        public void ManageSheetScale(object sender, Document doc)
        {
            /*
            Application app = sender as Application;
            UIApplication uiApp = new UIApplication(app);
            Document doc = uiApp.ActiveUIDocument.Document;
            */

            // Traverse all sheets
            FilteredElementCollector col = new FilteredElementCollector(doc);
            col.OfCategory(BuiltInCategory.OST_Sheets);
            col.OfClass(typeof(ViewSheet));


            Transaction trans = new Transaction(doc);
            trans.Start("test");


            foreach (ViewSheet S in col)
            {
                // Base scale string value
                int MaxScale = 0;

                // Get all views placed on this sheet.
                ICollection<ElementId> Views = new List<ElementId>();
                Views = S.GetAllPlacedViews();

                // Traverse all these views and get the smallest scale...
                foreach (ElementId Eid in Views)
                {
                    View V = doc.GetElement(Eid) as View;
                    MaxScale = Math.Max(V.Scale, MaxScale);
                }

                // Update the scale
                string strScale = "";
                Parameter P = S.LookupParameter("NLRS_C_tekening_schaal");
                if (P != null)
                {

                    if (MaxScale > 0)
                    {
                        strScale = MaxScale.ToString();
                    }

                    string oldScale = P.AsString();

                    if (oldScale != strScale)
                    {
                        //P.Set(strScale);
                        P.SetValueString(strScale);
                    }
                }
            }

            trans.Commit();
            doc.Regenerate();
        }
    }

 

 

0 Likes
Accepted solutions (1)
1,619 Views
5 Replies
Replies (5)
Message 2 of 6

jeremytammik
Autodesk
Autodesk

Dear Wouter,

 

Thank you for your query.

 

I only checked the DocumentPrinting event:

 

http://www.revitapidocs.com/2018.1/86546cf5-eb2f-ffd7-3931-fc361f1264e2.htm

 

It clearly states:

 

"This event is raised when Revit is just about to print a view or ViewSet of the document.

 

Handlers of this event are permitted to make modifications to any document (including the active document), except for documents that are currently in read-only mode.

 

Event is cancellable. To cancel it, call the 'Cancel()' method of event's argument to True. Your application is responsible for providing feedback to the user about the reason for the cancellation."

 

Please submit a minimal reproducible case with exact steps to reproduce the issue that I can forward to the development team for analysis:

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#1b

 

Either something is wrong with your code, or the documentation needs fixing.

 

Oh no, re-reading your code, I see another possible issue:

 

You are using SetValueString.

 

Are you sure that that works at all?

 

Please implement access to the ManageSheetScale method from an external command that you call manually yourself from the user interface just before printing or saving and ensure that this really works.

 

I recommend never using SetValueString.

 

Try to use the appropriate override of the Parameter.Set method instead.

 

Then you really know what you are doing.

 

With SetValueString, you have no idea.

 

I hope this helps.

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 6

wouter_vandebergh
Contributor
Contributor
Accepted solution

Hi Jeremy

 

It seems the problem was with my transaction. If I place it inside the loop ( a unique transaction for each element I modify) it works, else it doesn't. 

 

You are correct about using the .set function, I tried both because I thought my problem was there at first.

 

Kind regards.

0 Likes
Message 4 of 6

jeremytammik
Autodesk
Autodesk

Dear Wouter,

 

Thank you for your update.

 

That sounds a bit weird.

 

Congratulations on solving it, anyway!

 

By the way, it is cleaner and safer to encapsulate your transaction in a using statement:

 

http://thebuildingcoder.typepad.com/blog/2012/04/using-using-automagically-disposes-and-rolls-back.h...

 

If you use multiple transactions, you might want to group them in a TransactionGroup.

 

That might improve performance as well, if you have many individual ones.

 

More on handling transactions and transaction groups in The Building Coder topic group:

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#5.53

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 5 of 6

wouter_vandebergh
Contributor
Contributor

Yes that or the title missing from the declaration of the transaction was causing it.

I've by now added a transaction group as you suggested.

 

Is there any way I could avoid the user being able to undo this action?

It doesn't matter in this case as I make sure the data is correct prior to saving/printing so any file leaving our office always has the correct values, but If the user undo's, it can keep go back and forth between these values (and load) which could be troublesome for other tools.

0 Likes
Message 6 of 6

jeremytammik
Autodesk
Autodesk

Dear Wouter,

 

Thank you for your update.

 

The title in the call to the transaction constructor should make no difference.

 

I am not aware of any possibility to prevent the user from undoing.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes