Skip to content

Instantly share code, notes, and snippets.

@j0hnm4r5
Created November 2, 2014 21:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j0hnm4r5/b834d6a845a822257818 to your computer and use it in GitHub Desktop.
Save j0hnm4r5/b834d6a845a822257818 to your computer and use it in GitHub Desktop.
Rhino to HPGL
'''This script creates an HPGL file from curves in a Rhino file.
Place all curves in the model world as they would sit in relation to the origin on the plotter.
File will export to same location as script.'''
import rhinoscriptsyntax as rs
from datetime import datetime
import os.path
filename = rs.GetString("What would you like to name the file? (Use \"#\" for ISO 8601) ", defaultString="output#")
if "#" in filename:
filename = filename[:-1]
ISO_8601 = datetime.now().strftime('%Y%m%dT%H%M%S')
filename = os.path.abspath('/Users/johnmars/Desktop/' + filename + ISO_8601 + '.hpgl')
hpglOut = file(filename, 'w')
else:
filename = os.path.abspath('/Users/johnmars/Desktop/' + filename + '.hpgl')
hpglOut = file(filename, 'w')
pen = 1
plotter_step = .025 #mm
plotter_step /= 25.4 #convert to in
# Changes the model units to inches, but does not scale model.
rs.UnitSystem(unit_system=8, scale=False)
# Initialize Plotter
hpglOut.write('IN;\n')
hpglOut.write('SP' + str(pen) + ';\n')
allCurves = rs.ObjectsByType(4)
for curve in allCurves:
if (rs.CurveDegree(curve) == 2 or rs.CurveDegree(curve) == 3) and rs.IsPolyCurve(curve):
rs.ExplodeCurves(curve, True)
allCurves = rs.ObjectsByType(4)
for curve in allCurves:
if rs.CurveDegree(curve) == 1: #polyline or line
points = rs.CurveEditPoints(curve)
print "1", points
elif rs.CurveDegree(curve) >= 2: #polycurve or curve
points = rs.DivideCurveLength(curve, .01)
print "2", points
if not points:
continue
# PU to the first point
x = points[0][0]
y = points[0][1]
hpglOut.write('PU' + str(int(x * (1 / plotter_step))) + ',' + str(int(y * (1 / plotter_step))) + ';\n')
# PD to every subsequent point
i = 1
while i < len(points):
x = points[i][0]
y = points[i][1]
hpglOut.write('PD' + str(int(x * (1 / plotter_step))) + ',' + str(int(y * (1 / plotter_step))) + ';\n')
i += 1
hpglOut.write('SP0;\n')
hpglOut.close()
print "Done! File exported as" + filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment