1 // Created on: 2006-10-08
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2006-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef VrmlData_Scene_HeaderFile
17 #define VrmlData_Scene_HeaderFile
19 #include <VrmlData_ListOfNode.hxx>
20 #include <VrmlData_MapOfNode.hxx>
21 #include <VrmlData_ErrorStatus.hxx>
22 #include <VrmlData_Geometry.hxx>
23 #include <VrmlData_WorldInfo.hxx>
24 #include <TopoDS_Shape.hxx>
25 #include <Standard_OStream.hxx>
26 #include <Standard_IStream.hxx>
27 #include <TCollection_ExtendedString.hxx>
28 #include <NCollection_IncAllocator.hxx>
29 #include <Standard_Mutex.hxx>
30 #include <VrmlData_DataMapOfShapeAppearance.hxx>
32 struct VrmlData_InBuffer;
35 * Block of comments describing class VrmlData_Scene
42 * Iterator type to get all contained Nodes one-by-one.
44 typedef VrmlData_ListOfNode::Iterator Iterator;
46 // ---------- PUBLIC METHODS ----------
51 Standard_EXPORT VrmlData_Scene (const Handle(NCollection_IncAllocator)& = 0L);
54 * Query the status of the previous operation.
55 * Normally it should be equal to VrmlData_StatusOK (no error).
57 inline VrmlData_ErrorStatus Status () const
61 * Add the given directory path to the list of VRML file search directories.
62 * This method forms the list of directories ordered according to the
63 * sequence of this method calls. When an Inline node is found, the URLs
64 * in that node are matched with these directories.
65 * The last (implicit) search directory is the current process directory
66 * ("."). It takes effect if the list is empty or if there is no match with
67 * exisiting directories.
69 Standard_EXPORT void SetVrmlDir (const TCollection_ExtendedString&);
72 * Set the scale factor that would be further used in methods
73 * ReadReal, ReadXYZ and ReadXY. All coordinates, distances and sized are
74 * multiplied by this factor during reading the data.
76 inline void SetLinearScale (const Standard_Real theScale)
77 { myLinearScale = theScale; }
80 * Returns the directory iterator, to check the presence of requested VRML
81 * file in each iterated directory.
83 inline NCollection_List<TCollection_ExtendedString>::Iterator
84 VrmlDirIterator () const
85 { return NCollection_List<TCollection_ExtendedString>::Iterator(myVrmlDir); }
90 inline Iterator GetIterator () const
91 { return Iterator (myLstNodes); }
94 * Get the iterator of named nodes.
96 inline VrmlData_MapOfNode::Iterator
97 NamedNodesIterator() const
98 { return myNamedNodes; }
101 * Allocator used by all nodes contained in the Scene.
103 inline const Handle(NCollection_IncAllocator)&
105 { return myAllocator; }
108 * Add a Node. If theN belongs to another Scene, it is cloned.
109 * <p>VrmlData_WorldInfo cannot be added, in this case the method
110 * returns a NULL handle.
112 Standard_EXPORT const Handle(VrmlData_Node)&
113 AddNode (const Handle(VrmlData_Node)& theN,
114 const Standard_Boolean isTopLevel
118 * Find a node by its name.
120 * Name of the node to find.
122 * Type to match. If this value is NULL, the first found node with the
123 * given name is returned. If theType is given, only the node that has
124 * that type is returned.
126 Standard_EXPORT Handle(VrmlData_Node)
127 FindNode (const char * theName,
128 const Handle(Standard_Type)&
132 * Find a node by its name.
134 * Name of the node to search for.
136 * Location of the found node with respect to the whole VRML shape.
138 Standard_EXPORT Handle(VrmlData_Node)
139 FindNode(const char * theName,
140 gp_Trsf& theLocation) const;
143 * Export to text stream (file or else).
144 * This method is protected by Mutex, it is not allowed to read/write
145 * two VRML streams concurrently.
146 * The stream should give as the first line the VRML header:
148 * #VRML V2.0 <encoding type> [optional comment] <line terminator>
152 friend Standard_EXPORT Standard_OStream&
153 operator << (Standard_OStream& theOutput,
154 const VrmlData_Scene& theScene);
157 * Import from text stream (file or else).
158 * This method is protected by Mutex, it is not allowed to read/write
159 * two VRML streams concurrently.
161 Standard_EXPORT VrmlData_Scene& operator<<(Standard_IStream& theInput);
164 * Convert the scene to a Shape.
166 Standard_EXPORT operator TopoDS_Shape () const;
169 * Convert the scene to a Shape, with the information on materials defined
170 * for each sub-shape. This method should be used instead of TopoDS_Shape
171 * explicit conversion operator when you need to retrieve the material
172 * aspect for each face or edge in the returned topological object.
174 * Data Map that binds an Appearance instance to each created TFace or
175 * TEdge if the Appearance node is defined in VRML scene for that geometry.
177 * TopoDS_Shape (Compound) holding all the scene, similar to the result of
178 * explicit TopoDS_Shape conversion operator.
180 Standard_EXPORT TopoDS_Shape GetShape (VrmlData_DataMapOfShapeAppearance& M);
183 * Query the WorldInfo member.
185 Standard_EXPORT const Handle(VrmlData_WorldInfo)&
189 * Read a VRML line. Empty lines and comments are skipped.
190 * The processing starts here from theBuffer.LinePtr; if there is at least
191 * one non-empty character (neither space nor comment), this line is used
192 * without reading the next one.
194 * Buffer receiving the input line
198 * Length of the input buffer (maximal line length)
200 Standard_EXPORT static VrmlData_ErrorStatus
201 ReadLine (VrmlData_InBuffer& theBuffer);
204 * Read a singel word from the input stream, delimited by whitespace.
206 Standard_EXPORT static VrmlData_ErrorStatus
207 ReadWord (VrmlData_InBuffer& theBuffer,
208 TCollection_AsciiString& theStr);
211 * Diagnostic dump of the contents
213 Standard_EXPORT void Dump (Standard_OStream& theStream) const;
216 * Read one real value.
218 Standard_EXPORT VrmlData_ErrorStatus
219 ReadReal (VrmlData_InBuffer& theBuffer,
220 Standard_Real& theResult,
221 Standard_Boolean isApplyScale,
222 Standard_Boolean isOnlyPositive)
226 * Read one triplet of real values.
228 Standard_EXPORT VrmlData_ErrorStatus
229 ReadXYZ (VrmlData_InBuffer& theBuffer,
231 Standard_Boolean isApplyScale,
232 Standard_Boolean isOnlyPositive)
236 * Read one doublet of real values.
238 Standard_EXPORT VrmlData_ErrorStatus
239 ReadXY (VrmlData_InBuffer& theBuffer,
241 Standard_Boolean isApplyScale,
242 Standard_Boolean isOnlyPositive)
245 * Read an array of integer indices, for IndexedfaceSet and IndexedLineSet.
247 Standard_EXPORT VrmlData_ErrorStatus
248 ReadArrIndex(VrmlData_InBuffer& theBuffer,
249 const Standard_Integer **& theArr,
250 Standard_Size& theNBl)
254 * Query the line where the error occurred (if the status is not OK)
256 inline Standard_Integer GetLineError() const { return myLineError; }
259 * Store the indentation for VRML output.
261 * number of spaces to insert at every indentation level
263 inline void SetIndent (const Standard_Integer nSpc)
267 * Write a triplet of real values on a separate line.
269 * The value to be output.
271 * If True, then each component is divided by myLinearScale.
273 * Optional string that is added before the end of the line.
275 Standard_EXPORT VrmlData_ErrorStatus
276 WriteXYZ (const gp_XYZ& theXYZ,
277 const Standard_Boolean isScale,
278 const char * thePostfix
281 * Write an array of integer indices, for IndexedFaceSet and IndexedLineSet.
283 Standard_EXPORT VrmlData_ErrorStatus
284 WriteArrIndex(const char * thePrefix,
285 const Standard_Integer ** theArr,
286 const Standard_Size theNbBl)
291 * Write a string to the output stream respecting the indentation. The string
292 * can be defined as two substrings that will be separated by a space.
293 * Each of the substrings can be NULL, then it is ignored. If both
294 * are NULL, then a single newline is output (without indent).
296 * The first part of string to output
298 * The second part of string to output
301 * - negative decreases the current indent and then outputs.
302 * - positive outputs and then increases the current indent.
304 * Error status of the stream, or a special error if myOutput == NULL.
306 Standard_EXPORT VrmlData_ErrorStatus
307 WriteLine (const char * theLine0,
308 const char * theLine1=0L,
309 const Standard_Integer theIndent
313 * Write the given node to output stream 'myOutput'.
315 Standard_EXPORT VrmlData_ErrorStatus
316 WriteNode (const char * thePrefix,
317 const Handle(VrmlData_Node)&) const;
320 * Query if the current write operation is dummy, i.e., for the purpose of
321 * collecting information before the real write is commenced.
323 inline Standard_Boolean IsDummyWrite() const
324 { return myOutput == 0L; }
327 // ---------- PRIVATE METHODS (PROHIBITED) ----------
328 VrmlData_Scene (const VrmlData_Scene&);
329 VrmlData_Scene& operator = (const VrmlData_Scene&);
333 * Read whatever line from the input checking the istream flags.
335 Standard_EXPORT static VrmlData_ErrorStatus
336 readLine (VrmlData_InBuffer& theBuffer);
339 * Read and verify the VRML header (the 1st line of the file)
341 Standard_EXPORT static VrmlData_ErrorStatus
342 readHeader (VrmlData_InBuffer& theBuffer);
347 * Input buffer from where the node is created
349 * Output parameter, contains the created node on exit
351 * Node type to be checked. If it is NULL no type checking is done.
352 * Otherwise the created node is matched and an error is returned if
355 Standard_EXPORT VrmlData_ErrorStatus
356 createNode (VrmlData_InBuffer& theBuffer,
357 Handle(VrmlData_Node)& theNode,
358 const Handle(Standard_Type)& Type);
361 * Create a single Shape object from all geometric nodes in the list.
363 Standard_EXPORT static void createShape (TopoDS_Shape& outShape,
364 const VrmlData_ListOfNode&,
365 VrmlData_DataMapOfShapeAppearance*);
369 // ---------- PRIVATE FIELDS ----------
370 Standard_Real myLinearScale;
371 VrmlData_ListOfNode myLstNodes; ///! top-level nodes
372 VrmlData_ListOfNode myAllNodes; ///! all nodes
373 VrmlData_ErrorStatus myStatus;
374 Handle(NCollection_IncAllocator) myAllocator;
375 Handle(VrmlData_WorldInfo) myWorldInfo;
376 VrmlData_MapOfNode myNamedNodes;
379 NCollection_List<TCollection_ExtendedString> myVrmlDir;
380 Standard_Mutex myMutex;
381 Standard_Integer myLineError;///! #0 if error
384 Standard_OStream * myOutput;
385 Standard_Integer myIndent;
386 Standard_Integer myCurrentIndent;
388 * This map is used to avoid multiple storage of the same named node: each
389 * named node is added here when it is written the first time.
391 VrmlData_MapOfNode myNamedNodesOut;
393 * This map allows to resolve multiple reference to any unnamed node. It
394 * is used during the dummy write (myOutput == 0L). When a node is processed
395 * the first time it is added to this map, the second time it is automatically
398 NCollection_Map<Standard_Address> myUnnamedNodesOut;
399 Standard_Integer myAutoNameCounter;
400 friend class VrmlData_Group;
401 friend class VrmlData_Node;