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 sprintf( 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";
174 layerName =
"Eco1.User";
178 layerName =
"Eco2.User";
200 m_Data +=
"%%!PS-Adobe-3.0 EPSF-3.0\n";
210 "(footprint \"%s\" (version 20221018) (generator bitmap2component)\n"
211 " (layer \"F.Cu\")\n",
215 sprintf( strbuf,
" (attr board_only exclude_from_pos_files exclude_from_bom)\n" );
218 " (fp_text reference \"G***\" (at 0 0) (layer \"%s\")\n"
219 " (effects (font (size 1.5 1.5) (thickness 0.3)))\n"
220 " (tstamp %s)\n )\n",
221 aBrdLayerName,
KIID().
AsString().ToStdString().c_str() );
225 " (fp_text value \"%s\" (at 0.75 0) (layer \"%s\") hide\n"
226 " (effects (font (size 1.5 1.5) (thickness 0.3)))\n"
227 " (tstamp %s)\n )\n",
234 m_Data +=
"(kicad_wks (version 20220228) (generator bitmap2component)\n";
235 m_Data +=
" (setup (textsize 1.5 1.5)(linewidth 0.15)(textlinewidth 0.15)\n";
236 m_Data +=
" (left_margin 10)(right_margin 10)(top_margin 10)(bottom_margin 10))\n";
237 m_Data +=
" (polygon (name \"\") (pos 0 0) (linewidth 0.01)\n";
243 Ypos += fieldSize / 2;
246 "(kicad_symbol_lib (version 20220914) (generator bitmap2component)\n"
247 " (symbol \"%s\" (pin_names (offset 1.016)) (in_bom yes) (on_board yes)\n",
252 " (property \"Reference\" \"#G\" (at 0 %g 0)\n"
253 " (effects (font (size %g %g)) hide)\n )\n",
254 -Ypos, fieldSize, fieldSize );
258 " (property \"Value\" \"%s\" (at 0 %g 0)\n"
259 " (effects (font (size %g %g)) hide)\n )\n",
260 m_CmpName.c_str(), Ypos, fieldSize, fieldSize );
264 " (property \"Footprint\" \"\" (at 0 0 0)\n"
265 " (effects (font (size %g %g)) hide)\n )\n",
266 fieldSize, fieldSize );
270 " (property \"Datasheet\" \"\" (at 0 0 0)\n"
271 " (effects (font (size %g %g)) hide)\n )\n",
272 fieldSize, fieldSize );
275 sprintf( strbuf,
" (symbol \"%s_0_0\"\n",
m_CmpName.c_str() );
325 sprintf( strbuf,
"newpath\n%d %d moveto\n", startpoint.
x, offsetY - startpoint.
y );
329 for( ii = 1; ii < aPolygon.
PointCount(); ii++ )
331 currpoint = aPolygon.
CPoint( ii );
332 sprintf( strbuf,
" %d %d lineto", currpoint.
x, offsetY - currpoint.
y );
342 m_Data +=
"\nclosepath fill\n";
348 m_Data +=
" (fp_poly\n (pts\n";
352 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
354 currpoint = aPolygon.
CPoint( ii );
355 sprintf( strbuf,
" (xy %f %f)\n",
364 " (stroke (width %f) (type solid)) (fill solid) (layer \"%s\") (tstamp %s))\n",
365 width, aBrdLayerName,
KIID().
AsString().ToStdString().c_str() );
377 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
379 currpoint = aPolygon.
CPoint( ii );
380 sprintf( strbuf,
" (xy %.3f %.3f)",
393 sprintf( strbuf,
" (xy %.3f %.3f) )\n",
401 #define SCH_LINE_THICKNESS_MM 0.01
403 m_Data +=
" (polyline\n (pts\n";
405 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
407 currpoint = aPolygon.
CPoint( ii );
408 sprintf( strbuf,
" (xy %f %f)\n",
415 sprintf( strbuf,
" (xy %f %f)\n",
422 " (stroke (width %g) (type default))\n (fill (type outline))\n",
434 std::vector <potrace_dpoint_t> cornersBuffer;
442 potrace_dpoint_t( *c )[3];
451 bool main_outline =
true;
456 potrace_path_t* paths =
m_Paths;
460 m_errors +=
"No shape in black and white image to convert: no outline created\n";
463 while( paths !=
nullptr )
465 int cnt = paths->curve.n;
466 int* tag = paths->curve.tag;
468 potrace_dpoint_t startpoint = c[cnt - 1][2];
470 for(
int i = 0; i < cnt; i++ )
475 cornersBuffer.push_back( c[i][1] );
476 cornersBuffer.push_back( c[i][2] );
477 startpoint = c[i][2];
480 case POTRACE_CURVETO:
482 startpoint = c[i][2];
490 main_outline =
false;
494 for(
unsigned int i = 0; i < cornersBuffer.size(); i++ )
497 int( cornersBuffer[i].y *
m_ScaleY ) );
505 for(
unsigned int i = 0; i < cornersBuffer.size(); i++ )
508 int( cornersBuffer[i].y *
m_ScaleY ) );
512 cornersBuffer.clear();
515 if( paths->next ==
nullptr || paths->next->sign ==
'+' )
528 for(
int ii = 0; ii < polyset_areas.
OutlineCount(); ii++ )
565 potrace_dpoint_t p4 )
567 double dd0, dd1, dd,
delta, e2, epsilon, t;
580 dd0 =
square( p1.x - 2 * p2.x + p3.x ) +
square( p1.y - 2 * p2.y + p3.y );
581 dd1 =
square( p2.x - 2 * p3.x + p4.x ) +
square( p2.y - 2 * p3.y + p4.y );
582 dd = 6 * sqrt( std::max( dd0, dd1 ) );
584 epsilon = sqrt( e2 );
586 for( t = epsilon; t<1; t += epsilon )
588 potrace_dpoint_t intermediate_point;
589 intermediate_point.x = p1.x *
cube( 1 - t ) +
590 3* p2.x*
square( 1 - t ) * t +
591 3 * p3.x * (1 - t) *
square( t ) +
594 intermediate_point.y = p1.y *
cube( 1 - t ) +
595 3* p2.y*
square( 1 - t ) * t +
596 3 * p3.y * (1 - t) *
square( t ) + p4.y*
cube( t );
598 aCornersBuffer.push_back( intermediate_point );
601 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 BooleanSubtract(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Perform boolean polyset intersection For aFastMode meaning, see function booleanOp.
void Fracture(POLYGON_MODE aFastMode)
Convert a single outline slitted ("fractured") polygon into a set ouf outlines with holes.
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)
Add a new vertex to the contour indexed by aOutline and aHole (defaults to the outline of the last po...
void Simplify(POLYGON_MODE aFastMode)
SHAPE_LINE_CHAIN & Outline(int aIndex)
int NewOutline()
Creates a new hole in a given outline.
int OutlineCount() const
Return the number of vertices in a given outline/hole.
_OUT_STRING AsString(const std::string &aString)