generate_pdf.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. # Copyright 2015-2021 Scott Bezek and the splitflap contributors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import argparse
  16. import logging
  17. import os
  18. import pcbnew
  19. import shutil
  20. import subprocess
  21. from collections import namedtuple
  22. import pcb_util
  23. electronics_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  24. logging.basicConfig(level=logging.DEBUG)
  25. logger = logging.getLogger(__name__)
  26. def run(pcb_file):
  27. output_directory = os.path.join(electronics_root, 'build')
  28. temp_dir = os.path.join(output_directory, 'temp_pdfs')
  29. shutil.rmtree(temp_dir, ignore_errors=True)
  30. try:
  31. os.makedirs(temp_dir)
  32. plot_to_directory(pcb_file, output_directory, temp_dir)
  33. finally:
  34. shutil.rmtree(temp_dir, ignore_errors=True)
  35. def plot_to_directory(pcb_file, output_directory, temp_dir):
  36. board_name = os.path.splitext(os.path.basename(pcb_file))[0]
  37. with pcb_util.get_plotter(pcb_file, temp_dir) as plotter:
  38. plotter.plot_options.SetDrillMarksType(pcbnew.PCB_PLOT_PARAMS.NO_DRILL_SHAPE)
  39. plotter.plot_options.SetExcludeEdgeLayer(False)
  40. LayerDef = namedtuple('LayerDef', ['layer', 'mirror'])
  41. layers = [
  42. LayerDef(pcbnew.F_Cu, False),
  43. LayerDef(pcbnew.B_Cu, True),
  44. LayerDef(pcbnew.F_SilkS, False),
  45. LayerDef(pcbnew.B_SilkS, True),
  46. LayerDef(pcbnew.F_Mask, False),
  47. LayerDef(pcbnew.B_Mask, True),
  48. LayerDef(pcbnew.F_Paste, False),
  49. ]
  50. pdfs = []
  51. for layer in layers:
  52. plotter.plot_options.SetMirror(layer.mirror)
  53. output_filename = plotter.plot(layer.layer, pcbnew.PLOT_FORMAT_PDF)
  54. pdfs.append(output_filename)
  55. _, map_file = plotter.plot_drill()
  56. pdfs.append(map_file)
  57. output_pdf_filename = os.path.join(output_directory, '%s-pcb-packet.pdf' % (board_name,))
  58. command = ['pdfunite'] + pdfs + [output_pdf_filename]
  59. subprocess.check_call(command)
  60. if __name__ == '__main__':
  61. parser = argparse.ArgumentParser('Generate a pdf of the PCB')
  62. parser.add_argument('pcb_file')
  63. args = parser.parse_args()
  64. run(args.pcb_file)