Skip to content

Commit

Permalink
fixed issue #1082 [tool]
Browse files Browse the repository at this point in the history
  • Loading branch information
eirannejad committed Mar 22, 2021
1 parent d600412 commit 0f1e51a
Showing 1 changed file with 57 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-
#pylint: disable=unused-argument,too-many-lines
#pylint: disable=missing-function-docstring,missing-class-docstring
"""Print sheets in order from a sheet index.
Note:
When using the `Combine into one file` option,
the tool adds non-printable character u'\u200e'
(Left-To-Right Mark) at the start of the sheet names
to push Revit's interenal printing engine to sort
the sheets correctly per the drawing index order.
the sheets correctly per the drawing index order.
Make sure your drawings indices consider this
when filtering for sheet numbers.
Expand Down Expand Up @@ -58,19 +60,22 @@


class NamingFormat(forms.Reactive):
"""Print File Naming Format"""
def __init__(self, name, template, builtin=False):
self._name = name
self._template = self.verify_template(template)
self.builtin = builtin

@staticmethod
def verify_template(value):
"""Verify template is valid"""
if not value.lower().endswith('.pdf'):
value += '.pdf'
return value

@forms.reactive
def name(self):
"""Format name"""
return self._name

@name.setter
Expand All @@ -79,6 +84,7 @@ def name(self, value):

@forms.reactive
def template(self):
"""Format template string"""
return self._template

@template.setter
Expand All @@ -87,6 +93,8 @@ def template(self, value):


class ViewSheetListItem(forms.Reactive):
"""Revit Sheet show in Print Window"""

def __init__(self, view_sheet, view_tblock,
print_settings=None, rev_settings=None):
self._sheet = view_sheet
Expand Down Expand Up @@ -128,18 +136,22 @@ def __init__(self, view_sheet, view_tblock,

@property
def revit_sheet(self):
"""Revit sheet instance"""
return self._sheet

@property
def revit_tblock(self):
"""Revit titleblock instance"""
return self._tblock

@property
def revit_tblock_type(self):
"""Revit titleblock type"""
return self._tblock_type

@forms.reactive
def print_settings(self):
"""Sheet pring settings"""
return self._print_settings

@print_settings.setter
Expand All @@ -148,6 +160,7 @@ def print_settings(self, value):

@forms.reactive
def print_index(self):
"""Sheet print index"""
return self._print_index

@print_index.setter
Expand All @@ -156,6 +169,7 @@ def print_index(self, value):

@forms.reactive
def print_filename(self):
"""Sheet print output filename"""
return self._print_filename

@print_filename.setter
Expand All @@ -165,6 +179,8 @@ def print_filename(self, value):


class PrintSettingListItem(forms.TemplateListItem):
"""Print Setting shown in Print Window"""

def __init__(self, print_settings=None):
super(PrintSettingListItem, self).__init__(print_settings)
self.is_compatible = \
Expand Down Expand Up @@ -472,9 +488,9 @@ def __init__(self, xaml_file_name):
@property
def selected_doc(self):
selected_doc = self.documents_cb.SelectedItem
for open_doc in revit.docs:
if open_doc.GetHashCode() == selected_doc.hash:
return open_doc
for opened_doc in revit.docs:
if opened_doc.GetHashCode() == selected_doc.hash:
return opened_doc

@property
def selected_schedule(self):
Expand Down Expand Up @@ -504,7 +520,7 @@ def index_digits(self):

@property
def index_start(self):
return int(self.indexstart_tb.Text or 0)
return int(self.indexstart_tb.Text or 0)

@property
def include_placeholders(self):
Expand Down Expand Up @@ -658,7 +674,8 @@ def _setup_printers(self):
print_mgr = self._get_printmanager()
self.printers_cb.SelectedItem = print_mgr.PrinterName

def _get_psetting_items(self, doc, psettings=None, include_varsettings=False):
def _get_psetting_items(self, doc,
psettings=None, include_varsettings=False):
if include_varsettings:
psetting_items = [VariablePaperPrintSettingListItem()]
else:
Expand Down Expand Up @@ -709,7 +726,7 @@ def _setup_print_settings(self):
def _update_combine_option(self):
self.enable_element(self.combine_cb)
if self.selected_doc.IsLinked \
or ((self.selected_schedule and self.has_print_settings)
or ((self.selected_schedule and self.has_print_settings)
and self.selected_print_setting.allows_variable_paper):
self.disable_element(self.combine_cb)
self.combine_cb.IsChecked = False
Expand All @@ -722,6 +739,16 @@ def _setup_sheet_list(self):
else:
self.disable_element(self.schedules_cb)

def _verify_print_filename(self, sheet_name, sheet_print_filepath):
if op.exists(sheet_print_filepath):
logger.warning(
"Skipping sheet \"%s\" "
"File already exist at %s.",
sheet_name, sheet_print_filepath
)
return False
return True

def _print_combined_sheets_in_order(self, target_sheets):
# make sure we can access the print config
print_mgr = self._get_printmanager()
Expand Down Expand Up @@ -814,12 +841,14 @@ def _print_combined_sheets_in_order(self, target_sheets):
forms.alert(str(e) +
'\nSet printer correctly in Print settings.')
script.exit()
print_filepath = op.join(r'C:\\', 'Ordered Sheet Set.pdf')
print_mgr.PrintToFile = True
print_mgr.PrintToFileName = \
op.join(r'C:\\', 'Ordered Sheet Set.pdf')
print_mgr.PrintToFileName = print_filepath

print_mgr.Apply()
print_mgr.SubmitPrint()


# now fix the sheet names
with revit.Transaction('Restore Sheet Numbers',
doc=self.selected_doc):
Expand All @@ -828,6 +857,8 @@ def _print_combined_sheets_in_order(self, target_sheets):
rvtsheet = sheet.revit_sheet
rvtsheet.SheetNumber = sheetnum

self._reset_psettings()

def _print_sheets_in_order(self, target_sheets):
# make sure we can access the print config
print_mgr = self._get_printmanager()
Expand All @@ -851,15 +882,18 @@ def _print_sheets_in_order(self, target_sheets):
for sheet in target_sheets:
if sheet.printable:
if sheet.print_filename:
print_mgr.PrintToFileName = \
print_filepath = \
op.join(USER_DESKTOP, sheet.print_filename)
print_mgr.PrintToFileName = print_filepath

# set the per-sheet print settings if required
if per_sheet_psettings:
print_mgr.PrintSetup.CurrentPrintSetting = \
sheet.print_settings

print_mgr.SubmitPrint(sheet.revit_sheet)
if self._verify_print_filename(sheet.name,
print_filepath):
print_mgr.SubmitPrint(sheet.revit_sheet)
else:
logger.debug(
'Sheet %s does not have a valid file name.',
Expand All @@ -879,9 +913,11 @@ def _print_linked_sheets_in_order(self, target_sheets):
# print_mgr.PrintSetup.CurrentPrintSetting =
for sheet in target_sheets:
if sheet.printable:
print_mgr.PrintToFileName = \
op.join(USER_DESKTOP, sheet.print_filename)
print_mgr.SubmitPrint(sheet.revit_sheet)
print_filepath = op.join(USER_DESKTOP, sheet.print_filename)
print_mgr.PrintToFileName = print_filepath

if self._verify_print_filename(sheet.name, print_filepath):
print_mgr.SubmitPrint(sheet.revit_sheet)
else:
logger.debug(
'Linked sheet %s is not printable. Skipping print.',
Expand Down Expand Up @@ -1027,7 +1063,8 @@ def _get_sheet_printsettings(self, tblocks, psettings):
psetting_name = psparam.AsString()
psparam_psetting = \
next(
(x for x in psettings if x.Name == psetting_name),
(x for x in psettings
if x.Name == psetting_name),
None
)
if psparam_psetting:
Expand Down Expand Up @@ -1056,7 +1093,8 @@ def _get_sheet_printsettings(self, tblocks, psettings):
def _reset_psettings(self):
if self._init_psettings:
print_mgr = self._get_printmanager()
print_mgr.PrintSetup.CurrentPrintSetting = self._init_psettings
with revit.Transaction("Revert to Original Print Settings"):
print_mgr.PrintSetup.CurrentPrintSetting = self._init_psettings

def _update_index_slider(self):
index_digits = \
Expand Down Expand Up @@ -1085,7 +1123,9 @@ def sheetlist_changed(self, sender, args):
sheet_printsettings = \
self._get_sheet_printsettings(
tblocks,
revit.query.get_all_print_settings(doc=self.selected_doc)
revit.query.get_all_print_settings(
doc=self.selected_doc
)
)
self.show_element(self.varsizeguide)
self.show_element(self.psettingcol)
Expand Down Expand Up @@ -1281,7 +1321,6 @@ def print_sheets(self, sender, args):
self._print_linked_sheets_in_order(target_sheets)
else:
self._print_sheets_in_order(target_sheets)
self._reset_psettings()


def cleanup_sheetnumbers(doc):
Expand Down

0 comments on commit 0f1e51a

Please sign in to comment.