38#include <potracelib.h>
40#include <fmt/format.h>
41#include <wx/translation.h>
47static void bm_free( potrace_bitmap_t* bm )
57 potrace_dpoint_t p1, potrace_dpoint_t p2,
58 potrace_dpoint_t p3, potrace_dpoint_t p4 );
63 m_reporter( aReporter )
76 int aDpi_X,
int aDpi_Y,
const wxString& aLayer )
78 potrace_param_t* param;
82 param = potrace_param_default();
86 m_reporter.
Report( fmt::format(
"Error allocating parameters: {}\n", strerror( errno ) ),
94 param->opttolerance = 0.2;
97 st = potrace_trace( param, aPotrace_bitmap );
99 if( !st || st->status != POTRACE_STATUS_OK )
102 potrace_state_free( st );
104 potrace_param_free( param );
106 m_reporter.
Report( fmt::format(
"Error tracing bitmap: {}\n", strerror( errno ) ),
145 potrace_state_free( st );
146 potrace_param_free( param );
160 m_Data +=
"%!PS-Adobe-3.0 EPSF-3.0\n";
168 m_Data += fmt::format(
"(footprint \"{}\" (version 20221018) (generator \"bitmap2component\") (generator_version \"{}\")\n"
169 " (layer \"F.Cu\")\n",
173 m_Data += fmt::format(
" (attr board_only exclude_from_pos_files exclude_from_bom)\n" );
174 m_Data += fmt::format(
" (fp_text reference \"G***\" (at 0 0) (layer \"{}\")\n"
175 " (effects (font (size 1.5 1.5) (thickness 0.3)))\n"
177 aBrdLayerName.ToStdString().c_str(),
178 KIID().AsString().ToStdString().c_str() );
180 m_Data += fmt::format(
" (fp_text value \"{}\" (at 0.75 0) (layer \"{}\") hide\n"
181 " (effects (font (size 1.5 1.5) (thickness 0.3)))\n"
184 aBrdLayerName.ToStdString().c_str(),
185 KIID().AsString().ToStdString().c_str() );
189 m_Data += fmt::format(
"(kicad_wks (version 20220228) (generator \"bitmap2component\") (generator_version \"{}\")\n",
191 m_Data +=
" (setup (textsize 1.5 1.5)(linewidth 0.15)(textlinewidth 0.15)\n";
192 m_Data +=
" (left_margin 10)(right_margin 10)(top_margin 10)(bottom_margin 10))\n";
193 m_Data +=
" (polygon (name \"\") (pos 0 0) (linewidth 0.01)\n";
197 m_Data += fmt::format(
"(kicad_symbol_lib (version 20220914) (generator \"bitmap2component\") (generator_version \"{}\")\n",
204 Ypos += fieldSize / 2;
205 m_Data += fmt::format(
" (symbol \"{}\" (pin_names (offset 1.016)) (in_bom yes) (on_board yes)\n",
208 m_Data += fmt::format(
" (property \"Reference\" \"#G\" (at 0 {:g} 0)\n"
209 " (effects (font (size {:g} {:g})) hide)\n )\n",
214 m_Data += fmt::format(
" (property \"Value\" \"{}\" (at 0 {:g} 0)\n"
215 " (effects (font (size {:g} {:g})) hide)\n )\n",
221 m_Data += fmt::format(
" (property \"Footprint\" \"\" (at 0 0 0)\n"
222 " (effects (font (size {:g} {:g})) hide)\n )\n",
226 m_Data += fmt::format(
" (property \"Datasheet\" \"\" (at 0 0 0)\n"
227 " (effects (font (size {:g} {:g})) hide)\n )\n",
283 m_Data += fmt::format(
"newpath\n{} {} moveto\n", startpoint.
x, offsetY - startpoint.
y );
286 for( ii = 1; ii < aPolygon.
PointCount(); ii++ )
288 currpoint = aPolygon.
CPoint( ii );
289 m_Data += fmt::format(
" {} {} lineto", currpoint.
x, offsetY - currpoint.
y );
298 m_Data +=
"\nclosepath fill\n";
302 m_Data +=
" (fp_poly\n (pts\n";
306 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
308 currpoint = aPolygon.
CPoint( ii );
309 m_Data += fmt::format(
" (xy {} {})\n",
316 m_Data += fmt::format(
" (stroke (width {:f}) (type solid)) (fill solid) (layer \"{}\") (uuid {}))\n",
318 aBrdLayerName.ToStdString().c_str(),
319 KIID().AsString().ToStdString().c_str() );
328 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
330 currpoint = aPolygon.
CPoint( ii );
331 m_Data += fmt::format(
" (xy {:.3f} {:.3f})",
343 m_Data += fmt::format(
" (xy {:.3f} {:.3f}) )\n",
351#define SCH_LINE_THICKNESS_MM 0.01
353 m_Data +=
" (polyline\n (pts\n";
355 for( ii = 0; ii < aPolygon.
PointCount(); ii++ )
357 currpoint = aPolygon.
CPoint( ii );
358 m_Data += fmt::format(
" (xy {:f} {:f})\n",
364 m_Data += fmt::format(
" (xy {:f} {:f})\n",
369 m_Data += fmt::format(
" (stroke (width {:g}) (type default))\n"
370 " (fill (type outline))\n",
380 std::vector <potrace_dpoint_t> cornersBuffer;
388 potrace_dpoint_t( *c )[3];
397 bool main_outline =
true;
402 potrace_path_t* paths =
m_Paths;
406 m_reporter.
Report(
_(
"No shape in black and white image to convert: no outline created." ),
410 while( paths !=
nullptr )
412 int cnt = paths->curve.n;
413 int* tag = paths->curve.tag;
415 potrace_dpoint_t startpoint = c[cnt - 1][2];
417 for(
int i = 0; i < cnt; i++ )
422 cornersBuffer.push_back( c[i][1] );
423 cornersBuffer.push_back( c[i][2] );
424 startpoint = c[i][2];
427 case POTRACE_CURVETO:
429 startpoint = c[i][2];
437 main_outline =
false;
442 for(
const potrace_dpoint_s& pt : cornersBuffer )
453 for(
const potrace_dpoint_s& pt : cornersBuffer )
460 cornersBuffer.clear();
463 if( paths->next ==
nullptr || paths->next->sign ==
'+' )
476 for(
int ii = 0; ii < polyset_areas.
OutlineCount(); ii++ )
513 potrace_dpoint_t p4 )
528 dd0 =
square( p1.x - 2 * p2.x + p3.x ) +
square( p1.y - 2 * p2.y + p3.y );
529 dd1 =
square( p2.x - 2 * p3.x + p4.x ) +
square( p2.y - 2 * p3.y + p4.y );
530 dd = 6 * sqrt( std::max( dd0, dd1 ) );
536 potrace_dpoint_t intermediate_point;
537 intermediate_point.x = p1.x *
cube( 1 - t ) +
538 3* p2.x*
square( 1 - t ) * t +
539 3 * p3.x * (1 - t) *
square( t ) +
542 intermediate_point.y = p1.y *
cube( 1 - t ) +
543 3* p2.y*
square( 1 - t ) * t +
544 3 * p3.y * (1 - t) *
square( t ) + p4.y*
cube( t );
546 aCornersBuffer.push_back( intermediate_point );
549 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.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
A pure virtual class used to derive REPORTER objects from.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
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 Fracture()
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()
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 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 ...