PrintManager SaveAs Throws managed exception

PrintManager SaveAs Throws managed exception

daniel
Enthusiast Enthusiast
1,904 Views
7 Replies
Message 1 of 8

PrintManager SaveAs Throws managed exception

daniel
Enthusiast
Enthusiast

I have an application which prints multiple PDF files to file. 

 

This app is using Revits Printmanager to store settings. 

foreach print, I get the printmanger to set new settings. 

 

I think the issue may be occuring when trying to print multiple sheet sizes in one print session. 

 

then i create new print settings foreach size. 

 

Have anyone had these kinds of troubles ? 

 

Selecte new printdriver: 

var pm = _document.PrintManager;
pm.PrintSetup.CurrentPrintSetting = pm.PrintSetup.InSession;
pm.SelectNewPrintDriver(_pdfPrinter.Name);
pm = TrySaveAs($"Transmittal {new Random().Next().ToString()}", pm);

 

 

The try save as : 

private PrintManager TrySaveAs(string name, PrintManager pm, int attempt = 1)
{
 if (attempt > 10)
   throw new Exception($"Could not save print settings. Tried {attempt} times ...");
var saveResult = pm.PrintSetup.SaveAs(name);
if (!saveResult)
 return TrySaveAs(name + " " + attempt, pm, attempt += 1);

_lastUsedPrintSetup = name;
var pTTemp = new 
FilteredElementCollector(_document).OfClass(typeof(PrintSetting)).ToElement 
 s().FirstOrDefault(el => el.Name.Equals(name)) as PrintSetting;

pm.PrintSetup.CurrentPrintSetting = pTTemp;
return pm;
}

 

=== The Exception logged == 

Exception: A managed exception was thrown by Revit or by one of its external applications.
Stack trace: at Autodesk.Revit.DB.PrintSetup.SaveAs(String newName)
at FocusRevitAdvancedTools.Helpers.PDFPrinter.PDFExportHandler.TrySaveAs(String name, PrintManager pm, Int32 attempt) in D:\Focu...
at FocusRevitAdvancedTools.Helpers.PDFPrinter.PDFExportHandler.SetViewSpecificAutodeskPrintParameters(IPrintViewItem printViewItem) in D:\Fo..
at FocusRevitAdvancedTools.Helpers.PDFPrinter.PDFExportHandler.PrintPdf(IPrintViewItem printViewItem) in D:\Foc..
at FocusRevitAdvancedTools.Helpers.PDFPrinter.PDFExportHandler.Print(IEnumerable`1 viewsToPrint) in D:\Foc..
at FocusRevitAdvancedTools.Helpers.PDFPrinter.PDFExportHandler.PrintPDFs(IEnumerable`1 printViewItems) in D:\Foc..
at FocusRevitAdvancedTools.Tools.Basis.PDFExportTool.Execute(ExternalCommandData commandData, String& message, ElementSet elements) in D:\Foc..

0 Likes
1,905 Views
7 Replies
Replies (7)
Message 2 of 8

RPTHOMAS108
Mentor
Mentor

You should try catching the exception, a Managed Exception doesn't narrow it down much. According to Revit.chm the only exception thrown for .SaveAs is duplication of an existing setting name.

 

I note you are using the random class for this and what follows is likely not the cause of the issue but it is worth considering. Ideally you should create the Random() object elsewhere so it lives longer (not in the same function you call Random.Next).

 

"...on the .NET Framework only, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers. "

0 Likes
Message 3 of 8

daniel
Enthusiast
Enthusiast

The execption i posted, is the cathced exception, that is one of my problems. 

 

I have tried to narrow it down as much as possible. The think the error only occurs in projects where 

Revit post a messages "Printer X could not be used with settings Y in session will be used" 

 

In my problem project, i managed to get rid of the problem when I changed the printer setup from A0 to A3. 

