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.

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