export_3d.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 psutil
  19. import sys
  20. import time
  21. electronics_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  22. repo_root = os.path.dirname(electronics_root)
  23. sys.path.append(repo_root)
  24. from util import file_util
  25. from export_util import (
  26. patch_config,
  27. PopenContext,
  28. versioned_file,
  29. xdotool,
  30. wait_for_window,
  31. recorded_xvfb,
  32. )
  33. logging.basicConfig(level=logging.DEBUG)
  34. logger = logging.getLogger(__name__)
  35. RENDER_TIMEOUT = 10 * 60
  36. def _wait_for_pcbnew_idle():
  37. start = time.time()
  38. while time.time() < start + RENDER_TIMEOUT:
  39. for proc in psutil.process_iter():
  40. if proc.name() == 'pcbnew':
  41. cpu = proc.cpu_percent(interval=1)
  42. print(f'CPU={cpu}', flush=True)
  43. if cpu < 5:
  44. print('Render took %d seconds' % (time.time() - start))
  45. return
  46. time.sleep(1)
  47. raise RuntimeError('Timeout waiting for pcbnew to go idle')
  48. def _zoom_in():
  49. xdotool([
  50. 'click',
  51. '4',
  52. ])
  53. time.sleep(0.2)
  54. def _invoke_view_option(index):
  55. command = ['key', 'alt+v'] + ['Down']*index + ['Return']
  56. xdotool(command)
  57. time.sleep(2)
  58. _transforms = {
  59. 'z+': ('Zoom in', _zoom_in),
  60. 'rx+': ('Rotate X Clockwise', lambda: _invoke_view_option(4)),
  61. 'rx-': ('Rotate X Counterclockwise', lambda: _invoke_view_option(5)),
  62. 'ry+': ('Rotate Y Clockwise', lambda: _invoke_view_option(6)),
  63. 'ry-': ('Rotate Y Counterclockwise', lambda: _invoke_view_option(7)),
  64. 'rz+': ('Rotate Z Clockwise', lambda: _invoke_view_option(8)),
  65. 'rz-': ('Rotate Z Counterclockwise', lambda: _invoke_view_option(9)),
  66. 'ml': ('Move left', lambda: _invoke_view_option(10)),
  67. 'mr': ('Move right', lambda: _invoke_view_option(11)),
  68. 'mu': ('Move up', lambda: _invoke_view_option(12)),
  69. 'md': ('Move down', lambda: _invoke_view_option(13)),
  70. }
  71. def _pcbnew_export_3d(output_file, width, height, transforms):
  72. if os.path.exists(output_file):
  73. os.remove(output_file)
  74. wait_for_window('pcbnew', 'Pcbnew ', additional_commands=['windowfocus'])
  75. time.sleep(1)
  76. logger.info('Open 3d viewer')
  77. xdotool(['key', 'alt+3'])
  78. wait_for_window('3D Viewer', '3D Viewer', additional_commands=['windowfocus'])
  79. time.sleep(3)
  80. # Maximize window
  81. xdotool(['search', '--name', '3D Viewer', 'windowmove', '0', '0'])
  82. xdotool(['search', '--name', '3D Viewer', 'windowsize', str(width), str(height)])
  83. time.sleep(3)
  84. for transform in transforms:
  85. description, func = _transforms[transform]
  86. logger.info(description)
  87. func()
  88. logger.info('Wait for rendering...')
  89. _wait_for_pcbnew_idle()
  90. time.sleep(5)
  91. logger.info('Export current view')
  92. xdotool([
  93. 'key',
  94. 'alt+f',
  95. 'Return',
  96. ])
  97. logger.info('Enter build output filename')
  98. xdotool([
  99. 'key',
  100. 'ctrl+a',
  101. ])
  102. xdotool(['type', output_file])
  103. logger.info('Save')
  104. xdotool(['key', 'Return'])
  105. logger.info('Wait before shutdown')
  106. time.sleep(2)
  107. def export_3d(filename, suffix, width, height, transforms, raytrace, virtual, color_soldermask, color_silk):
  108. pcb_file = os.path.abspath(filename)
  109. output_dir = os.path.join(electronics_root, 'build')
  110. file_util.mkdir_p(output_dir)
  111. screencast_output_file = os.path.join(output_dir, 'export_3d_screencast.ogv')
  112. name, _ = os.path.splitext(os.path.basename(pcb_file))
  113. if suffix:
  114. name = name + '-' + suffix
  115. output_file = os.path.join(output_dir, f'{name}-3d.png')
  116. settings = {
  117. 'canvas_type': '1',
  118. 'SMaskColor_Red': str(color_soldermask[0]),
  119. 'SMaskColor_Green': str(color_soldermask[1]),
  120. 'SMaskColor_Blue': str(color_soldermask[2]),
  121. 'SilkColor_Red': str(color_silk[0]),
  122. 'SilkColor_Green': str(color_silk[1]),
  123. 'SilkColor_Blue': str(color_silk[2]),
  124. 'RenderEngine': '1' if raytrace else '0',
  125. 'ShowFootprints_Virtual': '1' if virtual else '0',
  126. 'Render_RAY_ProceduralTextures': '0',
  127. }
  128. with patch_config(os.path.expanduser('~/.config/kicad/pcbnew'), settings):
  129. with versioned_file(pcb_file):
  130. with recorded_xvfb(screencast_output_file, width=width, height=height, colordepth=24):
  131. with PopenContext(['pcbnew', pcb_file], close_fds=True) as pcbnew_proc:
  132. _pcbnew_export_3d(output_file, width, height, transforms)
  133. pcbnew_proc.terminate()
  134. if __name__ == '__main__':
  135. parser = argparse.ArgumentParser()
  136. parser.add_argument('pcb')
  137. parser.add_argument('--suffix', default='')
  138. parser.add_argument('--width', type=int, default=2560)
  139. parser.add_argument('--height', type=int, default=1440)
  140. parser.add_argument('--skip-raytrace', action='store_true')
  141. parser.add_argument('--skip-virtual', action='store_true', help='Don\'t render virtual footprints')
  142. parser.add_argument('--color-soldermask', type=float, nargs=3, help='Soldermask color as 3 floats from 0-1', default=[0.1, 0.1, 0.1])
  143. parser.add_argument('--color-silk', type=float, nargs=3, help='Silkscreen color as 3 floats from 0-1', default=[0.9, 0.9, 0.9])
  144. # Use subparsers to for an optional nargs="*" choices argument (workaround for https://bugs.python.org/issue9625)
  145. subparsers = parser.add_subparsers(dest='which')
  146. transform_parser = subparsers.add_parser('transform', help='Apply one or more transforms before capturing image')
  147. transform_parser.add_argument('transform', nargs='+', choices=list(_transforms.keys()))
  148. args = parser.parse_args()
  149. transforms = args.transform if args.which == 'transform' else []
  150. export_3d(args.pcb, args.suffix, args.width, args.height, transforms, not args.skip_raytrace, not args.skip_virtual, args.color_soldermask, args.color_silk)