34#include <potracelib.h>
36#include <fmt/format.h>
37#include <wx/translation.h>
43static void bm_free( potrace_bitmap_t* bm )
53 potrace_dpoint_t p1, potrace_dpoint_t p2,
54 potrace_dpoint_t p3, potrace_dpoint_t p4 );
72 int aDpi_X,
int aDpi_Y,
const wxString& aLayer )
74 potrace_param_t* param;
78 param = potrace_param_default();
82 m_reporter.Report( fmt::format(
"Error allocating parameters: {}\n", strerror( errno ) ),
90 param->opttolerance = 0.2;
93 st = potrace_trace( param, aPotrace_bitmap );
95 if( !st || st->status != POTRACE_STATUS_OK )
98 potrace_state_free( st );
100 potrace_param_free( param );
102 m_reporter.Report( fmt::format(
"Error tracing bitmap: {}\n", strerror( errno ) ),
141 potrace_state_free( st );
142 potrace_param_free( param );
156 m_Data +=
"%!PS-Adobe-3.0 EPSF-3.0\n";
164 m_Data += fmt::format(
"(footprint \"{}\" (version 20221018) (generator \"bitmap2component\") (generator_version \"{}\")\n"
165 " (layer \"F.Cu\")\n",
169 m_Data += fmt::format(
" (attr board_only exclude_from_pos_files exclude_from_bom)\n" );
170 m_Data += fmt::format(
" (fp_text reference \"G***\" (at 0 0) (layer \"{}\")\n"
171 " (effects (font (size 1.5 1.5) (thickness 0.3)))\n"
173 aBrdLayerName.ToStdString().c_str(),
174 KIID().AsString().ToStdString().c_str() );
176 m_Data += fmt::format(
" (fp_text value \"{}\" (at 0.75 0) (layer \"{}\") hide\n"
177 " (effects (font (size 1.5 1.5) (thickness 0.3)))\n"
180 aBrdLayerName.ToStdString().c_str(),
181 KIID().AsString().ToStdString().c_str() );
185 m_Data += fmt::format(
"(kicad_wks (version 20220228) (generator \"bitmap2component\") (generator_version \"{}\")\n",
187 m_Data +=
" (setup (textsize 1.5 1.5)(linewidth 0.15)(textlinewidth 0.15)\n";
188 m_Data +=
" (left_margin 10)(right_margin 10)(top_margin 10)(bottom_margin 10))\n";
189 m_Data +=
" (polygon (name \"\") (pos 0 0) (linewidth 0.01)\n";
193 m_Data += fmt::format(
"(kicad_symbol_lib (version 20220914) (generator \"bitmap2component\") (generator_version \"{}\")\n",
200 Ypos += fieldSize / 2;
201 m_Data += fmt::format(
" (symbol \"{}\" (pin_names (offset 1.016)) (in_bom yes) (on_board yes)\n",
204 m_Data += fmt::format(
" (property \"Reference\" \"#G\" (at 0 {:g} 0)\n"
205 " (effects (font (size {:g} {:g})) hide)\n )\n",
210 m_Data += fmt::format(
" (property \"Value\" \"{}\" (at 0 {:g} 0)\n"
211 " (effects (font (size {:g} {:g})) hide)\n )\n",
217 m_Data += fmt::format(
" (property \"Footprint\" \"\" (at 0 0 0)\n"
218 " (effects (font (size {:g} {:g})) hide)\n )\n",
222 m_Data += fmt::format(
" (property \"Datasheet\" \"\" (at 0 0 0)\n"
223 " (effects (font (size {:g} {:g})) hide)\n )\n",
279 m_Data += fmt::format(
"newpath\n{} {} moveto\n", startpoint.
x, offsetY - startpoint.
y );
282 for( ii = 1; ii < aPolygon.
PointCount(); ii++ )
284 currpoint = aPolygon.
CPoint( ii );
285 m_Data += fmt::format(
" {} {} lineto", currpoint.
x, offsetY - currpoint.
y );
294 m_Data +=
"\nclosepath fill\n";
298 m_Data +=
" (fp_poly\n (pts\n";
302 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
304 currpoint = aPolygon.
CPoint( ii );
305 m_Data += fmt::format(
" (xy {} {})\n",
312 m_Data += fmt::format(
" (stroke (width {:f}) (type solid)) (fill solid) (layer \"{}\") (uuid {}))\n",
314 aBrdLayerName.ToStdString().c_str(),
315 KIID().AsString().ToStdString().c_str() );
324 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
326 currpoint = aPolygon.
CPoint( ii );
327 m_Data += fmt::format(
" (xy {:.3f} {:.3f})",
339 m_Data += fmt::format(
" (xy {:.3f} {:.3f}) )\n",
347#define SCH_LINE_THICKNESS_MM 0.01
349 m_Data +=
" (polyline\n (pts\n";
351 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
353 currpoint = aPolygon.
CPoint( ii );
354 m_Data += fmt::format(
" (xy {:f} {:f})\n",
360 m_Data += fmt::format(
" (xy {:f} {:f})\n",
365 m_Data += fmt::format(
" (stroke (width {:g}) (type default))\n"
366 " (fill (type outline))\n",
376 std::vector <potrace_dpoint_t> cornersBuffer;
384 potrace_dpoint_t( *c )[3];
391 bool main_outline =
true;
396 potrace_path_t* paths =
m_Paths;
400 m_reporter.Report(
_(
"No shape in black and white image to convert: no outline created." ),
404 while( paths !=
nullptr )
406 int cnt = paths->curve.n;
407 int* tag = paths->curve.tag;
409 potrace_dpoint_t startpoint = c[cnt - 1][2];
411 for(
int i = 0; i < cnt; i++ )
416 cornersBuffer.push_back( c[i][1] );
417 cornersBuffer.push_back( c[i][2] );
418 startpoint = c[i][2];
421 case POTRACE_CURVETO:
423 startpoint = c[i][2];
431 main_outline =
false;
436 for(
const potrace_dpoint_s& pt : cornersBuffer )
447 for(
const potrace_dpoint_s& pt : cornersBuffer )
454 cornersBuffer.clear();
457 if( paths->next ==
nullptr || paths->next->sign ==
'+' )
470 for(
int ii = 0; ii < polyset_areas.
OutlineCount(); ii++ )
507 potrace_dpoint_t p4 )
522 dd0 =
square( p1.x - 2 * p2.x + p3.x ) +
square( p1.y - 2 * p2.y + p3.y );
523 dd1 =
square( p2.x - 2 * p3.x + p4.x ) +
square( p2.y - 2 * p3.y + p4.y );
524 dd = 6 * sqrt( std::max( dd0, dd1 ) );
530 potrace_dpoint_t intermediate_point;
531 intermediate_point.x = p1.x *
cube( 1 - t ) +
532 3* p2.x*
square( 1 - t ) * t +
533 3 * p3.x * (1 - t) *
square( t ) +
536 intermediate_point.y = p1.y *
cube( 1 - t ) +
537 3* p2.y*
square( 1 - t ) * t +
538 3 * p3.y * (1 - t) *
square( t ) + p4.y*
cube( t );
540 aCornersBuffer.push_back( intermediate_point );
543 aCornersBuffer.push_back( p4 );
constexpr double SCH_IU_PER_MM
Schematic internal units 1=100nm.
constexpr double PCB_IU_PER_MM
Pcbnew IU is 1 nanometer.
constexpr double PL_IU_PER_MM
Internal units in micron (should be enough).
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
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
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, REPORTER &aReporter)
void outputOnePolygon(SHAPE_LINE_CHAIN &aPolygon, const wxString &aBrdLayerName)
Function outputOnePolygon write one polygon to output file.
void outputDataHeader(const wxString &aBrdLayerName)
Function outputDataHeader write to file the header depending on file format.
enum OUTPUT_FMT_ID m_Format
int ConvertBitmap(potrace_bitmap_t *aPotrace_bitmap, OUTPUT_FMT_ID aFormat, int aDpi_X, int aDpi_Y, const wxString &aLayer)
Run the conversion of the bitmap.
void createOutputData(const wxString &aBrdLayerName=wxT("F.SilkS"))
Creates the data specified by m_Format.
A pure virtual class used to derive REPORTER objects from.
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.
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()
Simplify the polyset (merges overlapping polys, eliminates degeneracy/self-intersections)
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.
void Fracture(bool aSimplify=true)
Convert a set of polygons with holes to a single outline with "slits"/"fractures" connecting the oute...
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
This file contains miscellaneous commonly used macros and functions.
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
VECTOR2< int32_t > VECTOR2I