You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

581 lines
24 KiB

  1. #ifndef _SCH_IO_MGR_H_
  2. #define _SCH_IO_MGR_H_
  3. /*
  4. * This program source code file is part of KiCad, a free EDA CAD application.
  5. *
  6. * Copyright (C) 2016 CERN
  7. * Copyright (C) 2016-2021 KiCad Developers, see AUTHORS.txt for contributors.
  8. *
  9. * @author Wayne Stambaugh <stambaughw@gmail.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 3
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include <richio.h>
  25. #include <import_export.h>
  26. #include <map>
  27. #include <enum_vector.h>
  28. #include <reporter.h>
  29. class SCH_SHEET;
  30. class SCH_SCREEN;
  31. class SCH_PLUGIN;
  32. class SCHEMATIC;
  33. class SYMBOL_LIB_TABLE;
  34. class KIWAY;
  35. class LIB_SYMBOL;
  36. class SYMBOL_LIB;
  37. class STRING_UTF8_MAP;
  38. class PROGRESS_REPORTER;
  39. /**
  40. * A factory which returns an instance of a #SCH_PLUGIN.
  41. */
  42. class SCH_IO_MGR
  43. {
  44. public:
  45. /**
  46. * A set of file types that the #SCH_IO_MGR knows about, and for which there
  47. * has been a plugin written.
  48. */
  49. DEFINE_ENUM_VECTOR( SCH_FILE_T,
  50. {
  51. SCH_LEGACY, ///< Legacy Eeschema file formats prior to s-expression.
  52. SCH_KICAD, ///< The s-expression version of the schematic file formats.
  53. SCH_ALTIUM, ///< Altium file format
  54. SCH_CADSTAR_ARCHIVE, ///< CADSTAR Schematic Archive
  55. SCH_EAGLE, ///< Autodesk Eagle file format
  56. SCH_DATABASE, ///< KiCad database library
  57. // Add your schematic type here.
  58. SCH_FILE_UNKNOWN
  59. } )
  60. /**
  61. * Return a #SCH_PLUGIN which the caller can use to import, export, save, or load
  62. * design documents.
  63. *
  64. * The returned #SCH_PLUGIN, may be reference counted, so please call PluginRelease()
  65. * when you are done using the returned #SCH_PLUGIN. It may or may not be code running
  66. * from a DLL/DSO.
  67. *
  68. * @param aFileType is from #SCH_FILE_T and tells which plugin to find.
  69. *
  70. * @return the plugin corresponding to aFileType or NULL if not found.
  71. * Caller owns the returned object, and must call PluginRelease when done using it.
  72. */
  73. APIEXPORT
  74. static SCH_PLUGIN* FindPlugin( SCH_FILE_T aFileType );
  75. /**
  76. * Release a #SCH_PLUGIN back to the system, and may cause it to be unloaded from memory.
  77. *
  78. * @param aPlugin is the one to be released, and which is no longer usable
  79. * after calling this.
  80. */
  81. static void ReleasePlugin( SCH_PLUGIN* aPlugin );
  82. /**
  83. * Return a brief name for a plugin, given aFileType enum.
  84. */
  85. static const wxString ShowType( SCH_FILE_T aFileType );
  86. /**
  87. * Return the #SCH_FILE_T from the corresponding plugin type name: "kicad", "legacy", etc.
  88. */
  89. static SCH_FILE_T EnumFromStr( const wxString& aFileType );
  90. /**
  91. * Return the schematic file extension for \a aFileType.
  92. *
  93. * @param aFileType is the #SCH_FILE_T type.
  94. *
  95. * @return the file extension for \a aFileType or an empty string if \a aFileType is invalid.
  96. */
  97. static const wxString GetFileExtension( SCH_FILE_T aFileType );
  98. /**
  99. * Return the symbol library file extension (if any) for \a aFileType.
  100. *
  101. * @param aFileType is the #SCH_FILE_T type.
  102. *
  103. * @return the file extension for \a aFileType or an empty string if \a aFileType is invalid.
  104. */
  105. static const wxString GetLibraryFileExtension( SCH_FILE_T aFileType );
  106. /**
  107. * Return a plugin type given a symbol library using the file extension of \a aLibPath.
  108. */
  109. static SCH_FILE_T GuessPluginTypeFromLibPath( const wxString& aLibPath );
  110. /**
  111. * Return a plugin type given a schematic using the file extension of \a aSchematicPath.
  112. */
  113. static SCH_FILE_T GuessPluginTypeFromSchPath( const wxString& aSchematicPath );
  114. };
  115. /**
  116. * Base class that schematic file and library loading and saving plugins should derive from.
  117. * Implementations can provide either Load() or Save() functions, or both.
  118. * SCH_PLUGINs throw exceptions, so it is best that you wrap your calls to these
  119. * functions in a try catch block. Plugins throw exceptions because it is illegal
  120. * for them to have any user interface calls in them whatsoever, i.e. no windowing
  121. * or screen printing at all.
  122. *
  123. * <pre>
  124. * try
  125. * {
  126. * SCH_IO_MGR::Load(...);
  127. * or
  128. * SCH_IO_MGR::Save(...);
  129. * }
  130. * catch( const IO_ERROR& ioe )
  131. * {
  132. * // grab text from ioe, show in error window.
  133. * }
  134. * </pre>
  135. */
  136. class SCH_PLUGIN
  137. {
  138. public:
  139. //-----<PUBLIC SCH_PLUGIN API>-------------------------------------------------
  140. /**
  141. * Return a brief hard coded name for this SCH_PLUGIN.
  142. */
  143. virtual const wxString GetName() const = 0;
  144. /**
  145. * Set an optional reporter for warnings/errors.
  146. */
  147. virtual void SetReporter( REPORTER* aReporter ) {}
  148. /**
  149. * Set an optional progress reporter.
  150. */
  151. virtual void SetProgressReporter( PROGRESS_REPORTER* aReporter ) {}
  152. /**
  153. * Return the file extension for the #SCH_PLUGIN.
  154. */
  155. virtual const wxString GetFileExtension() const = 0;
  156. /**
  157. * Return the library file extension for the #SCH_PLUGIN object.
  158. */
  159. virtual const wxString GetLibraryFileExtension() const = 0;
  160. /**
  161. * Return the modification hash from the library cache.
  162. *
  163. * @note This is temporary until the new s-expr file format is implement. The new file
  164. * format will embed symbols instead of referencing them from the library. This
  165. * function can be removed when the new file format is implemented.
  166. *
  167. * @return the modification hash of the library cache.
  168. */
  169. virtual int GetModifyHash() const = 0;
  170. virtual void SaveLibrary( const wxString& aFileName, const STRING_UTF8_MAP* aProperties = nullptr );
  171. /**
  172. * Load information from some input file format that this #SCH_PLUGIN implementation
  173. * knows about, into either a new #SCH_SHEET or an existing one. This may be used to load an
  174. * entire new #SCH_SHEET, or to augment an existing one if \a aAppendToMe is not NULL.
  175. *
  176. * @param aFileName is the name of the file to use as input and may be foreign in
  177. * nature or native in nature.
  178. *
  179. * @param aKiway is the #KIWAY object used to access the symbol libraries loaded
  180. * by the project.
  181. *
  182. * @param aAppendToMe is an existing #SCH_SHEET to append to, but if NULL then this means
  183. * "do not append, rather load anew".
  184. *
  185. * @param aProperties is an associative array that can be used to tell the loader how to
  186. * load the file, because it can take any number of additional named
  187. * arguments that the plugin is known to support. These are tuning
  188. * parameters for the import or load. The caller continues to own
  189. * this object (plugin may not delete it), and plugins should expect
  190. * it to be optionally NULL.
  191. *
  192. * @return the successfully loaded schematic, or the same one as \a aAppendToMe
  193. * if \a aAppendToMe was not NULL, and the caller owns it.
  194. *
  195. * @throw IO_ERROR if there is a problem loading, and its contents should say what went
  196. * wrong, using line number and character offsets of the input file if
  197. * possible.
  198. */
  199. virtual SCH_SHEET* Load( const wxString& aFileName, SCHEMATIC* aSchematic,
  200. SCH_SHEET* aAppendToMe = nullptr,
  201. const STRING_UTF8_MAP* aProperties = nullptr );
  202. /**
  203. * Write \a aSchematic to a storage file in a format that this #SCH_PLUGIN implementation
  204. * knows about, or it can be used to write a portion of \a aSchematic to a special kind
  205. * of export file.
  206. *
  207. * @param aFileName is the name of a file to save to on disk.
  208. *
  209. * @param aSheet is the class #SCH_SHEET in memory document tree from which to extract
  210. * information when writing to \a aFileName. The caller continues to
  211. * own the SCHEMATIC, and the plugin should refrain from modifying the
  212. * SCHEMATIC if possible.
  213. *
  214. * @param aSchematic is the #SCHEMATIC object used to access any schematic-wide or project
  215. * information needed to save the document.
  216. *
  217. * @param aProperties is an associative array that can be used to tell the saver how to
  218. * save the file, because it can take any number of additional named
  219. * tuning arguments that the plugin is known to support. The caller
  220. * continues to own this object (plugin may not delete it), and plugins
  221. * should expect it to be optionally NULL. Set the
  222. * #PropSaveCurrentSheetOnly property to only save the current sheet.
  223. * Otherwise, all hierarchical sheets are saved.
  224. *
  225. * @throw IO_ERROR if there is a problem saving or exporting.
  226. */
  227. virtual void Save( const wxString& aFileName, SCH_SHEET* aSheet, SCHEMATIC* aSchematic,
  228. const STRING_UTF8_MAP* aProperties = nullptr );
  229. /**
  230. * Populate a list of #LIB_SYMBOL alias names contained within the library \a aLibraryPath.
  231. *
  232. * @param aSymbolNameList is an array to populate with the #LIB_SYMBOL names associated with
  233. * the library.
  234. *
  235. * @param aLibraryPath is a locator for the "library", usually a directory, file,
  236. * or URL containing one or more #LIB_SYMBOL objects.
  237. *
  238. * @param aProperties is an associative array that can be used to tell the plugin anything
  239. * needed about how to perform with respect to \a aLibraryPath. The
  240. * caller continues to own this object (plugin may not delete it), and
  241. * plugins should expect it to be optionally NULL.
  242. *
  243. * @throw IO_ERROR if the library cannot be found, the part library cannot be loaded.
  244. */
  245. virtual void EnumerateSymbolLib( wxArrayString& aSymbolNameList, const wxString& aLibraryPath,
  246. const STRING_UTF8_MAP* aProperties = nullptr );
  247. /**
  248. * Populate a list of #LIB_SYMBOL aliases contained within the library \a aLibraryPath.
  249. *
  250. * @note It is the responsibility of the caller to delete the returned object from the heap.
  251. * Failure to do this will result in memory leaks.
  252. *
  253. * @param aSymbolList is an array to populate with the #LIB_SYMBOL pointers associated with
  254. * the library.
  255. *
  256. * @param aLibraryPath is a locator for the "library", usually a directory, file,
  257. * or URL containing one or more #LIB_SYMBOL objects.
  258. *
  259. * @param aProperties is an associative array that can be used to tell the plugin anything
  260. * needed about how to perform with respect to \a aLibraryPath. The
  261. * caller continues to own this object (plugin may not delete it), and
  262. * plugins should expect it to be optionally NULL.
  263. *
  264. * @throw IO_ERROR if the library cannot be found, the part library cannot be loaded.
  265. */
  266. virtual void EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
  267. const wxString& aLibraryPath,
  268. const STRING_UTF8_MAP* aProperties = nullptr );
  269. /**
  270. * Load a #LIB_SYMBOL object having \a aPartName from the \a aLibraryPath containing
  271. * a library format that this #SCH_PLUGIN knows about.
  272. *
  273. * @param aLibraryPath is a locator for the "library", usually a directory, file,
  274. * or URL containing several symbols.
  275. *
  276. * @param aPartName is the name of the #LIB_SYMBOL to load.
  277. *
  278. * @param aProperties is an associative array that can be used to tell the loader
  279. * implementation to do something special, because it can take
  280. * any number of additional named tuning arguments that the plugin
  281. * is known to support. The caller continues to own this object
  282. * (plugin may not delete it), and plugins should expect it to be
  283. * optionally NULL.
  284. *
  285. * @return the part created on the heap if found caller shares it or NULL if not found.
  286. *
  287. * @throw IO_ERROR if the library cannot be found or read. No exception
  288. * is thrown in the case where aAliasName cannot be found.
  289. */
  290. virtual LIB_SYMBOL* LoadSymbol( const wxString& aLibraryPath, const wxString& aPartName,
  291. const STRING_UTF8_MAP* aProperties = nullptr );
  292. /**
  293. * Write \a aSymbol to an existing library located at \a aLibraryPath. If a #LIB_SYMBOL
  294. * by the same name already exists or there are any conflicting alias names, the new
  295. * #LIB_SYMBOL will silently overwrite any existing aliases and/or part because libraries
  296. * cannot have duplicate alias names. It is the responsibility of the caller to check
  297. * the library for conflicts before saving.
  298. *
  299. * @param aLibraryPath is a locator for the "library", usually a directory, file,
  300. * or URL containing several symbols.
  301. *
  302. * @param aSymbol is what to store in the library. The library is refreshed and the
  303. * caller must update any #LIB_SYMBOL pointers that may have changed.
  304. *
  305. * @param aProperties is an associative array that can be used to tell the
  306. * saver how to save the symbol, because it can take any number of
  307. * additional named tuning arguments that the plugin is known to support.
  308. * The caller continues to own this object (plugin may not delete it), and
  309. * plugins should expect it to be optionally NULL.
  310. *
  311. * @throw IO_ERROR if there is a problem saving.
  312. */
  313. virtual void SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
  314. const STRING_UTF8_MAP* aProperties = nullptr );
  315. /**
  316. * Delete the entire #LIB_SYMBOL associated with \a aAliasName from the library
  317. * \a aLibraryPath.
  318. *
  319. * @param aLibraryPath is a locator for the "library", usually a directory, file,
  320. * or URL containing several symbols.
  321. *
  322. * @param aSymbolName is the name of a #LIB_SYMBOL associated with it's root #LIB_SYMBOL
  323. * object to delete from the specified library.
  324. *
  325. * @param aProperties is an associative array that can be used to tell the library
  326. * delete function anything special, because it can take any number
  327. * of additional named tuning arguments that the plugin is known to
  328. * support. The caller continues to own this object (plugin may not
  329. * delete it), and plugins should expect it to be optionally NULL.
  330. *
  331. * @throw IO_ERROR if there is a problem finding the alias or the library or deleting it.
  332. */
  333. virtual void DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
  334. const STRING_UTF8_MAP* aProperties = nullptr );
  335. /**
  336. * Create a new empty symbol library at \a aLibraryPath. It is an error to attempt
  337. * to create an existing library or to attempt to create on a "read only" location.
  338. *
  339. * @param aLibraryPath is a locator for the "library", usually a directory, file,
  340. * or URL containing several footprints.
  341. *
  342. * @param aProperties is an associative array that can be used to tell the library
  343. * create function anything special, because it can take any number
  344. * of additional named tuning arguments that the plugin is known to
  345. * support. The caller continues to own this object (plugin may not
  346. * delete it), and plugins should expect it to be optionally NULL.
  347. *
  348. * @throw IO_ERROR if there is a problem finding the library, or creating it.
  349. */
  350. virtual void CreateSymbolLib( const wxString& aLibraryPath,
  351. const STRING_UTF8_MAP* aProperties = nullptr );
  352. /**
  353. * Delete an existing symbol library and returns true if successful, or if library
  354. * does not exist returns false, or throws an exception if library exists but is read
  355. * only or cannot be deleted for some other reason.
  356. *
  357. * @param aLibraryPath is a locator for the "library", usually a directory or file
  358. * which will contain symbols.
  359. *
  360. * @param aProperties is an associative array that can be used to tell the library
  361. * delete implementation function anything special, because it can
  362. * take any number of additional named tuning arguments that the
  363. * plugin is known to support. The caller continues to own this
  364. * object (plugin may not delete it), and plugins should expect
  365. * it to be optionally NULL.
  366. *
  367. * @return true if library deleted or false if library did not exist.
  368. *
  369. * @throw IO_ERROR if there is a problem deleting an existing library.
  370. */
  371. virtual bool DeleteSymbolLib( const wxString& aLibraryPath,
  372. const STRING_UTF8_MAP* aProperties = nullptr );
  373. /**
  374. * Return true if the library at \a aLibraryPath is writable. (Often
  375. * system libraries are read only because of where they are installed.)
  376. *
  377. * @param aLibraryPath is a locator for the "library", usually a directory, file,
  378. * or URL containing several symbols.
  379. *
  380. * @throw IO_ERROR if no library at aLibraryPath exists.
  381. */
  382. virtual bool IsSymbolLibWritable( const wxString& aLibraryPath );
  383. /**
  384. * Append supported #SCH_PLUGIN options to \a aListToAppenTo along with internationalized
  385. * descriptions. Options are typically appended so that a derived SCH_PLUGIN can call
  386. * its base class function by the same name first, thus inheriting options declared there.
  387. * (Some base class options could pertain to all Symbol*() functions in all derived
  388. * SCH_PLUGINs.) Note that since aListToAppendTo is a PROPERTIES object, all options
  389. * will be unique and last guy wins.
  390. *
  391. * @param aListToAppendTo holds a tuple of
  392. * <dl>
  393. * <dt>option</dt>
  394. * <dd>This eventually is what shows up into the fp-lib-table "options"
  395. * field, possibly combined with others.</dd>
  396. * <dt>internationalized description</dt>
  397. * <dd>The internationalized description is displayed in DIALOG_FP_SCH_PLUGIN_OPTIONS.
  398. * It may be multi-line and be quite explanatory of the option.</dd>
  399. * </dl>
  400. * <br>
  401. * In the future perhaps \a aListToAppendTo evolves to something capable of also
  402. * holding a wxValidator for the cells in said dialog:
  403. * http://forums.wxwidgets.org/viewtopic.php?t=23277&p=104180.
  404. * This would require a 3 column list, and introducing wx GUI knowledge to
  405. * #SCH_PLUGIN, which has been avoided to date.
  406. */
  407. virtual void SymbolLibOptions( STRING_UTF8_MAP* aListToAppendTo ) const;
  408. /**
  409. * @return true if this plugin supports libraries that contain sub-libraries.
  410. */
  411. virtual bool SupportsSubLibraries() const { return false; }
  412. /**
  413. * Retrieves a list of sub-libraries in this library.
  414. *
  415. * Some types of symbol library support sub-libraries, which are a single-level organizational
  416. * hierarchy that is implementation-defined per plugin. Most of KiCad ignores sub-libraries and
  417. * treats the hierarchy between library and symbol as flat, but the sub-libraries are used for
  418. * sorting and grouping symbols in the symbol chooser.
  419. *
  420. * Has no effect if SupportsSubLibraries() returns false.
  421. *
  422. * @param aNames will be filled with a list of sub-libraries within this symbol library
  423. */
  424. virtual void GetSubLibraryNames( std::vector<wxString>& aNames ) {}
  425. /**
  426. * Retrieves a list of (custom) field names that are present on symbols in this library.
  427. * The plugin is responsible for guaranteeing that this list contains the set of unique
  428. * custom field names present on any symbols contained in the library.
  429. *
  430. * The required KiCad fields are not included in this list.
  431. *
  432. * @param aNames will be filled with any custom fields present in this library.
  433. */
  434. virtual void GetAvailableSymbolFields( std::vector<wxString>& aNames ) {}
  435. /**
  436. * Retrieves a list of (custom) field names that should be shown by default for this library
  437. * in the symbol chooser. This list should be a subset of the result returned by
  438. * GetAvailableSymbolFields().
  439. *
  440. * The preference for which fields to hide and show for a given library is stored on a
  441. * per-library basis in a user's preferences (or in the project local settings for a project-
  442. * local library). The set of fields returned by GetDefaultSymbolFields() will be used if this
  443. * preference is missing.
  444. *
  445. * @param aNames will be filled with the custom field names that should be shown by default
  446. */
  447. virtual void GetDefaultSymbolFields( std::vector<wxString>& aNames )
  448. {
  449. return GetAvailableSymbolFields( aNames );
  450. }
  451. /**
  452. * Return true if the first line in @a aFileName begins with the expected header.
  453. *
  454. * @param aFileName is the name of the file to use as input
  455. *
  456. */
  457. virtual bool CheckHeader( const wxString& aFileName );
  458. /**
  459. * Return an error string to the caller.
  460. *
  461. * This is useful for schematic loaders that can load partial schematics where throwing
  462. * an exception would be problematic such as the KiCad legacy plugin.
  463. *
  464. * @return an unformatted string containing errors if any.
  465. */
  466. virtual const wxString& GetError() const;
  467. /**
  468. * Some library plugins need to have access to their parent library table.
  469. * @param aTable is the table this plugin is registered within.
  470. */
  471. virtual void SetLibTable( SYMBOL_LIB_TABLE* aTable ) {}
  472. //-----</PUBLIC SCH_PLUGIN API>------------------------------------------------
  473. /* The compiler writes the "zero argument" constructor for a SCH_PLUGIN
  474. automatically if you do not provide one. If you decide you need to
  475. provide a zero argument constructor of your own design, that is allowed.
  476. It must be public, and it is what the SCH_IO_MGR uses. Parameters may be
  477. passed into a SCH_PLUGIN via the PROPERTIES variable for any of the public
  478. API functions which take one.
  479. */
  480. virtual ~SCH_PLUGIN() { }
  481. /**
  482. * Helper object to release a #SCH_PLUGIN in the context of a potential thrown exception
  483. * through its destructor.
  484. */
  485. class SCH_PLUGIN_RELEASER
  486. {
  487. SCH_PLUGIN* plugin;
  488. // private assignment operator so it's illegal
  489. SCH_PLUGIN_RELEASER& operator=( SCH_PLUGIN_RELEASER& aOther ) { return *this; }
  490. // private copy constructor so it's illegal
  491. SCH_PLUGIN_RELEASER( const SCH_PLUGIN_RELEASER& aOther ) {}
  492. public:
  493. SCH_PLUGIN_RELEASER( SCH_PLUGIN* aPlugin = nullptr ) :
  494. plugin( aPlugin )
  495. {
  496. }
  497. ~SCH_PLUGIN_RELEASER()
  498. {
  499. if( plugin )
  500. release();
  501. }
  502. void release()
  503. {
  504. SCH_IO_MGR::ReleasePlugin( plugin );
  505. plugin = nullptr;
  506. }
  507. void set( SCH_PLUGIN* aPlugin )
  508. {
  509. if( plugin )
  510. release();
  511. plugin = aPlugin;
  512. }
  513. operator SCH_PLUGIN* () const
  514. {
  515. return plugin;
  516. }
  517. SCH_PLUGIN* operator -> () const
  518. {
  519. return plugin;
  520. }
  521. };
  522. };
  523. #endif // _SCH_IO_MGR_H_