2016-07-22 06:04:42 +03:00
|
|
|
/*
|
|
|
|
* fig.c - Generate FIG output for Eeschema items
|
|
|
|
*
|
|
|
|
* Written 2016 by Werner Almesberger
|
|
|
|
* Copyright 2016 by Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-07-25 04:30:38 +03:00
|
|
|
#include <stdbool.h>
|
2016-07-23 12:58:21 +03:00
|
|
|
#include <stdlib.h>
|
2016-07-22 05:54:32 +03:00
|
|
|
#include <stdio.h>
|
2016-07-23 12:58:21 +03:00
|
|
|
#include <math.h>
|
2016-07-24 07:03:23 +03:00
|
|
|
#include <assert.h>
|
2016-07-22 05:54:32 +03:00
|
|
|
|
2016-07-24 07:03:23 +03:00
|
|
|
#include "util.h"
|
2016-07-23 06:48:59 +03:00
|
|
|
#include "style.h"
|
|
|
|
#include "text.h"
|
2016-07-22 05:54:32 +03:00
|
|
|
#include "fig.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIG works with 1/1200 in
|
|
|
|
* KiCad works with mil
|
|
|
|
* 1 point = 1/72 in
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static inline int cx(int x)
|
|
|
|
{
|
|
|
|
return x * 1200 / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline int cy(int y)
|
|
|
|
{
|
|
|
|
return y * 1200 / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-23 06:48:59 +03:00
|
|
|
static inline float pt(int x)
|
2016-07-22 05:54:32 +03:00
|
|
|
{
|
2016-07-23 06:48:59 +03:00
|
|
|
return cx(x) * 72 * 1.5 / 1200.0;
|
2016-07-22 05:54:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-23 21:07:50 +03:00
|
|
|
/* ----- Schematics items -------------------------------------------------- */
|
|
|
|
|
2016-07-23 12:58:21 +03:00
|
|
|
|
2016-07-31 03:55:23 +03:00
|
|
|
void fig_line(void *ctx, int sx, int sy, int ex, int ey,
|
|
|
|
int color, unsigned layer)
|
2016-07-22 05:54:32 +03:00
|
|
|
{
|
|
|
|
// TypeStyle FillCol AreaFil Cap FwdAr
|
|
|
|
// SubTy Color Pen StyleV Rad BwdAr
|
|
|
|
// Thick Depth Join Points
|
2016-07-23 06:48:59 +03:00
|
|
|
printf("2 1 2 %d %d 7 %d -1 -1 3.0 1 1 -1 0 0 2\n",
|
2016-07-31 03:55:23 +03:00
|
|
|
WIDTH_LINE, color, layer);
|
2016-07-22 05:54:32 +03:00
|
|
|
printf("\t%d %d %d %d\n", cx(sx), cy(sy), cx(ex), cy(ey));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-23 21:07:50 +03:00
|
|
|
/* ----- General items ----------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2016-07-31 03:50:02 +03:00
|
|
|
void fig_rect(void *ctx, int sx, int sy, int ex, int ey,
|
2016-07-25 04:57:52 +03:00
|
|
|
int color, int fill_color, unsigned layer)
|
|
|
|
{
|
|
|
|
// Type Thick Depth StyleV Rad
|
|
|
|
// SubTy Color Pen Join FwdAr
|
|
|
|
// Style FillCol AreaFil Cap BwdAr
|
|
|
|
printf("2 2 0 %d %d %d %d -1 %d 0.0 1 1 -1 0 0 5\n",
|
|
|
|
color == -1 ? 0 : WIDTH_COMP_DWG, color, fill_color, layer,
|
|
|
|
fill_color == -1 ? -1 : 20);
|
|
|
|
printf("\t%d %d %d %d %d %d %d %d %d %d\n",
|
|
|
|
cx(sx), cy(sy), cx(ex), cy(sy), cx(ex), cy(ey), cx(sx), cy(ey),
|
|
|
|
cx(sx), cy(sy));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-31 03:50:02 +03:00
|
|
|
void fig_poly(void *ctx, int points, int x[points], int y[points],
|
2016-07-25 04:57:52 +03:00
|
|
|
int color, int fill_color, unsigned layer)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char ch = '\t';
|
|
|
|
|
|
|
|
// Type Thick Depth StyleV Rad
|
|
|
|
// SubTy Color Pen Join FwdAr
|
|
|
|
// Style FillCol AreaFil Cap BwdAr
|
|
|
|
printf("2 1 0 %d %d %d %d -1 %d 0.0 1 1 -1 0 0 %d\n",
|
|
|
|
color == -1 ? 0 : WIDTH_COMP_DWG, color, fill_color, layer,
|
|
|
|
fill_color == -1 ? -1 : 20, points);
|
|
|
|
for (i = 0; i != points; i++) {
|
|
|
|
printf("%c%d %d", ch, cx(x[i]), cy(y[i]));
|
|
|
|
ch = ' ';
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-31 03:50:02 +03:00
|
|
|
void fig_circ(void *ctx, int x, int y, int r,
|
|
|
|
int color, int fill_color, unsigned layer)
|
2016-07-25 06:50:48 +03:00
|
|
|
{
|
2016-07-25 08:59:33 +03:00
|
|
|
// Type Thick Depth StyleV Cx Rx Sx Ex
|
2016-07-25 06:50:48 +03:00
|
|
|
// SubTy Color Pen Dir Cy Ry Sy Ey
|
|
|
|
// Style FillCol AreaFil Angle
|
|
|
|
printf("1 3 0 %d %d %d %d -1 %d 0.0 1 0.0 %d %d %d %d %d %d %d %d\n",
|
|
|
|
color == -1 ? 0 : WIDTH_COMP_DWG, color, fill_color, layer,
|
|
|
|
fill_color == -1 ? -1 : 20,
|
|
|
|
cx(x), cy(y), r, r,
|
|
|
|
cx(x), cy(y), cx(x) + r, cy(y));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-25 08:59:33 +03:00
|
|
|
static int ax(int x, int y, int r, int angle)
|
|
|
|
{
|
|
|
|
float a = angle / 180.0 * M_PI;
|
|
|
|
|
|
|
|
return cx(x + r * cos(a));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ay(int x, int y, int r, int angle)
|
|
|
|
{
|
|
|
|
float a = angle / 180.0 * M_PI;
|
|
|
|
|
|
|
|
return cy(y - r * sin(a));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-31 03:50:02 +03:00
|
|
|
void fig_arc(void *ctx, int x, int y, int r, int sa, int ea,
|
2016-07-25 08:59:33 +03:00
|
|
|
int color, int fill_color, unsigned layer)
|
|
|
|
{
|
|
|
|
int ma = (sa + ea) / 2;
|
|
|
|
|
|
|
|
// Type Thick Depth StyleV FwdAr
|
|
|
|
// SubTy Color Pen Cap BwdAr
|
|
|
|
// Style FillCol AreaFil Dir points
|
2016-07-25 19:38:58 +03:00
|
|
|
printf("5 1 0 %d %d %d %d -1 %d 0.0 1 1 0 0 %d %d %d %d %d %d %d %d\n",
|
2016-07-25 08:59:33 +03:00
|
|
|
color == -1 ? 0 : WIDTH_COMP_DWG, color, fill_color, layer,
|
|
|
|
fill_color == -1 ? -1 : 20,
|
|
|
|
cx(x), cy(y),
|
|
|
|
ax(x, y, r, sa), ay(x, y, r, sa),
|
|
|
|
ax(x, y, r, ma), ay(x, y, r, ma),
|
|
|
|
ax(x, y, r, ea), ay(x, y, r, ea));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-31 03:50:02 +03:00
|
|
|
void fig_text(void *ctx, int x, int y, const char *s, unsigned size,
|
2016-07-23 06:48:59 +03:00
|
|
|
enum text_align align, int rot, unsigned color, unsigned layer)
|
|
|
|
{
|
2016-07-25 20:06:48 +03:00
|
|
|
// Type Depth FontSiz Height
|
|
|
|
// Just Pen Angle Length
|
|
|
|
// Color Font Flags X Y
|
2016-07-23 06:48:59 +03:00
|
|
|
printf("4 %u %d %d -1 %d %f %f 4 0.0 0.0 %d %d %s\\001\n",
|
|
|
|
align, color, layer, FONT_HELVETICA_BOLD,
|
2016-07-23 12:58:21 +03:00
|
|
|
pt(size), rot / 180.0 * M_PI, cx(x), cy(y), s);
|
2016-07-23 06:48:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-23 21:07:50 +03:00
|
|
|
/* ----- FIG file header --------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2016-07-26 08:30:58 +03:00
|
|
|
static void fig_header(void)
|
2016-07-22 05:54:32 +03:00
|
|
|
{
|
|
|
|
printf("#FIG 3.2\n");
|
|
|
|
printf("Landscape\n");
|
|
|
|
printf("Center\n");
|
|
|
|
printf("Metric\n");
|
|
|
|
printf("A4\n");
|
2016-07-23 06:48:59 +03:00
|
|
|
printf("100.00\n");
|
2016-07-22 05:54:32 +03:00
|
|
|
printf("Single\n");
|
|
|
|
printf("-2\n");
|
|
|
|
printf("1200 2\n");
|
|
|
|
}
|
2016-07-26 08:30:58 +03:00
|
|
|
|
|
|
|
|
2016-07-26 09:15:13 +03:00
|
|
|
static bool apply_vars(char *buf, int n_vars, const char **vars)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
const char **var, *eq;
|
|
|
|
int var_len, value_len;
|
|
|
|
|
|
|
|
p = strchr(buf, '<');
|
|
|
|
if (!p)
|
|
|
|
return 0;
|
|
|
|
for (var = vars; var != vars + n_vars; var++) {
|
|
|
|
eq = strchr(*var, '=');
|
|
|
|
assert(eq);
|
|
|
|
var_len = eq - *var;
|
|
|
|
if (strncmp(p + 1, *var, var_len))
|
|
|
|
continue;
|
|
|
|
value_len = strlen(eq + 1);
|
|
|
|
memmove(p + value_len, p + var_len + 2,
|
|
|
|
strlen(p + var_len + 2) + 1);
|
|
|
|
memcpy(p, eq + 1, value_len);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-31 03:50:02 +03:00
|
|
|
void *fig_init(const char *template, int n_vars, const char **vars)
|
2016-07-26 08:30:58 +03:00
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
char buf[1000];
|
|
|
|
|
|
|
|
if (!template) {
|
|
|
|
fig_header();
|
2016-07-31 03:50:02 +03:00
|
|
|
return NULL;
|
2016-07-26 08:30:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
file = fopen(template, "r");
|
|
|
|
if (!file) {
|
|
|
|
perror(template);
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-07-26 09:15:13 +03:00
|
|
|
while (fgets(buf, sizeof(buf), file)) {
|
|
|
|
while (apply_vars(buf, n_vars, vars));
|
2016-07-26 08:30:58 +03:00
|
|
|
printf("%s", buf);
|
2016-07-26 09:15:13 +03:00
|
|
|
}
|
2016-07-26 08:30:58 +03:00
|
|
|
fclose(file);
|
2016-07-31 03:50:02 +03:00
|
|
|
|
|
|
|
return NULL;
|
2016-07-26 08:30:58 +03:00
|
|
|
}
|
2016-07-31 03:50:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
/* ----- Operations -------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
const struct gfx_ops fig_ops = {
|
|
|
|
.line = fig_line,
|
|
|
|
.rect = fig_rect,
|
|
|
|
.poly = fig_poly,
|
|
|
|
.circ = fig_circ,
|
|
|
|
.arc = fig_arc,
|
|
|
|
.text = fig_text,
|
|
|
|
.init = fig_init,
|
|
|
|
};
|