Hello Dynamo Friends
I have a code to create a print setting with all parameters i want to set.
Now i want to print and set the parameters only in session without saving a print setting.
I use the code from @Konrad_K_Sobon, revised by @viktor_kuzev, @ Michael Park and @Alban_de_Chasteigner
I added the following parts:
get in session setting.
printSettings = FilteredElementCollector(doc).OfClass(PrintSetting)
for ps in printSettings:
if ps.Name == "<in session>":
printSetting = ps
and setting the orientation:
# apply print setting
try:
printSetup = printManager.PrintSetup
printSetup.CurrentPrintSetting = printSetting
printManager.Apply()
# set orientation
printManager.PrintSetup.CurrentPrintSetting.PrintParameters.PageOrientation = printManager.PrintSetup.CurrentPrintSetting.PrintParameters.PageOrientation.Landscape
printManager.Apply()
But it does not work, my PageOrientation setting has no effect.
So how can i make changes to the printsetting before printing?
Happy about any advice!
Full Code:
#Copyright(c) 2015, Konrad K Sobon
# @arch_laboratory, http://archi-lab.net
# Revised by viktor_kuzev
# Revised by Michael Park
# Revised by Alban de Chasteigner
import clr
# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
import System
import sys
sys.path.append(r"C:\Program Files (x86)\IronPython 2.7\Lib")
import os
filePath = ["C:\\Print\\Test.pdf"]
sheets = IN[0]
printSettings = FilteredElementCollector(doc).OfClass(PrintSetting)
for ps in printSettings:
if ps.Name == "<in session>":
printSetting = ps
printSettings = [printSetting]
printerName = "Microsoft Print to PDF"
pRange = System.Enum.Parse(Autodesk.Revit.DB.PrintRange, "Select")
combined = True
runIt = True
if isinstance(filePath, list):
s_length = len(sheets)
fp_length = len(filePath)
if len(sheets) == len(filePath):
pass
else:
filePath = filePath * (s_length / fp_length)
if len(filePath) == len(sheets):
pass
else:
OUT = "Filepaths and Sheets weren't the same length"
exit()
elif isinstance(filePath, str):
filePath = [filePath] * len(sheets)
if isinstance(sheets, list):
viewSheets = []
for i in sheets:
viewSheets.append(UnwrapElement(i))
else:
viewSheets = UnwrapElement(sheets)
if isinstance(printSettings, list):
printSetting = []
for i in printSettings:
printSetting.append(UnwrapElement(i))
else:
printSetting = UnwrapElement(printSettings)
TransactionManager.Instance.EnsureInTransaction(doc)
printManager = doc.PrintManager
printSetup = printManager.PrintSetup
printManager.SelectNewPrintDriver(printerName)
if isinstance(printSettings, list):
printSetup.CurrentPrintSetting = printSetting[0]
else:
printSetup.CurrentPrintSetting = printSetting
printManager.Apply()
TransactionManager.Instance.TransactionTaskDone()
def PrintView(doc, sheet, pRange, printerName, combined, filePath, printSetting):
# create view set
viewSet = ViewSet()
viewSet.Insert(sheet)
# determine print range
printManager = doc.PrintManager
printManager.PrintRange = pRange
printManager.Apply()
# make new view set current
viewSheetSetting = printManager.ViewSheetSetting
viewSheetSetting.CurrentViewSheetSet.Views = viewSet
# set printer
printManager.SelectNewPrintDriver(printerName)
printManager.Apply()
# set combined and print to file
if printManager.IsVirtual:
printManager.CombinedFile = combined
printManager.Apply()
printManager.PrintToFile = True
printManager.Apply()
else:
printManager.CombinedFile = combined
printManager.Apply()
printManager.PrintToFile = False
printManager.Apply()
# set file path
printManager.PrintToFileName = filePath
# delete file. will prevent prompt for override existing
if os.path.exists(filePath):
os.remove(filePath)
printManager.Apply()
# apply print setting
try:
printSetup = printManager.PrintSetup
printSetup.CurrentPrintSetting = printSetting
printManager.Apply()
# set orientation
printManager.PrintSetup.CurrentPrintSetting.PrintParameters.PageOrientation = printManager.PrintSetup.CurrentPrintSetting.PrintParameters.PageOrientation.Landscape
printManager.Apply()
except:
pass
# save settings and submit print
TransactionManager.Instance.EnsureInTransaction(doc)
viewSheetSetting.SaveAs("tempSetName")
printManager.Apply()
printManager.SubmitPrint()
viewSheetSetting.Delete()
TransactionManager.Instance.TransactionTaskDone()
return True
try:
viewSets = FilteredElementCollector(doc).OfClass(ViewSheetSet)
for i in viewSets:
if i.Name == "tempSetName":
TransactionManager.Instance.EnsureInTransaction(doc)
doc.Delete(i.Id)
TransactionManager.Instance.ForceCloseTransaction()
else:
continue
errorReport = None
message = "Success"
if runIt:
if isinstance(viewSheets, list) and isinstance(printSetting, list):
for i, j, fp in zip(viewSheets, printSetting, filePath):
PrintView(doc, i, pRange, printerName, combined, fp, j)
elif isinstance(viewSheets, list) and not isinstance(printSetting, list):
for i, fp in zip(viewSheets, filePath):
PrintView(doc, i, pRange, printerName, combined, fp, printSetting)
elif not isinstance(viewSheets, list) and not isinstance(printSetting, list):
PrintView(doc, viewSheets, pRange, printerName, combined, filePath[0], printSetting)
else:
message = "Set RunIt to True"
except:
# if error accurs anywhere in the process catch it
import traceback
errorReport = traceback.format_exc()
# Assign your output to the OUT variable
if errorReport is None:
OUT = message
else:
OUT = errorReport
edit:
I still could create and delete a tempPrintSetting if i find no other method, just wondering why my method doesn´t work.