38#include <potracelib.h>
44static void bm_free( potrace_bitmap_t* bm )
59 potrace_dpoint_t p4 );
77 potrace_param_t* param;
81 param = potrace_param_default();
86 snprintf( msg,
sizeof( msg ),
"Error allocating parameters: %s\n", strerror( errno ) );
94 param->opttolerance = 0.2;
97 st = potrace_trace( param, aPotrace_bitmap );
99 if( !st || st->status != POTRACE_STATUS_OK )
103 potrace_state_free( st );
106 potrace_param_free( param );
109 sprintf( msg,
"Error tracing bitmap: %s\n", strerror( errno ) );
156 potrace_state_free( st );
157 potrace_param_free( param );
165 const char* layerName =
"F.SilkS";
170 layerName =
"F.Mask";
178 layerName =
"Dwgs.User";
182 layerName =
"Cmts.User";
186 layerName =
"Eco1.User";
190 layerName =
"Eco2.User";
211 m_Data +=
"%%!PS-Adobe-3.0 EPSF-3.0\n";
212 snprintf( strbuf,
sizeof( strbuf ),
"%%%%BoundingBox: 0 0 %d %d\n",
m_PixmapWidth,
221 snprintf( strbuf,
sizeof( strbuf ),
222 "(footprint \"%s\" (version 20221018) (generator bitmap2component)\n"
223 " (layer \"F.Cu\")\n",
227 snprintf( strbuf,
sizeof( strbuf ),
228 " (attr board_only exclude_from_pos_files exclude_from_bom)\n" );
230 snprintf( strbuf,
sizeof( strbuf ),
231 " (fp_text reference \"G***\" (at 0 0) (layer \"%s\")\n"
232 " (effects (font (size 1.5 1.5) (thickness 0.3)))\n"
233 " (tstamp %s)\n )\n",
234 aBrdLayerName,
KIID().AsString().ToStdString().c_str() );
237 snprintf( strbuf,
sizeof( strbuf ),
238 " (fp_text value \"%s\" (at 0.75 0) (layer \"%s\") hide\n"
239 " (effects (font (size 1.5 1.5) (thickness 0.3)))\n"
240 " (tstamp %s)\n )\n",
241 m_CmpName.c_str(), aBrdLayerName,
KIID().AsString().ToStdString().c_str() );
247 m_Data +=
"(kicad_wks (version 20220228) (generator bitmap2component)\n";
248 m_Data +=
" (setup (textsize 1.5 1.5)(linewidth 0.15)(textlinewidth 0.15)\n";
249 m_Data +=
" (left_margin 10)(right_margin 10)(top_margin 10)(bottom_margin 10))\n";
250 m_Data +=
" (polygon (name \"\") (pos 0 0) (linewidth 0.01)\n";
256 Ypos += fieldSize / 2;
258 snprintf( strbuf,
sizeof( strbuf ),
259 "(kicad_symbol_lib (version 20220914) (generator bitmap2component)\n"
260 " (symbol \"%s\" (pin_names (offset 1.016)) (in_bom yes) (on_board yes)\n",
264 snprintf( strbuf,
sizeof( strbuf ),
265 " (property \"Reference\" \"#G\" (at 0 %g 0)\n"
266 " (effects (font (size %g %g)) hide)\n )\n",
267 -Ypos, fieldSize, fieldSize );
270 snprintf( strbuf,
sizeof( strbuf ),
271 " (property \"Value\" \"%s\" (at 0 %g 0)\n"
272 " (effects (font (size %g %g)) hide)\n )\n",
273 m_CmpName.c_str(), Ypos, fieldSize, fieldSize );
276 snprintf( strbuf,
sizeof( strbuf ),
277 " (property \"Footprint\" \"\" (at 0 0 0)\n"
278 " (effects (font (size %g %g)) hide)\n )\n",
279 fieldSize, fieldSize );
282 snprintf( strbuf,
sizeof( strbuf ),
283 " (property \"Datasheet\" \"\" (at 0 0 0)\n"
284 " (effects (font (size %g %g)) hide)\n )\n",
285 fieldSize, fieldSize );
288 snprintf( strbuf,
sizeof( strbuf ),
" (symbol \"%s_0_0\"\n",
m_CmpName.c_str() );
338 snprintf( strbuf,
sizeof( strbuf ),
"newpath\n%d %d moveto\n", startpoint.
x,
339 offsetY - startpoint.
y );
343 for( ii = 1; ii < aPolygon.
PointCount(); ii++ )
345 currpoint = aPolygon.
CPoint( ii );
346 snprintf( strbuf,
sizeof( strbuf ),
" %d %d lineto", currpoint.
x,
347 offsetY - currpoint.
y );
357 m_Data +=
"\nclosepath fill\n";
363 m_Data +=
" (fp_poly\n (pts\n";
367 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
369 currpoint = aPolygon.
CPoint( ii );
370 snprintf( strbuf,
sizeof( strbuf ),
" (xy %f %f)\n",
378 snprintf( strbuf,
sizeof( strbuf ),
379 " (stroke (width %f) (type solid)) (fill solid) (layer \"%s\") (tstamp %s))\n",
380 width, aBrdLayerName,
KIID().AsString().ToStdString().c_str() );
392 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
394 currpoint = aPolygon.
CPoint( ii );
395 snprintf( strbuf,
sizeof( strbuf ),
" (xy %.3f %.3f)",
408 snprintf( strbuf,
sizeof( strbuf ),
" (xy %.3f %.3f) )\n",
416#define SCH_LINE_THICKNESS_MM 0.01
418 m_Data +=
" (polyline\n (pts\n";
420 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
422 currpoint = aPolygon.
CPoint( ii );
423 snprintf( strbuf,
sizeof( strbuf ),
" (xy %f %f)\n",
430 snprintf( strbuf,
sizeof( strbuf ),
" (xy %f %f)\n",
436 snprintf( strbuf,
sizeof( strbuf ),
437 " (stroke (width %g) (type default))\n (fill (type outline))\n",
449 std::vector <potrace_dpoint_t> cornersBuffer;
457 potrace_dpoint_t( *c )[3];
466 bool main_outline =
true;
471 potrace_path_t* paths =
m_Paths;
475 m_errors +=
"No shape in black and white image to convert: no outline created\n";
478 while( paths !=
nullptr )
480 int cnt = paths->curve.n;
481 int* tag = paths->curve.tag;
483 potrace_dpoint_t startpoint = c[cnt - 1][2];
485 for(
int i = 0; i < cnt; i++ )
490 cornersBuffer.push_back( c[i][1] );
491 cornersBuffer.push_back( c[i][2] );
492 startpoint = c[i][2];
495 case POTRACE_CURVETO:
497 startpoint = c[i][2];
505 main_outline =
false;
509 for(
unsigned int i = 0; i < cornersBuffer.size(); i++ )
512 int( cornersBuffer[i].y *
m_ScaleY ) );
520 for(
unsigned int i = 0; i < cornersBuffer.size(); i++ )
523 int( cornersBuffer[i].y *
m_ScaleY ) );
527 cornersBuffer.clear();
530 if( paths->next ==
nullptr || paths->next->sign ==
'+' )
543 for(
int ii = 0; ii < polyset_areas.
OutlineCount(); ii++ )
580 potrace_dpoint_t p4 )
582 double dd0, dd1, dd,
delta, e2, epsilon, t;
595 dd0 =
square( p1.x - 2 * p2.x + p3.x ) +
square( p1.y - 2 * p2.y + p3.y );
596 dd1 =
square( p2.x - 2 * p3.x + p4.x ) +
square( p2.y - 2 * p3.y + p4.y );
597 dd = 6 * sqrt( std::max( dd0, dd1 ) );
599 epsilon = sqrt( e2 );
601 for( t = epsilon; t<1; t += epsilon )
603 potrace_dpoint_t intermediate_point;
604 intermediate_point.x = p1.x *
cube( 1 - t ) +
605 3* p2.x*
square( 1 - t ) * t +
606 3 * p3.x * (1 - t) *
square( t ) +
609 intermediate_point.y = p1.y *
cube( 1 - t ) +
610 3* p2.y*
square( 1 - t ) * t +
611 3 * p3.y * (1 - t) *
square( t ) + p4.y*
cube( t );
613 aCornersBuffer.push_back( intermediate_point );
616 aCornersBuffer.push_back( p4 );
constexpr double SCH_IU_PER_MM
constexpr double PCB_IU_PER_MM
constexpr double PL_IU_PER_MM
static void BezierToPolyline(std::vector< potrace_dpoint_t > &aCornersBuffer, potrace_dpoint_t p1, potrace_dpoint_t p2, potrace_dpoint_t p3, potrace_dpoint_t p4)
static void bm_free(potrace_bitmap_t *bm)
#define SCH_LINE_THICKNESS_MM
void outputDataEnd()
Function outputDataEnd write to file the last strings depending on file format.
BITMAPCONV_INFO(std::string &aData)
void outputDataHeader(const char *aBrdLayerName)
Function outputDataHeader write to file the header depending on file format.
void outputOnePolygon(SHAPE_LINE_CHAIN &aPolygon, const char *aBrdLayerName)
Function outputOnePolygon write one polygon to output file.
enum OUTPUT_FMT_ID m_Format
void createOutputData(BMP2CMP_MOD_LAYER aModLayer=(BMP2CMP_MOD_LAYER) 0)
Creates the data specified by m_Format.
int ConvertBitmap(potrace_bitmap_t *aPotrace_bitmap, OUTPUT_FMT_ID aFormat, int aDpi_X, int aDpi_Y, BMP2CMP_MOD_LAYER aModLayer)
Run the conversion of the bitmap.
const char * getBoardLayerName(BMP2CMP_MOD_LAYER aChoice)
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
Represent a set of closed polygons.
void RemoveAllContours()
Remove all outlines & holes (clears) the polygon set.
void BooleanSubtract(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Perform boolean polyset difference For aFastMode meaning, see function booleanOp.
void Fracture(POLYGON_MODE aFastMode)
Convert a set of polygons with holes to a single outline with "slits"/"fractures" connecting the oute...
int NormalizeAreaOutlines()
Convert a self-intersecting polygon to one (or more) non self-intersecting polygon(s).
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
void Simplify(POLYGON_MODE aFastMode)
Simplify the polyset (merges overlapping polys, eliminates degeneracy/self-intersections) For aFastMo...
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int NewOutline()
Creates a new empty polygon in the set and returns its index.
int OutlineCount() const
Return the number of outlines in the set.