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.

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