PrintToFile returns, but doesn't create a file

PrintToFile returns, but doesn't create a file

nate_gordon
Participant Participant
252 Views
6 Replies
Message 1 of 7

PrintToFile returns, but doesn't create a file

nate_gordon
Participant
Participant

I'm using the Inventor API with C# and trying to get some drawings as PDFs. Unfortunately it just isn't saving the output file. The method returns as if it was successful, but there just isn't any output file. If I manually print to a file with the same settings from the UI it works as expected. And usually the PrintToFile will succeed for that specific file afterwards until I restart Inventor. So it feels like something is getting cached in the app, but I can't figure out what.

 

Here is a code snippet showing what I am doing:

var drawing = app.Documents.Open(drawingFile, false);
var printManager = drawing.PrintManager as DrawingPrintManager;
printManager.Printer = "Microsoft Print to PDF";
printManager.PrintRange = PrintRangeEnum.kPrintAllSheets;
printManager.ScaleMode = PrintScaleModeEnum.kPrintBestFitScale;
printManager.PrintToFile(pdfFile);

 

0 Likes
253 Views
6 Replies
Replies (6)
Message 2 of 7

Zach.Stauffer
Advocate
Advocate

I use the built-in PDF Addin, seems to work fine for me.

		public static void Print(DrawingDocument drawingDoc, Application invApp, string outputPath = "")
		{
			TranslatorAddIn PDFAddin = (TranslatorAddIn)invApp.ApplicationAddIns.ItemById["{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}"];
			TranslationContext context = invApp.TransientObjects.CreateTranslationContext();
			NameValueMap options = invApp.TransientObjects.CreateNameValueMap();
			DataMedium dataMedium = invApp.TransientObjects.CreateDataMedium();

			string filepath;

			if (outputPath != "") filepath = outputPath;
			else filepath = SettingService.GetSetting("DrawingExportPath");

			string fileName = System.IO.Path.GetFileNameWithoutExtension(drawingDoc.DisplayName);
			string revision = (string)drawingDoc.PropertySets["Inventor Summary Information"]["Revision Number"].Value;
			var revisionText = revision == "1" ? "" : $" rev {revision}";

			context.Type = IOMechanismEnum.kFileBrowseIOMechanism;

			if (!PDFAddin.Activated) PDFAddin.Activate();

			options.Value["All_Color_AS_Black"] = 0;
			options.Value["Remove_Line_Weights"] = 0;
			options.Value["Vector_Resolution"] = 400;
			options.Value["Sheet_Range"] = PrintRangeEnum.kPrintAllSheets;

			dataMedium.FileName = $"{filepath}{fileName}{revisionText}.pdf";

			foreach (Sheet sheet in drawingDoc.Sheets)
			{
				sheet.Activate();
				sheet.Update();
				foreach (DrawingView view in sheet.DrawingViews)
				{
					do
					{
						invApp.UserInterfaceManager.DoEvents();
						invApp.StatusBarText = "Updating drawing views";
					} while (view.IsUpdateComplete == false);
				}
			}

			PDFAddin.SaveCopyAs(drawingDoc, context, options, dataMedium);
		}
0 Likes
Message 3 of 7

nate_gordon
Participant
Participant

I have experimented with this method, but my drawings currently trigger a warning prompt to the user during export. So until I can resolve that issue (which I am exploring) I have to use print. But even if that was resolved, PrintToFile shouldn't just return as if it succeeded when it actually didn't.

0 Likes
Message 4 of 7

Zach.Stauffer
Advocate
Advocate

I thought PrintToFile() was a void function with no return type. If there's no built-in error handling in that method you'll never know if or how it failed.

0 Likes
Message 5 of 7

nate_gordon
Participant
Participant

Yes, it is a void method. It should throw an exception if it failed like other APIs currently do.

0 Likes
Message 6 of 7

Zach.Stauffer
Advocate
Advocate

The Inventor API is full of functions that don't throw exceptions if they fail.

0 Likes
Message 7 of 7

Michael.Navara
Advisor
Advisor

Hi @nate_gordon 

You are not able to print the file, which is not visible. Try to change the first line of your code to this

var drawing = app.Documents.Open(drawingFile, true);

 

But as @Zach.Stauffer mentined above, it is much better to use TranslatorAddIn instead of PrintManager.

0 Likes