46#include <wx/filename.h>
47#include <wx/wfstream.h>
48#include <boost/ptr_container/ptr_map.hpp>
52static inline long parseInt(
const wxString& aValue,
double aScalar )
54 double value = std::numeric_limits<double>::max();
77 if( aValue.EndsWith( wxT(
"mm" ) ) )
79 aScalar *= 100000.0 / 25.4;
81 else if( aValue.EndsWith( wxT(
"mil" ) ) )
88 aValue.ToCDouble(&value);
90 if( value == std::numeric_limits<double>::max() )
92 THROW_IO_ERROR( wxString::Format(
_(
"Cannot convert '%s' to an integer." ),
96 return KiROUND( value * aScalar );
123 m_filename( aFileName ),
124 m_footprint( aFootprint )
149 void Remove(
const wxString& aFootprintName );
157 static long long GetTimestamp(
const wxString& aLibPath );
177 bool testFlags(
const wxString& aFlag,
long aMask,
const wxChar* aName );
226 if( !dir.IsOpened() )
228 THROW_IO_ERROR( wxString::Format(
_(
"Footprint library '%s' not found." ),
239 if( !dir.GetFirst( &fullName, fileSpec ) )
242 wxString cacheErrorMsg;
262 if( !cacheErrorMsg.IsEmpty() )
263 cacheErrorMsg += wxT(
"\n\n" );
265 cacheErrorMsg += ioe.
What();
267 }
while( dir.GetNext( &fullName ) );
269 if( !cacheErrorMsg.IsEmpty() )
276 std::string footprintName =
TO_UTF8( aFootprintName );
278 FOOTPRINT_MAP::const_iterator it =
m_footprints.find( footprintName );
282 THROW_IO_ERROR( wxString::Format(
_(
"Library '%s' has no footprint '%s'." ),
284 aFootprintName.GetData() ) );
288 wxString fullPath = it->second->GetFileName().GetFullPath();
290 wxRemoveFile( fullPath );
312 #define TEXT_DEFAULT_SIZE ( 40*pcbIUScale.IU_PER_MILS )
313 #define OLD_GPCB_UNIT_CONV pcbIUScale.IU_PER_MILS
316 #define NEW_GPCB_UNIT_CONV ( 0.01*pcbIUScale.IU_PER_MILS )
324 wxArrayString parameters;
325 std::unique_ptr<FOOTPRINT> footprint = std::make_unique<FOOTPRINT>(
nullptr );
327 if( aLineReader->
ReadLine() ==
nullptr )
329 msg = aLineReader->
GetSource() + wxT(
": empty file" );
335 paramCnt = parameters.GetCount();
345 if( parameters[0].CmpNoCase( wxT(
"Element" ) ) != 0 )
347 msg.Printf(
_(
"Unknown token '%s'" ), parameters[0] );
352 if( paramCnt < 10 || paramCnt > 14 )
354 msg.Printf(
_(
"Element token contains %d parameters." ), paramCnt );
360 if( parameters[1] == wxT(
"(" ) )
365 footprint->SetLibDescription( parameters[3] );
366 footprint->SetReference( parameters[4] );
370 footprint->SetLibDescription( parameters[2] );
371 footprint->SetReference( parameters[3] );
376 footprint->SetValue( parameters[5] );
380 if( footprint->Value().GetText().IsEmpty() )
381 footprint->Value().SetText( wxT(
"VAL**" ) );
383 if( footprint->Reference().GetText().IsEmpty() )
384 footprint->Reference().SetText( wxT(
"REF**" ) );
391 if( parameters.IsEmpty() || parameters[0] == wxT(
"(" ) )
394 if( parameters[0] == wxT(
")" ) )
397 paramCnt = parameters.GetCount();
402 if( parameters[1] == wxT(
"(" ) )
409 parameters[0], paramCnt );
412 if( parameters[0].CmpNoCase( wxT(
"ElementLine" ) ) == 0 )
416 msg.Printf( wxT(
"ElementLine token contains %d parameters." ), paramCnt );
424 parseInt( parameters[3], conv_unit ) ) );
426 parseInt( parameters[5], conv_unit ) ) );
428 LINE_STYLE::SOLID ) );
430 shape->
Rotate( { 0, 0 }, footprint->GetOrientation() );
431 shape->
Move( footprint->GetPosition() );
433 footprint->Add( shape );
438 if( parameters[0].CmpNoCase( wxT(
"ElementArc" ) ) == 0 )
442 msg.Printf( wxT(
"ElementArc token contains %d parameters." ), paramCnt );
450 footprint->Add( shape );
453 int radius = (
parseInt( parameters[4], conv_unit ) +
454 parseInt( parameters[5], conv_unit ) ) / 2;
457 parseInt( parameters[3], conv_unit ) );
479 shape->
SetStart( arcStart + centre );
486 LINE_STYLE::SOLID ) );
488 shape->
Rotate( { 0, 0 }, footprint->GetOrientation() );
489 shape->
Move( footprint->GetPosition() );
498 if( parameters[0].CmpNoCase( wxT(
"Pad" ) ) == 0 )
500 if( paramCnt < 10 || paramCnt > 13 )
502 msg.Printf( wxT(
"Pad token contains %d parameters." ), paramCnt );
507 std::unique_ptr<PAD>
pad = std::make_unique<PAD>( footprint.get() );
512 pad->SetShape( PAD_SHAPE::RECTANGLE );
513 pad->SetAttribute( PAD_ATTRIB::SMD );
514 pad->SetLayerSet( pad_front );
516 if(
testFlags( parameters[paramCnt-2], 0x0080, wxT(
"onsolder" ) ) )
517 pad->SetLayerSet( pad_back );
527 pad->SetNumber( parameters[paramCnt-3] );
529 int x1 =
parseInt( parameters[2], conv_unit );
530 int x2 =
parseInt( parameters[4], conv_unit );
531 int y1 =
parseInt( parameters[3], conv_unit );
532 int y2 =
parseInt( parameters[5], conv_unit );
533 int width =
parseInt( parameters[6], conv_unit );
535 double angle = atan2( (
double)
delta.y, (
double)
delta.x );
540 int clearance =
parseInt( parameters[7], conv_unit );
544 pad->SetLocalClearance( clearance / 2 );
549 int maskMargin =
parseInt( parameters[8], conv_unit );
550 maskMargin = ( maskMargin - width ) / 2;
551 pad->SetLocalSolderMaskMargin( maskMargin );
556 pad->SetOrientation( orient );
558 VECTOR2I padPos( ( x1 + x2 ) / 2, ( y1 + y2 ) / 2 );
562 padPos += footprint->GetPosition();
563 pad->SetPosition( padPos );
565 if( !
testFlags( parameters[paramCnt-2], 0x0100, wxT(
"square" ) ) )
567 if(
pad->GetSize().x ==
pad->GetSize().y )
568 pad->SetShape( PAD_SHAPE::CIRCLE );
570 pad->SetShape( PAD_SHAPE::OVAL );
573 if(
pad->GetSizeX() > 0 &&
pad->GetSizeY() > 0 )
575 footprint->Add(
pad.release() );
579 wxLogError(
_(
"Invalid zero-sized pad ignored in\nfile: %s" ),
592 if( parameters[0].CmpNoCase( wxT(
"Pin" ) ) == 0 )
594 if( paramCnt < 8 || paramCnt > 12 )
596 msg.Printf( wxT(
"Pin token contains %d parameters." ), paramCnt );
603 pad->SetShape( PAD_SHAPE::CIRCLE );
607 pad->SetLayerSet( pad_set );
609 if(
testFlags( parameters[paramCnt-2], 0x0100, wxT(
"square" ) ) )
610 pad->SetShape( PAD_SHAPE::RECTANGLE );
616 pad->SetNumber( parameters[paramCnt-3] );
619 parseInt( parameters[3], conv_unit ) );
621 int padSize =
parseInt( parameters[4], conv_unit );
630 int clearance =
parseInt( parameters[5], conv_unit );
634 pad->SetLocalClearance( clearance / 2 );
639 int maskMargin =
parseInt( parameters[6], conv_unit );
640 maskMargin = ( maskMargin - padSize ) / 2;
641 pad->SetLocalSolderMaskMargin( maskMargin );
643 drillSize =
parseInt( parameters[7], conv_unit );
647 drillSize =
parseInt( parameters[5], conv_unit );
650 pad->SetDrillSize(
VECTOR2I( drillSize, drillSize ) );
652 padPos += footprint->GetPosition();
653 pad->SetPosition( padPos );
655 if(
pad->GetShape() == PAD_SHAPE::CIRCLE &&
pad->GetSize().x !=
pad->GetSize().y )
656 pad->SetShape( PAD_SHAPE::OVAL );
658 footprint->Add(
pad );
663 footprint->AutoPositionFields();
665 return footprint.release();
673 char* line = aLineReader->
Line();
687 aParameterList.Add( tmp );
692 aParameterList.Add( tmp );
697 if( aParameterList.GetCount() == 1 )
709 aParameterList.Add( tmp );
714 aParameterList.Add( tmp );
728 aParameterList.Add( tmp );
740 aParameterList.Add( wxEmptyString );
751 aParameterList.Add( tmp );
779 if( aFlag.StartsWith( wxT(
"0x" ), &number ) || aFlag.StartsWith( wxT(
"0X" ), &number ) )
783 if( number.ToLong( &lflags, 16 ) && ( lflags & aMask ) )
786 else if( aFlag.Contains( aName ) )
806 m_ctl( aControlFlags )
838 wxString& aFootprintNameOut,
841 wxFileName fn( aFootprintPath );
847 char* line = reader.
Line();
852 if( strncasecmp( line,
"Element", strlen(
"Element" ) ) != 0 )
855 aFootprintNameOut = fn.GetName();
865 wxDir dir( aLibraryPath );
868 if( !dir.IsOpened() )
874 THROW_IO_ERROR( wxString::Format(
_(
"Footprint library '%s' not found." ),
887 errorMsg = ioe.
What();
894 aFootprintNames.Add(
From_UTF8( footprint.first.c_str() ) );
896 if( !errorMsg.IsEmpty() && !aBestEfforts )
902 const wxString& aFootprintName,
914 FOOTPRINT_MAP::const_iterator it = mods.find(
TO_UTF8( aFootprintName ) );
916 if( it == mods.end() )
919 return it->second->GetFootprint();
924 const wxString& aFootprintName,
927 return getFootprint( aLibraryPath, aFootprintName, aProperties,
false );
932 const wxString& aFootprintName,
941 copy->SetParent(
nullptr );
961 aLibraryPath.GetData() ) );
971 fn.SetPath( aLibraryPath );
974 if( !fn.DirExists() )
977 if( !fn.IsDirWritable() )
979 THROW_IO_ERROR( wxString::Format(
_(
"Insufficient permissions to delete folder '%s'." ),
980 aLibraryPath.GetData() ) );
983 wxDir dir( aLibraryPath );
985 if( dir.HasSubDirs() )
987 THROW_IO_ERROR( wxString::Format(
_(
"Library folder '%s' has unexpected sub-folders." ),
988 aLibraryPath.GetData() ) );
998 wxDir::GetAllFiles( aLibraryPath, &files );
1000 for( i = 0; i < files.GetCount(); i++ )
1006 THROW_IO_ERROR( wxString::Format(
_(
"Unexpected file '%s' found in library '%s'." ),
1008 aLibraryPath.GetData() ) );
1012 for( i = 0; i < files.GetCount(); i++ )
1014 wxRemoveFile( files[i] );
1019 aLibraryPath.GetData() );
1023 if( !wxRmdir( aLibraryPath ) )
1025 THROW_IO_ERROR( wxString::Format(
_(
"Footprint library '%s' cannot be deleted." ),
1026 aLibraryPath.GetData() ) );
1033 wxMilliSleep( 250L );
void SetCenter(const VECTOR2I &aCenter)
void SetStart(const VECTOR2I &aStart)
void SetShape(SHAPE_T aShape)
void SetEnd(const VECTOR2I &aEnd)
void SetArcAngleAndEnd(const EDA_ANGLE &aAngle, bool aCheckNegativeAngle=false)
Set the end point from the angle center and start.
A LINE_READER that reads from an open file.
helper class for creating a footprint library cache.
FOOTPRINT * GetFootprint() const
GPCB_FPL_CACHE_ITEM(FOOTPRINT *aFootprint, const WX_FILENAME &aFileName)
WX_FILENAME GetFileName() const
std::unique_ptr< FOOTPRINT > m_footprint
WX_FILENAME m_filename
The full file name and path of the footprint to cache.
FOOTPRINT * parseFOOTPRINT(LINE_READER *aLineReader)
FOOTPRINT_MAP & GetFootprints()
void Remove(const wxString &aFootprintName)
FOOTPRINT_MAP m_footprints
Map of footprint file name to FOOTPRINT*.
bool IsModified()
Return true if the cache is not up-to-date.
static long long GetTimestamp(const wxString &aLibPath)
Generate a timestamp representing all source files in the cache (including the parent directory).
GPCB_FPL_CACHE(GPCB_PLUGIN *aOwner, const wxString &aLibraryPath)
void parseParameters(wxArrayString &aParameterList, LINE_READER *aLineReader)
Extract parameters and tokens from aLineReader and adds them to aParameterList.
long long m_cache_timestamp
A hash of the timestamps for all the footprint files.
wxFileName m_lib_path
The path of the library.
bool m_cache_dirty
Stored separately because it's expensive to check m_cache_timestamp against all the files.
GPCB_PLUGIN * m_owner
Plugin object that owns the cache.
void Load()
Save not implemented for the Geda PCB footprint library format.
bool testFlags(const wxString &aFlag, long aMask, const wxChar *aName)
Test aFlag for aMask or aName.
A PLUGIN derivation for saving and loading Geda PCB files.
bool FootprintLibDelete(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
Delete an existing footprint library and returns true, or if library does not exist returns false,...
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const STRING_UTF8_MAP *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
void validateCache(const wxString &aLibraryPath, bool checkModified=true)
void init(const STRING_UTF8_MAP *aProperties)
bool IsFootprintLibWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const STRING_UTF8_MAP *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PL...
LINE_READER * m_reader
no ownership here.
GPCB_FPL_CACHE * m_cache
Footprint library cache.
const FOOTPRINT * GetEnumeratedFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr) override
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management.
FOOTPRINT * ImportFootprint(const wxString &aFootprintPath, wxString &aFootprintNameOut, const STRING_UTF8_MAP *aProperties) override
Load a single footprint from aFootprintPath and put its name in aFootprintNameOut.
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
const FOOTPRINT * getFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties, bool checkModified)
void FootprintDelete(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr) override
Delete aFootprintName from the library at aLibraryPath.
const STRING_UTF8_MAP * m_props
passed via Save() or Load(), no ownership, may be NULL.
friend class GPCB_FPL_CACHE
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
A logical library item identifier and consists of various portions much like a URI.
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
virtual const wxString & GetSource() const
Returns the name of the source of the lines in an abstract sense.
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
char * Line() const
Return a pointer to the last line that was read in.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
LSET is a set of PCB_LAYER_IDs.
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate this object.
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
void Move(const VECTOR2I &aMoveVector) override
Move this object.
void SetStroke(const STROKE_PARAMS &aStroke) override
A name/value tuple with unique names and optional values.
Simple container to manage line stroke parameters.
Read lines of text from another LINE_READER but only returns non-comment lines and non-blank lines wi...
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
A wrapper around a wxFileName which is much more performant with a subset of the API.
void SetFullName(const wxString &aFileNameAndExtension)
wxString GetFullPath() const
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
A copy of ConvertFileTimeToWx() because wxWidgets left it as a static function private to src/common/...
std::map< wxString, FOOTPRINT * > FOOTPRINT_MAP
static constexpr EDA_ANGLE & ANGLE_180
static constexpr EDA_ANGLE & ANGLE_360
static long parseInt(const wxString &aValue, double aScalar)
#define NEW_GPCB_UNIT_CONV
#define OLD_GPCB_UNIT_CONV
boost::ptr_map< std::string, GPCB_FPL_CACHE_ITEM > FOOTPRINT_MAP
const std::string KiCadFootprintFileExtension
const std::string GedaPcbFootprintLibFileExtension
const wxChar *const traceGedaPcbPlugin
Flag to enable GEDA PCB plugin debug output.
#define THROW_IO_ERROR(msg)
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
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 ...
wxString From_UTF8(const char *cstring)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
wxLogTrace helper definitions.
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
double EuclideanNorm(const VECTOR2I &vector)
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition of file extensions used in Kicad.