User:Lucatorsera/Prototyping: Difference between revisions

From XPUB & Lens-Based wiki
Line 41: Line 41:


== Text2Graph ==
== Text2Graph ==
Motivated by the experiments above, I developed a python script that converts sentences to graphs using Graphviz within its code. The script takes an input .txt file, removes any spaces/punctuation and replaces them with "->". Then Graphviz is called within the script to render an svg.
Both .gv (pure dot language) and .svg files are generated. At the moment, the script is quite dumb and I usually tweak the output .gv file using an [https://dreampuf.github.io/GraphvizOnline/ online graphviz editor].
The script was heavily used for my SI28 project [[User:Lucatorsera/Special-Issue28#Marginalia|Marginalia]].
====== ToDo (25.11.25) ======
* Add more input arguments to choose between Neato, Dot, twopi or circle
* Add arguments to choose modes and models for Neato
====== Code ======
Remember to activate conda environment (HPGL-plotter or graphviz)!<syntaxhighlight lang="python3" line="1">
'''
txt to graph generator, v1
usage:
1. paste text on input.txt file (any name)
2. from cmd, parser.py {filename}
3. both .gv and and svg will be generated, in neato only
'''
import argparse
import subprocess
parser = argparse.ArgumentParser(description="text2GraphViz")
parser.add_argument("filename", help="input filename as txt")
args=parser.parse_args()
with open(args.filename) as i:
content = i.read()
print(content)
content = content.replace(".", "")
content = content.replace(",", "")
content = content.replace("-", "")
n = content.replace(" ", "->")
print(n)
output_file = args.filename.replace(".txt", "")+"_output.gv"
with open(output_file, "w") as o:
o.write("digraph G {\n ")
o.write("normalize=false\nmode=sgd\n")
o.write(n)
o.write("[len=1.2]\n")
o.write("}")
render = f"neato -Tsvg {output_file} -o {output_file.replace(".gv", "")}.svg"
print(render)
execute_cmd = subprocess.call(render, shell=True)
print(execute_cmd)
</syntaxhighlight>
[[File:Graphviz-result01.svg|thumb|552x552px|"By Which the World is Open"]]
[[File:Gaza point20.svg|none|thumb|475x475px|US plan for the reconstruction of Gaza: Point 20]]


== Taxan XY Plotter ==
== Taxan XY Plotter ==

Revision as of 18:09, 25 November 2025

Experiments

Graph-ing Music

Following Special Issue#28 first radio show, where I shared the song Construção and prototyping class, I played around with GraphViz to translate the song in a visual format. By substituting the spaces between words with "->", you can easily create graphs, and understant the repetition of words in a song.

This became one of the fundaments of my SI28 public project, Marginalia.

Construção, visualized in dot layout
Construção visualized in fdp layout

















Text2Graph

Motivated by the experiments above, I developed a python script that converts sentences to graphs using Graphviz within its code. The script takes an input .txt file, removes any spaces/punctuation and replaces them with "->". Then Graphviz is called within the script to render an svg.


Both .gv (pure dot language) and .svg files are generated. At the moment, the script is quite dumb and I usually tweak the output .gv file using an online graphviz editor.

The script was heavily used for my SI28 project Marginalia.

ToDo (25.11.25)
  • Add more input arguments to choose between Neato, Dot, twopi or circle
  • Add arguments to choose modes and models for Neato
Code

Remember to activate conda environment (HPGL-plotter or graphviz)!

'''
txt to graph generator, v1
usage:
1. paste text on input.txt file (any name)
2. from cmd, parser.py {filename}
3. both .gv and and svg will be generated, in neato only

'''
import argparse
import subprocess

parser = argparse.ArgumentParser(description="text2GraphViz")
parser.add_argument("filename", help="input filename as txt")
args=parser.parse_args()

with open(args.filename) as i:
	content = i.read()
print(content)

content = content.replace(".", "")
content = content.replace(",", "")
content = content.replace("-", "")

n = content.replace(" ", "->")
print(n)

output_file = args.filename.replace(".txt", "")+"_output.gv"
with open(output_file, "w") as o:
	o.write("digraph G {\n	")
	o.write("normalize=false\nmode=sgd\n")

	o.write(n)

	o.write("[len=1.2]\n")
	o.write("}")

render = f"neato -Tsvg {output_file} -o {output_file.replace(".gv", "")}.svg"
print(render)
execute_cmd = subprocess.call(render, shell=True)
print(execute_cmd)
Error creating thumbnail: Unable to create temporary thumbnail file
"By Which the World is Open"
Error creating thumbnail: Unable to create temporary thumbnail file
US plan for the reconstruction of Gaza: Point 20

Taxan XY Plotter

On Prototyping session #6 (28.10.25), there was a pen plotter jam session. We learned HPGL and how to interface with some of them via RS232.


I was graced with the TAXAN X-Y KPL710. According to the logs, it was a difficult one to talk to, and latest reports called it fixed succesfully. Nevertheless, it wouldn't print files from InkScape or other programs fully. Chiplotle also didn't work for me.

TaxanXY-01.jpg
TaxanXY-02.jpg


My solution was to print a design "command by command" using serial.write() within processing. It works, but it is quite cumbersome depending on the design to be plotted.

/**
 * TAXAN KPL710 Test Plot
 * Sends a simple HPGL test pattern to the plotter.
 * 
 * Before running:
 *  - Set the correct serial port name (e.g. "COM3" on Windows, "/dev/ttyUSB0" on Linux)
 *  - Match the plotter’s baud rate, parity, stop bits, etc. from your manual.
 */

import processing.serial.*;

Serial plotter;


void plotRect(float xPos, float yPos, float width, float height){
  float p1x = xPos - width/2;
  float p1y = yPos - height/2;
  
  float p2x = xPos + width/2;
  float p2y = yPos - height/2;
  
  float p3x = xPos + width/2;
  float p3y = yPos + height/2;
  
  float p4x = xPos - width/2;
  float p4y = yPos + height/2;
  sendCmd("PD"+p1x+","+p1y+";");
  delay(100);
  sendCmd("PD"+p2x+","+p2y+";");
  delay(100);
  sendCmd("PD"+p3x+","+p3y+";");
  delay(100);
  sendCmd("PD"+p4x+","+p4y+";");
  delay(100);
}

void setup() {
  // List available ports
  println(Serial.list());
  
  // Replace this with your actual serial port name
  String portName = "COM10";
  int baudRate = 9600; // or 4800, 19200, etc. depending on your plotter settings
  
  plotter = new Serial(this, portName, baudRate);
  
  // Give plotter a moment to initialize
  delay(2000);
  
  // Send HPGL-like initialization sequence
  sendCmd("IN;");   // Initialize
  sendCmd("SP1;");  // Select pen 1
  sendCmd("PA0,0;"); // Absolute mode, move to origin
  sendCmd("PU;");    // Pen up

  for(int i = 0; i < 10; i++){
    float posX = random(5000, 10000);
    float posY = random(5000, 10000);
    float w = random(100, 1000);
    float h = random(100,1000);
    plotRect(posX,posY,w, h);
  }



  // Optionally move pen away and end
  sendCmd("PA500,500;");
  sendCmd("SP0;"); // Deselect pen
  
  println("Test drawing sent!");
}

void draw() {
  // Nothing here
}

void sendCmd(String cmd) {
  print(">> " + cmd);
  plotter.write(cmd);
}

Something to look further is the HPGL processing library that spits out pure HPGL. With that, it's also possible to preview the design. Then the same processing sketch could read that file and send the commands one by one.