48#include <wx/filename.h>
49#include <wx/wfstream.h>
50#include <boost/ptr_container/ptr_map.hpp>
54static inline long parseInt(
const wxString& aValue,
double aScalar )
56 double value = std::numeric_limits<double>::max();
79 if( aValue.EndsWith( wxT(
"mm" ) ) )
81 aScalar *= 100000.0 / 25.4;
83 else if( aValue.EndsWith( wxT(
"mil" ) ) )
90 aValue.ToCDouble(&value);
92 if( value == std::numeric_limits<double>::max() )
94 THROW_IO_ERROR( wxString::Format(
_(
"Cannot convert '%s' to an integer." ),
98 return KiROUND( value * aScalar );
125 m_filename( aFileName ),
126 m_footprint( aFootprint )
151 void Remove(
const wxString& aFootprintName );
159 static long long GetTimestamp(
const wxString& aLibPath );
179 bool testFlags(
const wxString& aFlag,
long aMask,
const wxChar* aName );
228 if( !dir.IsOpened() )
230 THROW_IO_ERROR( wxString::Format(
_(
"Footprint library '%s' not found." ),
241 if( !dir.GetFirst( &fullName, fileSpec ) )
244 wxString cacheErrorMsg;
264 if( !cacheErrorMsg.IsEmpty() )
265 cacheErrorMsg += wxT(
"\n\n" );
267 cacheErrorMsg += ioe.
What();
269 }
while( dir.GetNext( &fullName ) );
271 if( !cacheErrorMsg.IsEmpty() )
278 std::string footprintName =
TO_UTF8( aFootprintName );
280 FOOTPRINT_MAP::const_iterator it =
m_footprints.find( footprintName );
284 THROW_IO_ERROR( wxString::Format(
_(
"Library '%s' has no footprint '%s'." ),
286 aFootprintName.GetData() ) );
290 wxString fullPath = it->second->GetFileName().GetFullPath();
292 wxRemoveFile( fullPath );
314 #define TEXT_DEFAULT_SIZE ( 40*pcbIUScale.IU_PER_MILS )
315 #define OLD_GPCB_UNIT_CONV pcbIUScale.IU_PER_MILS
318 #define NEW_GPCB_UNIT_CONV ( 0.01*pcbIUScale.IU_PER_MILS )
326 wxArrayString parameters;
327 std::unique_ptr<FOOTPRINT> footprint = std::make_unique<FOOTPRINT>(
nullptr );
329 if( aLineReader->
ReadLine() ==
nullptr )
331 msg = aLineReader->
GetSource() + wxT(
": empty file" );
337 paramCnt = parameters.GetCount();
347 if( parameters[0].CmpNoCase( wxT(
"Element" ) ) != 0 )
349 msg.Printf(
_(
"Unknown token '%s'" ), parameters[0] );
354 if( paramCnt < 10 || paramCnt > 14 )
356 msg.Printf(
_(
"Element token contains %d parameters." ), paramCnt );
362 if( parameters[1] == wxT(
"(" ) )
367 footprint->SetLibDescription( parameters[3] );
368 footprint->SetReference( parameters[4] );
372 footprint->SetLibDescription( parameters[2] );
373 footprint->SetReference( parameters[3] );
378 footprint->SetValue( parameters[5] );
382 if( footprint->Value().GetText().IsEmpty() )
383 footprint->Value().SetText( wxT(
"VAL**" ) );
385 if( footprint->Reference().GetText().IsEmpty() )
386 footprint->Reference().SetText( wxT(
"REF**" ) );
393 if( parameters.IsEmpty() || parameters[0] == wxT(
"(" ) )
396 if( parameters[0] == wxT(
")" ) )
399 paramCnt = parameters.GetCount();
404 if( parameters[1] == wxT(
"(" ) )
411 parameters[0], paramCnt );
414 if( parameters[0].CmpNoCase( wxT(
"ElementLine" ) ) == 0 )
418 msg.Printf( wxT(
"ElementLine token contains %d parameters." ), paramCnt );
426 parseInt( parameters[3], conv_unit ) ) );
428 parseInt( parameters[5], conv_unit ) ) );
430 LINE_STYLE::SOLID ) );
432 shape->
Rotate( { 0, 0 }, footprint->GetOrientation() );
433 shape->
Move( footprint->GetPosition() );
435 footprint->Add( shape );
440 if( parameters[0].CmpNoCase( wxT(
"ElementArc" ) ) == 0 )
444 msg.Printf( wxT(
"ElementArc token contains %d parameters." ), paramCnt );
452 footprint->Add( shape );
455 int radius = (
parseInt( parameters[4], conv_unit ) +
456 parseInt( parameters[5], conv_unit ) ) / 2;
459 parseInt( parameters[3], conv_unit ) );
481 shape->
SetStart( arcStart + centre );
488 LINE_STYLE::SOLID ) );
490 shape->
Rotate( { 0, 0 }, footprint->GetOrientation() );
491 shape->
Move( footprint->GetPosition() );
500 if( parameters[0].CmpNoCase( wxT(
"Pad" ) ) == 0 )
502 if( paramCnt < 10 || paramCnt > 13 )
504 msg.Printf( wxT(
"Pad token contains %d parameters." ), paramCnt );
509 std::unique_ptr<PAD>
pad = std::make_unique<PAD>( footprint.get() );
514 pad->SetShape( PAD_SHAPE::RECTANGLE );
515 pad->SetAttribute( PAD_ATTRIB::SMD );
516 pad->SetLayerSet( pad_front );
518 if(
testFlags( parameters[paramCnt-2], 0x0080, wxT(
"onsolder" ) ) )
519 pad->SetLayerSet( pad_back );
529 pad->SetNumber( parameters[paramCnt-3] );
531 int x1 =
parseInt( parameters[2], conv_unit );
532 int x2 =
parseInt( parameters[4], conv_unit );
533 int y1 =
parseInt( parameters[3], conv_unit );
534 int y2 =
parseInt( parameters[5], conv_unit );
535 int width =
parseInt( parameters[6], conv_unit );
537 double angle = atan2( (
double)
delta.y, (
double)
delta.x );
542 int clearance =
parseInt( parameters[7], conv_unit );
546 pad->SetLocalClearance( clearance / 2 );
551 int maskMargin =
parseInt( parameters[8], conv_unit );
552 maskMargin = ( maskMargin - width ) / 2;
553 pad->SetLocalSolderMaskMargin( maskMargin );
558 pad->SetOrientation( orient );
560 VECTOR2I padPos( ( x1 + x2 ) / 2, ( y1 + y2 ) / 2 );
564 padPos += footprint->GetPosition();
565 pad->SetPosition( padPos );
567 if( !
testFlags( parameters[paramCnt-2], 0x0100, wxT(
"square" ) ) )
569 if(
pad->GetSize().x ==
pad->GetSize().y )
570 pad->SetShape( PAD_SHAPE::CIRCLE );
572 pad->SetShape( PAD_SHAPE::OVAL );
575 if(
pad->GetSizeX() > 0 &&
pad->GetSizeY() > 0 )
577 footprint->Add(
pad.release() );
581 wxLogError(
_(
"Invalid zero-sized pad ignored in\nfile: %s" ),
594 if( parameters[0].CmpNoCase( wxT(
"Pin" ) ) == 0 )
596 if( paramCnt < 8 || paramCnt > 12 )
598 msg.Printf( wxT(
"Pin token contains %d parameters." ), paramCnt );
605 pad->SetShape( PAD_SHAPE::CIRCLE );
609 pad->SetLayerSet( pad_set );
611 if(
testFlags( parameters[paramCnt-2], 0x0100, wxT(
"square" ) ) )
612 pad->SetShape( PAD_SHAPE::RECTANGLE );
618 pad->SetNumber( parameters[paramCnt-3] );
621 parseInt( parameters[3], conv_unit ) );
623 int padSize =
parseInt( parameters[4], conv_unit );
632 int clearance =
parseInt( parameters[5], conv_unit );
636 pad->SetLocalClearance( clearance / 2 );
641 int maskMargin =
parseInt( parameters[6], conv_unit );
642 maskMargin = ( maskMargin - padSize ) / 2;
643 pad->SetLocalSolderMaskMargin( maskMargin );
645 drillSize =
parseInt( parameters[7], conv_unit );
649 drillSize =
parseInt( parameters[5], conv_unit );
652 pad->SetDrillSize(
VECTOR2I( drillSize, drillSize ) );
654 padPos += footprint->GetPosition();
655 pad->SetPosition( padPos );
657 if(
pad->GetShape() == PAD_SHAPE::CIRCLE &&
pad->GetSize().x !=
pad->GetSize().y )
658 pad->SetShape( PAD_SHAPE::OVAL );
660 footprint->Add(
pad );
665 footprint->AutoPositionFields();
667 return footprint.release();
675 char* line = aLineReader->
Line();
689 aParameterList.Add( tmp );
694 aParameterList.Add( tmp );
699 if( aParameterList.GetCount() == 1 )
711 aParameterList.Add( tmp );
716 aParameterList.Add( tmp );
730 aParameterList.Add( tmp );
742 aParameterList.Add( wxEmptyString );
753 aParameterList.Add( tmp );
781 if( aFlag.StartsWith( wxT(
"0x" ), &number ) || aFlag.StartsWith( wxT(
"0X" ), &number ) )
785 if( number.ToLong( &lflags, 16 ) && ( lflags & aMask ) )
788 else if( aFlag.Contains( aName ) )
808 m_ctl( aControlFlags )
840 wxString& aFootprintNameOut,
843 wxFileName fn( aFootprintPath );
849 char* line = reader.
Line();
854 if( strncasecmp( line,
"Element", strlen(
"Element" ) ) != 0 )
857 aFootprintNameOut = fn.GetName();
867 wxDir dir( aLibraryPath );
870 if( !dir.IsOpened() )
876 THROW_IO_ERROR( wxString::Format(
_(
"Footprint library '%s' not found." ),
889 errorMsg = ioe.
What();
896 aFootprintNames.Add(
From_UTF8( footprint.first.c_str() ) );
898 if( !errorMsg.IsEmpty() && !aBestEfforts )
904 const wxString& aFootprintName,
916 FOOTPRINT_MAP::const_iterator it = mods.find(
TO_UTF8( aFootprintName ) );
918 if( it == mods.end() )
921 return it->second->GetFootprint();
926 const wxString& aFootprintName,
929 return getFootprint( aLibraryPath, aFootprintName, aProperties,
false );
934 const wxString& aFootprintName,
945 copy->SetParent(
nullptr );
965 aLibraryPath.GetData() ) );
975 fn.SetPath( aLibraryPath );
978 if( !fn.DirExists() )
981 if( !fn.IsDirWritable() )
983 THROW_IO_ERROR( wxString::Format(
_(
"Insufficient permissions to delete folder '%s'." ),
984 aLibraryPath.GetData() ) );
987 wxDir dir( aLibraryPath );
989 if( dir.HasSubDirs() )
991 THROW_IO_ERROR( wxString::Format(
_(
"Library folder '%s' has unexpected sub-folders." ),
992 aLibraryPath.GetData() ) );
1000 wxArrayString files;
1002 wxDir::GetAllFiles( aLibraryPath, &files );
1004 for( i = 0; i < files.GetCount(); i++ )
1010 THROW_IO_ERROR( wxString::Format(
_(
"Unexpected file '%s' found in library '%s'." ),
1012 aLibraryPath.GetData() ) );
1016 for( i = 0; i < files.GetCount(); i++ )
1018 wxRemoveFile( files[i] );
1023 aLibraryPath.GetData() );
1027 if( !wxRmdir( aLibraryPath ) )
1029 THROW_IO_ERROR( wxString::Format(
_(
"Footprint library '%s' cannot be deleted." ),
1030 aLibraryPath.GetData() ) );
1037 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*.
GPCB_FPL_CACHE(PCB_IO_GEDA *aOwner, const wxString &aLibraryPath)
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).
PCB_IO_GEDA * m_owner
Plugin object that owns the cache.
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.
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.
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.
A #PLUGIN derivation for saving and loading Geda PCB files.
void init(const STRING_UTF8_MAP *aProperties)
void FootprintDelete(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr) override
Delete aFootprintName from the library at aLibraryPath.
const FOOTPRINT * getFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties, bool checkModified)
bool IsLibraryWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
GPCB_FPL_CACHE * m_cache
Footprint library cache.
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.
bool DeleteLibrary(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
Delete an existing library and returns true, or if library does not exist returns false,...
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.
void validateCache(const wxString &aLibraryPath, bool checkModified=true)
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.
LINE_READER * m_reader
no ownership here.
friend class GPCB_FPL_CACHE
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 PC...
A base class that BOARD loading and saving plugins should derive from.
const STRING_UTF8_MAP * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
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
static void SetReporter(REPORTER *aReporter)
Set the reporter to use for reporting font substitution warnings.
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/...
static constexpr EDA_ANGLE ANGLE_360
static constexpr EDA_ANGLE ANGLE_180
static const std::string GedaPcbFootprintLibFileExtension
static const std::string KiCadFootprintFileExtension
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 ...
std::map< wxString, FOOTPRINT * > FOOTPRINT_MAP
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
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.
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
VECTOR2< int32_t > VECTOR2I
Definition of file extensions used in Kicad.