dxf.sleela
autocad/cad/dxf.sleela · 104 line(s) · sleela
#sleela 2.1
// dxf.sleela -- emits a geometry Model as AutoCAD DXF (R12 text). R12 is the
// most interoperable DXF level: group-code/value pairs, no handles, no object
// database -- it opens directly in AutoCAD and LibreCAD. This is the
// authoritative writer; the GUI never produces DXF itself.
//
// DXF format recap: every record is two lines -- an integer "group code" then
// its value. Code 0 starts an entity, 8 is the layer, 10/20 is a start point,
// 11/21 an end point, 1 is text, 40 a text height.
class Dxf {
// Emit the full DXF document for a model.
string emit(Model model, Model.ModelData m) {
string s = "";
s = s + header(m);
s = s + tables();
s = s + entities(model, m);
s = s + "0\nEOF\n";
return s;
}
// HEADER section: declare the drawing extents so a viewer zooms to fit.
string header(Model.ModelData m) {
string s = "";
s = s + "0\nSECTION\n2\nHEADER\n";
s = s + "9\n$EXTMIN\n10\n" + num(m.minX) + "\n20\n" + num(m.minY) + "\n";
s = s + "9\n$EXTMAX\n10\n" + num(m.maxX) + "\n20\n" + num(m.maxY) + "\n";
s = s + "0\nENDSEC\n";
return s;
}
// TABLES section: define the three layers with their colors.
string tables() {
string s = "";
s = s + "0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nLAYER\n70\n3\n";
s = s + layer("walls", 7);
s = s + layer("dims", 1);
s = s + layer("notes", 3);
s = s + "0\nENDTAB\n0\nENDSEC\n";
return s;
}
string layer(string name, int color) {
return "0\nLAYER\n2\n" + name + "\n70\n0\n62\n" + istr(color) + "\n6\nCONTINUOUS\n";
}
// ENTITIES section: walls as LINEs, dimensions as extension LINEs plus a
// measured TEXT label, and notes as TEXT.
string entities(Model model, Model.ModelData m) {
string s = "";
s = s + "0\nSECTION\n2\nENTITIES\n";
int i = 0;
while (i < m.wallCount) {
s = s + line("walls", m.walls[i].x1, m.walls[i].y1, m.walls[i].x2, m.walls[i].y2);
i = i + 1;
}
i = 0;
while (i < m.dimCount) {
double x1 = m.dims[i].x1; double y1 = m.dims[i].y1;
double x2 = m.dims[i].x2; double y2 = m.dims[i].y2;
// Dimension line itself.
s = s + line("dims", x1, y1, x2, y2);
// Measured value, placed at the midpoint.
double mx = (x1 + x2) / 2.0;
double my = (y1 + y2) / 2.0;
double d = model.distance(x1, y1, x2, y2);
s = s + text("dims", mx, my, 120.0, num(d));
i = i + 1;
}
i = 0;
while (i < m.noteCount) {
s = s + text("notes", m.notes[i].x, m.notes[i].y, 150.0, m.notes[i].text);
i = i + 1;
}
s = s + "0\nENDSEC\n";
return s;
}
// A DXF LINE entity on a layer.
string line(string layer, double x1, double y1, double x2, double y2) {
string s = "";
s = s + "0\nLINE\n8\n" + layer + "\n";
s = s + "10\n" + num(x1) + "\n20\n" + num(y1) + "\n30\n0.0\n";
s = s + "11\n" + num(x2) + "\n21\n" + num(y2) + "\n31\n0.0\n";
return s;
}
// A DXF TEXT entity on a layer.
string text(string layer, double x, double y, double height, string value) {
string s = "";
s = s + "0\nTEXT\n8\n" + layer + "\n";
s = s + "10\n" + num(x) + "\n20\n" + num(y) + "\n30\n0.0\n";
s = s + "40\n" + num(height) + "\n1\n" + value + "\n";
return s;
}
string num(double v) { return "" + v; }
string istr(int v) { return "" + v; }
}