0024180: Eliminate CLang / GCC compiler warning -Wswitch
[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;
566f8441 163 case TNaming_REPLACE:
164 aBld.Modify(anOldShape,aNewShape);
165 break; // for compatibility
1ec8a59e 166 // case TNaming_REPLACE:
167 // aBld.Replace(anOldShape,aNewShape);
168 // break;
7fd59977 169 default:
170 Standard_DomainError::Raise("TNaming_Evolution; enum term unknown");
171 }
172 anOldShape.Nullify();
173 aNewShape.Nullify();
174 }
175 return Standard_True;
176}
177
178//=======================================================================
179//function : Paste()
180//purpose : storage of TNaming_NamedShape
181//=======================================================================
182
183void XmlMNaming_NamedShapeDriver::Paste (const Handle(TDF_Attribute)& theSource,
184 XmlObjMgt_Persistent& theTarget,
185 XmlObjMgt_SRelocationTable&) const
186{
187//AGV XmlObjMgt_Document& aDoc =
188//AGV (XmlObjMgt_Document&) theTarget.Element().getOwnerDocument();
189 XmlObjMgt_Document aDoc =
190 XmlObjMgt_Document (theTarget.Element().getOwnerDocument());
191
192 Handle(TNaming_NamedShape) aNamedShape =
193 Handle(TNaming_NamedShape)::DownCast(theSource);
194 TNaming_Evolution evol = aNamedShape->Evolution();
195
196 // Create arrays
197 Standard_Integer NbShapes = 0;
198 TNaming_Iterator SItr (aNamedShape);
199 while (SItr.More()) {
200 NbShapes++;
201 SItr.Next ();
202 }
203
204 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
205 XmlMNaming_Array1OfShape1 OldPShapes (1,NbShapes), NewPShapes (1,NbShapes);
206
207 OldPShapes.CreateArrayElement (theTarget, ::OldsString());
208 NewPShapes.CreateArrayElement (theTarget, ::NewsString());
209
210 // Fill arrays
211 Standard_Integer i = 1;
212 TNaming_Iterator SIterator(aNamedShape);
213 while (SIterator.More())
214 {
215 const TopoDS_Shape& OldShape = SIterator.OldShape();
216 const TopoDS_Shape& NewShape = SIterator.NewShape();
217
218 if ( evol != TNaming_PRIMITIVE )
219 {
220 XmlMNaming_Shape1 anOldPShape (aDoc);
221 ::doTranslate (OldShape, anOldPShape, aShapeSet);
222 OldPShapes.SetValue (i, anOldPShape.Element());
223 }
224
225 if (evol != TNaming_DELETE)
226 {
227 XmlMNaming_Shape1 aNewPShape (aDoc);
228 ::doTranslate (NewShape, aNewPShape, aShapeSet);
229 NewPShapes.SetValue (i, aNewPShape.Element());
230 }
231 i++;
232 SIterator.Next();
233 }
234
235 theTarget.Element().setAttribute (::StatusString(), EvolutionString(evol));
236 Standard_Integer aVersion = aNamedShape -> Version();
237 if (aVersion != 0)
238 theTarget.Element().setAttribute (::VersionString(), aVersion);
239}
240
241//=======================================================================
242//function : EvolutionEnum
243//purpose : static
244//=======================================================================
245
246static const XmlObjMgt_DOMString& EvolutionString(const TNaming_Evolution i)
247{
248 switch(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();
566f8441 254 case TNaming_REPLACE : return ::EvolModifyString(); // case TNaming_REPLACE : return ::EvolReplaceString(); for compatibility
7fd59977 255 default:
256 Standard_DomainError::Raise("TNaming_Evolution; enum term unknown");
257 }
258 static XmlObjMgt_DOMString aNullString;
259 return aNullString; // To avoid compilation error message.
260}
261
262//=======================================================================
263//function : EvolutionEnum
264//purpose : static
265//=======================================================================
266
267static TNaming_Evolution EvolutionEnum (const XmlObjMgt_DOMString& theString)
268{
269 TNaming_Evolution aResult = TNaming_PRIMITIVE;
270 if (theString.equals (::EvolPrimitiveString()) == Standard_False) {
271 if (theString.equals (::EvolGeneratedString()))
272 aResult = TNaming_GENERATED;
273 else if (theString.equals (::EvolModifyString()))
274 aResult = TNaming_MODIFY;
275 else if (theString.equals (::EvolDeleteString()))
276 aResult = TNaming_DELETE;
277 else if (theString.equals (::EvolSelectedString()))
278 aResult = TNaming_SELECTED;
279 else if (theString.equals (::EvolReplaceString()))
1ec8a59e 280 aResult = TNaming_MODIFY; // for compatibility //TNaming_REPLACE;
7fd59977 281 else
282 Standard_DomainError::Raise
283 ("TNaming_Evolution; string value without enum term equivalence");
284 }
285 return aResult;
286}
287
288//=======================================================================
289//function : doTranslate
290//purpose : shape storage to XML
291//=======================================================================
292
293static void doTranslate (const TopoDS_Shape& theShape,
294 XmlMNaming_Shape1& theResult,
295 BRepTools_ShapeSet& theShapeSet)
296{
297 // Check for empty shape
298 if (theShape.IsNull()) return;
299
300 // Add to shape set both TShape and Location contained in theShape
301 const Standard_Integer aTShapeId = theShapeSet.Add (theShape);
302 const Standard_Integer aLocId =
303 theShapeSet.Locations().Index (theShape.Location());
304
305 // Fill theResult with shape parameters: TShape ID, Location, Orientation
306 theResult.SetShape (aTShapeId, aLocId, theShape.Orientation());
307
308 if (theShape.ShapeType() == TopAbs_VERTEX)
309 {
310 theResult.SetVertex(theShape);
311 }
312}
313
314//=======================================================================
315//function : doTranslate
316//purpose : shape retrieval from XML
317//=======================================================================
318
319static int doTranslate (const XmlMNaming_Shape1& thePShape,
320 TopoDS_Shape& theResult,
321 BRepTools_ShapeSet& theShapeSet)
322{
323 const Standard_Integer aShapeId = thePShape.TShapeId();
324
325 // Read TShape and Orientation
326 if (aShapeId <= 0 || aShapeId > theShapeSet.NbShapes())
327 return 1;
328 theResult.TShape (theShapeSet.Shape(aShapeId).TShape());
329 theResult.Orientation (thePShape.Orientation());
330 theResult.Location (theShapeSet.Locations().Location (thePShape.LocId()));
331
332 return 0;
333}
334
335//=======================================================================
336//function : ReadShapeSection
337//purpose :
338//=======================================================================
339
340void XmlMNaming_NamedShapeDriver::ReadShapeSection
341 (const XmlObjMgt_Element& theElement)
342{
343 XmlObjMgt_Element anElement =
344 XmlObjMgt::FindChildByName (theElement, ::ShapesString());
345 if (anElement != NULL) {
346 for (LDOM_Node aNode = anElement.getFirstChild();
347 aNode != NULL;
348 aNode = anElement.getNextSibling())
349 {
350 if (aNode.getNodeType() == LDOM_Node::TEXT_NODE) {
351 LDOM_Text aText = (LDOM_Text&) aNode;
352 LDOMString aData = aText.getData();
353 #ifdef USE_STL_STREAM
354 std::stringstream aStream (std::string(aData.GetString()));
355 #else
356 istrstream aStream (Standard_CString(aData.GetString()));
357 #endif
358 myShapeSet.Clear();
359 myShapeSet.Read (aStream);
360 break;
361 }
362 }
363 }
364}
365
366//=======================================================================
367//function : WriteShapeSection
368//purpose :
369//=======================================================================
370
371void XmlMNaming_NamedShapeDriver::WriteShapeSection
372 (XmlObjMgt_Element& theElement)
373{
374 // Create "shapes" element and append it as child
375 XmlObjMgt_Document aDoc = theElement.getOwnerDocument();
376 XmlObjMgt_Element anElement = aDoc.createElement (::ShapesString());
377 theElement.appendChild (anElement);
378
379 // Add text to the "shapes" element
380 if (myShapeSet.NbShapes() > 0) {
381 myShapeSet.SetFormatNb(2);
382 LDOM_OSStream aStream (1024);
383// ostrstream aStream;
384// aStream.rdbuf() -> setbuf (0, 16380);
385 myShapeSet.Write (aStream);
386 aStream << ends;
387 char * aStr = (char *)aStream.str();
388 LDOM_Text aText = aDoc.createTextNode (aStr);
389 delete [] aStr;
390 aText.SetValueClear(); // There are no characters '<' and '&' and like
391// aStream.rdbuf() -> freeze(0); // release the buffer
392 anElement.appendChild (aText);
393 // Clear the shape set to avoid appending to it on the next write
394 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
395 aShapeSet.Clear();
396 }
397}
398
399//=======================================================================
400//function : Clear
401//purpose :
402//=======================================================================
403
404void XmlMNaming_NamedShapeDriver::Clear()
405{
406 myShapeSet.Clear();
407}