def tempOutputPattern(self, sheet): """ Create a glob pattern to identify a printed PDF in the system output directory to be able to move it to its correct location and rename it according to the given template. Please note that the PDF network printer has to be configured to save PDFs following the below naming scheme:: [Revit File] - Sheet - [Sheet Number] - [Sheet Name].pdf For example:: Project1 - Sheet - A101 - Unnamed.pdf Args: sheet (object): A Revit sheet objetc Returns: string: The generated glob pattern """ import revitron nr = re.sub(r'[^a-zA-Z0-9]+', '*', revitron.Element(sheet).get('Sheet Number')) name = re.sub(r'[^a-zA-Z0-9]+', '*', revitron.Element(sheet).get('Sheet Name')) rvt = re.sub(r'\.rvt$', '', os.path.basename(revitron.DOC.PathName)) return '{}/{}*Sheet*{}*{}*.pdf'.format(self.output, rvt, nr, name)
def reCallback(self, match): """ The callback function used by the ``get()`` method. Args: match (object): The regex match object Returns: string: The processed string """ import revitron parameter = match.group(1) try: match = re.match('^%(.+?)%$', parameter) parameter = match.group(1) string = str(revitron.Element(self.projectInfo).get(parameter)) except: string = str(revitron.Element(self.element).get(parameter)) if self.sanitize: string = revitron.String.sanitize(string) return string
def exportSheet(self, sheet, directory, unit, template=False): """ Exports a sheet. Args: sheet (object): A Revit sheet directory (string): The export directory unit (object): The `export unit <https://www.revitapidocs.com/2020/1d3eb4f4-81d2-10a6-3eab-4a9c20e39053.htm>`_ template (string, optional): A name template. Defaults to '{Sheet Number}-{Sheet Name}'. Returns: string: The path of the exported PDF. False on error. """ import revitron if revitron.Element(sheet).getClassName() != 'ViewSheet': revitron.Log().warning('Element is not a sheet!') return False if not directory: revitron.Log().warning('There is no DWG export directory defined!') sys.exit() if not template: template = '{Sheet Number}-{Sheet Name}' fullPath = os.path.join( directory, revitron.ParameterTemplate(sheet, template).render() + '.dwg') path = os.path.dirname(fullPath) file = os.path.basename(fullPath) if not os.path.exists(path): os.makedirs(path) db = revitron.DB self.options.MergedViews = True self.options.TargetUnit = unit success = revitron.DOC.Export(path, file, List[db.ElementId]([sheet.Id]), self.options) if success: return fullPath return False
def exportSheet(self, sheet, directory, template=False): """ Exports a sheet. Args: sheet (object): A Revit sheet directory (string): The export directory template (string, optional): A name template. Defaults to '{Sheet Number}-{Sheet Name}'. Returns: bool: False on error, True on success """ import revitron if revitron.Element(sheet).getClassName() != 'ViewSheet': print(':face_with_rolling_eyes: Element is not a sheet!') return False if not directory: directory = self.output if not template: template = '{Sheet Number}-{Sheet Name}' fullPath = os.path.join( directory, revitron.ParameterTemplate(sheet, template).render() + '.dwg') path = os.path.dirname(fullPath) file = os.path.basename(fullPath) if not os.path.exists(path): os.makedirs(path) db = revitron.DB self.options.MergedViews = True self.options.TargetUnit = db.ExportUnit.Default success = revitron.DOC.Export(path, file, List[db.ElementId]([sheet.Id]), self.options) if success: return fullPath return False
def exportSchedule( self, schedule, directory, template=False, delimiter=';', hasTitle=False ): """ Exports a schedule. Args: schedule (object): A Revit schedule directory (string): A custom output directory. Defaults to False. template (string, optional): A name template. Defaults to '{View Name}'. delimiter (string, optional): A csv delimiter. Defaults to ';'. hasTitle (bool, optional): Set True to export schedule title. Defaults to False. Returns: string: The path of the exported CSV. False on error. """ import revitron if revitron.Element(schedule).getClassName() != 'ViewSchedule': revitron.Log().warning('Element is not a schedule!') return False if not directory: revitron.Log().warning('No directory specified!') return False if not template: template = '{View Name}' name = revitron.ParameterTemplate(schedule, template).render() + '.csv' if not os.path.exists(directory): os.makedirs(directory) options = revitron.DB.ViewScheduleExportOptions() options.FieldDelimiter = delimiter options.Title = hasTitle options.TextQualifier = revitron.DB.ExportTextQualifier['None'] schedule.Export(directory, name, options) file = os.path.join(directory, name) return file
def __init__(self, sheets): """ Inits an new ViewSheetList object. Args: sheets (list): A list with sheets or sheet ids """ import revitron self.views = [] for sheet in sheets: if isinstance(sheet, revitron.DB.ElementId): sheet = revitron.DOC.GetElement(sheet) if revitron.Element(sheet).getClassName() == 'ViewSheet': for viewId in sheet.GetAllPlacedViews(): item = revitron.AttrDict() item.id = viewId item.sheet = sheet item.view = revitron.DOC.GetElement(viewId) self.views.append(item)
def printSheet(self, sheet, size, orientation='Landscape', directory=False, template=False): """ Prints a sheet. Args: sheet (object): A Revit sheet size (string): A size name like A0 or A4 orientation (string, optional): The orientation, 'Landscape' or 'Portrait'. Defaults to 'Landscape'. directory (string, optional): A custom output directory. Defaults to False. template (string, optional): A name template. Defaults to '{Sheet Number}-{Sheet Name}'. Returns: bool: False on error, True on success """ import revitron if revitron.Element(sheet).getClassName() != 'ViewSheet': print(':face_with_rolling_eyes: Element is not a sheet!') return False if not directory: directory = self.output if not template: template = '{Sheet Number}-{Sheet Name}' path = os.path.join( directory, revitron.ParameterTemplate(sheet, template).render() + '.pdf') if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) transaction = revitron.Transaction() viewSet = revitron.DB.ViewSet() viewSet.Insert(sheet) viewSheetSetting = self.manager.ViewSheetSetting viewSheetSetting.CurrentViewSheetSet.Views = viewSet viewSheetSetting.SaveAs("_temp_") self.manager.PrintSetup.SaveAs("_temp_") self.manager.Apply() orientation = getattr(revitron.DB.PageOrientationType, orientation) # Set current print page settings. printParameters = self.manager.PrintSetup.CurrentPrintSetting.PrintParameters printParameters.ZoomType = revitron.DB.ZoomType.Zoom printParameters.Zoom = 100 printParameters.PaperPlacement = revitron.DB.PaperPlacementType.Center printParameters.PageOrientation = orientation printParameters.PaperSize = self.sizes[size] printParameters.RasterQuality = revitron.DB.RasterQualityType.High # Set in-session print settings. printParameters = self.manager.PrintSetup.InSession.PrintParameters printParameters.ZoomType = revitron.DB.ZoomType.Zoom printParameters.Zoom = 100 printParameters.PaperPlacement = revitron.DB.PaperPlacementType.Center printParameters.PageOrientation = orientation printParameters.PaperSize = self.sizes[size] printParameters.RasterQuality = revitron.DB.RasterQualityType.High # Again save settings. try: self.manager.PrintSetup.Save() except: self.manager.PrintSetup.SaveAs("_temp2_") self.manager.Apply() self.manager.SubmitPrint(sheet) viewSheetSetting.Delete() transaction.rollback() # Move file form temp output to directory. timePassed = time.time() moved = False while (time.time() - timePassed) < 30 and not moved: time.sleep(0.5) tempFiles = glob.glob(self.tempOutputPattern(sheet)) if tempFiles: tempFile = tempFiles[0] time.sleep(2) if os.access(tempFile, os.W_OK): try: shutil.move(tempFile, path) moved = True except: pass if moved: return path return False