Skip to content

Instantly share code, notes, and snippets.

@stepleton
Created May 3, 2024 22:26
Show Gist options
  • Save stepleton/928ceeaeae0dc3a83053133b8005fc16 to your computer and use it in GitHub Desktop.
Save stepleton/928ceeaeae0dc3a83053133b8005fc16 to your computer and use it in GitHub Desktop.
Rudimentary Calcomp to HPGL converter
#!/usr/bin/python3
# Copyright 2024 Google LLC.
# SPDX-License-Identifier: Apache-2.0
# Extremely limited Calcomp plotter language to HPGL converter, targeting
# specifically files output by code at
# https://gitlab.com/metagrowing/VZ.AND..NOT.VT.OR..NOT.VZ.AND.VT
from sys import stdin, stdout
from math import pi
stdout.write('IN;PU;SP1')
pen_up = False
xfix = lambda x_str: max(int(2e4*float(x_str) / 8 / pi), 0)
yfix = lambda y_str: max(1e4 - int(2e4*float(y_str) / 8 / pi), 0)
for line in stdin:
match line.strip().lower().split():
case ['plot', x_str, y_str, '2']:
stdout.write(f'{";PD" if pen_up else ","}{yfix(y_str)},{xfix(x_str)}')
pen_up = False
case ['plot', x_str, y_str, '3']:
stdout.write(f'{"," if pen_up else ";PU"}{yfix(y_str)},{xfix(x_str)}')
pen_up = True
case ['plot', x_str, y_str, '999']:
stdout.write(';IN;') # All done
@stepleton
Copy link
Author

NB: "Calcomp plotter language" here means the FORTRAN subroutine calls that would command the plotter to draw etc.
Here's a reference for Calcomp's FORTRAN API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment