39#include <potracelib.h>
41#include <fmt/format.h>
47static void bm_free( potrace_bitmap_t* bm )
62 potrace_dpoint_t p4 );
80 potrace_param_t* param;
84 param = potrace_param_default();
88 m_errors += fmt::format(
"Error allocating parameters: {}\n", strerror( errno ) );
95 param->opttolerance = 0.2;
98 st = potrace_trace( param, aPotrace_bitmap );
100 if( !st || st->status != POTRACE_STATUS_OK )
104 potrace_state_free( st );
107 potrace_param_free( param );
109 m_errors += fmt::format(
"Error tracing bitmap: {}\n", strerror( errno ) );
155 potrace_state_free( st );
156 potrace_param_free( param );
164 const char* layerName =
"F.SilkS";
169 layerName =
"F.Mask";
177 layerName =
"Dwgs.User";
181 layerName =
"Cmts.User";
185 layerName =
"Eco1.User";
189 layerName =
"Eco2.User";
210 m_Data +=
"%!PS-Adobe-3.0 EPSF-3.0\n";
219 strbuf = fmt::format(
"(footprint \"{}\" (version 20221018) (generator \"bitmap2component\") (generator_version \"{}\")\n"
220 " (layer \"F.Cu\")\n",
224 strbuf = fmt::format(
225 " (attr board_only exclude_from_pos_files exclude_from_bom)\n" );
227 strbuf = fmt::format(
228 " (fp_text reference \"G***\" (at 0 0) (layer \"{}\")\n"
229 " (effects (font (size 1.5 1.5) (thickness 0.3)))\n"
231 aBrdLayerName,
KIID().AsString().ToStdString().c_str() );
234 strbuf = fmt::format(
235 " (fp_text value \"{}\" (at 0.75 0) (layer \"{}\") hide\n"
236 " (effects (font (size 1.5 1.5) (thickness 0.3)))\n"
238 m_CmpName.c_str(), aBrdLayerName,
KIID().AsString().ToStdString().c_str() );
244 m_Data += fmt::format(
"(kicad_wks (version 20220228) (generator \"bitmap2component\") (generator_version \"{}\")\n",
GetMajorMinorVersion().ToStdString() );
245 m_Data +=
" (setup (textsize 1.5 1.5)(linewidth 0.15)(textlinewidth 0.15)\n";
246 m_Data +=
" (left_margin 10)(right_margin 10)(top_margin 10)(bottom_margin 10))\n";
247 m_Data +=
" (polygon (name \"\") (pos 0 0) (linewidth 0.01)\n";
253 Ypos += fieldSize / 2;
255 strbuf = fmt::format(
256 "(kicad_symbol_lib (version 20220914) (generator \"bitmap2component\") (generator_version \"{}\")\n"
257 " (symbol \"{}\" (pin_names (offset 1.016)) (in_bom yes) (on_board yes)\n",
261 strbuf = fmt::format(
262 " (property \"Reference\" \"#G\" (at 0 {:g} 0)\n"
263 " (effects (font (size {:g} {:g})) hide)\n )\n",
264 -Ypos, fieldSize, fieldSize );
267 strbuf = fmt::format(
268 " (property \"Value\" \"{}\" (at 0 {:g} 0)\n"
269 " (effects (font (size {:g} {:g})) hide)\n )\n",
270 m_CmpName.c_str(), Ypos, fieldSize, fieldSize );
273 strbuf = fmt::format(
274 " (property \"Footprint\" \"\" (at 0 0 0)\n"
275 " (effects (font (size {:g} {:g})) hide)\n )\n",
276 fieldSize, fieldSize );
279 strbuf = fmt::format(
280 " (property \"Datasheet\" \"\" (at 0 0 0)\n"
281 " (effects (font (size {:g} {:g})) hide)\n )\n",
282 fieldSize, fieldSize );
285 strbuf = fmt::format(
" (symbol \"{}_0_0\"\n",
m_CmpName.c_str() );
335 strbuf = fmt::format(
"newpath\n{} {} moveto\n", startpoint.
x, offsetY - startpoint.
y );
339 for( ii = 1; ii < aPolygon.
PointCount(); ii++ )
341 currpoint = aPolygon.
CPoint( ii );
342 strbuf = fmt::format(
" {} {} lineto", currpoint.
x, offsetY - currpoint.
y );
352 m_Data +=
"\nclosepath fill\n";
358 m_Data +=
" (fp_poly\n (pts\n";
362 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
364 currpoint = aPolygon.
CPoint( ii );
365 strbuf = fmt::format(
" (xy {} {})\n",
373 strbuf = fmt::format(
374 " (stroke (width {:f}) (type solid)) (fill solid) (layer \"{}\") (uuid {}))\n",
375 width, aBrdLayerName,
KIID().AsString().ToStdString().c_str() );
387 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
389 currpoint = aPolygon.
CPoint( ii );
390 strbuf = fmt::format(
" (xy {:.3f} {:.3f})",
403 strbuf = fmt::format(
" (xy {:.3f} {:.3f}) )\n",
411#define SCH_LINE_THICKNESS_MM 0.01
413 m_Data +=
" (polyline\n (pts\n";
415 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
417 currpoint = aPolygon.
CPoint( ii );
418 strbuf = fmt::format(
" (xy {:f} {:f})\n",
425 strbuf = fmt::format(
" (xy {:f} {:f})\n",
431 strbuf = fmt::format(
432 " (stroke (width {:g}) (type default))\n (fill (type outline))\n",
444 std::vector <potrace_dpoint_t> cornersBuffer;
452 potrace_dpoint_t( *c )[3];
461 bool main_outline =
true;
466 potrace_path_t* paths =
m_Paths;
470 m_errors +=
"No shape in black and white image to convert: no outline created\n";
473 while( paths !=
nullptr )
475 int cnt = paths->curve.n;
476 int* tag = paths->curve.tag;
478 potrace_dpoint_t startpoint = c[cnt - 1][2];
480 for(
int i = 0; i < cnt; i++ )
485 cornersBuffer.push_back( c[i][1] );
486 cornersBuffer.push_back( c[i][2] );
487 startpoint = c[i][2];
490 case POTRACE_CURVETO:
492 startpoint = c[i][2];
500 main_outline =
false;
504 for(
unsigned int i = 0; i < cornersBuffer.size(); i++ )
507 int( cornersBuffer[i].y *
m_ScaleY ) );
515 for(
unsigned int i = 0; i < cornersBuffer.size(); i++ )
518 int( cornersBuffer[i].y *
m_ScaleY ) );
522 cornersBuffer.clear();
525 if( paths->next ==
nullptr || paths->next->sign ==
'+' )
538 for(
int ii = 0; ii < polyset_areas.
OutlineCount(); ii++ )
575 potrace_dpoint_t p4 )
590 dd0 =
square( p1.x - 2 * p2.x + p3.x ) +
square( p1.y - 2 * p2.y + p3.y );
591 dd1 =
square( p2.x - 2 * p3.x + p4.x ) +
square( p2.y - 2 * p3.y + p4.y );
592 dd = 6 * sqrt( std::max( dd0, dd1 ) );
598 potrace_dpoint_t intermediate_point;
599 intermediate_point.x = p1.x *
cube( 1 - t ) +
600 3* p2.x*
square( 1 - t ) * t +
601 3 * p3.x * (1 - t) *
square( t ) +
604 intermediate_point.y = p1.y *
cube( 1 - t ) +
605 3* p2.y*
square( 1 - t ) * t +
606 3 * p3.y * (1 - t) *
square( t ) + p4.y*
cube( t );
608 aCornersBuffer.push_back( intermediate_point );
611 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
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
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.