Delete PrintSetup and ViewSheetSettings

Delete PrintSetup and ViewSheetSettings

Anonymous
Not applicable
2,364 Views
7 Replies
Message 1 of 8

Delete PrintSetup and ViewSheetSettings

Anonymous
Not applicable

Hi all,

 

TL;DR

Is there a way of deleting a specific PrintSetup and ViewSheetSetting programatically if i know its name?

 

 

Long version:

I'm writing a function that can print a large set of sheets in various paper sizes to separate/combined PDFs. To be able to apply the PrintSetup, it needs to be set in an "in-session" state and saved using SaveAs() (to my knowledge). But after saving an "in-session"-PrintSetup to a new PrintSetup, I cannot find a way of deleting said new PrintSetup. After the PrintManager has printed the sheets, I'm stuck with lots of temporary PrintSetups. The same goes for ViewSheetSettings.

 

            try
            {
                pMgr.PrintSetup.Delete();
                pMgr.ViewSheetSetting.Delete();
            }
            catch (Exception ex)
            {
                //Shows 'The <in-session> print setup cannot be deleted'
                TaskDialog.Show("REVIT", ex.Message);
            }

 

So the problem is: I'm not able to apply the PrintSetup and ViewSheetSettings unless i'm using them "in-session" and saving them using SaveAs, and i'm not able to delete the PrintSetup and ViewSheetSettings afterwards.

 

Has anyone experiences similar issues, or found a way to solve it? 

 

 

Cheers,

Eirik Aasved Holst

Accepted solutions (1)
2,365 Views
7 Replies
Replies (7)
Message 2 of 8

jeremytammik
Autodesk
Autodesk

Dear Eirik,

 

This discussion makes one brief mention of deleting a print setting, afaik can tell:

 

http://stackoverflow.com/questions/14946217/setting-viewsheetsetting-insession-views-property

 

This discussion appears to suggest a similar approach:

 

http://forums.autodesk.com/t5/revit-api/printermanager-printsetup-do-not-apply-settings/td-p/3676618

 

I hope this helps.

 

Cheers,

 

Jeremy



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

0 Likes
Message 3 of 8

Anonymous
Not applicable

Jeremy,

 

Thank you for the links. Unfortunatley they do not help much. It would be nice if it was possible to loop through the saved PrintSetup's, or at least get a PrintSetup if you knew it's name, so that you can delete it.

 

In the thread PrinterManager PrintSetup do not apply settings, the user aricke mentions:

 

Note that once you have done the saveas, you can then delete the newly saved printsetup.

I cannot seem to get that to work. even the following code will raise an exception

pSetup.SaveAs("tmp");
pSetup.Delete();

 

I appreciate any suggestions as to how to overcome/bypass this issue.

 

 

Cheers,

Eirik Aasved Holst

0 Likes
Message 4 of 8

Anonymous
Not applicable
Accepted solution

No worries, i finally managed to create a CleanUp-method that works. If others are interested, here it goes 

 

        private void CleanUp(Document doc)
        {
            var pMgr = doc.PrintManager;
            using (var trans = new Transaction(doc))
            {
                trans.Start("CleanUp");
                CleanUpTemporaryViewSheets(doc, pMgr);
                CleanUpTemporaryPrintSettings(doc, pMgr);
                trans.Commit();
            }
        }

        private void CleanUpTemporaryPrintSettings(Document doc, PrintManager pMgr)
        {
            var printSetup = pMgr.PrintSetup;
            foreach (var printSettingsToDelete in (from element in new FilteredElementCollector(doc).OfClass(typeof(PrintSetting)).ToElements()
                                                   where element.Name.Contains(_tmpName) && element.IsValidObject
                                                   select element as PrintSetting).ToList().Distinct(new EqualElementId()))
            {
                printSetup.CurrentPrintSetting = pMgr.PrintSetup.InSession;
                printSetup.CurrentPrintSetting = printSettingsToDelete as PrintSetting;
                pMgr.PrintSetup.Delete();
            }
        }

        private void CleanUpTemporaryViewSheets(Document doc, PrintManager pMgr)
        {
            var viewSheetSettings = pMgr.ViewSheetSetting;
            foreach (var viewSheetSetToDelete in (from element in new FilteredElementCollector(doc).OfClass(typeof(ViewSheetSet)).ToElements()
                                                  where element.Name.Contains(_tmpName) && element.IsValidObject
                                                  select element as ViewSheetSet).ToList().Distinct(new EqualElementId()))
            {
                viewSheetSettings.CurrentViewSheetSet = pMgr.ViewSheetSetting.InSession;
                viewSheetSettings.CurrentViewSheetSet = viewSheetSetToDelete as ViewSheetSet;
                pMgr.ViewSheetSetting.Delete();
            }
        }
Message 5 of 8

jeremytammik
Autodesk
Autodesk

Dear Eirik,

 

Congratulations on solving this!

 

Thank you for sharing!

 

You have my kudo.

 

Cheers,

 

Jeremy



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

0 Likes
Message 6 of 8

jeremytammik
Autodesk
Autodesk

Dear Eirik,

 

Thank you again for sharing this.

 

I published an edited version of this thread now:

 

http://thebuildingcoder.typepad.com/blog/2016/03/index-reloading-curves-distance-and-deleting-prints...

 

Cheers, 

 

Jeremy



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

0 Likes
Message 7 of 8

jeremytammik
Autodesk
Autodesk

Dear Eirik,

 

Two comments on this from The Building Coder, by Arif and Matt:

 

  1. by Arif Hanif: This is awesome, I was working on a solution that involved a similar workflow for print setup but was running into issues. This helped solve my issue of creating and print panel schedules from the MEP model.

 

  1. by Matt Taylor: Rolling back the transaction would nullify the need for this, wouldn't it?

 

Would you care to respond?

 

Thank you!

 

Cheers,

 

Jeremy

 


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

0 Likes
Message 8 of 8

Anonymous
Not applicable

Sure!

 

Arif, I'm very glad it helped! This issue (and similar) has been arround for a while it seems. The PrintManager is in genral rather unintuitive.

 

Matt, rolling back a transaction would indeed undo/remove saved PrintSettings and SheetSets made in the current transaction, but it is not given that the scope of the event is within the same transaction throughout the event. Nor is it given that one want to undo/remove all changes made to the document during the transaction. One might event want to run a separate function which sole purpose is to remove certain PrintSetups, SheetSets, etc. However, use of sub-transactions might nullify the need for this CleanUp-method. Thank you for your comment.

 

 

Cheers,

Eirik

Edit: Typo

0 Likes