47#include <wx/filename.h>
48#include <wx/wfstream.h>
49#include <boost/ptr_container/ptr_map.hpp>
53static inline long parseInt(
const wxString& aValue,
double aScalar )
55 double value = std::numeric_limits<double>::max();
78 if( aValue.EndsWith( wxT(
"mm" ) ) )
80 aScalar *= 100000.0 / 25.4;
82 else if( aValue.EndsWith( wxT(
"mil" ) ) )
89 aValue.ToCDouble(&value);
91 if( value == std::numeric_limits<double>::max() )
93 THROW_IO_ERROR( wxString::Format(
_(
"Cannot convert '%s' to an integer." ),
97 return KiROUND( value * aScalar );
143 void Remove(
const wxString& aFootprintName );
151 static long long GetTimestamp(
const wxString& aLibPath );
171 bool testFlags(
const wxString& aFlag,
long aMask,
const wxChar* aName );
222 if( !dir.IsOpened() )
224 THROW_IO_ERROR( wxString::Format(
_(
"Footprint library '%s' not found." ),
235 if( !dir.GetFirst( &fullName, fileSpec ) )
238 wxString cacheErrorMsg;
258 if( !cacheErrorMsg.IsEmpty() )
259 cacheErrorMsg += wxT(
"\n\n" );
261 cacheErrorMsg += ioe.
What();
263 }
while( dir.GetNext( &fullName ) );
265 if( !cacheErrorMsg.IsEmpty() )
272 std::string footprintName =
TO_UTF8( aFootprintName );
278 THROW_IO_ERROR( wxString::Format(
_(
"Library '%s' has no footprint '%s'." ),
280 aFootprintName.GetData() ) );
284 wxString fullPath = it->second->GetFileName().GetFullPath();
286 wxRemoveFile( fullPath );
308 #define TEXT_DEFAULT_SIZE ( 40*pcbIUScale.IU_PER_MILS )
309 #define OLD_GPCB_UNIT_CONV pcbIUScale.IU_PER_MILS
312 #define NEW_GPCB_UNIT_CONV ( 0.01*pcbIUScale.IU_PER_MILS )
319 wxArrayString parameters;
320 std::unique_ptr<FOOTPRINT> footprint = std::make_unique<FOOTPRINT>(
nullptr );
322 if( aLineReader->
ReadLine() ==
nullptr )
324 msg = aLineReader->
GetSource() + wxT(
": empty file" );
330 paramCnt = parameters.GetCount();
340 if( parameters[0].CmpNoCase( wxT(
"Element" ) ) != 0 )
342 msg.Printf(
_(
"Unknown token '%s'" ), parameters[0] );
347 if( paramCnt < 10 || paramCnt > 14 )
349 msg.Printf(
_(
"Element token contains %d parameters." ), paramCnt );
355 if( parameters[1] == wxT(
"(" ) )
360 footprint->SetLibDescription( parameters[3] );
361 footprint->SetReference( parameters[4] );
365 footprint->SetLibDescription( parameters[2] );
366 footprint->SetReference( parameters[3] );
371 footprint->SetValue( parameters[5] );
375 if( footprint->Value().GetText().IsEmpty() )
376 footprint->Value().SetText( wxT(
"VAL**" ) );
378 if( footprint->Reference().GetText().IsEmpty() )
379 footprint->Reference().SetText( wxT(
"REF**" ) );
386 if( parameters.IsEmpty() || parameters[0] == wxT(
"(" ) )
389 if( parameters[0] == wxT(
")" ) )
392 paramCnt = parameters.GetCount();
397 if( parameters[1] == wxT(
"(" ) )
404 parameters[0], paramCnt );
407 if( parameters[0].CmpNoCase( wxT(
"ElementLine" ) ) == 0 )
411 msg.Printf( wxT(
"ElementLine token contains %d parameters." ), paramCnt );
419 parseInt( parameters[3], conv_unit ) ) );
421 parseInt( parameters[5], conv_unit ) ) );
423 LINE_STYLE::SOLID ) );
425 shape->
Rotate( { 0, 0 }, footprint->GetOrientation() );
426 shape->
Move( footprint->GetPosition() );
428 footprint->Add( shape );
433 if( parameters[0].CmpNoCase( wxT(
"ElementArc" ) ) == 0 )
437 msg.Printf( wxT(
"ElementArc token contains %d parameters." ), paramCnt );
445 footprint->Add( shape );
449 parseInt( parameters[5], conv_unit ) ) / 2;
452 parseInt( parameters[3], conv_unit ) );
474 shape->
SetStart( arcStart + centre );
481 LINE_STYLE::SOLID ) );
483 shape->
Rotate( { 0, 0 }, footprint->GetOrientation() );
484 shape->
Move( footprint->GetPosition() );
493 if( parameters[0].CmpNoCase( wxT(
"Pad" ) ) == 0 )
495 if( paramCnt < 10 || paramCnt > 13 )
497 msg.Printf( wxT(
"Pad token contains %d parameters." ), paramCnt );
502 std::unique_ptr<PAD>
pad = std::make_unique<PAD>( footprint.get() );
508 pad->SetAttribute( PAD_ATTRIB::SMD );
509 pad->SetLayerSet( pad_front );
511 if(
testFlags( parameters[paramCnt-2], 0x0080, wxT(
"onsolder" ) ) )
512 pad->SetLayerSet( pad_back );
522 pad->SetNumber( parameters[paramCnt-3] );
524 int x1 =
parseInt( parameters[2], conv_unit );
525 int x2 =
parseInt( parameters[4], conv_unit );
526 int y1 =
parseInt( parameters[3], conv_unit );
527 int y2 =
parseInt( parameters[5], conv_unit );
528 int width =
parseInt( parameters[6], conv_unit );
530 double angle = atan2( (
double)
delta.y, (
double)
delta.x );
544 int maskMargin =
parseInt( parameters[8], conv_unit );
545 maskMargin = ( maskMargin - width ) / 2;
546 pad->SetLocalSolderMaskMargin( maskMargin );
551 pad->SetOrientation( orient );
553 VECTOR2I padPos( ( x1 + x2 ) / 2, ( y1 + y2 ) / 2 );
557 padPos += footprint->GetPosition();
558 pad->SetPosition( padPos );
560 if( !
testFlags( parameters[paramCnt-2], 0x0100, wxT(
"square" ) ) )
568 if(
pad->GetSizeX() > 0 &&
pad->GetSizeY() > 0 )
570 footprint->Add(
pad.release() );
574 wxLogError(
_(
"Invalid zero-sized pad ignored in\nfile: %s" ),
587 if( parameters[0].CmpNoCase( wxT(
"Pin" ) ) == 0 )
589 if( paramCnt < 8 || paramCnt > 12 )
591 msg.Printf( wxT(
"Pin token contains %d parameters." ), paramCnt );
602 pad->SetLayerSet( pad_set );
604 if(
testFlags( parameters[paramCnt-2], 0x0100, wxT(
"square" ) ) )
611 pad->SetNumber( parameters[paramCnt-3] );
614 parseInt( parameters[3], conv_unit ) );
616 int padSize =
parseInt( parameters[4], conv_unit );
634 int maskMargin =
parseInt( parameters[6], conv_unit );
635 maskMargin = ( maskMargin - padSize ) / 2;
636 pad->SetLocalSolderMaskMargin( maskMargin );
638 drillSize =
parseInt( parameters[7], conv_unit );
642 drillSize =
parseInt( parameters[5], conv_unit );
645 pad->SetDrillSize(
VECTOR2I( drillSize, drillSize ) );
647 padPos += footprint->GetPosition();
648 pad->SetPosition( padPos );
656 footprint->Add(
pad );
661 footprint->AutoPositionFields();
663 return footprint.release();
671 char* line = aLineReader->
Line();
685 aParameterList.Add( tmp );
690 aParameterList.Add( tmp );
695 if( aParameterList.GetCount() == 1 )
707 aParameterList.Add( tmp );
712 aParameterList.Add( tmp );
726 aParameterList.Add( tmp );
738 aParameterList.Add( wxEmptyString );
749 aParameterList.Add( tmp );
777 if( aFlag.StartsWith( wxT(
"0x" ), &number ) || aFlag.StartsWith( wxT(
"0X" ), &number ) )
781 if( number.ToLong( &lflags, 16 ) && ( lflags & aMask ) )
784 else if( aFlag.Contains( aName ) )
804 m_ctl( aControlFlags )
836 wxString& aFootprintNameOut,
837 const std::map<std::string, UTF8>* aProperties )
839 wxFileName fn( aFootprintPath );
845 char* line = reader.
Line();
850 if( strncasecmp( line,
"Element", strlen(
"Element" ) ) != 0 )
853 aFootprintNameOut = fn.GetName();
860 bool aBestEfforts,
const std::map<std::string, UTF8>* aProperties )
862 wxDir dir( aLibraryPath );
865 if( !dir.IsOpened() )
870 THROW_IO_ERROR( wxString::Format(
_(
"Footprint library '%s' not found." ), aLibraryPath ) );
881 errorMsg = ioe.
What();
888 aFootprintNames.Add(
From_UTF8( footprint.first.c_str() ) );
890 if( !errorMsg.IsEmpty() && !aBestEfforts )
896 const wxString& aFootprintName,
897 const std::map<std::string, UTF8>* aProperties,
909 return it->second->GetFootprint().get();
914 const wxString& aFootprintName,
916 const std::map<std::string, UTF8>* aProperties )
925 copy->SetParent(
nullptr );
934 const std::map<std::string, UTF8>* aProperties )
943 aLibraryPath.GetData() ) );
953 fn.SetPath( aLibraryPath );
956 if( !fn.DirExists() )
959 if( !fn.IsDirWritable() )
961 THROW_IO_ERROR( wxString::Format(
_(
"Insufficient permissions to delete folder '%s'." ),
962 aLibraryPath.GetData() ) );
965 wxDir dir( aLibraryPath );
967 if( dir.HasSubDirs() )
969 THROW_IO_ERROR( wxString::Format(
_(
"Library folder '%s' has unexpected sub-folders." ),
970 aLibraryPath.GetData() ) );
980 wxDir::GetAllFiles( aLibraryPath, &files );
982 for( i = 0; i < files.GetCount(); i++ )
988 THROW_IO_ERROR( wxString::Format(
_(
"Unexpected file '%s' found in library '%s'." ),
990 aLibraryPath.GetData() ) );
994 for( i = 0; i < files.GetCount(); i++ )
996 wxRemoveFile( files[i] );
1001 aLibraryPath.GetData() );
1005 if( !wxRmdir( aLibraryPath ) )
1007 THROW_IO_ERROR( wxString::Format(
_(
"Footprint library '%s' cannot be deleted." ),
1008 aLibraryPath.GetData() ) );
1015 wxMilliSleep( 250L );
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
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.
std::unique_ptr< FOOTPRINT > m_footprint
WX_FILENAME GetFileName() const
std::unique_ptr< FOOTPRINT > & GetFootprint()
WX_FILENAME m_filename
The full file name and path of the footprint to cache.
GPCB_FPL_CACHE_ENTRY(FOOTPRINT *aFootprint, const WX_FILENAME &aFileName)
FOOTPRINT * parseFOOTPRINT(LINE_READER *aLineReader)
void Remove(const wxString &aFootprintName)
boost::ptr_map< std::string, GPCB_FPL_CACHE_ENTRY > & GetFootprints()
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.
boost::ptr_map< std::string, GPCB_FPL_CACHE_ENTRY > m_footprints
Map of footprint filename to cache entries.
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.
LSET is a set of PCB_LAYER_IDs.
static LSET AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
A #PLUGIN derivation for saving and loading Geda PCB files.
void FootprintDelete(const wxString &aLibraryPath, const wxString &aFootprintName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Delete aFootprintName from the library at aLibraryPath.
bool IsLibraryWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
bool DeleteLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Delete an existing library and returns true, or if library does not exist returns false,...
FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
void init(const std::map< std::string, UTF8 > *aProperties)
GPCB_FPL_CACHE * m_cache
Footprint library cache.
void validateCache(const wxString &aLibraryPath, bool checkModified=true)
LINE_READER * m_reader
no ownership here.
friend class GPCB_FPL_CACHE
FOOTPRINT * ImportFootprint(const wxString &aFootprintPath, wxString &aFootprintNameOut, const std::map< std::string, UTF8 > *aProperties) override
Load a single footprint from aFootprintPath and put its name in aFootprintNameOut.
const FOOTPRINT * getFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const std::map< std::string, UTF8 > *aProperties, bool checkModified)
A base class that BOARD loading and saving plugins should derive from.
const std::map< std::string, UTF8 > * 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
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
#define IGNORE_PARENT_GROUP
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 ...
static long parseInt(const wxString &aValue, double aScalar)
#define NEW_GPCB_UNIT_CONV
#define OLD_GPCB_UNIT_CONV
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.
VECTOR2< int32_t > VECTOR2I
Definition of file extensions used in Kicad.