1 // Created on: 2001-07-11
2 // Created by: Julia DOROVSKIKH
3 // Copyright (c) 2001-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 //AGV 150202: Changed prototype LDOM_Node::getOwnerDocument()
18 #include <BRepTools.hxx>
19 #include <Message_Messenger.hxx>
20 #include <Message_ProgressScope.hxx>
21 #include <LDOM_OSStream.hxx>
22 #include <LDOM_Text.hxx>
23 #include <Standard_SStream.hxx>
24 #include <Standard_Type.hxx>
25 #include <TDF_Attribute.hxx>
26 #include <TDF_Label.hxx>
27 #include <TNaming_Builder.hxx>
28 #include <TNaming_Iterator.hxx>
29 #include <TNaming_NamedShape.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopTools_LocationSet.hxx>
32 #include <XmlMNaming_NamedShapeDriver.hxx>
33 #include <XmlMNaming_Shape1.hxx>
34 #include <XmlObjMgt.hxx>
35 #include <XmlObjMgt_Array1.hxx>
36 #include <XmlObjMgt_Persistent.hxx>
38 IMPLEMENT_STANDARD_RTTIEXT(XmlMNaming_NamedShapeDriver,XmlMDF_ADriver)
40 static TNaming_Evolution EvolutionEnum (const XmlObjMgt_DOMString&);
41 static const XmlObjMgt_DOMString& EvolutionString (const TNaming_Evolution);
42 static void doTranslate (const TopoDS_Shape&,
45 static int doTranslate (const XmlMNaming_Shape1&,
49 IMPLEMENT_DOMSTRING (OldsString, "olds")
50 IMPLEMENT_DOMSTRING (NewsString, "news")
51 IMPLEMENT_DOMSTRING (StatusString, "evolution")
52 IMPLEMENT_DOMSTRING (VersionString, "version")
53 IMPLEMENT_DOMSTRING (ShapesString, "shapes")
55 IMPLEMENT_DOMSTRING (EvolPrimitiveString, "primitive")
56 IMPLEMENT_DOMSTRING (EvolGeneratedString, "generated")
57 IMPLEMENT_DOMSTRING (EvolModifyString, "modify")
58 IMPLEMENT_DOMSTRING (EvolDeleteString, "delete")
59 IMPLEMENT_DOMSTRING (EvolSelectedString, "selected")
60 IMPLEMENT_DOMSTRING (EvolReplaceString, "replace")
62 //=======================================================================
63 //function : XmlMNaming_NamedShapeDriver
64 //purpose : Constructor
65 //=======================================================================
67 XmlMNaming_NamedShapeDriver::XmlMNaming_NamedShapeDriver
68 (const Handle(Message_Messenger)& theMessageDriver)
69 : XmlMDF_ADriver (theMessageDriver, NULL),
70 myShapeSet (Standard_False) // triangles mode
73 //=======================================================================
74 //function : NewEmpty()
76 //=======================================================================
77 Handle(TDF_Attribute) XmlMNaming_NamedShapeDriver::NewEmpty () const
79 return (new TNaming_NamedShape());
82 //=======================================================================
84 //purpose : retrieval of TNaming_NamedShape
85 //=======================================================================
87 Standard_Boolean XmlMNaming_NamedShapeDriver::Paste
88 (const XmlObjMgt_Persistent& theSource,
89 const Handle(TDF_Attribute)& theTarget,
90 XmlObjMgt_RRelocationTable&) const
92 Handle(TNaming_NamedShape) aTarget =
93 Handle(TNaming_NamedShape)::DownCast(theTarget);
94 TDF_Label Label = aTarget -> Label();
95 TNaming_Builder aBld (Label);
98 Standard_Integer aVersion = 0;
99 const XmlObjMgt_Element& anElement = theSource;
100 XmlObjMgt_DOMString aVerString = anElement.getAttribute (::VersionString());
101 if (aVerString != NULL)
102 aVerString.GetInteger (aVersion);
104 // Get Evolution status
105 XmlObjMgt_DOMString aStatus = anElement.getAttribute (::StatusString());
106 TNaming_Evolution evol = EvolutionEnum (aStatus);
107 // apres creation Builder qui a mis la version a 1 :
108 aTarget -> SetVersion (aVersion);
110 const XmlObjMgt_Array1 OldPShapes (anElement, ::OldsString());
111 const XmlObjMgt_Array1 NewPShapes (anElement, ::NewsString());
112 if (NewPShapes.Length() == 0 && OldPShapes.Length() == 0)
113 return Standard_True;
115 TopoDS_Shape anOldShape;
116 TopoDS_Shape aNewShape;
117 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
119 Standard_Integer lower = NewPShapes.Lower();
120 if (OldPShapes.Lower() < lower) lower = OldPShapes.Lower();
122 Standard_Integer upper = NewPShapes.Upper();
123 if (OldPShapes.Upper() > upper) upper = OldPShapes.Upper();
125 for (Standard_Integer i = upper; i >= lower; --i)
127 const XmlMNaming_Shape1 aNewPShape = NewPShapes.Value(i);
128 const XmlMNaming_Shape1 anOldPShape = OldPShapes.Value(i);
130 if ( evol != TNaming_PRIMITIVE && anOldPShape.Element() != NULL )
132 if (::doTranslate (anOldPShape, anOldShape, aShapeSet)) {
133 myMessageDriver->Send ("NamedShapeDriver: Error reading a shape from array", Message_Fail);
134 return Standard_False;
138 if (evol != TNaming_DELETE && aNewPShape.Element() != NULL )
140 if (::doTranslate (aNewPShape, aNewShape, aShapeSet)) {
141 myMessageDriver->Send ("NamedShapeDriver: Error reading a shape from array", Message_Fail);
142 return Standard_False;
148 case TNaming_PRIMITIVE:
149 aBld.Generated(aNewShape);
151 case TNaming_GENERATED:
152 aBld.Generated(anOldShape,aNewShape);
155 aBld.Modify(anOldShape,aNewShape);
158 aBld.Delete(anOldShape);
160 case TNaming_SELECTED:
161 aBld.Select(aNewShape, anOldShape);
163 case TNaming_REPLACE:
164 aBld.Modify(anOldShape,aNewShape);
165 break; // for compatibility
166 // case TNaming_REPLACE:
167 // aBld.Replace(anOldShape,aNewShape);
170 throw Standard_DomainError("TNaming_Evolution; enum term unknown");
172 anOldShape.Nullify();
175 return Standard_True;
178 //=======================================================================
180 //purpose : storage of TNaming_NamedShape
181 //=======================================================================
183 void XmlMNaming_NamedShapeDriver::Paste (const Handle(TDF_Attribute)& theSource,
184 XmlObjMgt_Persistent& theTarget,
185 XmlObjMgt_SRelocationTable&) const
187 //AGV XmlObjMgt_Document& aDoc =
188 //AGV (XmlObjMgt_Document&) theTarget.Element().getOwnerDocument();
189 XmlObjMgt_Document aDoc =
190 XmlObjMgt_Document (theTarget.Element().getOwnerDocument());
192 Handle(TNaming_NamedShape) aNamedShape =
193 Handle(TNaming_NamedShape)::DownCast(theSource);
194 TNaming_Evolution evol = aNamedShape->Evolution();
197 Standard_Integer NbShapes = 0;
198 TNaming_Iterator SItr (aNamedShape);
199 while (SItr.More()) {
204 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
205 XmlObjMgt_Array1 OldPShapes (1,NbShapes), NewPShapes (1,NbShapes);
207 OldPShapes.CreateArrayElement (theTarget, ::OldsString());
208 NewPShapes.CreateArrayElement (theTarget, ::NewsString());
211 Standard_Integer i = 1;
212 TNaming_Iterator SIterator(aNamedShape);
213 while (SIterator.More())
215 const TopoDS_Shape& OldShape = SIterator.OldShape();
216 const TopoDS_Shape& NewShape = SIterator.NewShape();
218 if ( evol != TNaming_PRIMITIVE )
220 XmlMNaming_Shape1 anOldPShape (aDoc);
221 ::doTranslate (OldShape, anOldPShape, aShapeSet);
222 OldPShapes.SetValue (i, anOldPShape.Element());
225 if (evol != TNaming_DELETE)
227 XmlMNaming_Shape1 aNewPShape (aDoc);
228 ::doTranslate (NewShape, aNewPShape, aShapeSet);
229 NewPShapes.SetValue (i, aNewPShape.Element());
235 theTarget.Element().setAttribute (::StatusString(), EvolutionString(evol));
236 Standard_Integer aVersion = aNamedShape -> Version();
238 theTarget.Element().setAttribute (::VersionString(), aVersion);
241 //=======================================================================
242 //function : EvolutionEnum
244 //=======================================================================
246 static const XmlObjMgt_DOMString& EvolutionString(const TNaming_Evolution i)
249 case TNaming_PRIMITIVE : return ::EvolPrimitiveString();
250 case TNaming_GENERATED : return ::EvolGeneratedString();
251 case TNaming_MODIFY : return ::EvolModifyString();
252 case TNaming_DELETE : return ::EvolDeleteString();
253 case TNaming_SELECTED : return ::EvolSelectedString();
254 case TNaming_REPLACE : return ::EvolModifyString(); // case TNaming_REPLACE : return ::EvolReplaceString(); for compatibility
256 throw Standard_DomainError("TNaming_Evolution; enum term unknown");
260 //=======================================================================
261 //function : EvolutionEnum
263 //=======================================================================
265 static TNaming_Evolution EvolutionEnum (const XmlObjMgt_DOMString& theString)
267 TNaming_Evolution aResult = TNaming_PRIMITIVE;
268 if (theString.equals (::EvolPrimitiveString()) == Standard_False) {
269 if (theString.equals (::EvolGeneratedString()))
270 aResult = TNaming_GENERATED;
271 else if (theString.equals (::EvolModifyString()))
272 aResult = TNaming_MODIFY;
273 else if (theString.equals (::EvolDeleteString()))
274 aResult = TNaming_DELETE;
275 else if (theString.equals (::EvolSelectedString()))
276 aResult = TNaming_SELECTED;
277 else if (theString.equals (::EvolReplaceString()))
278 aResult = TNaming_MODIFY; // for compatibility //TNaming_REPLACE;
280 throw Standard_DomainError("TNaming_Evolution; string value without enum term equivalence");
285 //=======================================================================
286 //function : doTranslate
287 //purpose : shape storage to XML
288 //=======================================================================
290 static void doTranslate (const TopoDS_Shape& theShape,
291 XmlMNaming_Shape1& theResult,
292 BRepTools_ShapeSet& theShapeSet)
294 // Check for empty shape
295 if (theShape.IsNull()) return;
297 // Add to shape set both TShape and Location contained in theShape
298 const Standard_Integer aTShapeId = theShapeSet.Add (theShape);
299 const Standard_Integer aLocId =
300 theShapeSet.Locations().Index (theShape.Location());
302 // Fill theResult with shape parameters: TShape ID, Location, Orientation
303 theResult.SetShape (aTShapeId, aLocId, theShape.Orientation());
305 if (theShape.ShapeType() == TopAbs_VERTEX)
307 theResult.SetVertex(theShape);
311 //=======================================================================
312 //function : doTranslate
313 //purpose : shape retrieval from XML
314 //=======================================================================
316 static int doTranslate (const XmlMNaming_Shape1& thePShape,
317 TopoDS_Shape& theResult,
318 BRepTools_ShapeSet& theShapeSet)
320 const Standard_Integer aShapeId = thePShape.TShapeId();
322 // Read TShape and Orientation
323 if (aShapeId <= 0 || aShapeId > theShapeSet.NbShapes())
325 theResult.TShape (theShapeSet.Shape(aShapeId).TShape());
326 theResult.Orientation (thePShape.Orientation());
327 theResult.Location (theShapeSet.Locations().Location (thePShape.LocId()));
332 //=======================================================================
333 //function : ReadShapeSection
335 //=======================================================================
337 void XmlMNaming_NamedShapeDriver::ReadShapeSection (const XmlObjMgt_Element& theElement,
338 const Message_ProgressRange& theRange)
340 XmlObjMgt_Element anElement =
341 XmlObjMgt::FindChildByName (theElement, ::ShapesString());
342 if (anElement != NULL) {
343 for (LDOM_Node aNode = anElement.getFirstChild();
345 aNode = anElement.getNextSibling())
347 if (aNode.getNodeType() == LDOM_Node::TEXT_NODE) {
348 LDOMString aData = aNode.getNodeValue();
349 std::stringstream aStream (std::string(aData.GetString()));
351 myShapeSet.Read (aStream, theRange);
358 //=======================================================================
359 //function : WriteShapeSection
361 //=======================================================================
363 void XmlMNaming_NamedShapeDriver::WriteShapeSection (XmlObjMgt_Element& theElement,
364 const Message_ProgressRange& theRange)
366 // Create "shapes" element and append it as child
367 XmlObjMgt_Document aDoc = theElement.getOwnerDocument();
368 XmlObjMgt_Element anElement = aDoc.createElement (::ShapesString());
369 theElement.appendChild (anElement);
371 // Add text to the "shapes" element
372 if (myShapeSet.NbShapes() > 0) {
373 myShapeSet.SetFormatNb(TopTools_FormatVersion_VERSION_1);
374 LDOM_OSStream aStream (16 * 1024);
375 // ostrstream aStream;
376 // aStream.rdbuf() -> setbuf (0, 16380);
377 Message_ProgressScope aPS(theRange, "Writing shape section", 2);
378 myShapeSet.Write (aStream, aPS.Next());
382 aStream << std::ends;
383 char * aStr = (char *)aStream.str();
384 LDOM_Text aText = aDoc.createTextNode (aStr);
386 aText.SetValueClear(); // There are no characters '<' and '&' and like
387 // aStream.rdbuf() -> freeze(0); // release the buffer
388 anElement.appendChild (aText);
389 // Clear the shape set to avoid appending to it on the next write
390 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
398 //=======================================================================
401 //=======================================================================
403 void XmlMNaming_NamedShapeDriver::Clear()