Added velocitySelect, some extra checks to the custom commands. Finally settled on a scaling strategy (Stick with the default IP but scale according to a given size (this size should ideally be the screen size openrndr is using on the screen)).

This commit is contained in:
David Diaz 2019-10-26 14:41:22 -06:00
parent 6c06d8ea37
commit adce888367
2 changed files with 36 additions and 37 deletions

View File

@ -18,11 +18,10 @@ fun main() = application {
val c = CompositionDrawer()
c.selectPen(1)
c.circle(Vector2(40.0, 40.0), 30.0)
c.rectangle(75.0, 60.0, 80.0, 200.0)
c.lineSegment(Vector2(0.0, 0.0), Vector2(200.0, 200.0))
c.composition.saveToHPGL(File("output.plt"))
c.composition.saveToHPGL(File("output.plt"), this.width, this.height)
this.application.exit()
drawer.background(ColorRGBa.WHITE)

View File

@ -4,44 +4,41 @@ import org.openrndr.math.Vector2
import org.openrndr.shape.*
import java.io.File
fun Composition.saveToHPGL(file: File) {
val plot = HPGL().generateFromComposition(this)
fun Composition.saveToHPGL(file: File, width: Int = 0, height: Int = 0) {
val plot = HPGL(width = width, height = height).generateFromComposition(this)
file.writeText(plot)
}
class HPGL(pageSize: PageSize = PageSize.A4) {
private var p1: Vector2
private var p2: Vector2
private val resolution = 35
init {
// TODO: Set these according to plotter
when (pageSize) {
PageSize.A3 -> {
p1 = Vector2(0.0, 0.0)
p2 = Vector2(0.0, 0.0)
}
PageSize.A4 -> {
p1 = Vector2(603.0, 521.0)
p2 = Vector2(10603.0, 7721.0)
}
}
}
class HPGL(
private val p1: Vector2 = Vector2.ZERO,
private val p2: Vector2 = Vector2.ZERO,
private val width: Int = 0,
private val height: Int = 0
) {
fun generateFromComposition(composition: Composition): String {
val result = StringBuilder()
result.appendln("IN;") // Initialize the plotter
result.appendln("IP${p1.getCoords()},${p2.getCoords()};") // Setup coordinates
result.appendln("SC0,${p2.div(10.0).x},0,${p2.div(10.0).y};") // Setup coordinates
// Setup coordinates
if (p1 != Vector2.ZERO || p2 != Vector2.ZERO) {
result.appendln("IP${p1.getCoords()},${p2.getCoords()};")
}
// Setup scaling
if (width == 0 && height == 0) {
result.appendln("SC;")
} else {
result.appendln("SC0,$width,0,$height;")
}
result.appendln("PA0,0;") // Plot absolutely
composition.root.traverse { stage ->
if (stage == Stage.Before) {
when (this) {
is ShapeNode -> {
this.shape.contours.forEach { shape ->
for (shape in this.shape.contours) {
val segments = shape.sampleLinear().segments
segments.forEachIndexed { i, segment ->
if (i == 0) {
@ -60,11 +57,15 @@ class HPGL(pageSize: PageSize = PageSize.A4) {
is SelectPen -> {
result.appendln("SP${this.penNumber};")
}
is VelocitySelect -> {
result.appendln("VS${this.velocity};")
}
}
}
}
result.appendln("PA0,0")
result.appendln("PA0,0;")
return result.toString()
}
}
@ -73,11 +74,6 @@ private fun Vector2.getCoords(): String {
return "$x,$y"
}
enum class PageSize {
A4,
A3
}
private enum class Stage {
Before,
After
@ -93,14 +89,18 @@ private fun CompositionNode.traverse(cb: CompositionNode.(stage: Stage) -> Unit)
this.cb(Stage.After)
}
// HPGL Custom Instructions
class SelectPen(val penNumber: Int) : GroupNode()
class VelocitySelect(val velocity: Int) : GroupNode()
fun CompositionDrawer.selectPen(pen: Int) {
require(pen >= 0) { "The pen can only be 0 or greater." }
this.root.children.add(SelectPen(pen))
}
fun CompositionDrawer.velocitySelect(velocity: Int) {
class VelocitySelect(val velocity: Double) : GroupNode()
fun CompositionDrawer.velocitySelect(velocity: Double) {
require(velocity in 0.0..127.9999) { "Invalid velocity value. The velocity should be a value between 0 and 127.9999" }
this.root.children.add(VelocitySelect(velocity))
}