1 /**
2  * @file    removeRenderInformation.cpp
3  * @brief   removes render information from the given SBML file
4  * @author  Frank Bergmann
5  *
6  * This file is part of libSBML.  Please visit http://sbml.org for more
7  * information about SBML, and the latest version of libSBML.
8  */
9 
10 #include <stdlib.h>
11 #include <sbml/SBMLTypes.h>
12 #include <sbml/extension/SBMLDocumentPlugin.h>
13 
14 #include <sbml/packages/layout/common/LayoutExtensionTypes.h>
15 #include <sbml/packages/render/common/RenderExtensionTypes.h>
16 
17 #include <iostream>
18 
19 using namespace std;
20 LIBSBML_CPP_NAMESPACE_USE
21 
22 
deleteRenderInformationFromLayout(LayoutModelPlugin * lPlugin)23 void deleteRenderInformationFromLayout(LayoutModelPlugin *lPlugin)
24 {
25   if (lPlugin == NULL) return;
26 
27   RenderListOfLayoutsPlugin *lolPlugin = (RenderListOfLayoutsPlugin*)lPlugin->getListOfLayouts()->getPlugin("render");
28   if (lolPlugin != NULL)
29   {
30     lolPlugin->getListOfGlobalRenderInformation()->clear();
31   }
32 
33   for (int i = 0; i < lPlugin->getNumLayouts(); i++)
34   {
35     Layout* current = lPlugin->getLayout(i);
36     if (current == NULL) continue;
37 
38     RenderLayoutPlugin *rPlugins = (RenderLayoutPlugin*) current->getPlugin("render");
39     if (rPlugins == NULL) continue;
40 
41     rPlugins->getListOfLocalRenderInformation()->clear();
42   }
43 }
44 
main(int argc,char ** argv)45 int main(int argc,char** argv){
46 
47   if (argc != 3)
48   {
49     cerr << "usage: removeRenderInformation <input file> <output file>" << endl;
50     cerr << "       removes the render information object from the input file." << endl;
51     return 1;
52   }
53 
54    string inputFile = argv[1];
55   string outputFile = argv[2];
56 
57   SBMLDocument* doc = readSBMLFromFile(inputFile.c_str());
58   unsigned int numErrors = doc->getErrorLog()->getNumFailsWithSeverity(LIBSBML_SEV_ERROR);
59 
60   if (numErrors > 0)
61   {
62     cerr << "Encountered errors while reading the file. " << endl;
63     cerr << "Please correct the following errors and try again." << endl;
64 	  doc->printErrors();
65     return 2;
66   }
67 
68   SBMLDocumentPlugin* plugin = (SBMLDocumentPlugin*) doc->getPlugin("render");
69   if (plugin == NULL)
70   {
71     // if this is a level 2 model, it could be that simply a render annotation is in place
72     if (doc->getLevel() < 3)
73     {
74       LayoutModelPlugin* lPlugin = (LayoutModelPlugin*)doc->getModel()->getPlugin("layout");
75       deleteRenderInformationFromLayout(lPlugin);
76     }
77     else
78     {
79       cout << "Warning: the document did not use the render information in the first place. " << endl;
80     }
81   }
82   else
83   {
84     // simply disable the package, this will cause it to no longer being written out
85     doc->disablePackage(plugin->getURI(), plugin->getPrefix());
86   }
87 
88   string sbml = writeSBMLToString(doc);
89 
90   writeSBMLToFile(doc, outputFile.c_str());
91 
92   return 0;
93 }
94 
95