Building a Pattern DSL

img

Introduction

The goal of this project is to create a very simple and concise language that I can use to create complex patterns which can then be sent to various machines to be processed (so I need to output in some sort of vector format.)

Before even starting the DSL portion of the project, I spent a lot of time experimenting with different ways to build patterns using python output to some sort of machine readable format. I started using DXF but it was weird and new (to me) and I didn't really like how much work it took to do anything in it. Then I switched over to using svg which is nice since it handles a lot of the transformations for me. Once I got some python code working that could output vector patterns, I moved on to making the DSL to control it.

The tech stack

  • Python
  • svgwrite : the library to create SVG for me
  • antlr using to build a grammar for the DSL

The DSL

I'm using ANTLR to write the parser for the language, one of the nice features is that it can output and work with multiple languages, so if I want to switch to javascript down the road (better vector support) at least the parser code will be easy to port over. ANTLR is also well documented (it has a book.) which makes learning it pretty easy. I also put together this ANTLR python quickstart document.

Sample

ga = ((E20,20 T10,0 C0 )(RS4), (E15,15 T10,0 C1 )(RS4), (E8,4 T13,0 C0)(RS4));
mrgrid = GRID60,60,10,10,60,ga;
gb = (R10,10 RT45 C0);
tmp  = GRID90,90,9,9,60,gb;
RENDER(mrgrid, tmp);

Basic overview of what is above: ( full grammar )

  • E : create an ellipse
  • T : Translate to a new position (everything starts at 0,0
  • RS : copy an object around a circle X times
  • GRID : take a group of objects and copy them onto a grid

img

Off to the presses (cutter)

img

Cleaning it up

img

code here

Where I'm going from Here?

What I have now is a good proof of concept, but I really want to implment CSG functionality into it (so I can get cleaner/smaller output to work with), which means i'll probablly need to scrap most of the way i'm currently doing things. Also, I need to scrap the idea of using "shapes" and just make everything a ploygon, which will make working with the CSG libraries and converting back to svg much easier.

Home