0023024: Update headers of OCCT files
[occt.git] / src / XmlMNaming / XmlMNaming_NamedShapeDriver.cxx
CommitLineData
b311480e 1// Created on: 2001-07-11
2// Created by: Julia DOROVSKIKH
3// Copyright (c) 2001-2012 OPEN CASCADE SAS
4//
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.
9//
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.
12//
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.
19
20//AGV 150202: Changed prototype LDOM_Node::getOwnerDocument()
7fd59977 21
22#include <XmlMNaming_NamedShapeDriver.ixx>
23
24#include <XmlObjMgt.hxx>
25#include <XmlMNaming_Array1OfShape1.hxx>
26#include <XmlMNaming_Shape1.hxx>
27
28#include <TDF_Label.hxx>
29#include <TNaming_Builder.hxx>
30#include <TNaming_NamedShape.hxx>
31#include <TNaming_Iterator.hxx>
32
33#include <TopoDS_Shape.hxx>
34#include <BRepTools.hxx>
35#include <LDOM_Text.hxx>
36#include <LDOM_OSStream.hxx>
37
38#include <Standard_SStream.hxx>
39
40static TNaming_Evolution EvolutionEnum (const XmlObjMgt_DOMString&);
41static const XmlObjMgt_DOMString& EvolutionString (const TNaming_Evolution);
42static void doTranslate (const TopoDS_Shape&,
43 XmlMNaming_Shape1&,
44 BRepTools_ShapeSet&);
45static int doTranslate (const XmlMNaming_Shape1&,
46 TopoDS_Shape&,
47 BRepTools_ShapeSet&);
48
49IMPLEMENT_DOMSTRING (OldsString, "olds")
50IMPLEMENT_DOMSTRING (NewsString, "news")
51IMPLEMENT_DOMSTRING (StatusString, "evolution")
52IMPLEMENT_DOMSTRING (VersionString, "version")
53IMPLEMENT_DOMSTRING (ShapesString, "shapes")
54
55IMPLEMENT_DOMSTRING (EvolPrimitiveString, "primitive")
56IMPLEMENT_DOMSTRING (EvolGeneratedString, "generated")
57IMPLEMENT_DOMSTRING (EvolModifyString, "modify")
58IMPLEMENT_DOMSTRING (EvolDeleteString, "delete")
59IMPLEMENT_DOMSTRING (EvolSelectedString, "selected")
60IMPLEMENT_DOMSTRING (EvolReplaceString, "replace")
61
62//=======================================================================
63//function : XmlMNaming_NamedShapeDriver
64//purpose : Constructor
65//=======================================================================
66
67XmlMNaming_NamedShapeDriver::XmlMNaming_NamedShapeDriver
68 (const Handle(CDM_MessageDriver&) theMessageDriver)
69 : XmlMDF_ADriver (theMessageDriver, NULL),
70 myShapeSet (Standard_False) // triangles mode
71{}
72
73//=======================================================================
74//function : NewEmpty()
75//purpose :
76//=======================================================================
77Handle(TDF_Attribute) XmlMNaming_NamedShapeDriver::NewEmpty () const
78{
79 return (new TNaming_NamedShape());
80}
81
82//=======================================================================
83//function : Paste()
84//purpose : retrieval of TNaming_NamedShape
85//=======================================================================
86
87Standard_Boolean XmlMNaming_NamedShapeDriver::Paste
88 (const XmlObjMgt_Persistent& theSource,
89 const Handle(TDF_Attribute)& theTarget,
90 XmlObjMgt_RRelocationTable&) const
91{
92 Handle(TNaming_NamedShape) aTarget =
93 Handle(TNaming_NamedShape)::DownCast(theTarget);
94 TDF_Label Label = aTarget -> Label();
95 TNaming_Builder aBld (Label);
96
97 // Get Version
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);
103
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);
109
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;
114
115 TopoDS_Shape anOldShape;
116 TopoDS_Shape aNewShape;
117 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
118
119 Standard_Integer lower = NewPShapes.Lower();
120 if (OldPShapes.Lower() < lower) lower = OldPShapes.Lower();
121
122 Standard_Integer upper = NewPShapes.Upper();
123 if (OldPShapes.Upper() > upper) upper = OldPShapes.Upper();
124
125 for (Standard_Integer i = lower; i <= upper; i++)
126 {
127 const XmlMNaming_Shape1 aNewPShape = NewPShapes.Value(i);
128 const XmlMNaming_Shape1 anOldPShape = OldPShapes.Value(i);
129
130 if ( evol != TNaming_PRIMITIVE && anOldPShape.Element() != NULL )
131 {
132 if (::doTranslate (anOldPShape, anOldShape, aShapeSet)) {
133 WriteMessage ("NamedShapeDriver: Error reading a shape from array");
134 return Standard_False;
135 }
136 }
137
138 if (evol != TNaming_DELETE && aNewPShape.Element() != NULL )
139 {
140 if (::doTranslate (aNewPShape, aNewShape, aShapeSet)) {
141 WriteMessage ("NamedShapeDriver: Error reading a shape from array");
142 return Standard_False;
143 }
144 }
145
146 switch (evol)
147 {
148 case TNaming_PRIMITIVE:
149 aBld.Generated(aNewShape);
150 break;
151 case TNaming_GENERATED:
152 aBld.Generated(anOldShape,aNewShape);
153 break;
154 case TNaming_MODIFY:
155 aBld.Modify(anOldShape,aNewShape);
156 break;
157 case TNaming_DELETE:
158 aBld.Delete(anOldShape);
159 break;
160 case TNaming_SELECTED:
161 aBld.Select(aNewShape, anOldShape);
162 break;
163 case TNaming_REPLACE:
164 aBld.Replace(anOldShape,aNewShape);
165 break;
166 default:
167 Standard_DomainError::Raise("TNaming_Evolution; enum term unknown");
168 }
169 anOldShape.Nullify();
170 aNewShape.Nullify();
171 }
172 return Standard_True;
173}
174
175//=======================================================================
176//function : Paste()
177//purpose : storage of TNaming_NamedShape
178//=======================================================================
179
180void XmlMNaming_NamedShapeDriver::Paste (const Handle(TDF_Attribute)& theSource,
181 XmlObjMgt_Persistent& theTarget,
182 XmlObjMgt_SRelocationTable&) const
183{
184//AGV XmlObjMgt_Document& aDoc =
185//AGV (XmlObjMgt_Document&) theTarget.Element().getOwnerDocument();
186 XmlObjMgt_Document aDoc =
187 XmlObjMgt_Document (theTarget.Element().getOwnerDocument());
188
189 Handle(TNaming_NamedShape) aNamedShape =
190 Handle(TNaming_NamedShape)::DownCast(theSource);
191 TNaming_Evolution evol = aNamedShape->Evolution();
192
193 // Create arrays
194 Standard_Integer NbShapes = 0;
195 TNaming_Iterator SItr (aNamedShape);
196 while (SItr.More()) {
197 NbShapes++;
198 SItr.Next ();
199 }
200
201 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
202 XmlMNaming_Array1OfShape1 OldPShapes (1,NbShapes), NewPShapes (1,NbShapes);
203
204 OldPShapes.CreateArrayElement (theTarget, ::OldsString());
205 NewPShapes.CreateArrayElement (theTarget, ::NewsString());
206
207 // Fill arrays
208 Standard_Integer i = 1;
209 TNaming_Iterator SIterator(aNamedShape);
210 while (SIterator.More())
211 {
212 const TopoDS_Shape& OldShape = SIterator.OldShape();
213 const TopoDS_Shape& NewShape = SIterator.NewShape();
214
215 if ( evol != TNaming_PRIMITIVE )
216 {
217 XmlMNaming_Shape1 anOldPShape (aDoc);
218 ::doTranslate (OldShape, anOldPShape, aShapeSet);
219 OldPShapes.SetValue (i, anOldPShape.Element());
220 }
221
222 if (evol != TNaming_DELETE)
223 {
224 XmlMNaming_Shape1 aNewPShape (aDoc);
225 ::doTranslate (NewShape, aNewPShape, aShapeSet);
226 NewPShapes.SetValue (i, aNewPShape.Element());
227 }
228 i++;
229 SIterator.Next();
230 }
231
232 theTarget.Element().setAttribute (::StatusString(), EvolutionString(evol));
233 Standard_Integer aVersion = aNamedShape -> Version();
234 if (aVersion != 0)
235 theTarget.Element().setAttribute (::VersionString(), aVersion);
236}
237
238//=======================================================================
239//function : EvolutionEnum
240//purpose : static
241//=======================================================================
242
243static const XmlObjMgt_DOMString& EvolutionString(const TNaming_Evolution i)
244{
245 switch(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();
252 default:
253 Standard_DomainError::Raise("TNaming_Evolution; enum term unknown");
254 }
255 static XmlObjMgt_DOMString aNullString;
256 return aNullString; // To avoid compilation error message.
257}
258
259//=======================================================================
260//function : EvolutionEnum
261//purpose : static
262//=======================================================================
263
264static TNaming_Evolution EvolutionEnum (const XmlObjMgt_DOMString& theString)
265{
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;
278 else
279 Standard_DomainError::Raise
280 ("TNaming_Evolution; string value without enum term equivalence");
281 }
282 return aResult;
283}
284
285//=======================================================================
286//function : doTranslate
287//purpose : shape storage to XML
288//=======================================================================
289
290static void doTranslate (const TopoDS_Shape& theShape,
291 XmlMNaming_Shape1& theResult,
292 BRepTools_ShapeSet& theShapeSet)
293{
294 // Check for empty shape
295 if (theShape.IsNull()) return;
296
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());
301
302 // Fill theResult with shape parameters: TShape ID, Location, Orientation
303 theResult.SetShape (aTShapeId, aLocId, theShape.Orientation());
304
305 if (theShape.ShapeType() == TopAbs_VERTEX)
306 {
307 theResult.SetVertex(theShape);
308 }
309}
310
311//=======================================================================
312//function : doTranslate
313//purpose : shape retrieval from XML
314//=======================================================================
315
316static int doTranslate (const XmlMNaming_Shape1& thePShape,
317 TopoDS_Shape& theResult,
318 BRepTools_ShapeSet& theShapeSet)
319{
320 const Standard_Integer aShapeId = thePShape.TShapeId();
321
322 // Read TShape and Orientation
323 if (aShapeId <= 0 || aShapeId > theShapeSet.NbShapes())
324 return 1;
325 theResult.TShape (theShapeSet.Shape(aShapeId).TShape());
326 theResult.Orientation (thePShape.Orientation());
327 theResult.Location (theShapeSet.Locations().Location (thePShape.LocId()));
328
329 return 0;
330}
331
332//=======================================================================
333//function : ReadShapeSection
334//purpose :
335//=======================================================================
336
337void XmlMNaming_NamedShapeDriver::ReadShapeSection
338 (const XmlObjMgt_Element& theElement)
339{
340 XmlObjMgt_Element anElement =
341 XmlObjMgt::FindChildByName (theElement, ::ShapesString());
342 if (anElement != NULL) {
343 for (LDOM_Node aNode = anElement.getFirstChild();
344 aNode != NULL;
345 aNode = anElement.getNextSibling())
346 {
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()));
352 #else
353 istrstream aStream (Standard_CString(aData.GetString()));
354 #endif
355 myShapeSet.Clear();
356 myShapeSet.Read (aStream);
357 break;
358 }
359 }
360 }
361}
362
363//=======================================================================
364//function : WriteShapeSection
365//purpose :
366//=======================================================================
367
368void XmlMNaming_NamedShapeDriver::WriteShapeSection
369 (XmlObjMgt_Element& theElement)
370{
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);
375
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);
383 aStream << ends;
384 char * aStr = (char *)aStream.str();
385 LDOM_Text aText = aDoc.createTextNode (aStr);
386 delete [] 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;
392 aShapeSet.Clear();
393 }
394}
395
396//=======================================================================
397//function : Clear
398//purpose :
399//=======================================================================
400
401void XmlMNaming_NamedShapeDriver::Clear()
402{
403 myShapeSet.Clear();
404}