1 #ifndef LEGACY_PLUGIN_H_
2 #define LEGACY_PLUGIN_H_
3 
4 /*
5  * This program source code file is part of KiCad, a free EDA CAD application.
6  *
7  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
8  * Copyright (C) 2016-2021 KiCad Developers, see AUTHORS.txt for contributors.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23  * or you may search the http://www.gnu.org website for the version 2 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
26  */
27 
28 #include <io_mgr.h>
29 #include <string>
30 #include <layer_ids.h>
31 #include <memory>
32 
33 
34 // FOOTPRINT_LIBRARY_HEADER_CNT gives the number of characters to compare to detect
35 // a footprint library. A few variants may have been used, and so we can only be
36 // sure that the header contains "PCBNEW-LibModule-V", not "PCBNEW-LibModule-V1".
37 
38 #define FOOTPRINT_LIBRARY_HEADER       "PCBNEW-LibModule-V1"
39 #define FOOTPRINT_LIBRARY_HEADER_CNT    18
40 
41 class PCB_TARGET;
42 class FOOTPRINT;
43 class NETCLASS;
44 class NETCLASSES;
45 class ZONE;
46 class PCB_DIMENSION_BASE;
47 class NETINFO_ITEM;
48 class FP_TEXT;
49 class PAD;
50 struct LP_CACHE;
51 
52 
53 /**
54  * A #PLUGIN derivation which could possibly be put into a DLL/DSO.
55  *
56  * As with any PLUGIN, there is no UI, i.e. windowing calls allowed.
57  */
58 class LEGACY_PLUGIN : public PLUGIN
59 {
60     friend struct LP_CACHE;
61 
62 public:
63     LEGACY_PLUGIN();
64     ~LEGACY_PLUGIN();
65 
PluginName()66     const wxString PluginName() const override
67     {
68         return wxT( "KiCad-Legacy" );
69     }
70 
GetFileExtension()71     const wxString GetFileExtension() const override
72     {
73         return wxT( "brd" );
74     }
75 
76     BOARD* Load( const wxString& aFileName, BOARD* aAppendToMe,
77                  const PROPERTIES* aProperties = nullptr, PROJECT* aProject = nullptr,
78                  PROGRESS_REPORTER* aProgressReporter = nullptr ) override;
79 
80     void FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibraryPath,
81                              bool aBestEfforts,
82                              const PROPERTIES* aProperties = nullptr ) override;
83 
84     FOOTPRINT* FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
85                               bool  aKeepUUID = false,
86                               const PROPERTIES* aProperties = nullptr ) override;
87 
88     bool FootprintLibDelete( const wxString& aLibraryPath,
89                              const PROPERTIES* aProperties = nullptr ) override;
90 
91     long long GetLibraryTimestamp( const wxString& aLibraryPath ) const override;
92 
93     bool IsFootprintLibWritable( const wxString& aLibraryPath ) override;
94 
95     typedef int BIU;
96 
SetReader(LINE_READER * aReader)97     void SetReader( LINE_READER* aReader )      { m_reader = aReader; }
98 
99     // return the new .kicad_pcb layer id from the old (legacy) layer id
100     static PCB_LAYER_ID leg_layer2new( int cu_count, LAYER_NUM aLayerNum );
101 
102     static LSET leg_mask2new( int cu_count, unsigned aMask );
103 
104 protected:
105     /// initialize PLUGIN like a constructor would, and futz with fresh BOARD if needed.
106     void init( const PROPERTIES* aProperties );
107 
108     void checkpoint();
109 
110     ///< Converts net code using the mapping table if available,
111     ///< otherwise returns unchanged net code
getNetCode(int aNetCode)112     inline int getNetCode( int aNetCode )
113     {
114         if( (unsigned int) aNetCode < m_netCodes.size() )
115             return m_netCodes[aNetCode];
116 
117         return aNetCode;
118     }
119 
120     /**
121      * Parse an ASCII decimal floating point value and scales it into a BIU according to the
122      * current value of diskToBui.
123      *
124      * This function is the complement of #fmtBIU().  One has to know what the other is doing.
125      *
126      * @param aValue is the ASCII value in C locale form with possible leading whitespace
127      * @param nptrptr may be NULL, but if not, then it tells where to put a pointer to the
128      *                next unconsumed input text. See "man strtod" for more information.
129      * @return the converted Board Internal Unit.
130      */
131     BIU biuParse( const char* aValue, const char** nptrptr = nullptr );
132 
133     /**
134      * Parse an ASCII decimal floating point value which is certainly an angle.
135      *
136      * This is a dedicated function for encapsulating support for the migration from
137      * tenths of degrees to degrees in floating point.  This function is the complement of
138      * fmtDEG().  One has to know what the other is doing.
139      *
140      * @param aValue is the ASCII value in C locale form with possible leading whitespace.
141      * @param nptrptr may be NULL, but if not, then it tells where to put a pointer to the
142      *                next unconsumed input text. See "man strtod" for more information.
143      * @return the string converted to a primitive double type
144      */
145     double degParse( const char* aValue, const char** nptrptr = nullptr );
146 
147     void checkVersion();
148 
149     void loadAllSections( bool doAppend );
150 
151 
152     void loadGENERAL();
153     void loadSETUP();
154     void loadSHEET();
155 
156     void load3D( FOOTPRINT* aFootprint );
157     void loadPAD( FOOTPRINT* aFootprint );
158     void loadMODULE_TEXT( FP_TEXT* aText );
159     void loadFP_SHAPE( FOOTPRINT* aFootprint );
160 
161     void loadPCB_LINE();
162     void loadNETINFO_ITEM();
163     void loadPCB_TEXT();
164     void loadNETCLASS();
165     void loadFOOTPRINT( FOOTPRINT* aFootprint );
166 
167     /**
168      * Read a list of segments (Tracks and Vias, or Segzones)
169      *
170      * @param aStructType is either PCB_TRACE_T to indicate tracks and vias, or NOT_USED
171      *                    to indicate oldschool zone segments (which are discarded).
172      */
173     void loadTrackList( int aStructType );
174 
175     void loadZONE_CONTAINER();      // "$CZONE_OUTLINE"
176     void loadDIMENSION();           // "$COTATION"
177     void loadPCB_TARGET();          // "$PCB_TARGET"
178 
179     /// we only cache one footprint library for now, this determines which one.
180     void cacheLib( const wxString& aLibraryPath );
181 
182 protected:
183     int                m_cu_count;
184 
185     wxString           m_error;                  ///< for throwing exceptions
186     BOARD*             m_board;                  ///< which BOARD, no ownership here
187     const PROPERTIES*  m_props;                  ///< passed via Save() or Load(), no ownership,
188                                                  ///<  may be NULL.
189     PROGRESS_REPORTER* m_progressReporter;       ///< may be NULL, no ownership
190     unsigned           m_lastProgressLine;
191     unsigned           m_lineCount;              ///< for progress reporting
192 
193     LINE_READER*       m_reader;                 ///< no ownership here.
194     FILE*              m_fp;                     ///< no ownership here.
195 
196     wxString           m_field;                  ///< reused to stuff FOOTPRINT fields.
197     int                m_loading_format_version; ///< which BOARD_FORMAT_VERSION am I Load()ing?
198     LP_CACHE*          m_cache;
199     bool               m_showLegacyZoneWarning;
200 
201     std::vector<int>   m_netCodes;               ///< net codes mapping for boards being loaded
202 
203     double  biuToDisk;              ///< convert from BIUs to disk engineering units
204                                     ///< with this scale factor
205 
206     double  diskToBiu;              ///< convert from disk engineering units to BIUs
207                                     ///< with this scale factor
208 };
209 
210 #endif  // LEGACY_PLUGIN_H_
211