From adce88836700671252a835fc4dec4444aafa4c8e Mon Sep 17 00:00:00 2001 From: David Diaz Date: Sat, 26 Oct 2019 14:41:22 -0600 Subject: [PATCH] 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)). --- src/main/kotlin/TemplateProgram.kt | 5 +- .../kotlin/com/martianwabbit/hpglWriter.kt | 68 +++++++++---------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/main/kotlin/TemplateProgram.kt b/src/main/kotlin/TemplateProgram.kt index 2fa1562..bd562e5 100644 --- a/src/main/kotlin/TemplateProgram.kt +++ b/src/main/kotlin/TemplateProgram.kt @@ -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) diff --git a/src/main/kotlin/com/martianwabbit/hpglWriter.kt b/src/main/kotlin/com/martianwabbit/hpglWriter.kt index 04a7400..a06b40c 100644 --- a/src/main/kotlin/com/martianwabbit/hpglWriter.kt +++ b/src/main/kotlin/com/martianwabbit/hpglWriter.kt @@ -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) { + 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)) }