(since moste printers don't print A0) 

 

I other words, there might be a problem when Document.PrintManager is invoked, and it throws this message. 

 

daniel_0-1599201259192.png

 

I have done som trial now, and it seems to be fine when switching to A4 or A3. This does so that the printmanger don't flag the printsetup as invalid for this printer. 

So I then need to create a settings, store it in the project and sett this setting as current. 

 

I have requested an API enhancement to be able to identify this situation when it occurs. 

 

Btw.

Tnx for the Random tip. I have now changed it to a variable on the class (instansiated only once)

But as excpected this was not the issue, but better code is better code 😃 

 

Daniel 

0 Likes
Message 4 of 8

RPTHOMAS108
Mentor
Mentor

Yes the PrintManager class and it's associated settings are not the easiest things to use.

 

You get that message usually because the printer doesn't support a paper size or an output tray etc. Looking over what I wrote years ago I am changing the printing device first and then the settings. I think there is also a certain amount of dialogue suppression going on in my solution because I couldn't quite get it to work as wanted i.e. you can't avoid these dialogues as even while getting the print manager it likes to tell you what is wrong with the current settings.

 

There is also worksharing to consider as the settings object is an element with the same ownership problems as other elements. 

0 Likes
Message 5 of 8

daniel
Enthusiast
Enthusiast

This is what I am doing now aswell, but at the same time I am trying to poke into Autodesk to get some information from the API when this things happening. 

 

Then I only need to do this men requierd, not all the time.. 

 

 

0 Likes
Message 6 of 8

jeremytammik
Autodesk
Autodesk

Dear Daniel,

 

Thank you for your query, and ever so many thanks to Richard for jumping in and providing advice and experience that goes way beyond anything I could suggest.

 

Can you narrow down the request you have for the development team a bit further? The more concrete it is, the better chances it will be understood and seriously considered.

 

Do you have a ticket number or description or hyperlink for the API enhancement request you mention, so I can highlight it to the development team?

 

Thank you!

 

By the way, this thread was escalated to a DAS support case 16909610 [PrintManager SaveAs Throws managed exception], and I will continue working on this with the development team to see how we can help further.

 

I see your other 16914514 [Error during multiple Pdf Export - when current settings is "Invalid" for Current printer] as well. Is that possibly the 'poke' you refer to above?

 

I suggest closing the latter as a duplicate of the former, so we can continue discussing this issue here in the forum where others can chip in and help and our thoughts and solutions are seen and shared by the entire community.

 

I hope that is OK.

 

Best regards,

 

Jeremy

 



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

0 Likes
Message 7 of 8

daniel
Enthusiast
Enthusiast

Hi, @jeremytammik . 

It is correct that the poke here and the support case is the same. 

So it's no problem closing the support case. (i posted it here due to the problems of finding the ADN support site) 

 

Here is the link to my ticket in the Revit Preview Forum (https://feedback.autodesk.com/project/forum/thread.html?cap=cb0fd5af18bb49b791dfa3f5efc47a72&forid={...) 

 

I this post I have explaind my problem again. 

 

I short, the problem is that print setup is saved to ex. A0, which is incompatible with most printers. Then Revit creates a message about this, and uses "In-session" settings. This is not a problem (if I can get rid of the message). The issue is when I then have this case where numerous sheets are to be printet, and all of them is identified as "custom sheets", then the printmanager is called to many times (one for each sheet) and this causes som internal error in Revit, which I can't get any more info about. 

 

I have discovered that the sheets in the project which had these problems where wrong size (because of some textnotes positions on the sheet, but that is another case) so fixing this, and setting the print setup to something other than A0 ex A4 solved my probles. 

 

This is why I have created the wish for API enhacement, so that I can get information from the print manager when there is incopatible print settings. Since Revit does display an dialog box on it, they know, and we should be able to respond to this problem. 

 

I think furter diging, might need code to be run by the Revit development team, but then I need to extract this out of my application. 

 

 

0 Likes
Message 8 of 8

jeremytammik
Autodesk
Autodesk

Dear Daniel,

 

Thank you very much for submitting this issue in the Revit Preview Forum.

 

That is definitely the best place for such a request and the most direct method to make your needs seen and understood by the development team.

 

I see an answer to your request confirming the need.

 

You might also want to submit a request for this functionality to the Revit Idea Station to enable others to vote for it and lend additional weight to the topic, helping to further raise its priority.

 

Best regards,

 

Jeremy

 



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

0 Likes