1 // Created on: 2001-07-11
2 // Created by: Julia DOROVSKIKH
3 // Copyright (c) 2001-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
20 //AGV 150202: Changed prototype LDOM_Node::getOwnerDocument()
22 #include <XmlMNaming_NamedShapeDriver.ixx>
24 #include <XmlObjMgt.hxx>
25 #include <XmlMNaming_Array1OfShape1.hxx>
26 #include <XmlMNaming_Shape1.hxx>
28 #include <TDF_Label.hxx>
29 #include <TNaming_Builder.hxx>
30 #include <TNaming_NamedShape.hxx>
31 #include <TNaming_Iterator.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <BRepTools.hxx>
35 #include <LDOM_Text.hxx>
36 #include <LDOM_OSStream.hxx>
38 #include <Standard_SStream.hxx>
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(CDM_MessageDriver&) 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 XmlMNaming_Array1OfShape1 OldPShapes (anElement, ::OldsString());
111 const XmlMNaming_Array1OfShape1 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 = lower; i <= upper; 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 WriteMessage ("NamedShapeDriver: Error reading a shape from array");
134 return Standard_False;
138 if (evol != TNaming_DELETE && aNewPShape.Element() != NULL )
140 if (::doTranslate (aNewPShape, aNewShape, aShapeSet)) {
141 WriteMessage ("NamedShapeDriver: Error reading a shape from array");
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.Replace(anOldShape,aNewShape);
167 Standard_DomainError::Raise("TNaming_Evolution; enum term unknown");
169 anOldShape.Nullify();
172 return Standard_True;
175 //=======================================================================
177 //purpose : storage of TNaming_NamedShape
178 //=======================================================================
180 void XmlMNaming_NamedShapeDriver::Paste (const Handle(TDF_Attribute)& theSource,
181 XmlObjMgt_Persistent& theTarget,
182 XmlObjMgt_SRelocationTable&) const
184 //AGV XmlObjMgt_Document& aDoc =
185 //AGV (XmlObjMgt_Document&) theTarget.Element().getOwnerDocument();
186 XmlObjMgt_Document aDoc =
187 XmlObjMgt_Document (theTarget.Element().getOwnerDocument());
189 Handle(TNaming_NamedShape) aNamedShape =
190 Handle(TNaming_NamedShape)::DownCast(theSource);
191 TNaming_Evolution evol = aNamedShape->Evolution();
194 Standard_Integer NbShapes = 0;
195 TNaming_Iterator SItr (aNamedShape);
196 while (SItr.More()) {
201 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
202 XmlMNaming_Array1OfShape1 OldPShapes (1,NbShapes), NewPShapes (1,NbShapes);
204 OldPShapes.CreateArrayElement (theTarget, ::OldsString());
205 NewPShapes.CreateArrayElement (theTarget, ::NewsString());
208 Standard_Integer i = 1;
209 TNaming_Iterator SIterator(aNamedShape);
210 while (SIterator.More())
212 const TopoDS_Shape& OldShape = SIterator.OldShape();
213 const TopoDS_Shape& NewShape = SIterator.NewShape();
215 if ( evol != TNaming_PRIMITIVE )
217 XmlMNaming_Shape1 anOldPShape (aDoc);
218 ::doTranslate (OldShape, anOldPShape, aShapeSet);
219 OldPShapes.SetValue (i, anOldPShape.Element());
222 if (evol != TNaming_DELETE)
224 XmlMNaming_Shape1 aNewPShape (aDoc);
225 ::doTranslate (NewShape, aNewPShape, aShapeSet);
226 NewPShapes.SetValue (i, aNewPShape.Element());
232 theTarget.Element().setAttribute (::StatusString(), EvolutionString(evol));
233 Standard_Integer aVersion = aNamedShape -> Version();
235 theTarget.Element().setAttribute (::VersionString(), aVersion);
238 //=======================================================================
239 //function : EvolutionEnum
241 //=======================================================================
243 static const XmlObjMgt_DOMString& EvolutionString(const TNaming_Evolution i)
246 case TNaming_PRIMITIVE : return ::EvolPrimitiveString();
247 case TNaming_GENERATED : return ::EvolGeneratedString();
248 case TNaming_MODIFY : return ::EvolModifyString();
249 case TNaming_DELETE : return ::EvolDeleteString();
250 case TNaming_SELECTED : return ::EvolSelectedString();
251 case TNaming_REPLACE : return ::EvolReplaceString();
253 Standard_DomainError::Raise("TNaming_Evolution; enum term unknown");
255 static XmlObjMgt_DOMString aNullString;
256 return aNullString; // To avoid compilation error message.
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_REPLACE;
279 Standard_DomainError::Raise
280 ("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
338 (const XmlObjMgt_Element& theElement)
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 LDOM_Text aText = (LDOM_Text&) aNode;
349 LDOMString aData = aText.getData();
350 #ifdef USE_STL_STREAM
351 std::stringstream aStream (std::string(aData.GetString()));
353 istrstream aStream (Standard_CString(aData.GetString()));
356 myShapeSet.Read (aStream);
363 //=======================================================================
364 //function : WriteShapeSection
366 //=======================================================================
368 void XmlMNaming_NamedShapeDriver::WriteShapeSection
369 (XmlObjMgt_Element& theElement)
371 // Create "shapes" element and append it as child
372 XmlObjMgt_Document aDoc = theElement.getOwnerDocument();
373 XmlObjMgt_Element anElement = aDoc.createElement (::ShapesString());
374 theElement.appendChild (anElement);
376 // Add text to the "shapes" element
377 if (myShapeSet.NbShapes() > 0) {
378 myShapeSet.SetFormatNb(2);
379 LDOM_OSStream aStream (1024);
380 // ostrstream aStream;
381 // aStream.rdbuf() -> setbuf (0, 16380);
382 myShapeSet.Write (aStream);
384 char * aStr = (char *)aStream.str();
385 LDOM_Text aText = aDoc.createTextNode (aStr);
387 aText.SetValueClear(); // There are no characters '<' and '&' and like
388 // aStream.rdbuf() -> freeze(0); // release the buffer
389 anElement.appendChild (aText);
390 // Clear the shape set to avoid appending to it on the next write
391 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
396 //=======================================================================
399 //=======================================================================
401 void XmlMNaming_NamedShapeDriver::Clear()