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 <CDM_MessageDriver.hxx>
20 #include <LDOM_OSStream.hxx>
21 #include <LDOM_Text.hxx>
22 #include <Standard_SStream.hxx>
23 #include <Standard_Type.hxx>
24 #include <TDF_Attribute.hxx>
25 #include <TDF_Label.hxx>
26 #include <TNaming_Builder.hxx>
27 #include <TNaming_Iterator.hxx>
28 #include <TNaming_NamedShape.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopTools_LocationSet.hxx>
31 #include <XmlMNaming_NamedShapeDriver.hxx>
32 #include <XmlMNaming_Shape1.hxx>
33 #include <XmlObjMgt.hxx>
34 #include <XmlObjMgt_Array1.hxx>
35 #include <XmlObjMgt_Persistent.hxx>
37 IMPLEMENT_STANDARD_RTTIEXT(XmlMNaming_NamedShapeDriver,XmlMDF_ADriver)
39 static TNaming_Evolution EvolutionEnum (const XmlObjMgt_DOMString&);
40 static const XmlObjMgt_DOMString& EvolutionString (const TNaming_Evolution);
41 static void doTranslate (const TopoDS_Shape&,
44 static int doTranslate (const XmlMNaming_Shape1&,
48 IMPLEMENT_DOMSTRING (OldsString, "olds")
49 IMPLEMENT_DOMSTRING (NewsString, "news")
50 IMPLEMENT_DOMSTRING (StatusString, "evolution")
51 IMPLEMENT_DOMSTRING (VersionString, "version")
52 IMPLEMENT_DOMSTRING (ShapesString, "shapes")
54 IMPLEMENT_DOMSTRING (EvolPrimitiveString, "primitive")
55 IMPLEMENT_DOMSTRING (EvolGeneratedString, "generated")
56 IMPLEMENT_DOMSTRING (EvolModifyString, "modify")
57 IMPLEMENT_DOMSTRING (EvolDeleteString, "delete")
58 IMPLEMENT_DOMSTRING (EvolSelectedString, "selected")
59 IMPLEMENT_DOMSTRING (EvolReplaceString, "replace")
61 //=======================================================================
62 //function : XmlMNaming_NamedShapeDriver
63 //purpose : Constructor
64 //=======================================================================
66 XmlMNaming_NamedShapeDriver::XmlMNaming_NamedShapeDriver
67 (const Handle(CDM_MessageDriver)& theMessageDriver)
68 : XmlMDF_ADriver (theMessageDriver, NULL),
69 myShapeSet (Standard_False) // triangles mode
72 //=======================================================================
73 //function : NewEmpty()
75 //=======================================================================
76 Handle(TDF_Attribute) XmlMNaming_NamedShapeDriver::NewEmpty () const
78 return (new TNaming_NamedShape());
81 //=======================================================================
83 //purpose : retrieval of TNaming_NamedShape
84 //=======================================================================
86 Standard_Boolean XmlMNaming_NamedShapeDriver::Paste
87 (const XmlObjMgt_Persistent& theSource,
88 const Handle(TDF_Attribute)& theTarget,
89 XmlObjMgt_RRelocationTable&) const
91 Handle(TNaming_NamedShape) aTarget =
92 Handle(TNaming_NamedShape)::DownCast(theTarget);
93 TDF_Label Label = aTarget -> Label();
94 TNaming_Builder aBld (Label);
97 Standard_Integer aVersion = 0;
98 const XmlObjMgt_Element& anElement = theSource;
99 XmlObjMgt_DOMString aVerString = anElement.getAttribute (::VersionString());
100 if (aVerString != NULL)
101 aVerString.GetInteger (aVersion);
103 // Get Evolution status
104 XmlObjMgt_DOMString aStatus = anElement.getAttribute (::StatusString());
105 TNaming_Evolution evol = EvolutionEnum (aStatus);
106 // apres creation Builder qui a mis la version a 1 :
107 aTarget -> SetVersion (aVersion);
109 const XmlObjMgt_Array1 OldPShapes (anElement, ::OldsString());
110 const XmlObjMgt_Array1 NewPShapes (anElement, ::NewsString());
111 if (NewPShapes.Length() == 0 && OldPShapes.Length() == 0)
112 return Standard_True;
114 TopoDS_Shape anOldShape;
115 TopoDS_Shape aNewShape;
116 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
118 Standard_Integer lower = NewPShapes.Lower();
119 if (OldPShapes.Lower() < lower) lower = OldPShapes.Lower();
121 Standard_Integer upper = NewPShapes.Upper();
122 if (OldPShapes.Upper() > upper) upper = OldPShapes.Upper();
124 for (Standard_Integer i = lower; i <= upper; i++)
126 const XmlMNaming_Shape1 aNewPShape = NewPShapes.Value(i);
127 const XmlMNaming_Shape1 anOldPShape = OldPShapes.Value(i);
129 if ( evol != TNaming_PRIMITIVE && anOldPShape.Element() != NULL )
131 if (::doTranslate (anOldPShape, anOldShape, aShapeSet)) {
132 WriteMessage ("NamedShapeDriver: Error reading a shape from array");
133 return Standard_False;
137 if (evol != TNaming_DELETE && aNewPShape.Element() != NULL )
139 if (::doTranslate (aNewPShape, aNewShape, aShapeSet)) {
140 WriteMessage ("NamedShapeDriver: Error reading a shape from array");
141 return Standard_False;
147 case TNaming_PRIMITIVE:
148 aBld.Generated(aNewShape);
150 case TNaming_GENERATED:
151 aBld.Generated(anOldShape,aNewShape);
154 aBld.Modify(anOldShape,aNewShape);
157 aBld.Delete(anOldShape);
159 case TNaming_SELECTED:
160 aBld.Select(aNewShape, anOldShape);
162 case TNaming_REPLACE:
163 aBld.Modify(anOldShape,aNewShape);
164 break; // for compatibility
165 // case TNaming_REPLACE:
166 // aBld.Replace(anOldShape,aNewShape);
169 throw Standard_DomainError("TNaming_Evolution; enum term unknown");
171 anOldShape.Nullify();
174 return Standard_True;
177 //=======================================================================
179 //purpose : storage of TNaming_NamedShape
180 //=======================================================================
182 void XmlMNaming_NamedShapeDriver::Paste (const Handle(TDF_Attribute)& theSource,
183 XmlObjMgt_Persistent& theTarget,
184 XmlObjMgt_SRelocationTable&) const
186 //AGV XmlObjMgt_Document& aDoc =
187 //AGV (XmlObjMgt_Document&) theTarget.Element().getOwnerDocument();
188 XmlObjMgt_Document aDoc =
189 XmlObjMgt_Document (theTarget.Element().getOwnerDocument());
191 Handle(TNaming_NamedShape) aNamedShape =
192 Handle(TNaming_NamedShape)::DownCast(theSource);
193 TNaming_Evolution evol = aNamedShape->Evolution();
196 Standard_Integer NbShapes = 0;
197 TNaming_Iterator SItr (aNamedShape);
198 while (SItr.More()) {
203 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
204 XmlObjMgt_Array1 OldPShapes (1,NbShapes), NewPShapes (1,NbShapes);
206 OldPShapes.CreateArrayElement (theTarget, ::OldsString());
207 NewPShapes.CreateArrayElement (theTarget, ::NewsString());
210 Standard_Integer i = 1;
211 TNaming_Iterator SIterator(aNamedShape);
212 while (SIterator.More())
214 const TopoDS_Shape& OldShape = SIterator.OldShape();
215 const TopoDS_Shape& NewShape = SIterator.NewShape();
217 if ( evol != TNaming_PRIMITIVE )
219 XmlMNaming_Shape1 anOldPShape (aDoc);
220 ::doTranslate (OldShape, anOldPShape, aShapeSet);
221 OldPShapes.SetValue (i, anOldPShape.Element());
224 if (evol != TNaming_DELETE)
226 XmlMNaming_Shape1 aNewPShape (aDoc);
227 ::doTranslate (NewShape, aNewPShape, aShapeSet);
228 NewPShapes.SetValue (i, aNewPShape.Element());
234 theTarget.Element().setAttribute (::StatusString(), EvolutionString(evol));
235 Standard_Integer aVersion = aNamedShape -> Version();
237 theTarget.Element().setAttribute (::VersionString(), aVersion);
240 //=======================================================================
241 //function : EvolutionEnum
243 //=======================================================================
245 static const XmlObjMgt_DOMString& EvolutionString(const TNaming_Evolution i)
248 case TNaming_PRIMITIVE : return ::EvolPrimitiveString();
249 case TNaming_GENERATED : return ::EvolGeneratedString();
250 case TNaming_MODIFY : return ::EvolModifyString();
251 case TNaming_DELETE : return ::EvolDeleteString();
252 case TNaming_SELECTED : return ::EvolSelectedString();
253 case TNaming_REPLACE : return ::EvolModifyString(); // case TNaming_REPLACE : return ::EvolReplaceString(); for compatibility
255 throw Standard_DomainError("TNaming_Evolution; enum term unknown");
259 //=======================================================================
260 //function : EvolutionEnum
262 //=======================================================================
264 static TNaming_Evolution EvolutionEnum (const XmlObjMgt_DOMString& theString)
266 TNaming_Evolution aResult = TNaming_PRIMITIVE;
267 if (theString.equals (::EvolPrimitiveString()) == Standard_False) {
268 if (theString.equals (::EvolGeneratedString()))
269 aResult = TNaming_GENERATED;
270 else if (theString.equals (::EvolModifyString()))
271 aResult = TNaming_MODIFY;
272 else if (theString.equals (::EvolDeleteString()))
273 aResult = TNaming_DELETE;
274 else if (theString.equals (::EvolSelectedString()))
275 aResult = TNaming_SELECTED;
276 else if (theString.equals (::EvolReplaceString()))
277 aResult = TNaming_MODIFY; // for compatibility //TNaming_REPLACE;
279 throw Standard_DomainError("TNaming_Evolution; string value without enum term equivalence");
284 //=======================================================================
285 //function : doTranslate
286 //purpose : shape storage to XML
287 //=======================================================================
289 static void doTranslate (const TopoDS_Shape& theShape,
290 XmlMNaming_Shape1& theResult,
291 BRepTools_ShapeSet& theShapeSet)
293 // Check for empty shape
294 if (theShape.IsNull()) return;
296 // Add to shape set both TShape and Location contained in theShape
297 const Standard_Integer aTShapeId = theShapeSet.Add (theShape);
298 const Standard_Integer aLocId =
299 theShapeSet.Locations().Index (theShape.Location());
301 // Fill theResult with shape parameters: TShape ID, Location, Orientation
302 theResult.SetShape (aTShapeId, aLocId, theShape.Orientation());
304 if (theShape.ShapeType() == TopAbs_VERTEX)
306 theResult.SetVertex(theShape);
310 //=======================================================================
311 //function : doTranslate
312 //purpose : shape retrieval from XML
313 //=======================================================================
315 static int doTranslate (const XmlMNaming_Shape1& thePShape,
316 TopoDS_Shape& theResult,
317 BRepTools_ShapeSet& theShapeSet)
319 const Standard_Integer aShapeId = thePShape.TShapeId();
321 // Read TShape and Orientation
322 if (aShapeId <= 0 || aShapeId > theShapeSet.NbShapes())
324 theResult.TShape (theShapeSet.Shape(aShapeId).TShape());
325 theResult.Orientation (thePShape.Orientation());
326 theResult.Location (theShapeSet.Locations().Location (thePShape.LocId()));
331 //=======================================================================
332 //function : ReadShapeSection
334 //=======================================================================
336 void XmlMNaming_NamedShapeDriver::ReadShapeSection
337 (const XmlObjMgt_Element& theElement)
339 XmlObjMgt_Element anElement =
340 XmlObjMgt::FindChildByName (theElement, ::ShapesString());
341 if (anElement != NULL) {
342 for (LDOM_Node aNode = anElement.getFirstChild();
344 aNode = anElement.getNextSibling())
346 if (aNode.getNodeType() == LDOM_Node::TEXT_NODE) {
347 LDOMString aData = aNode.getNodeValue();
348 std::stringstream aStream (std::string(aData.GetString()));
350 myShapeSet.Read (aStream);
357 //=======================================================================
358 //function : WriteShapeSection
360 //=======================================================================
362 void XmlMNaming_NamedShapeDriver::WriteShapeSection
363 (XmlObjMgt_Element& theElement)
365 // Create "shapes" element and append it as child
366 XmlObjMgt_Document aDoc = theElement.getOwnerDocument();
367 XmlObjMgt_Element anElement = aDoc.createElement (::ShapesString());
368 theElement.appendChild (anElement);
370 // Add text to the "shapes" element
371 if (myShapeSet.NbShapes() > 0) {
372 myShapeSet.SetFormatNb(2);
373 LDOM_OSStream aStream (16 * 1024);
374 // ostrstream aStream;
375 // aStream.rdbuf() -> setbuf (0, 16380);
376 myShapeSet.Write (aStream);
378 char * aStr = (char *)aStream.str();
379 LDOM_Text aText = aDoc.createTextNode (aStr);
381 aText.SetValueClear(); // There are no characters '<' and '&' and like
382 // aStream.rdbuf() -> freeze(0); // release the buffer
383 anElement.appendChild (aText);
384 // Clear the shape set to avoid appending to it on the next write
385 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
390 //=======================================================================
393 //=======================================================================
395 void XmlMNaming_NamedShapeDriver::Clear()