utils
|
00001 00002 /* 00003 00004 geda_sym_format.h - gEDA symbol write function 00005 Copyright (C) 2002-2010 Mario Pascucci <m.pas@libero.it> 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 or 00021 visit the URL http://www.fsf.org/ 00022 00023 */ 00024 00025 00026 00027 /* general sizes */ 00028 #define SCALE 100 00029 #define PIN_SPACE 3*SCALE 00030 #define PIN_LEN 3*SCALE 00031 #define LABEL_SPACE 2*SCALE 00032 #define DOT_SIZE SCALE 00033 #define CLK_SIZE SCALE 00034 00035 00036 /* text alignment */ 00037 #define EDATEXT_LB 0 00038 #define EDATEXT_LC 1 00039 #define EDATEXT_LT 2 00040 #define EDATEXT_CB 3 00041 #define EDATEXT_CC 4 00042 #define EDATEXT_CT 5 00043 #define EDATEXT_RB 6 00044 #define EDATEXT_RC 7 00045 #define EDATEXT_RT 8 00046 00047 00048 /* color macros */ 00049 #define EDACOLOR_REF 8 /* detached attrib = RED */ 00050 #define EDACOLOR_LABEL 9 /* text and label = GREEN */ 00051 #define EDACOLOR_ATTRIB 5 /* attached attribute = YELLOW */ 00052 #define EDACOLOR_PIN 1 /* pin = WHITE */ 00053 #define EDACOLOR_DOT 6 /* logic bubble = CYAN */ 00054 #define EDACOLOR_BODY 3 /* body part = GREEN */ 00055 00056 00057 /* text size macros */ 00058 #define EDASIZE_LABEL 10 /* label size */ 00059 #define EDASIZE_ATTRIB 10 /* attribute */ 00060 #define EDASIZE_PIN 8 /* pin number size */ 00061 #define EDASIZE_TYPE 8 /* type of pin */ 00062 00063 00064 00065 /* scale for text */ 00066 static int text_scale[] = { 00067 0, 00068 8, 00069 10 00070 }; 00071 00072 00073 /* 00074 * Format of gEDA .sym files 00075 * 00076 * text 00077 * T x y color size visibility show_name_value angle alignment 00078 * 00079 * pin 00080 * P x1 y1 x2 y2 color 00081 * 00082 * line 00083 * L x1 y1 x2 y2 color width end type length space 00084 * 00085 * box 00086 * type x y width height color width end type length space filling fillwidth angle1 pitch1 angle2 pitch2 00087 * Example: 00088 * B 33000 67300 2000 2000 3 60 0 2 75 50 0 -1 -1 -1 -1 -1 00089 * 00090 * circle 00091 * V 38000 67000 900 3 0 0 2 75 50 0 -1 -1 -1 -1 -1 00092 * type x y radius color width end type length space filling fillwidth angle1 pitch1 angle2 pitch2 00093 * 00094 * arc 00095 * type x y radius start_angle sweep_angle color width end type length space 00096 * Example: 00097 * A 30600 75000 2000 0 45 3 0 0 3 75 50 00098 * 00099 */ 00100 00101 00102 #define PI 3.141592653588 00103 00104 00105 00106 int initangle(float dx, float dy) 00107 { 00108 float a,b; 00109 00110 if (dx == 0.0) 00111 { 00112 if (dy >= 0.0) return 90; 00113 else return 270; 00114 } 00115 if (dy == 0.0) 00116 { 00117 if (dx >= 0.0) return 0; 00118 else return 180; 00119 } 00120 a = atan2(dy,dx); 00121 b = 180.0/PI * a; 00122 return (int) b; 00123 } 00124 00125 00126 00127 /* write down the gEDA symbol file 00128 most of the work must be done for 00129 correct symbol rendering 00130 */ 00131 int write_sym(void) 00132 { 00133 int i,xs,ys,xe,ye; 00134 int xt,yt,xb,yb; 00135 int labely; 00136 int txtangle; 00137 int pinx,piny,pinalign; /* pin number */ 00138 int npinx,npiny,npinalign; /* pin name */ 00139 int tpinx,tpiny,tpinalign; /* pin type */ 00140 00141 xe = ye = xs = ys = txtangle = pinx = piny = pinalign = 00142 npinx = npiny = npinalign = tpinx = tpiny = tpinalign = 0; 00143 if (pincount == 0) 00144 { 00145 fprintf(stderr,"Unable to write down sym file, no pin definition available\n"); 00146 return -1; 00147 } 00148 /* if 'VECTOR' keyword was found, but no draw instruction 00149 * makes file .sym.part that stands for "partial" symbol */ 00150 if (vector_found && vector_count == 0) 00151 strcat(fnsym,".part"); 00152 if (!(fsym = fopen(fnsym,"wb"))) 00153 { 00154 perror("Opening sym output file"); 00155 return -1; 00156 } 00157 xt = sizex + PIN_LEN; 00158 xb = PIN_LEN; 00159 yt = sizey + PIN_LEN; 00160 yb = PIN_LEN; 00161 /* gEDA file version */ 00162 fprintf(fsym,"v %s\n",sym_version); 00163 /* reference: U? */ 00164 fprintf(fsym,"T %d %d %d %d 1 1 0 %d\n",xt,yt+SCALE,EDACOLOR_REF,EDASIZE_ATTRIB,EDATEXT_RB); 00165 if (use_old_version) 00166 fprintf(fsym,"uref=%s?\n",ref_str); 00167 else 00168 fprintf(fsym,"refdes=%s?\n",ref_str); 00169 /* part name */ 00170 fprintf(fsym,"T %d %d %d %d 1 0 0 %d\n",xb,yt+SCALE,EDACOLOR_LABEL,EDASIZE_LABEL,EDATEXT_LB); 00171 fprintf(fsym,"%s\n",part_aliases[0]); 00172 /* device name */ 00173 fprintf(fsym,"T %d %d %d %d 0 0 0 %d\n",xb,yt+3*SCALE,EDACOLOR_ATTRIB,EDASIZE_ATTRIB,EDATEXT_LB); 00174 fprintf(fsym,"device=%s\n",part_aliases[0]); 00175 if (vector_count == 0) 00176 { 00177 /* main box */ 00178 fprintf(fsym,"B %d %d %d %d %d 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",xb,yb,sizex,sizey,EDACOLOR_BODY); 00179 } 00180 else 00181 { 00182 /* drawing vectors */ 00183 for (i = 0; i< vector_count; i++) 00184 { 00185 switch (part_vectors[i].type) 00186 { 00187 case 'T': 00188 /* text */ 00189 xs = (int) (part_vectors[i].p[0] * PIN_SPACE + 0.5) + PIN_LEN; 00190 ys = (int) yt - (part_vectors[i].p[1] * PIN_SPACE + 0.5); 00191 fprintf(fsym,"T %d %d %d %d 1 0 0 %d\n",xs,ys,EDACOLOR_BODY, 00192 text_scale[(int)part_vectors[i].p[2]],EDATEXT_LB); 00193 fprintf(fsym,"%s\n",part_vectors[i].str); 00194 free(part_vectors[i].str); 00195 break; 00196 case 'L': 00197 /* line */ 00198 xs = (int) (part_vectors[i].p[0] * PIN_SPACE + 0.5) + PIN_LEN; 00199 ys = (int) yt - (part_vectors[i].p[1] * PIN_SPACE + 0.5); 00200 xe = (int) (part_vectors[i].p[2] * PIN_SPACE + 0.5) + PIN_LEN; 00201 ye = (int) yt - (part_vectors[i].p[3] * PIN_SPACE + 0.5); 00202 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys,xe,ye, 00203 EDACOLOR_BODY); 00204 break; 00205 case 'V': 00206 /* circle */ 00207 xs = (int) (part_vectors[i].p[0] * PIN_SPACE + PIN_LEN + 0.5); 00208 ys = (int) yt - (part_vectors[i].p[1] * PIN_SPACE + 0.5); 00209 xe = (int) (part_vectors[i].p[2] * PIN_SPACE + 0.5); /* radius */ 00210 fprintf(fsym,"V %d %d %d %d 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",xs,ys,xe, 00211 EDACOLOR_BODY); 00212 break; 00213 case 'A': 00214 /* arc */ 00215 xs = (int) (part_vectors[i].p[0] * PIN_SPACE + PIN_LEN + 0.5); 00216 ys = (int) yt - (part_vectors[i].p[1] * PIN_SPACE + 0.5); 00217 xe = initangle(part_vectors[i].p[2],-part_vectors[i].p[3]); /* init angle */ 00218 ye = initangle(part_vectors[i].p[4],-part_vectors[i].p[5]); /* end angle */ 00219 if (xe < 0) xe += 360; 00220 if (ye < 0) ye += 360; 00221 ye -= xe; 00222 if (abs(ye) > 90) 00223 { 00224 if (ye > 0) 00225 ye -= 360; 00226 else 00227 ye += 360; 00228 } 00229 pinx = (int) (part_vectors[i].p[6] * PIN_SPACE + 0.5); /* radius */ 00230 fprintf(fsym,"A %d %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys,pinx,xe,ye, 00231 EDACOLOR_BODY); 00232 break; 00233 } 00234 } 00235 } 00236 /* pin definiton and drawing */ 00237 labely = 5 * SCALE; 00238 for (i=0; i<pincount; i++) 00239 { 00240 if ((pinlist[i].num == 0) && (pinlist[i].type == PIN_TYPE_PWR) && 00241 ((pinlist[i].pos == 'T') || (pinlist[i].pos == 'B'))) 00242 { 00243 /* is a "virtual" pin, i.e. a net */ 00244 fprintf(fsym,"T %d %d %d %d 0 0 0 %d\n",xb,yt+labely,EDACOLOR_ATTRIB, 00245 EDASIZE_ATTRIB,EDATEXT_LB); 00246 fprintf(fsym,"net=%s:%d\n",pinlist[i].name,pinlist[i].pin); 00247 labely += LABEL_SPACE; 00248 continue; 00249 } 00250 switch (pinlist[i].pos) 00251 { 00252 case 'L': 00253 xs = xb; 00254 xe = 0; 00255 ys = ye = yt - pinlist[i].num * PIN_SPACE; 00256 txtangle = 0; 00257 /* pin number */ 00258 pinx = xs - SCALE; 00259 piny = ys + SCALE/2; 00260 pinalign = EDATEXT_RB; 00261 /* pin name */ 00262 npinx = xs + SCALE; 00263 npiny = ys; 00264 npinalign = EDATEXT_LB; 00265 /* pin type */ 00266 tpinx = xs + SCALE; 00267 tpiny = ys; 00268 tpinalign = EDATEXT_LT; 00269 if ((pinlist[i].flags & PIN_CLKFLAG) != 0) 00270 { 00271 /* there is "CLK" modifier */ 00272 npinx += CLK_SIZE/2; 00273 tpinx += CLK_SIZE/2; 00274 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys+CLK_SIZE,xs+CLK_SIZE,ys, 00275 EDACOLOR_BODY); 00276 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys-CLK_SIZE,xs+CLK_SIZE,ys, 00277 EDACOLOR_BODY); 00278 } 00279 if ((pinlist[i].flags & PIN_DOTFLAG) != 0) 00280 { 00281 /* there is "DOT" modifier */ 00282 xs -= DOT_SIZE; 00283 pinx -= DOT_SIZE/2; 00284 fprintf(fsym,"V %d %d %d %d 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",xs+DOT_SIZE/2,ys,DOT_SIZE/2, 00285 EDACOLOR_DOT); 00286 } 00287 break; 00288 case 'R': 00289 xs = xt; 00290 xe = xt + PIN_LEN; 00291 ys = ye = yt - pinlist[i].num * PIN_SPACE; 00292 txtangle = 0; 00293 /* pin number */ 00294 pinx = xs + SCALE; 00295 piny = ys + SCALE/2; 00296 pinalign = EDATEXT_LB; 00297 /* pin name */ 00298 npinx = xs - SCALE; 00299 npiny = ys; 00300 npinalign = EDATEXT_RB; 00301 /* pin type */ 00302 tpinx = xs - SCALE; 00303 tpiny = ys; 00304 tpinalign = EDATEXT_RT; 00305 if ((pinlist[i].flags & PIN_CLKFLAG) != 0) 00306 { 00307 /* there is "CLK" modifier */ 00308 npinx -= CLK_SIZE/2; 00309 tpinx -= CLK_SIZE/2; 00310 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys+CLK_SIZE,xs-CLK_SIZE,ys, 00311 EDACOLOR_BODY); 00312 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys-CLK_SIZE,xs-CLK_SIZE,ys, 00313 EDACOLOR_BODY); 00314 } 00315 if ((pinlist[i].flags & PIN_DOTFLAG) != 0) 00316 { 00317 /* there is "DOT" modifier */ 00318 xs += DOT_SIZE; 00319 pinx += DOT_SIZE/2; 00320 fprintf(fsym,"V %d %d %d %d 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",xs-DOT_SIZE/2,ys,DOT_SIZE/2, 00321 EDACOLOR_DOT); 00322 } 00323 break; 00324 case 'T': 00325 ys = yt; 00326 ye = yt + PIN_LEN; 00327 xs = xe = xb + pinlist[i].num * PIN_SPACE; 00328 txtangle = 90; 00329 /* pin number */ 00330 pinx = xs - SCALE/2; 00331 piny = ys + SCALE; 00332 pinalign = EDATEXT_LB; 00333 /* pin name */ 00334 npinx = xs; 00335 npiny = ys - SCALE; 00336 npinalign = EDATEXT_RB; 00337 /* pin type */ 00338 tpinx = xs; 00339 tpiny = ys - SCALE; 00340 tpinalign = EDATEXT_RT; 00341 if ((pinlist[i].flags & PIN_CLKFLAG) != 0) 00342 { 00343 /* there is "CLK" modifier */ 00344 npiny -= CLK_SIZE/2; 00345 tpiny -= CLK_SIZE/2; 00346 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs-CLK_SIZE,ys,xs,ys-CLK_SIZE, 00347 EDACOLOR_BODY); 00348 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys-CLK_SIZE,xs+CLK_SIZE,ys, 00349 EDACOLOR_BODY); 00350 } 00351 if ((pinlist[i].flags & PIN_DOTFLAG) != 0) 00352 { 00353 /* there is "DOT" modifier */ 00354 ys += DOT_SIZE; 00355 piny += DOT_SIZE/2; 00356 fprintf(fsym,"V %d %d %d %d 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",xs,ys-DOT_SIZE/2,DOT_SIZE/2, 00357 EDACOLOR_DOT); 00358 } 00359 break; 00360 case 'B': 00361 ys = yb; 00362 ye = yb - PIN_LEN; /* should be always 0 */ 00363 xs = xe = xb + pinlist[i].num * PIN_SPACE; 00364 txtangle = 270; 00365 /* pin number */ 00366 pinx = xs + SCALE/2; 00367 piny = ys - SCALE; 00368 pinalign = EDATEXT_LB; 00369 /* pin name */ 00370 npinx = xs; 00371 npiny = ys + SCALE; 00372 npinalign = EDATEXT_RB; 00373 /* pin type */ 00374 tpinx = xs; 00375 tpiny = ys + SCALE; 00376 tpinalign = EDATEXT_RT; 00377 if ((pinlist[i].flags & PIN_CLKFLAG) != 0) 00378 { 00379 /* there is "CLK" modifier */ 00380 npiny += CLK_SIZE/2; 00381 tpiny += CLK_SIZE/2; 00382 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs-CLK_SIZE,ys,xs,ys+CLK_SIZE, 00383 EDACOLOR_BODY); 00384 fprintf(fsym,"L %d %d %d %d %d 0 0 0 -1 -1\n",xs,ys+CLK_SIZE,xs+CLK_SIZE,ys, 00385 EDACOLOR_BODY); 00386 } 00387 if ((pinlist[i].flags & PIN_DOTFLAG) != 0) 00388 { 00389 /* there is "DOT" modifier */ 00390 ys -= DOT_SIZE; 00391 piny -= DOT_SIZE/2; 00392 fprintf(fsym,"V %d %d %d %d 0 0 0 -1 -1 0 -1 -1 -1 -1 -1\n",xs,ys+DOT_SIZE/2,DOT_SIZE/2, 00393 EDACOLOR_DOT); 00394 } 00395 break; 00396 } 00397 /* pin draw */ 00398 fprintf(fsym,"P %d %d %d %d %d\n",xs,ys,xe,ye,EDACOLOR_PIN); 00399 /* pin attributes */ 00400 fputs("{\n",fsym); 00401 /* pin sequence and number */ 00402 if (partspp != 0) /* handling for parts per package = 0 */ 00403 { 00404 fprintf(fsym,"T %d %d %d %d 1 1 %d %d\n",pinx,piny,EDACOLOR_ATTRIB, 00405 EDASIZE_PIN,txtangle,pinalign); 00406 } 00407 else 00408 { 00409 /* if no pin number in the original symbol, make pin number hidden */ 00410 fprintf(fsym,"T %d %d %d %d 0 1 %d %d\n",pinx,piny,EDACOLOR_ATTRIB, 00411 EDASIZE_PIN,txtangle,pinalign); 00412 } 00413 if (use_old_version) 00414 fprintf(fsym,"pin%d=%d\n",i+1,pinlist[i].pin); 00415 else 00416 { 00417 fprintf(fsym,"pinnumber=%d\n",pinlist[i].pin); 00418 fprintf(fsym,"T %d %d %d %d 0 1 %d %d\n",pinx,piny,EDACOLOR_ATTRIB, 00419 EDASIZE_PIN,txtangle,pinalign); 00420 fprintf(fsym,"pinseq=%d\n",i+1); 00421 } 00422 /* pin name */ 00423 /* hides pin name if symbol is VECTOR drawed and '-n' switch given */ 00424 if ((vector_count != 0) && (pin_name_hidden)) 00425 { 00426 fprintf(fsym,"T %d %d %d %d 0 1 %d %d\n",npinx,npiny,EDACOLOR_LABEL, 00427 EDASIZE_LABEL,txtangle,npinalign); 00428 } 00429 else 00430 { 00431 fprintf(fsym,"T %d %d %d %d 1 1 %d %d\n",npinx,npiny,EDACOLOR_LABEL, 00432 EDASIZE_LABEL,txtangle,npinalign); 00433 } 00434 if (use_old_version) 00435 fprintf(fsym,"label=%s\n",pinlist[i].name); 00436 else 00437 fprintf(fsym,"pinlabel=%s\n",pinlist[i].name); 00438 /* pin type */ 00439 fprintf(fsym,"T %d %d %d %d 0 1 %d %d\n",tpinx,tpiny,EDACOLOR_ATTRIB, 00440 EDASIZE_LABEL,txtangle,tpinalign); 00441 if (use_old_version) 00442 fprintf(fsym,"type=%s\n",edapintype[pinlist[i].type]); 00443 else 00444 fprintf(fsym,"pintype=%s\n",edapintype[pinlist[i].type]); 00445 fputs("}\n",fsym); 00446 } 00447 fclose(fsym); 00448 return 0; 00449 } 00450 00451 00452