32 #include <glib/gstdio.h>
40 #define round(x) floor(x + 0.5)
43 export_rs274x_write_macro(FILE* fd, gerbv_aperture_t* currentAperture, gint apertureNumber) {
44 gerbv_simplified_amacro_t* ls = currentAperture->simplified;
47 fprintf(fd,
"%%AMMACRO%d*\n", apertureNumber);
51 fd,
"1,%d,%f,%f,%f*\n", (
int)ls->parameter[CIRCLE_EXPOSURE], ls->parameter[CIRCLE_DIAMETER],
52 ls->parameter[CIRCLE_CENTER_X], ls->parameter[CIRCLE_CENTER_Y]
56 int numberOfPoints = (int)ls->parameter[OUTLINE_NUMBER_OF_POINTS];
59 fprintf(fd,
"4,%d,%d,", (
int)ls->parameter[OUTLINE_EXPOSURE], numberOfPoints);
61 for (pointCounter = 0; pointCounter <= numberOfPoints; pointCounter++) {
63 fd,
"%f,%f,", ls->parameter[pointCounter * 2 + OUTLINE_FIRST_X],
64 ls->parameter[pointCounter * 2 + OUTLINE_FIRST_Y]
67 fprintf(fd,
"%f*\n", ls->parameter[pointCounter * 2 + OUTLINE_FIRST_X]);
70 fd,
"5,%d,%d,%f,%f,%f,%f*\n", (
int)ls->parameter[POLYGON_EXPOSURE],
71 (
int)ls->parameter[POLYGON_NUMBER_OF_POINTS], ls->parameter[POLYGON_CENTER_X],
72 ls->parameter[POLYGON_CENTER_Y], ls->parameter[POLYGON_DIAMETER], ls->parameter[POLYGON_ROTATION]
76 fd,
"6,%f,%f,%f,%f,%f,%d,%f,%f,%f*\n", ls->parameter[MOIRE_CENTER_X], ls->parameter[MOIRE_CENTER_Y],
77 ls->parameter[MOIRE_OUTSIDE_DIAMETER], ls->parameter[MOIRE_CIRCLE_THICKNESS],
78 ls->parameter[MOIRE_GAP_WIDTH], (
int)ls->parameter[MOIRE_NUMBER_OF_CIRCLES],
79 ls->parameter[MOIRE_CROSSHAIR_THICKNESS], ls->parameter[MOIRE_CROSSHAIR_LENGTH],
80 ls->parameter[MOIRE_ROTATION]
84 fd,
"7,%f,%f,%f,%f,%f,%f*\n", ls->parameter[THERMAL_CENTER_X], ls->parameter[THERMAL_CENTER_Y],
85 ls->parameter[THERMAL_OUTSIDE_DIAMETER], ls->parameter[THERMAL_INSIDE_DIAMETER],
86 ls->parameter[THERMAL_CROSSHAIR_THICKNESS], ls->parameter[THERMAL_ROTATION]
90 fd,
"20,%d,%f,%f,%f,%f,%f,%f*\n", (
int)ls->parameter[LINE20_EXPOSURE], ls->parameter[LINE20_LINE_WIDTH],
91 ls->parameter[LINE20_START_X], ls->parameter[LINE20_START_Y], ls->parameter[LINE20_END_X],
92 ls->parameter[LINE20_END_Y], ls->parameter[LINE20_ROTATION]
96 fd,
"21,%d,%f,%f,%f,%f,%f*\n", (
int)ls->parameter[LINE21_EXPOSURE], ls->parameter[LINE21_WIDTH],
97 ls->parameter[LINE21_HEIGHT], ls->parameter[LINE21_CENTER_X], ls->parameter[LINE21_CENTER_Y],
98 ls->parameter[LINE21_ROTATION]
102 fd,
"22,%d,%f,%f,%f,%f,%f*\n", (
int)ls->parameter[LINE22_EXPOSURE], ls->parameter[LINE22_WIDTH],
103 ls->parameter[LINE22_HEIGHT], ls->parameter[LINE22_LOWER_LEFT_X], ls->parameter[LINE22_LOWER_LEFT_Y],
104 ls->parameter[LINE22_ROTATION]
111 fprintf(fd,
"%%ADD%dMACRO%d*%%\n", apertureNumber, apertureNumber);
115 export_rs274x_write_apertures(FILE* fd,
gerbv_image_t* image) {
116 gerbv_aperture_t* currentAperture;
117 gint numberOfRequiredParameters = 0, numberOfOptionalParameters = 0, i, j;
121 for (i = APERTURE_MIN; i < APERTURE_MAX; i++) {
122 gboolean writeAperture = TRUE;
124 currentAperture = image->
aperture[i];
126 if (!currentAperture)
129 switch (currentAperture->type) {
131 fprintf(fd,
"%%ADD%d", i);
133 numberOfRequiredParameters = 1;
134 numberOfOptionalParameters = 2;
137 fprintf(fd,
"%%ADD%d", i);
139 numberOfRequiredParameters = 2;
140 numberOfOptionalParameters = 2;
143 fprintf(fd,
"%%ADD%d", i);
145 numberOfRequiredParameters = 2;
146 numberOfOptionalParameters = 2;
149 fprintf(fd,
"%%ADD%d", i);
151 numberOfRequiredParameters = 2;
152 numberOfOptionalParameters = 3;
155 export_rs274x_write_macro(fd, currentAperture, i);
156 writeAperture = FALSE;
158 default: writeAperture = FALSE;
break;
162 for (j = 0; j < (numberOfRequiredParameters + numberOfOptionalParameters); j++) {
163 if ((j < numberOfRequiredParameters) || (currentAperture->parameter[j] != 0)) {
167 fprintf(fd,
"%.4f", currentAperture->parameter[j]);
170 fprintf(fd,
"*%%\n");
180 fprintf(fd,
"%%LPC*%%\n");
182 fprintf(fd,
"%%LPD*%%\n");
193 const double decimal_coeff = 1e6;
197 gboolean insidePolygon = FALSE;
201 setlocale(LC_NUMERIC,
"C");
203 if (transform != NULL) {
204 thisTransform = transform;
207 thisTransform = &identityTransform;
209 if ((fd = g_fopen(filename,
"w")) == NULL) {
210 GERB_COMPILE_ERROR(_(
"Can't open file for writing: %s"), filename);
218 fprintf(fd,
"G04 This is an RS-274x file exported by *\n");
219 fprintf(fd,
"G04 gerbv version %s *\n", VERSION);
220 fprintf(fd,
"G04 More information is available about gerbv at *\n");
221 fprintf(fd,
"G04 https://gerbv.github.io/ *\n");
222 fprintf(fd,
"G04 --End of header info--*\n");
223 fprintf(fd,
"%%MOIN*%%\n");
224 fprintf(fd,
"%%FSLAX36Y36*%%\n");
228 if ((image->
info->offsetA > 0.0) || (image->
info->offsetB > 0.0))
229 fprintf(fd,
"%%IOA%fB%f*%%\n", image->
info->offsetA, image->
info->offsetB);
232 fprintf(fd,
"%%IPNEG*%%\n");
234 fprintf(fd,
"%%IPPOS*%%\n");
236 if (image->
info->name)
237 fprintf(fd,
"%%IN%s*%%\n", image->
info->name);
239 if (image->
info->plotterFilm)
240 fprintf(fd,
"%%PF%s*%%\n", image->
info->plotterFilm);
243 if ((image->
info->imageRotation != 0.0) || (thisTransform->
rotation != 0.0))
244 fprintf(fd,
"%%IR%d*%%\n", (
int)round(RAD2DEG(image->
info->imageRotation)) % 360);
246 if ((image->
info->imageJustifyTypeA != GERBV_JUSTIFY_NOJUSTIFY)
247 || (image->
info->imageJustifyTypeB != GERBV_JUSTIFY_NOJUSTIFY)) {
248 fprintf(fd,
"%%IJA");
249 if (image->
info->imageJustifyTypeA == GERBV_JUSTIFY_CENTERJUSTIFY)
252 fprintf(fd,
"%.4f", image->
info->imageJustifyOffsetA);
254 if (image->
info->imageJustifyTypeB == GERBV_JUSTIFY_CENTERJUSTIFY)
257 fprintf(fd,
"%.4f", image->
info->imageJustifyOffsetB);
258 fprintf(fd,
"*%%\n");
261 if (fabs(thisTransform->
scaleX - 1) > GERBV_PRECISION_LINEAR_INCH
262 || fabs(thisTransform->
scaleY - 1) > GERBV_PRECISION_LINEAR_INCH) {
263 fprintf(fd,
"%%SFA%.4fB%.4f*%%\n", thisTransform->
scaleX, thisTransform->
scaleY);
271 fprintf(fd,
"G04 --Define apertures--*\n");
272 export_rs274x_write_apertures(fd, image);
275 fprintf(fd,
"G04 --Start main section--*\n");
276 gint currentAperture = 0;
282 for (currentNet = image->
netlist->
next; currentNet; currentNet = currentNet->
next) {
284 if (currentNet->
layer != oldLayer)
285 export_rs274x_write_layer_change(oldLayer, currentNet->
layer, fd);
288 if (currentNet->
state != oldState)
289 export_rs274x_write_state_change(oldState, currentNet->
state, fd);
295 fprintf(fd,
"G54D%02d*\n", currentNet->
aperture);
296 currentAperture = currentNet->
aperture;
299 oldLayer = currentNet->
layer;
300 oldState = currentNet->
state;
302 long xVal, yVal, endX, endY, centerX, centerY;
311 xVal = (long)round(currentNet->
start_x * decimal_coeff);
312 yVal = (long)round(currentNet->
start_y * decimal_coeff);
313 fprintf(fd,
"G01X%07ldY%07ldD02*\n", xVal, yVal);
315 xVal = (long)round(currentNet->
stop_x * decimal_coeff);
316 yVal = (long)round(currentNet->
stop_y * decimal_coeff);
317 fprintf(fd,
"G01X%07ldY%07ld", xVal, yVal);
320 fprintf(fd,
"D02*\n");
322 fprintf(fd,
"D01*\n");
324 fprintf(fd,
"D03*\n");
331 xVal = (long)round(currentNet->
start_x * decimal_coeff);
332 yVal = (long)round(currentNet->
start_y * decimal_coeff);
333 fprintf(fd,
"G01X%07ldY%07ldD02*\n", xVal, yVal);
335 centerX = (long)round((currentNet->
cirseg->cp_x - currentNet->
start_x) * decimal_coeff);
336 centerY = (long)round((currentNet->
cirseg->cp_y - currentNet->
start_y) * decimal_coeff);
337 endX = (long)round(currentNet->
stop_x * decimal_coeff);
338 endY = (long)round(currentNet->
stop_y * decimal_coeff);
342 fprintf(fd,
"G75*\n");
351 fprintf(fd,
"X%07ldY%07ldI%07ldJ%07ld", endX, endY, centerX, centerY);
353 fprintf(fd,
"X%07ldY%07ld", endX, endY);
356 fprintf(fd,
"D02*\n");
358 fprintf(fd,
"D01*\n");
360 fprintf(fd,
"D03*\n");
363 fprintf(fd,
"G36*\n");
364 insidePolygon = TRUE;
367 fprintf(fd,
"G37*\n");
368 insidePolygon = FALSE;
374 fprintf(fd,
"M02*\n");
380 setlocale(LC_NUMERIC,
"");
gboolean gerbv_export_rs274x_file_from_image(const gchar *filename, gerbv_image_t *inputImage, gerbv_user_transformation_t *transform)
Export an image to a new file in RS274X format.
void gerbv_destroy_image(gerbv_image_t *image)
Free an image structure.
gerbv_image_t * gerbv_image_duplicate_image(gerbv_image_t *sourceImage, gerbv_user_transformation_t *transform)
Duplicate an existing image and return the new copy.
The main header file for the libgerbv library.
@ GERBV_APERTURE_STATE_OFF
@ GERBV_APERTURE_STATE_ON
@ GERBV_APTYPE_MACRO_LINE20
@ GERBV_APTYPE_MACRO_LINE21
@ GERBV_APTYPE_MACRO_OUTLINE
@ GERBV_APTYPE_MACRO_CIRCLE
@ GERBV_APTYPE_MACRO_POLYGON
@ GERBV_APTYPE_MACRO_THERMAL
@ GERBV_APTYPE_MACRO_LINE22
@ GERBV_APTYPE_MACRO_MOIRE
@ GERBV_INTERPOLATION_LINEARx01
@ GERBV_INTERPOLATION_PAREA_START
@ GERBV_INTERPOLATION_LINEARx001
@ GERBV_INTERPOLATION_CW_CIRCULAR
@ GERBV_INTERPOLATION_PAREA_END
@ GERBV_INTERPOLATION_LINEARx10
@ GERBV_INTERPOLATION_CCW_CIRCULAR
@ GERBV_INTERPOLATION_LINEARx1
gerbv_aperture_t * aperture[APERTURE_MAX]
gerbv_netstate_t * states
gerbv_image_info_t * info
gerbv_polarity_t polarity
gerbv_aperture_state_t aperture_state
gerbv_interpolation_t interpolation