G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsFileData.h
Go to the documentation of this file.
1
14#pragma once
15
16#include <iostream>
17#include <string>
18#include <list>
19
20#include <gsIO/gsXml.h>
21#include <gsIO/gsFileManager.h>
22
23namespace gismo
24{
25
32template<class T>
34{
35public:
36 typedef internal::gsXmlTree FileData;
37 typedef internal::gsXmlNode gsXmlNode;
38 typedef internal::gsXmlAttribute gsXmlAttribute;
39 typedef std::string String;
40
41 typedef gsVector3d<T> Point_3;
42
43public:
44
45 gsFileData();
46
53 explicit gsFileData(String const & fn, bool recursive=false);
54
63 bool read(String const & fn, bool recursive=false) ;
64
66
68 void clear();
69
71 int numData() const { return data->numNodes();}
72
74 void save(String const & fname = "dump", bool compress = false) const;
75
77 void writeIges(String const & fname);
78
80 void saveCompressed(String const & fname = "dump") const;
81
83 void dump(String const & fname = "dump") const;
84
85 void addComment(String const & message);
86
87 // Returns the path of the last file where data was read from
88 //
89 // If the corresponding file did not exist, the return value is an
90 // empty string.
91 String lastPath() const { return m_lastPath; }
92
96 void setFloatPrecision(const unsigned k) { data->setFloatPrecision(k); }
97
101 unsigned getFloatPrecision() const { return data->getFloatPrecision(); }
102
103private:
105 FileData * data;
106
107 // Used to hold parsed data of native gismo XML files
108 std::list<std::vector<char> > m_buffer;
109
110 // Holds the last path that was used in an I/O operation
111 mutable String m_lastPath;
112
113protected:
114
115/*
116 * File readers
117 */
118
120 bool readXmlFile( String const & fn, bool recursive=false);
121
123 bool readXmlGzFile( String const & fn, bool recursive=false);
124
126 bool readGismoXmlStream(std::istream & is, bool recursive=false);
127
129 bool readAxelFile(String const & fn);
130 bool readAxelSurface( gsXmlNode * node );
131 bool readAxelCurve ( gsXmlNode * node );
132 bool readAxelMesh ( gsXmlNode * node );
133
135 bool readGeompFile( String const & fn );
136
138 bool readGoToolsFile(String const & fn);
139
141 bool readOffFile(String const & fn);
142
144 bool readStlFile(String const & fn);
145
147 bool readObjFile(String const & fn);
148
150 bool readBrepFile(String const & fn);
151
153 bool readIgesFile(String const & fn);
154
156 bool readX3dFile(String const & fn);
157
159 bool read3dmFile(String const & fn);
160
162 bool readParasolidFile(String const & fn);
163
164 bool readCsvFile(String const & fn);
165
166 // Show the line number where something went wrong
167 void ioError(int lineNumber,const String& str);
168
169public:
170
171 // Generic functions to fetch Gismo object
172 // template<class Object>
173 // inline Object * get( gsXmlNode * node)
174 // {
175 // return internal::gsXml<Object>::get(node);// Using gsXmlUtils
176 // }
177
179 template<class Object>
180 inline memory::unique_ptr<Object> getId( const int & id) const
181 {
182 return memory::make_unique( internal::gsXml<Object>::getId( getXmlRoot(), id ) );
183 }
184
186 template<class Object>
187 inline void getId( const int & id, Object& result) const
188 {
189 memory::unique_ptr<Object> obj = getId<Object>(id);
190 result = give(*obj);
191 }
192
194 template<class Object>
195 inline memory::unique_ptr<Object> getLabel(const std::string & name) const
196 {
197 return memory::make_unique( internal::gsXml<Object>::getLabel( getXmlRoot(), name ) );
198 }
199
201 template<class Object>
202 inline void getLabel(const std::string & name, Object& result) const
203 {
204 memory::unique_ptr<Object> obj = getLabel<Object>(name);
205 result = give(*obj);
206 }
207
209 template<class Object>
210 inline String tag() const
211 { return internal::gsXml<Object>::tag(); }
212
214 template<class Object>
215 inline String type() const
216 { return internal::gsXml<Object>::type(); }
217
219 template<class Object>
220 inline bool has() const
221 {
222 return getFirstNode( internal::gsXml<Object>::tag(),
223 internal::gsXml<Object>::type() ) != 0 ;
224 }
225
227 inline bool hasId(int id) const {
228 gsXmlNode* root = getXmlRoot();
229 // const gsXmlAttribute * id_at;
230 gsXmlNode* nd = internal::searchId(id, root, NULL, false);
231 return (bool)nd;
232 }
233
235 inline bool hasLabel(std::string label) const {
236 gsXmlNode* root = getXmlRoot();
237 // const gsXmlAttribute * id_at;
238 gsXmlNode* nd = internal::searchLabel(label, root, NULL, false);
239 return (bool)nd;
240 }
241
243 inline bool hasTag(std::string tag) const
244 {
245 return getAnyFirstNode(tag.c_str());
246 }
247
250 template<class Object>
251 inline bool hasAny() const
252 {
253 return getAnyFirstNode( internal::gsXml<Object>::tag(),
254 internal::gsXml<Object>::type() ) != 0 ;
255 }
256
258 template<class Object>
259 inline int count() const
260 {
261 int i(0);
262 for (gsXmlNode * child = getFirstNode( internal::gsXml<Object>::tag(),
263 internal::gsXml<Object>::type() ) ;
264 child; child = getNextSibling(child, internal::gsXml<Object>::tag(),
265 internal::gsXml<Object>::type() ))
266 ++i;
267 return i;
268 }
269
270
272 template<class Object>
273 void operator<<(const Object & obj)
274 {
275 this->add<Object>(obj);
276 }
277
279 template<class Object>
280 void add (const Object & obj, int id = -1)
281 {
282 gsXmlNode* node =
283 internal::gsXml<Object>::put(obj, *data);
284 if ( ! node )
285 {
286 gsInfo<<"gsFileData: Trouble inserting "<<internal::gsXml<Object>::tag()
287 <<" to the XML tree. is \"put\" implemented ??\n";
288 }
289 else
290 {
291 data->appendToRoot(node,id);
292 }
293 }
294
296 template<class Object>
297 void addWithLabel (const Object & obj, std::string label)
298 {
299 gsXmlNode* node =
300 internal::gsXml<Object>::put(obj, *data);
301 if ( ! node )
302 {
303 gsInfo<<"gsFileData: Trouble inserting "<<internal::gsXml<Object>::tag()
304 <<" to the XML tree. is \"put\" implemented ??\n";
305 }
306 else
307 {
308 data->appendToRoot(node,-1,label);
309 }
310 }
311
313 void addString (const std::string & s)
314 {
315 gsXmlNode* node = internal::makeNode("string",s,*data);
316 data->appendToRoot(node);
317 }
318
320 void addString (const std::string & s, const std::string & label)
321 {
322 gsXmlNode* node = internal::makeNode("string",s,*data);
323 node->append_attribute(internal::makeAttribute("label", label, *data));
324 data->appendToRoot(node);
325 }
326
332 void addInclude( const std::string & filename, const real_t & time=-1.,
333 const index_t & id=-1, const std::string & label="");
334
335protected:
336 void getInclude(gsFileData & res, index_t id, real_t time, std::string label);
337
338public:
343 {
344 return getInclude(res, id, -1., "");
345 }
346
350 void getIncludeByTime(gsFileData & res, real_t time)
351 {
352 return getInclude(res, -1,time, "");
353 }
354
358 void getIncludeByLabel(gsFileData & res, std::string label)
359 {
360 return getInclude(res, -1,-1.,label);
361 }
362
363 std::string getString () const
364 {
365
366 gsXmlNode * node = getFirstNode("string");
367 //node = getNextSibling(node, "string");
368 std::string res( node->value() );
369 return res;
370 }
371
372 std::string getString(index_t id) const
373 {
374 //GISMO_ASSERT(id < 0, "Id " << id << " should be >= 0!");
375
376 gsXmlNode * root = getXmlRoot();
377 gsXmlNode * nd = internal::searchId(id, root, "string");
378 if (nd)
379 {
380 std::string res(nd->value());
381 return res;
382 }
383 GISMO_ERROR("String with id " << id << " does not exist!");
384 }
385
386 std::string getStringByLabel (const std::string & label) const
387 {
388 //GISMO_ASSERT(id < 0, "Id " << id << " should be >= 0!");
389
390 gsXmlNode * root = getXmlRoot();
391 gsXmlNode * nd = internal::searchNode( root, "label", label, "string");
392 if (nd)
393 {
394 std::string res(nd->value());
395 return res;
396 }
397 GISMO_ERROR("String with label " << label << " does not exist!");
398 }
399
401 size_t bufferSize() const { return m_buffer.front().size(); };
402
404 std::ostream &print(std::ostream &os) const;
405
406/*
413 template<class Object>
414 bool operator>>(Object * obj)
415 {
416 gsWarn<< "getting "<< typeid(Object).name() <<"\n";
417 gsXmlNode* node = getFirstNode(internal::gsXml<Object>::tag(),
418 internal::gsXml<Object>::type() );
419 if ( !node )
420 {
421 gsWarn<<"gsFileData: false!\n";
422 return false;
423 }
424 else
425 {
426 obj = internal::gsXml<Object>::get(node);
427 this->deleteXmlSubtree( node );
428 return true;
429 }
430 }
431*/
432
445 template<class Object>
446 inline memory::unique_ptr<Object> getFirst() const
447 {
448 gsXmlNode* node = getFirstNode(internal::gsXml<Object>::tag(),
449 internal::gsXml<Object>::type() );
450 if ( !node )
451 {
452 gsWarn<<"gsFileData: getFirst: Didn't find any "<<
453 internal::gsXml<Object>::type()<<" "<<
454 internal::gsXml<Object>::tag() <<". Error.\n";
455 return memory::unique_ptr<Object>();
456 }
457 return memory::make_unique( internal::gsXml<Object>::get(node) );// Using gsXmlUtils
458 }
459
473 template<class Object>
474 bool getFirst(Object & result) const
475 {
476 gsXmlNode* node = getFirstNode(internal::gsXml<Object>::tag(),
477 internal::gsXml<Object>::type() );
478 if ( !node )
479 {
480 gsWarn<<"gsFileData: getFirst: Didn't find any "<<
481 internal::gsXml<Object>::type()<<" "<<
482 internal::gsXml<Object>::tag() <<". Error.\n";
483 return false;
484 }
485 internal::gsXml<Object>::get_into(node, result);// Using gsXmlUtils
486 return true;
487 }
488
490 template<class Object>
491 inline std::vector< memory::unique_ptr<Object> > getAll() const
492 {
493 std::vector< memory::unique_ptr<Object> > result;
494
495 for (gsXmlNode * child = getFirstNode( internal::gsXml<Object>::tag(),
496 internal::gsXml<Object>::type() ) ;
497 child; child = getNextSibling(child, internal::gsXml<Object>::tag(),
498 internal::gsXml<Object>::type() ))
499 {
500 result.push_back( memory::make_unique(internal::gsXml<Object>::get(child)) );
501 }
502 return result;
503 }
504
517 template<class Object>
518 inline memory::unique_ptr<Object> getAnyFirst() const
519 {
520 gsXmlNode* node = getAnyFirstNode(internal::gsXml<Object>::tag(),
521 internal::gsXml<Object>::type() );
522 if ( !node )
523 {
524 gsWarn <<"gsFileData: getAnyFirst: Didn't find any "<<
525 internal::gsXml<Object>::type()<<" "<<
526 internal::gsXml<Object>::tag() <<". Error.\n";
527 return memory::unique_ptr<Object>();
528 }
529 return memory::make_unique( internal::gsXml<Object>::get(node) );// Using gsXmlUtils
530 }
531
545 template<class Object>
546 bool getAnyFirst(Object & result) const
547 {
548 gsXmlNode* node = getAnyFirstNode(internal::gsXml<Object>::tag(),
549 internal::gsXml<Object>::type() );
550 if ( !node )
551 {
552 gsWarn <<"gsFileData: getAnyFirst: Didn't find any "<<
553 internal::gsXml<Object>::type()<<" "<<
554 internal::gsXml<Object>::tag() <<". Error.\n";
555 return false;
556 }
557 internal::gsXml<Object>::get_into(node, result);// Using gsXmlUtils
558 return true;
559 }
560
562 String contents () const;
563
565 int numTags () const;
566
567private:
568
569 gsXmlNode * getXmlRoot() const;
570 static void deleteXmlSubtree (gsXmlNode* node);
571
572 // getFirst ? (tag and or type)
573 gsXmlNode * getFirstNode ( const String & name = "",
574 const String & type = "" ) const;
575
576 // getAny
577 gsXmlNode * getAnyFirstNode( const String & name = "",
578 const String & type = "" ) const;
579
580 // getNext
581 static gsXmlNode * getNextSibling( gsXmlNode* const & node,
582 const String & name = "",
583 const String & type = "" );
584
585 // Helpers for X3D files
586 void addX3dShape(gsXmlNode * shape);
587 void addX3dTransform(gsXmlNode * shape);
588
589}; // class gsFileData
590
591// Print out operator
592template<class T>
593std::ostream &operator<<(std::ostream &os, const gsFileData<T> & fd)
594{return fd.print(os); }
595
596#ifdef GISMO_WITH_PYBIND11
597
601 void pybind11_init_gsFileData(pybind11::module &m);
602
603#endif // GISMO_WITH_PYBIND11
604
605} // namespace gismo
606
607#ifndef GISMO_BUILD_LIB
608
609#include GISMO_HPP_HEADER(gsFileData.hpp)
610#endif
This class represents an XML data tree which can be read from or written to a (file) stream.
Definition gsFileData.h:34
void writeIges(String const &fname)
Save multipatch contents to an IGES file.
void addInclude(const std::string &filename, const real_t &time=-1., const index_t &id=-1, const std::string &label="")
Add a reference ( <xmlfile> tag ) to another Gismo .xml file to the xml tree.
Definition gsFileData.hpp:296
memory::unique_ptr< Object > getId(const int &id) const
Searches and fetches the Gismo object with a given id.
Definition gsFileData.h:180
memory::unique_ptr< Object > getAnyFirst() const
Definition gsFileData.h:518
bool readObjFile(String const &fn)
Reads Wavefront OBJ file.
Definition gsFileData.hpp:1410
unsigned getFloatPrecision() const
Definition gsFileData.h:101
bool readIgesFile(String const &fn)
Reads Iges file.
Definition gsFileData.hpp:2149
bool readBrepFile(String const &fn)
Reads OpenCascade brep file.
Definition gsFileData.hpp:1694
String tag() const
Prints the XML tag of a Gismo object.
Definition gsFileData.h:210
void addWithLabel(const Object &obj, std::string label)
Add the object to the Xml tree, same as <<, but also allows to set the XML label attribute.
Definition gsFileData.h:297
void saveCompressed(String const &fname="dump") const
Save file contents to compressed xml file.
Definition gsFileData.hpp:131
void getLabel(const std::string &name, Object &result) const
Searches and fetches the Gismo object with a given label.
Definition gsFileData.h:202
bool readGoToolsFile(String const &fn)
Reads GoTools file.
Definition gsFileData.hpp:511
int count() const
Counts the number of Objects in the filedata.
Definition gsFileData.h:259
void operator<<(const Object &obj)
Inserts an object to the XML tree.
Definition gsFileData.h:273
FileData * data
File data as an xml tree.
Definition gsFileData.h:105
bool readStlFile(String const &fn)
Reads STL mesh file.
Definition gsFileData.hpp:1327
bool readXmlFile(String const &fn, bool recursive=false)
Reads a file with xml extension.
Definition gsFileData.hpp:229
void getId(const int &id, Object &result) const
Searches and fetches the Gismo object with a given id.
Definition gsFileData.h:187
bool hasLabel(std::string label) const
Returns true if an Object with such label exists in the filedata.
Definition gsFileData.h:235
int numTags() const
Counts the number of Objects/tags in the filedata.
Definition gsFileData.hpp:2602
bool read3dmFile(String const &fn)
Reads 3DM file.
Definition gsFileData.hpp:2527
String contents() const
Lists the contents of the filedata.
Definition gsFileData.hpp:2583
size_t bufferSize() const
Returns the size of the data.
Definition gsFileData.h:401
void setFloatPrecision(const unsigned k)
Definition gsFileData.h:96
void getIncludeById(gsFileData &res, index_t id)
Looks for a referenced Gismo .xml file ( <xmlfile> tag ) in the current xml tree, parses it in the gs...
Definition gsFileData.h:342
bool hasTag(std::string tag) const
Returns true if an entry of tag exists in the xml file.
Definition gsFileData.h:243
std::vector< memory::unique_ptr< Object > > getAll() const
Returns a vector with all Objects found in the XML data.
Definition gsFileData.h:491
bool getFirst(Object &result) const
Definition gsFileData.h:474
bool readGeompFile(String const &fn)
Reads GeoPDEs txt file.
Definition gsFileData.hpp:882
void save(String const &fname="dump", bool compress=false) const
Save file contents to an xml file.
Definition gsFileData.hpp:98
void addString(const std::string &s, const std::string &label)
Add a string to the Xml tree.
Definition gsFileData.h:320
void addString(const std::string &s)
Add a string to the Xml tree.
Definition gsFileData.h:313
bool readGismoXmlStream(std::istream &is, bool recursive=false)
Reads Gismo's native XML file.
Definition gsFileData.hpp:252
bool getAnyFirst(Object &result) const
Definition gsFileData.h:546
memory::unique_ptr< Object > getLabel(const std::string &name) const
Searches and fetches a poitner to a Gismo object with a given label.
Definition gsFileData.h:195
bool readX3dFile(String const &fn)
Reads X3D file.
Definition gsFileData.hpp:2460
memory::unique_ptr< Object > getFirst() const
Definition gsFileData.h:446
bool hasId(int id) const
Returns true if an Object exists in the filedata.
Definition gsFileData.h:227
void clear()
Clear all data.
Definition gsFileData.hpp:69
bool readXmlGzFile(String const &fn, bool recursive=false)
Reads a file with xml.gz extension.
Definition gsFileData.hpp:240
bool read(String const &fn, bool recursive=false)
Definition gsFileData.hpp:162
bool readOffFile(String const &fn)
Reads Off mesh file.
Definition gsFileData.hpp:1259
std::ostream & print(std::ostream &os) const
Prints the XML data as a string.
Definition gsFileData.hpp:77
bool readAxelFile(String const &fn)
Reads Axel file.
Definition gsFileData.hpp:349
bool readParasolidFile(String const &fn)
Reads parasolid files.
Definition gsFileData.hpp:2539
String type() const
Prints the XML tag type of a Gismo object.
Definition gsFileData.h:215
void getIncludeByTime(gsFileData &res, real_t time)
Looks for a referenced Gismo .xml file ( <xmlfile> tag ) in the current xml tree, parses it in the gs...
Definition gsFileData.h:350
void dump(String const &fname="dump") const
Dump file contents to an xml file.
Definition gsFileData.hpp:86
bool has() const
Returns true if an Object exists in the filedata.
Definition gsFileData.h:220
void getIncludeByLabel(gsFileData &res, std::string label)
Looks for a referenced Gismo .xml file ( <xmlfile> tag ) in the current xml tree, parses it in the gs...
Definition gsFileData.h:358
bool hasAny() const
Definition gsFileData.h:251
void add(const Object &obj, int id=-1)
Add the object to the Xml tree, same as <<, but also allows to set the XML id and label attributes.
Definition gsFileData.h:280
int numData() const
Reports the number of objects which are held in the file data.
Definition gsFileData.h:71
A fixed-size, statically allocated 3D vector.
Definition gsVector.h:219
#define index_t
Definition gsConfig.h:32
#define GISMO_ERROR(message)
Definition gsDebug.h:118
#define gsWarn
Definition gsDebug.h:50
#define gsInfo
Definition gsDebug.h:43
Utility class for finding files and handling paths.
Provides declaration of input/output XML utilities struct.
gsXmlNode * searchLabel(const std::string label, gsXmlNode *root, const char *tag_name=NULL, const bool print_warning=true)
Definition gsXml.h:273
gsXmlNode * searchNode(gsXmlNode *root, const std::string &attr_name, const std::string &value, const char *tag_name=NULL)
Definition gsXml.h:231
gsXmlNode * makeNode(const std::string &name, gsXmlTree &data)
Helper to allocate XML node.
Definition gsXml.cpp:54
gsXmlAttribute * makeAttribute(const std::string &name, const std::string &value, gsXmlTree &data)
Helper to allocate XML attribute.
Definition gsXml.cpp:37
gsXmlNode * searchId(const int id, gsXmlNode *root, const char *tag_name=NULL, const bool print_warning=true)
Definition gsXml.h:254
unique_ptr< T > make_unique(T *x)
Definition gsMemory.h:198
The G+Smo namespace, containing all definitions for the library.
S give(S &x)
Definition gsMemory.h:266