0027232: Configuration - fix mblen missing building issue on Android
[occt.git] / src / XmlMNaming / XmlMNaming_NamedShapeDriver.cxx
CommitLineData
b311480e 1// Created on: 2001-07-11
2// Created by: Julia DOROVSKIKH
973c2be1 3// Copyright (c) 2001-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
16//AGV 150202: Changed prototype LDOM_Node::getOwnerDocument()
7fd59977 17
42cf5bc1 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>
7fd59977 25#include <TDF_Label.hxx>
26#include <TNaming_Builder.hxx>
7fd59977 27#include <TNaming_Iterator.hxx>
42cf5bc1 28#include <TNaming_NamedShape.hxx>
7fd59977 29#include <TopoDS_Shape.hxx>
42cf5bc1 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>
7fd59977 36
92efcf78 37IMPLEMENT_STANDARD_RTTIEXT(XmlMNaming_NamedShapeDriver,XmlMDF_ADriver)
38
7fd59977 39static TNaming_Evolution EvolutionEnum (const XmlObjMgt_DOMString&);
40static const XmlObjMgt_DOMString& EvolutionString (const TNaming_Evolution);
41static void doTranslate (const TopoDS_Shape&,
42 XmlMNaming_Shape1&,
43 BRepTools_ShapeSet&);
44static int doTranslate (const XmlMNaming_Shape1&,
45 TopoDS_Shape&,
46 BRepTools_ShapeSet&);
47
48IMPLEMENT_DOMSTRING (OldsString, "olds")
49IMPLEMENT_DOMSTRING (NewsString, "news")
50IMPLEMENT_DOMSTRING (StatusString, "evolution")
51IMPLEMENT_DOMSTRING (VersionString, "version")
52IMPLEMENT_DOMSTRING (ShapesString, "shapes")
53
54IMPLEMENT_DOMSTRING (EvolPrimitiveString, "primitive")
55IMPLEMENT_DOMSTRING (EvolGeneratedString, "generated")
56IMPLEMENT_DOMSTRING (EvolModifyString, "modify")
57IMPLEMENT_DOMSTRING (EvolDeleteString, "delete")
58IMPLEMENT_DOMSTRING (EvolSelectedString, "selected")
59IMPLEMENT_DOMSTRING (EvolReplaceString, "replace")
60
61//=======================================================================
62//function : XmlMNaming_NamedShapeDriver
63//purpose : Constructor
64//=======================================================================
65
66XmlMNaming_NamedShapeDriver::XmlMNaming_NamedShapeDriver
c04c30b3 67 (const Handle(CDM_MessageDriver)& theMessageDriver)
7fd59977 68 : XmlMDF_ADriver (theMessageDriver, NULL),
69 myShapeSet (Standard_False) // triangles mode
70{}
71
72//=======================================================================
73//function : NewEmpty()
74//purpose :
75//=======================================================================
76Handle(TDF_Attribute) XmlMNaming_NamedShapeDriver::NewEmpty () const
77{
78 return (new TNaming_NamedShape());
79}
80
81//=======================================================================
82//function : Paste()
83//purpose : retrieval of TNaming_NamedShape
84//=======================================================================
85
86Standard_Boolean XmlMNaming_NamedShapeDriver::Paste
87 (const XmlObjMgt_Persistent& theSource,
88 const Handle(TDF_Attribute)& theTarget,
89 XmlObjMgt_RRelocationTable&) const
90{
91 Handle(TNaming_NamedShape) aTarget =
92 Handle(TNaming_NamedShape)::DownCast(theTarget);
93 TDF_Label Label = aTarget -> Label();
94 TNaming_Builder aBld (Label);
95
96 // Get Version
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);
102
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);
108
3804f298 109 const XmlObjMgt_Array1 OldPShapes (anElement, ::OldsString());
110 const XmlObjMgt_Array1 NewPShapes (anElement, ::NewsString());
7fd59977 111 if (NewPShapes.Length() == 0 && OldPShapes.Length() == 0)
112 return Standard_True;
113
114 TopoDS_Shape anOldShape;
115 TopoDS_Shape aNewShape;
116 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
117
118 Standard_Integer lower = NewPShapes.Lower();
119 if (OldPShapes.Lower() < lower) lower = OldPShapes.Lower();
120
121 Standard_Integer upper = NewPShapes.Upper();
122 if (OldPShapes.Upper() > upper) upper = OldPShapes.Upper();
123
124 for (Standard_Integer i = lower; i <= upper; i++)
125 {
126 const XmlMNaming_Shape1 aNewPShape = NewPShapes.Value(i);
127 const XmlMNaming_Shape1 anOldPShape = OldPShapes.Value(i);
128
129 if ( evol != TNaming_PRIMITIVE && anOldPShape.Element() != NULL )
130 {
131 if (::doTranslate (anOldPShape, anOldShape, aShapeSet)) {
132 WriteMessage ("NamedShapeDriver: Error reading a shape from array");
133 return Standard_False;
134 }
135 }
136
137 if (evol != TNaming_DELETE && aNewPShape.Element() != NULL )
138 {
139 if (::doTranslate (aNewPShape, aNewShape, aShapeSet)) {
140 WriteMessage ("NamedShapeDriver: Error reading a shape from array");
141 return Standard_False;
142 }
143 }
144
145 switch (evol)
146 {
147 case TNaming_PRIMITIVE:
148 aBld.Generated(aNewShape);
149 break;
150 case TNaming_GENERATED:
151 aBld.Generated(anOldShape,aNewShape);
152 break;
153 case TNaming_MODIFY:
154 aBld.Modify(anOldShape,aNewShape);
155 break;
156 case TNaming_DELETE:
157 aBld.Delete(anOldShape);
158 break;
159 case TNaming_SELECTED:
160 aBld.Select(aNewShape, anOldShape);
161 break;
566f8441 162 case TNaming_REPLACE:
163 aBld.Modify(anOldShape,aNewShape);
164 break; // for compatibility
1ec8a59e 165 // case TNaming_REPLACE:
166 // aBld.Replace(anOldShape,aNewShape);
167 // break;
7fd59977 168 default:
169 Standard_DomainError::Raise("TNaming_Evolution; enum term unknown");
170 }
171 anOldShape.Nullify();
172 aNewShape.Nullify();
173 }
174 return Standard_True;
175}
176
177//=======================================================================
178//function : Paste()
179//purpose : storage of TNaming_NamedShape
180//=======================================================================
181
182void XmlMNaming_NamedShapeDriver::Paste (const Handle(TDF_Attribute)& theSource,
183 XmlObjMgt_Persistent& theTarget,
184 XmlObjMgt_SRelocationTable&) const
185{
186//AGV XmlObjMgt_Document& aDoc =
187//AGV (XmlObjMgt_Document&) theTarget.Element().getOwnerDocument();
188 XmlObjMgt_Document aDoc =
189 XmlObjMgt_Document (theTarget.Element().getOwnerDocument());
190
191 Handle(TNaming_NamedShape) aNamedShape =
192 Handle(TNaming_NamedShape)::DownCast(theSource);
193 TNaming_Evolution evol = aNamedShape->Evolution();
194
195 // Create arrays
196 Standard_Integer NbShapes = 0;
197 TNaming_Iterator SItr (aNamedShape);
198 while (SItr.More()) {
199 NbShapes++;
200 SItr.Next ();
201 }
202
203 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
3804f298 204 XmlObjMgt_Array1 OldPShapes (1,NbShapes), NewPShapes (1,NbShapes);
7fd59977 205
206 OldPShapes.CreateArrayElement (theTarget, ::OldsString());
207 NewPShapes.CreateArrayElement (theTarget, ::NewsString());
208
209 // Fill arrays
210 Standard_Integer i = 1;
211 TNaming_Iterator SIterator(aNamedShape);
212 while (SIterator.More())
213 {
214 const TopoDS_Shape& OldShape = SIterator.OldShape();
215 const TopoDS_Shape& NewShape = SIterator.NewShape();
216
217 if ( evol != TNaming_PRIMITIVE )
218 {
219 XmlMNaming_Shape1 anOldPShape (aDoc);
220 ::doTranslate (OldShape, anOldPShape, aShapeSet);
221 OldPShapes.SetValue (i, anOldPShape.Element());
222 }
223
224 if (evol != TNaming_DELETE)
225 {
226 XmlMNaming_Shape1 aNewPShape (aDoc);
227 ::doTranslate (NewShape, aNewPShape, aShapeSet);
228 NewPShapes.SetValue (i, aNewPShape.Element());
229 }
230 i++;
231 SIterator.Next();
232 }
233
234 theTarget.Element().setAttribute (::StatusString(), EvolutionString(evol));
235 Standard_Integer aVersion = aNamedShape -> Version();
236 if (aVersion != 0)
237 theTarget.Element().setAttribute (::VersionString(), aVersion);
238}
239
240//=======================================================================
241//function : EvolutionEnum
242//purpose : static
243//=======================================================================
244
245static const XmlObjMgt_DOMString& EvolutionString(const TNaming_Evolution i)
246{
247 switch(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();
566f8441 253 case TNaming_REPLACE : return ::EvolModifyString(); // case TNaming_REPLACE : return ::EvolReplaceString(); for compatibility
7fd59977 254 default:
255 Standard_DomainError::Raise("TNaming_Evolution; enum term unknown");
256 }
257 static XmlObjMgt_DOMString aNullString;
258 return aNullString; // To avoid compilation error message.
259}
260
261//=======================================================================
262//function : EvolutionEnum
263//purpose : static
264//=======================================================================
265
266static TNaming_Evolution EvolutionEnum (const XmlObjMgt_DOMString& theString)
267{
268 TNaming_Evolution aResult = TNaming_PRIMITIVE;
269 if (theString.equals (::EvolPrimitiveString()) == Standard_False) {
270 if (theString.equals (::EvolGeneratedString()))
271 aResult = TNaming_GENERATED;
272 else if (theString.equals (::EvolModifyString()))
273 aResult = TNaming_MODIFY;
274 else if (theString.equals (::EvolDeleteString()))
275 aResult = TNaming_DELETE;
276 else if (theString.equals (::EvolSelectedString()))
277 aResult = TNaming_SELECTED;
278 else if (theString.equals (::EvolReplaceString()))
1ec8a59e 279 aResult = TNaming_MODIFY; // for compatibility //TNaming_REPLACE;
7fd59977 280 else
281 Standard_DomainError::Raise
282 ("TNaming_Evolution; string value without enum term equivalence");
283 }
284 return aResult;
285}
286
287//=======================================================================
288//function : doTranslate
289//purpose : shape storage to XML
290//=======================================================================
291
292static void doTranslate (const TopoDS_Shape& theShape,
293 XmlMNaming_Shape1& theResult,
294 BRepTools_ShapeSet& theShapeSet)
295{
296 // Check for empty shape
297 if (theShape.IsNull()) return;
298
299 // Add to shape set both TShape and Location contained in theShape
300 const Standard_Integer aTShapeId = theShapeSet.Add (theShape);
301 const Standard_Integer aLocId =
302 theShapeSet.Locations().Index (theShape.Location());
303
304 // Fill theResult with shape parameters: TShape ID, Location, Orientation
305 theResult.SetShape (aTShapeId, aLocId, theShape.Orientation());
306
307 if (theShape.ShapeType() == TopAbs_VERTEX)
308 {
309 theResult.SetVertex(theShape);
310 }
311}
312
313//=======================================================================
314//function : doTranslate
315//purpose : shape retrieval from XML
316//=======================================================================
317
318static int doTranslate (const XmlMNaming_Shape1& thePShape,
319 TopoDS_Shape& theResult,
320 BRepTools_ShapeSet& theShapeSet)
321{
322 const Standard_Integer aShapeId = thePShape.TShapeId();
323
324 // Read TShape and Orientation
325 if (aShapeId <= 0 || aShapeId > theShapeSet.NbShapes())
326 return 1;
327 theResult.TShape (theShapeSet.Shape(aShapeId).TShape());
328 theResult.Orientation (thePShape.Orientation());
329 theResult.Location (theShapeSet.Locations().Location (thePShape.LocId()));
330
331 return 0;
332}
333
334//=======================================================================
335//function : ReadShapeSection
336//purpose :
337//=======================================================================
338
339void XmlMNaming_NamedShapeDriver::ReadShapeSection
340 (const XmlObjMgt_Element& theElement)
341{
342 XmlObjMgt_Element anElement =
343 XmlObjMgt::FindChildByName (theElement, ::ShapesString());
344 if (anElement != NULL) {
345 for (LDOM_Node aNode = anElement.getFirstChild();
346 aNode != NULL;
347 aNode = anElement.getNextSibling())
348 {
349 if (aNode.getNodeType() == LDOM_Node::TEXT_NODE) {
fd03ee4b 350 LDOMString aData = aNode.getNodeValue();
7fd59977 351 std::stringstream aStream (std::string(aData.GetString()));
7fd59977 352 myShapeSet.Clear();
353 myShapeSet.Read (aStream);
354 break;
355 }
356 }
357 }
358}
359
360//=======================================================================
361//function : WriteShapeSection
362//purpose :
363//=======================================================================
364
365void XmlMNaming_NamedShapeDriver::WriteShapeSection
366 (XmlObjMgt_Element& theElement)
367{
368 // Create "shapes" element and append it as child
369 XmlObjMgt_Document aDoc = theElement.getOwnerDocument();
370 XmlObjMgt_Element anElement = aDoc.createElement (::ShapesString());
371 theElement.appendChild (anElement);
372
373 // Add text to the "shapes" element
374 if (myShapeSet.NbShapes() > 0) {
375 myShapeSet.SetFormatNb(2);
70167e69 376 LDOM_OSStream aStream (16 * 1024);
7fd59977 377// ostrstream aStream;
378// aStream.rdbuf() -> setbuf (0, 16380);
379 myShapeSet.Write (aStream);
380 aStream << ends;
381 char * aStr = (char *)aStream.str();
382 LDOM_Text aText = aDoc.createTextNode (aStr);
383 delete [] aStr;
384 aText.SetValueClear(); // There are no characters '<' and '&' and like
385// aStream.rdbuf() -> freeze(0); // release the buffer
386 anElement.appendChild (aText);
387 // Clear the shape set to avoid appending to it on the next write
388 BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
389 aShapeSet.Clear();
390 }
391}
392
393//=======================================================================
394//function : Clear
395//purpose :
396//=======================================================================
397
398void XmlMNaming_NamedShapeDriver::Clear()
399{
400 myShapeSet.Clear();
401}