46#include <wx/filename.h>
47#include <wx/wfstream.h>
48#include <boost/ptr_container/ptr_map.hpp>
51static inline long parseInt(
const wxString& aValue,
double aScalar )
53 double value = std::numeric_limits<double>::max();
76 if( aValue.EndsWith( wxT(
"mm" ) ) )
78 aScalar *= 100000.0 / 25.4;
80 else if( aValue.EndsWith( wxT(
"mil" ) ) )
87 aValue.ToCDouble(&value);
88 if( value == std::numeric_limits<double>::max() )
90 THROW_IO_ERROR( wxString::Format(
_(
"Cannot convert '%s' to an integer." ),
95 return KiROUND( value * aScalar );
122 m_filename( aFileName ),
123 m_footprint( aFootprint )
148 void Remove(
const wxString& aFootprintName );
156 static long long GetTimestamp(
const wxString& aLibPath );
176 bool testFlags(
const wxString& aFlag,
long aMask,
const wxChar* aName );
225 if( !dir.IsOpened() )
227 THROW_IO_ERROR( wxString::Format(
_(
"Footprint library '%s' not found." ),
238 if( !dir.GetFirst( &fullName, fileSpec ) )
241 wxString cacheErrorMsg;
261 if( !cacheErrorMsg.IsEmpty() )
262 cacheErrorMsg += wxT(
"\n\n" );
264 cacheErrorMsg += ioe.
What();
266 }
while( dir.GetNext( &fullName ) );
268 if( !cacheErrorMsg.IsEmpty() )
275 std::string footprintName =
TO_UTF8( aFootprintName );
277 FOOTPRINT_MAP::const_iterator it =
m_footprints.find( footprintName );
281 THROW_IO_ERROR( wxString::Format(
_(
"Library '%s' has no footprint '%s'." ),
283 aFootprintName.GetData() ) );
287 wxString fullPath = it->second->GetFileName().GetFullPath();
289 wxRemoveFile( fullPath );
311 #define TEXT_DEFAULT_SIZE ( 40*pcbIUScale.IU_PER_MILS )
312 #define OLD_GPCB_UNIT_CONV pcbIUScale.IU_PER_MILS
315 #define NEW_GPCB_UNIT_CONV ( 0.01*pcbIUScale.IU_PER_MILS )
323 wxArrayString parameters;
324 std::unique_ptr<FOOTPRINT> footprint = std::make_unique<FOOTPRINT>(
nullptr );
326 if( aLineReader->
ReadLine() ==
nullptr )
328 msg = aLineReader->
GetSource() + wxT(
": empty file" );
334 paramCnt = parameters.GetCount();
344 if( parameters[0].CmpNoCase( wxT(
"Element" ) ) != 0 )
346 msg.Printf(
_(
"Unknown token '%s'" ), parameters[0] );
351 if( paramCnt < 10 || paramCnt > 14 )
353 msg.Printf(
_(
"Element token contains %d parameters." ), paramCnt );
359 if( parameters[1] == wxT(
"(" ) )
364 footprint->SetDescription( parameters[3] );
365 footprint->SetReference( parameters[4] );
369 footprint->SetDescription( parameters[2] );
370 footprint->SetReference( parameters[3] );
375 footprint->SetValue( parameters[5] );
379 if( footprint->Value().GetText().IsEmpty() )
380 footprint->Value().SetText( wxT(
"Val**" ) );
386 parseInt( parameters[9], conv_unit ) );
391 parseInt( parameters[7], conv_unit ) );
394 int orientation =
parseInt( parameters[paramCnt-4], 1.0 );
401 int twsize = thsize * 30 / 40;
402 int thickness = thsize / 8;
407 textPos.
x = textPos.
x + twsize * footprint->GetReference().Len() / 2;
408 textPos.
y += thsize / 2;
412 textPos.
x -= thsize / 10;
413 textPos.
y += thsize / 2;
415 footprint->Reference().SetFPRelativePosition( textPos );
416 footprint->Reference().SetTextSize(
VECTOR2I( twsize, thsize ) );
417 footprint->Reference().SetTextThickness( thickness );
422 footprint->Value().SetTextAngle( footprint->Reference().GetTextAngle() );
423 footprint->Value().SetTextSize( footprint->Reference().GetTextSize() );
424 footprint->Value().SetTextThickness( footprint->Reference().GetTextThickness() );
425 textPos.
y += thsize * 13 / 10;
426 footprint->Value().SetFPRelativePosition( textPos );
433 if( parameters.IsEmpty() || parameters[0] == wxT(
"(" ) )
436 if( parameters[0] == wxT(
")" ) )
439 paramCnt = parameters.GetCount();
444 if( parameters[1] == wxT(
"(" ) )
451 parameters[0], paramCnt );
454 if( parameters[0].CmpNoCase( wxT(
"ElementLine" ) ) == 0 )
458 msg.Printf( wxT(
"ElementLine token contains %d parameters." ), paramCnt );
466 parseInt( parameters[3], conv_unit ) ) );
468 parseInt( parameters[5], conv_unit ) ) );
470 PLOT_DASH_TYPE::SOLID ) );
472 shape->
Rotate( { 0, 0 }, footprint->GetOrientation() );
473 shape->
Move( footprint->GetPosition() );
475 footprint->Add( shape );
480 if( parameters[0].CmpNoCase( wxT(
"ElementArc" ) ) == 0 )
484 msg.Printf( wxT(
"ElementArc token contains %d parameters." ), paramCnt );
492 footprint->Add( shape );
495 int radius = (
parseInt( parameters[4], conv_unit ) +
496 parseInt( parameters[5], conv_unit ) ) / 2;
499 parseInt( parameters[3], conv_unit ) );
517 shape->
SetStart( arcStart + centre );
523 PLOT_DASH_TYPE::SOLID ) );
525 shape->
Rotate( { 0, 0 }, footprint->GetOrientation() );
526 shape->
Move( footprint->GetPosition() );
535 if( parameters[0].CmpNoCase( wxT(
"Pad" ) ) == 0 )
537 if( paramCnt < 10 || paramCnt > 13 )
539 msg.Printf( wxT(
"Pad token contains %d parameters." ), paramCnt );
544 std::unique_ptr<PAD>
pad = std::make_unique<PAD>( footprint.get() );
549 pad->SetShape( PAD_SHAPE::RECT );
550 pad->SetAttribute( PAD_ATTRIB::SMD );
551 pad->SetLayerSet( pad_front );
553 if(
testFlags( parameters[paramCnt-2], 0x0080, wxT(
"onsolder" ) ) )
554 pad->SetLayerSet( pad_back );
564 pad->SetNumber( parameters[paramCnt-3] );
566 int x1 =
parseInt( parameters[2], conv_unit );
567 int x2 =
parseInt( parameters[4], conv_unit );
568 int y1 =
parseInt( parameters[3], conv_unit );
569 int y2 =
parseInt( parameters[5], conv_unit );
570 int width =
parseInt( parameters[6], conv_unit );
572 double angle = atan2( (
double)
delta.y, (
double)
delta.x );
577 int clearance =
parseInt( parameters[7], conv_unit );
581 pad->SetLocalClearance( clearance / 2 );
586 int maskMargin =
parseInt( parameters[8], conv_unit );
587 maskMargin = ( maskMargin - width ) / 2;
588 pad->SetLocalSolderMaskMargin( maskMargin );
593 pad->SetOrientation( orient );
595 VECTOR2I padPos( ( x1 + x2 ) / 2, ( y1 + y2 ) / 2 );
599 padPos += footprint->GetPosition();
600 pad->SetPosition( padPos );
602 if( !
testFlags( parameters[paramCnt-2], 0x0100, wxT(
"square" ) ) )
604 if(
pad->GetSize().x ==
pad->GetSize().y )
605 pad->SetShape( PAD_SHAPE::CIRCLE );
607 pad->SetShape( PAD_SHAPE::OVAL );
610 if(
pad->GetSizeX() > 0 &&
pad->GetSizeY() > 0 )
612 footprint->Add(
pad.release() );
616 wxLogError(
_(
"Invalid zero-sized pad ignored in\nfile: %s" ),
629 if( parameters[0].CmpNoCase( wxT(
"Pin" ) ) == 0 )
631 if( paramCnt < 8 || paramCnt > 12 )
633 msg.Printf( wxT(
"Pin token contains %d parameters." ), paramCnt );
640 pad->SetShape( PAD_SHAPE::CIRCLE );
644 pad->SetLayerSet( pad_set );
646 if(
testFlags( parameters[paramCnt-2], 0x0100, wxT(
"square" ) ) )
647 pad->SetShape( PAD_SHAPE::RECT );
653 pad->SetNumber( parameters[paramCnt-3] );
656 parseInt( parameters[3], conv_unit ) );
658 int padSize =
parseInt( parameters[4], conv_unit );
667 int clearance =
parseInt( parameters[5], conv_unit );
671 pad->SetLocalClearance( clearance / 2 );
676 int maskMargin =
parseInt( parameters[6], conv_unit );
677 maskMargin = ( maskMargin - padSize ) / 2;
678 pad->SetLocalSolderMaskMargin( maskMargin );
680 drillSize =
parseInt( parameters[7], conv_unit );
684 drillSize =
parseInt( parameters[5], conv_unit );
687 pad->SetDrillSize(
VECTOR2I( drillSize, drillSize ) );
689 padPos += footprint->GetPosition();
690 pad->SetPosition( padPos );
692 if(
pad->GetShape() == PAD_SHAPE::CIRCLE &&
pad->GetSize().x !=
pad->GetSize().y )
693 pad->SetShape( PAD_SHAPE::OVAL );
695 footprint->Add(
pad );
700 return footprint.release();
708 char* line = aLineReader->
Line();
722 aParameterList.Add( tmp );
727 aParameterList.Add( tmp );
732 if( aParameterList.GetCount() == 1 )
744 aParameterList.Add( tmp );
749 aParameterList.Add( tmp );
763 aParameterList.Add( tmp );
775 aParameterList.Add( wxEmptyString );
786 aParameterList.Add( tmp );
814 if( aFlag.StartsWith( wxT(
"0x" ), &number ) || aFlag.StartsWith( wxT(
"0X" ), &number ) )
818 if( number.ToLong( &lflags, 16 ) && ( lflags & aMask ) )
821 else if( aFlag.Contains( aName ) )
841 m_ctl( aControlFlags )
876 wxDir dir( aLibraryPath );
879 if( !dir.IsOpened() )
885 THROW_IO_ERROR( wxString::Format(
_(
"Footprint library '%s' not found." ),
898 errorMsg = ioe.
What();
905 aFootprintNames.Add(
FROM_UTF8( footprint.first.c_str() ) );
907 if( !errorMsg.IsEmpty() && !aBestEfforts )
913 const wxString& aFootprintName,
925 FOOTPRINT_MAP::const_iterator it = mods.find(
TO_UTF8( aFootprintName ) );
927 if( it == mods.end() )
930 return it->second->GetFootprint();
935 const wxString& aFootprintName,
938 return getFootprint( aLibraryPath, aFootprintName, aProperties,
false );
943 const wxString& aFootprintName,
952 copy->SetParent(
nullptr );
972 aLibraryPath.GetData() ) );
982 fn.SetPath( aLibraryPath );
985 if( !fn.DirExists() )
988 if( !fn.IsDirWritable() )
990 THROW_IO_ERROR( wxString::Format(
_(
"Insufficient permissions to delete folder '%s'." ),
991 aLibraryPath.GetData() ) );
994 wxDir dir( aLibraryPath );
996 if( dir.HasSubDirs() )
998 THROW_IO_ERROR( wxString::Format(
_(
"Library folder '%s' has unexpected sub-folders." ),
999 aLibraryPath.GetData() ) );
1003 if( dir.HasFiles() )
1007 wxArrayString files;
1009 wxDir::GetAllFiles( aLibraryPath, &files );
1011 for( i = 0; i < files.GetCount(); i++ )
1017 THROW_IO_ERROR( wxString::Format(
_(
"Unexpected file '%s' found in library '%s'." ),
1019 aLibraryPath.GetData() ) );
1023 for( i = 0; i < files.GetCount(); i++ )
1025 wxRemoveFile( files[i] );
1030 aLibraryPath.GetData() );
1034 if( !wxRmdir( aLibraryPath ) )
1036 THROW_IO_ERROR( wxString::Format(
_(
"Footprint library '%s' cannot be deleted." ),
1037 aLibraryPath.GetData() ) );
1044 wxMilliSleep( 250L );
constexpr EDA_IU_SCALE pcbIUScale
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
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.
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.
virtual void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate this object.
virtual 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.
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_HORIZONTAL
static constexpr EDA_ANGLE & ANGLE_360
static constexpr EDA_ANGLE & ANGLE_VERTICAL
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
#define TEXT_DEFAULT_SIZE
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 ...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString 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)
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.