geda.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * geda.c - Dump objects in the gEDA PCB board/module format
  3. *
  4. * Written 2009, 2011 by Werner Almesberger, and 2016 by Erich Heinzle
  5. * Copyright 2009, 2011 by Werner Almesberger
  6. * Copyright 2016, Erich Heinzle
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <assert.h>
  16. #include "coord.h"
  17. #include "inst.h"
  18. #include "geda.h"
  19. static void geda_centric(struct coord a, struct coord b,
  20. struct coord *center, struct coord *size)
  21. {
  22. struct coord min, max;
  23. min.x = units_to_geda(a.x);
  24. min.y = units_to_geda(a.y);
  25. max.x = units_to_geda(b.x);
  26. max.y = units_to_geda(b.y);
  27. sort_coord(&min, &max);
  28. size->x = max.x-min.x;
  29. size->y = max.y-min.y;
  30. center->x = (min.x+max.x)/2;
  31. center->y = -(min.y+max.y)/2;
  32. }
  33. static unit_type geda_pad_width(struct coord size)
  34. {
  35. if (size.x >= size.y) {
  36. return size.y;
  37. } else {
  38. return size.x;
  39. }
  40. }
  41. static void geda_pad_start_finish(struct coord a, struct coord b,
  42. struct coord *start, struct coord *finish)
  43. {
  44. struct coord size, center, min, max;
  45. min.x = units_to_geda(a.x);
  46. min.y = units_to_geda(a.y);
  47. max.x = units_to_geda(b.x);
  48. max.y = units_to_geda(b.y);
  49. sort_coord(&min, &max);
  50. size.x = max.x-min.x;
  51. size.y = max.y-min.y;
  52. center.x = (min.x+max.x)/2;
  53. center.y = -(min.y+max.y)/2;
  54. /* gEDA pads are drawn as a line, of a certain thickness */
  55. if (size.x >= size.y) { /* wider than tall, width = size.y */
  56. start->x = min.x + size.y/2;
  57. start->y = -center.y;
  58. finish->x = max.x - size.y/2;
  59. finish->y = -center.y;
  60. } else { /* taller than wide, width = size.x */
  61. start->y = (min.y + size.x/2);
  62. start->x = center.x;
  63. finish->y = (max.y - size.x/2);
  64. finish->x = center.x;
  65. }
  66. }
  67. static void do_geda_drill(FILE *file, const struct inst *pad, struct coord *padSize)
  68. {
  69. const struct inst *hole = pad->u.pad.hole;
  70. struct coord center, size;
  71. if (!hole)
  72. return;
  73. geda_centric(hole->base, hole->u.hole.other, &center, &size);
  74. fprintf(file, "%d %d ", center.x, -center.y); /* x,y position of hole */
  75. if (padSize->x <= padSize->y) { /* sort out diameter of copper annulus, remembering obrounds*/
  76. fprintf(file, "%d ", padSize->x);
  77. } else {
  78. fprintf(file, "%d ", padSize->y);
  79. }
  80. fprintf(file, "100 100 %d", size.x); /* default copper clearance, mask, then drill size */
  81. }
  82. static void geda_pad(FILE *file, const struct inst *inst)
  83. {
  84. struct coord center, size, start, finish;
  85. geda_centric(inst->base, inst->u.pad.other, &center, &size);
  86. if (inst->u.pad.hole) { /* pin */
  87. /* Pin[X Y Thickness Clearance Mask Drill Name Number SFlags] */
  88. fprintf(file, "\tPin[");
  89. do_geda_drill(file, inst, &size); /* need pad size to figure out annulus */
  90. fprintf(file, " \"%s\" \"%s\"", inst->u.pad.name, inst->u.pad.name);
  91. if (inst->obj->u.pad.rounded) { /* round pin */
  92. fprintf(file, " \"\"]\n");
  93. } else { /* square pad, ignore octagonal for now */
  94. fprintf(file, " \"square\"]\n");
  95. }
  96. } else { /* SMD */
  97. /* Pad[X1 Y1 X2 Y2 Thickness Clearance Mask Name Number SFlags] */
  98. geda_pad_start_finish(inst->base, inst->u.pad.other, &start, &finish);
  99. fprintf(file, "\tPad[%d %d %d %d %d 100 100 \"%s\" \"%s\" \"square\"]\n", start.x, -start.y, finish.x, -finish.y, geda_pad_width(size), inst->u.pad.name, inst->u.pad.name);
  100. }
  101. }
  102. static void geda_hole(FILE *file, const struct inst *inst)
  103. {
  104. struct coord center, size;
  105. if (inst->u.hole.pad)
  106. return;
  107. geda_centric(inst->base, inst->u.hole.other, &center, &size);
  108. /* Pin[X Y Thickness Clearance Mask Drill Name Number SFlags] */
  109. fprintf(file, "\tPin[%d %d", center.x, center.y);
  110. if (size.x <= size.y) { /* see which obround dimension is smallest */
  111. fprintf(file, " %d 100 100 %d", size.x, size.x);
  112. /* obround hole turned into round hole of diameter size.x */
  113. } else {
  114. fprintf(file, " %d 100 100 %d", size.y, size.y);
  115. /* obround hole turned into round hole of diameter size.y */
  116. }
  117. fprintf(file, " \"\" \"\" \"hole\"]\n");
  118. }
  119. static void geda_line(FILE *file, const struct inst *inst)
  120. {
  121. /*
  122. * Xstart, Ystart, Xend, Yend, Width
  123. */
  124. fprintf(file, "\tElementLine[%d %d %d %d %d]\n",
  125. units_to_geda(inst->base.x),
  126. -units_to_geda(inst->base.y),
  127. units_to_geda(inst->u.rect.end.x),
  128. -units_to_geda(inst->u.rect.end.y),
  129. units_to_geda(inst->u.rect.width));
  130. }
  131. static void geda_rect(FILE *file, const struct inst *inst)
  132. {
  133. unit_type xa, ya, xb, yb;
  134. unit_type width;
  135. xa = units_to_geda(inst->base.x);
  136. ya = units_to_geda(inst->base.y);
  137. xb = units_to_geda(inst->u.rect.end.x);
  138. yb = units_to_geda(inst->u.rect.end.y);
  139. width = units_to_geda(inst->u.rect.width);
  140. fprintf(file, "\tElementLine[%d %d %d %d %d]\n",
  141. xa, -ya, xa, -yb, width);
  142. fprintf(file, "\tElementLine[%d %d %d %d %d]\n",
  143. xa, -yb, xb, -yb, width);
  144. fprintf(file, "\tElementLine[%d %d %d %d %d]\n",
  145. xb, -yb, xb, -ya, width);
  146. fprintf(file, "\tElementLine[%d %d %d %d %d]\n",
  147. xb, -ya, xa, -ya, width);
  148. }
  149. static void geda_circ(FILE *file, const struct inst *inst)
  150. {
  151. /*
  152. * Xcenter, Ycenter, Width, Height, startAngle, stopAngle, Width
  153. */
  154. fprintf(file, "\tElementArc[ %d %d %d %d 0 360 %d]\n",
  155. units_to_geda(inst->base.x),
  156. -units_to_geda(inst->base.y),
  157. units_to_geda(inst->u.arc.r),
  158. units_to_geda(inst->u.arc.r),
  159. units_to_geda(inst->u.arc.width));
  160. }
  161. static void geda_arc(FILE *file, const struct inst *inst)
  162. {
  163. double b;
  164. /*
  165. * Xcenter, Ycenter, Width, Height, startAngle, stopAngle, Width
  166. */
  167. b = inst->u.arc.a1 - 180;
  168. while (b <= 0)
  169. b += 360;
  170. while (b > 360)
  171. b -= 360;
  172. fprintf(file, "\tElementArc[%d %d %d %d %d %d %d]\n",
  173. units_to_geda(inst->base.x),
  174. -units_to_geda(inst->base.y),
  175. units_to_geda(inst->u.arc.r),
  176. units_to_geda(inst->u.arc.r),
  177. (int) b,
  178. (int) (inst->u.arc.a2-inst->u.arc.a1),
  179. units_to_geda(inst->u.arc.width));
  180. }
  181. static void geda_layout_header(FILE *file)
  182. {
  183. fprintf(file, "# release: pcb 20110918\n\n");
  184. fprintf(file, "# To read pcb files, the pcb version (or the git source date) must be >= the file version\n");
  185. fprintf(file, "FileVersion[20070407]\n\n");
  186. fprintf(file, "PCB[\"\" 600000 500000]\n\n");
  187. fprintf(file, "Grid[2500.0 0 0 1]\n");
  188. fprintf(file, "Cursor[2500 62500 0.000000]\n");
  189. fprintf(file, "PolyArea[3100.006200]\n");
  190. fprintf(file, "Thermal[0.500000]\n");
  191. fprintf(file, "DRC[1200 900 1000 700 1500 1000]\n");
  192. fprintf(file, "Flags(\"nameonpcb,clearnew,snappin\")\n");
  193. fprintf(file, "Groups(\"1,3,4,c:2,5,6,s:7:8\")\n");
  194. fprintf(file, "Styles[\"Signal,1000,7874,3150,2000:Power,2000,8661,3937,2000:Fat,8000,13780,4724,2500:Sig-tight,1000,6400,3150,1200\"]\n\n");
  195. fprintf(file, "Attribute(\"PCB::grid::unit\" \"mil\")");
  196. }
  197. static void geda_layout_footer(FILE *file)
  198. {
  199. fprintf(file, "Layer(1 \"component\")\n(\n)\n");
  200. fprintf(file, "Layer(2 \"solder\")\n(\n)\n");
  201. fprintf(file, "Layer(3 \"comp-GND\")\n(\n)\n");
  202. fprintf(file, "Layer(4 \"comp-power\")\n(\n)\n");
  203. fprintf(file, "Layer(5 \"sold-GND\")\n(\n)\n");
  204. fprintf(file, "Layer(6 \"sold-power\")\n(\n)\n");
  205. fprintf(file, "Layer(7 \"signal3\")\n(\n)\n");
  206. fprintf(file, "Layer(8 \"outline\")\n(\n)\n");
  207. fprintf(file, "Layer(9 \"silk\")\n(\n)\n");
  208. fprintf(file, "Layer(10 \"silk\")\n(\n)\n");
  209. }
  210. static void geda_inst(FILE *file, enum inst_prio prio, const struct inst *inst)
  211. {
  212. switch (prio) {
  213. case ip_pad_copper:
  214. case ip_pad_special:
  215. geda_pad(file, inst);
  216. break;
  217. case ip_hole:
  218. geda_hole(file, inst); /* obround is exported as a round circle */
  219. break;
  220. case ip_line:
  221. geda_line(file, inst);
  222. break;
  223. case ip_rect:
  224. geda_rect(file, inst);
  225. break;
  226. case ip_circ:
  227. geda_circ(file, inst);
  228. break;
  229. case ip_arc:
  230. geda_arc(file, inst);
  231. break;
  232. default:
  233. /*
  234. * Don't try to export vectors, frame references, or
  235. * measurements.
  236. */
  237. break;
  238. }
  239. }
  240. static void geda_module(FILE *file, const struct pkg *pkg, time_t now)
  241. {
  242. enum inst_prio prio;
  243. const struct inst *inst;
  244. fprintf(file, "# Footprint generated by FPED utility\n");
  245. fprintf(file, "Element[\"\" \"%s\" \"\" \"\" 0 0 -25590 -14874 0 100 \"\"]\n", pkg->name);
  246. fprintf(file, "(\n");
  247. FOR_INST_PRIOS_UP(prio) {
  248. for (inst = pkgs->insts[prio]; inst; inst = inst->next)
  249. geda_inst(file, prio, inst);
  250. for (inst = pkg->insts[prio]; inst; inst = inst->next)
  251. geda_inst(file, prio, inst);
  252. }
  253. fprintf(file, ")\n\n"); /* extra newline between elements */
  254. }
  255. int geda(FILE *file, const char *one)
  256. {
  257. const struct pkg *pkg;
  258. time_t now = time(NULL);
  259. assert(!one);
  260. geda_layout_header(file); /* we place one or more elements in a layout file */
  261. for (pkg = pkgs; pkg; pkg = pkg->next)
  262. if (pkg->name)
  263. fprintf(file, "# %s\n", pkg->name);
  264. for (pkg = pkgs; pkg; pkg = pkg->next)
  265. if (pkg->name)
  266. geda_module(file, pkg, now);
  267. geda_layout_footer(file);
  268. fflush(file);
  269. return !ferror(file);
  270. }