summaryrefslogtreecommitdiff
path: root/src/XmlMNaming/XmlMNaming_NamedShapeDriver.cxx
blob: 6df804b7d047009162bd99eafe0f0bf32d38af8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// File:      XmlMNaming_NamedShapeDriver.cxx
// Created:   11.07.01 12:20:33
// Author:    Julia DOROVSKIKH
// Copyright: Matra Datavision 2001,02
// History:   AGV 150202: Changed prototype LDOM_Node::getOwnerDocument()

#include <XmlMNaming_NamedShapeDriver.ixx>

#include <XmlObjMgt.hxx>
#include <XmlMNaming_Array1OfShape1.hxx>
#include <XmlMNaming_Shape1.hxx>

#include <TDF_Label.hxx>
#include <TNaming_Builder.hxx>
#include <TNaming_NamedShape.hxx>
#include <TNaming_Iterator.hxx>

#include <TopoDS_Shape.hxx>
#include <BRepTools.hxx>
#include <LDOM_Text.hxx>
#include <LDOM_OSStream.hxx>

#include <Standard_SStream.hxx>

static TNaming_Evolution          EvolutionEnum   (const XmlObjMgt_DOMString&);
static const XmlObjMgt_DOMString& EvolutionString (const TNaming_Evolution);
static void                       doTranslate     (const TopoDS_Shape&,
                                                   XmlMNaming_Shape1&,
                                                   BRepTools_ShapeSet&);
static int                        doTranslate     (const XmlMNaming_Shape1&,
                                                   TopoDS_Shape&,
                                                   BRepTools_ShapeSet&);

IMPLEMENT_DOMSTRING (OldsString,    "olds")
IMPLEMENT_DOMSTRING (NewsString,    "news")
IMPLEMENT_DOMSTRING (StatusString,  "evolution")
IMPLEMENT_DOMSTRING (VersionString, "version")
IMPLEMENT_DOMSTRING (ShapesString,  "shapes")

IMPLEMENT_DOMSTRING (EvolPrimitiveString, "primitive")
IMPLEMENT_DOMSTRING (EvolGeneratedString, "generated")
IMPLEMENT_DOMSTRING (EvolModifyString,    "modify")
IMPLEMENT_DOMSTRING (EvolDeleteString,    "delete")
IMPLEMENT_DOMSTRING (EvolSelectedString,  "selected")
IMPLEMENT_DOMSTRING (EvolReplaceString,   "replace")

//=======================================================================
//function : XmlMNaming_NamedShapeDriver
//purpose  : Constructor
//=======================================================================

XmlMNaming_NamedShapeDriver::XmlMNaming_NamedShapeDriver
                        (const Handle(CDM_MessageDriver&) theMessageDriver)
     : XmlMDF_ADriver (theMessageDriver, NULL),
  myShapeSet (Standard_False) // triangles mode
{}

//=======================================================================
//function : NewEmpty()
//purpose  : 
//=======================================================================
Handle(TDF_Attribute) XmlMNaming_NamedShapeDriver::NewEmpty () const
{
  return (new TNaming_NamedShape());
}

//=======================================================================
//function : Paste()
//purpose  : retrieval of TNaming_NamedShape
//=======================================================================

Standard_Boolean XmlMNaming_NamedShapeDriver::Paste
                                       (const XmlObjMgt_Persistent&  theSource,
                                        const Handle(TDF_Attribute)& theTarget,
                                        XmlObjMgt_RRelocationTable&) const
{
  Handle(TNaming_NamedShape) aTarget =
    Handle(TNaming_NamedShape)::DownCast(theTarget);
  TDF_Label Label = aTarget -> Label();
  TNaming_Builder aBld (Label);

  //    Get Version
  Standard_Integer    aVersion = 0;
  const XmlObjMgt_Element& anElement = theSource;
  XmlObjMgt_DOMString aVerString = anElement.getAttribute (::VersionString());
  if (aVerString != NULL)
    aVerString.GetInteger (aVersion);

  //    Get Evolution status
  XmlObjMgt_DOMString aStatus = anElement.getAttribute (::StatusString());
  TNaming_Evolution evol = EvolutionEnum (aStatus);
  // apres creation Builder qui a mis la version a 1 :
  aTarget -> SetVersion (aVersion);

  const XmlMNaming_Array1OfShape1 OldPShapes (anElement, ::OldsString());
  const XmlMNaming_Array1OfShape1 NewPShapes (anElement, ::NewsString());
  if (NewPShapes.Length() == 0 && OldPShapes.Length() == 0)
    return Standard_True;

  TopoDS_Shape anOldShape;
  TopoDS_Shape aNewShape;
  BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;

  Standard_Integer lower = NewPShapes.Lower();
  if (OldPShapes.Lower() < lower) lower = OldPShapes.Lower();

  Standard_Integer upper = NewPShapes.Upper();
  if (OldPShapes.Upper() > upper) upper = OldPShapes.Upper();

  for (Standard_Integer i = lower; i <= upper; i++)
  {
    const XmlMNaming_Shape1 aNewPShape  = NewPShapes.Value(i);
    const XmlMNaming_Shape1 anOldPShape = OldPShapes.Value(i);

    if ( evol != TNaming_PRIMITIVE && anOldPShape.Element() != NULL )
    {
      if (::doTranslate (anOldPShape, anOldShape, aShapeSet)) {
        WriteMessage ("NamedShapeDriver: Error reading a shape from array");
        return Standard_False;
      }
    }

    if (evol != TNaming_DELETE && aNewPShape.Element() != NULL )
    {
      if (::doTranslate (aNewPShape, aNewShape, aShapeSet)) {
        WriteMessage ("NamedShapeDriver: Error reading a shape from array");
        return Standard_False;
      }
    }

    switch (evol)
    {
    case TNaming_PRIMITIVE:
      aBld.Generated(aNewShape);
      break;
    case TNaming_GENERATED:
      aBld.Generated(anOldShape,aNewShape);
      break;
    case TNaming_MODIFY:
      aBld.Modify(anOldShape,aNewShape);
      break;
    case TNaming_DELETE:
      aBld.Delete(anOldShape);
      break;
    case TNaming_SELECTED:
      aBld.Select(aNewShape, anOldShape);
      break;
    case TNaming_REPLACE:
      aBld.Replace(anOldShape,aNewShape);
      break;
    default:
      Standard_DomainError::Raise("TNaming_Evolution; enum term unknown");
    }
    anOldShape.Nullify();
    aNewShape.Nullify();
  }
  return Standard_True;
}

//=======================================================================
//function : Paste()
//purpose  : storage of TNaming_NamedShape
//=======================================================================

void XmlMNaming_NamedShapeDriver::Paste (const Handle(TDF_Attribute)& theSource,
                                         XmlObjMgt_Persistent&        theTarget,
                                         XmlObjMgt_SRelocationTable&) const
{
//AGV  XmlObjMgt_Document& aDoc =
//AGV    (XmlObjMgt_Document&) theTarget.Element().getOwnerDocument();
  XmlObjMgt_Document aDoc =
    XmlObjMgt_Document (theTarget.Element().getOwnerDocument());

  Handle(TNaming_NamedShape) aNamedShape =
    Handle(TNaming_NamedShape)::DownCast(theSource);
  TNaming_Evolution evol = aNamedShape->Evolution();

  //    Create arrays
  Standard_Integer NbShapes = 0;
  TNaming_Iterator SItr (aNamedShape);
  while (SItr.More()) {
    NbShapes++;
    SItr.Next ();
  }

  BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
  XmlMNaming_Array1OfShape1 OldPShapes (1,NbShapes), NewPShapes (1,NbShapes);

  OldPShapes.CreateArrayElement (theTarget, ::OldsString());
  NewPShapes.CreateArrayElement (theTarget, ::NewsString());

  //    Fill arrays
  Standard_Integer i = 1;
  TNaming_Iterator SIterator(aNamedShape);
  while (SIterator.More())
  {
    const TopoDS_Shape& OldShape = SIterator.OldShape();
    const TopoDS_Shape& NewShape = SIterator.NewShape();

    if ( evol != TNaming_PRIMITIVE )
    {
      XmlMNaming_Shape1 anOldPShape (aDoc);
      ::doTranslate (OldShape, anOldPShape, aShapeSet);
      OldPShapes.SetValue (i, anOldPShape.Element());
    }

    if (evol != TNaming_DELETE)
    {
      XmlMNaming_Shape1 aNewPShape (aDoc);
      ::doTranslate (NewShape, aNewPShape, aShapeSet);
      NewPShapes.SetValue (i, aNewPShape.Element());
    }
    i++;
    SIterator.Next();
  }

  theTarget.Element().setAttribute (::StatusString(), EvolutionString(evol));
  Standard_Integer aVersion = aNamedShape -> Version();
  if (aVersion != 0)
    theTarget.Element().setAttribute (::VersionString(), aVersion);
}

//=======================================================================
//function : EvolutionEnum
//purpose  : static
//=======================================================================

static const XmlObjMgt_DOMString& EvolutionString(const TNaming_Evolution i)
{
  switch(i) {
    case TNaming_PRIMITIVE    : return ::EvolPrimitiveString();
    case TNaming_GENERATED    : return ::EvolGeneratedString();
    case TNaming_MODIFY       : return ::EvolModifyString();
    case TNaming_DELETE       : return ::EvolDeleteString();
    case TNaming_SELECTED     : return ::EvolSelectedString();
    case TNaming_REPLACE      : return ::EvolReplaceString();
  default:
    Standard_DomainError::Raise("TNaming_Evolution; enum term unknown");
  }
  static XmlObjMgt_DOMString aNullString;
  return aNullString; // To avoid compilation error message.
}

//=======================================================================
//function : EvolutionEnum
//purpose  : static
//=======================================================================

static TNaming_Evolution EvolutionEnum (const XmlObjMgt_DOMString& theString)
{
  TNaming_Evolution aResult = TNaming_PRIMITIVE;
  if (theString.equals (::EvolPrimitiveString()) == Standard_False) {
    if (theString.equals (::EvolGeneratedString()))
      aResult = TNaming_GENERATED;
    else if (theString.equals (::EvolModifyString()))
      aResult = TNaming_MODIFY;
    else if (theString.equals (::EvolDeleteString()))
      aResult = TNaming_DELETE;
    else if (theString.equals (::EvolSelectedString()))
      aResult = TNaming_SELECTED;
    else if (theString.equals (::EvolReplaceString()))
      aResult = TNaming_REPLACE;
    else
      Standard_DomainError::Raise
        ("TNaming_Evolution; string value without enum term equivalence");
  }
  return aResult;
}

//=======================================================================
//function : doTranslate
//purpose  : shape storage to XML
//=======================================================================

static void doTranslate (const TopoDS_Shape&            theShape,
                         XmlMNaming_Shape1&             theResult,
                         BRepTools_ShapeSet&            theShapeSet)
{
  // Check for empty shape
  if (theShape.IsNull()) return;

  // Add to shape set both TShape and Location contained in theShape
  const Standard_Integer aTShapeId = theShapeSet.Add (theShape);
  const Standard_Integer aLocId =
    theShapeSet.Locations().Index (theShape.Location());

  // Fill theResult with shape parameters: TShape ID, Location, Orientation
  theResult.SetShape (aTShapeId, aLocId, theShape.Orientation());

  if (theShape.ShapeType() == TopAbs_VERTEX)
  {
    theResult.SetVertex(theShape);
  }
}

//=======================================================================
//function : doTranslate
//purpose  : shape retrieval from XML
//=======================================================================

static int doTranslate  (const XmlMNaming_Shape1&       thePShape,
                         TopoDS_Shape&                  theResult,
                         BRepTools_ShapeSet&            theShapeSet)
{
  const Standard_Integer aShapeId = thePShape.TShapeId();

  // Read TShape and Orientation
  if (aShapeId <= 0 || aShapeId > theShapeSet.NbShapes())
    return 1;
  theResult.TShape      (theShapeSet.Shape(aShapeId).TShape());
  theResult.Orientation (thePShape.Orientation());
  theResult.Location    (theShapeSet.Locations().Location (thePShape.LocId()));

  return 0;
}

//=======================================================================
//function : ReadShapeSection
//purpose  : 
//=======================================================================

void XmlMNaming_NamedShapeDriver::ReadShapeSection
                                        (const XmlObjMgt_Element& theElement)
{
  XmlObjMgt_Element anElement =
    XmlObjMgt::FindChildByName (theElement, ::ShapesString());
  if (anElement != NULL) {
    for (LDOM_Node aNode = anElement.getFirstChild();
         aNode != NULL;
         aNode = anElement.getNextSibling())
    {
      if (aNode.getNodeType() == LDOM_Node::TEXT_NODE) {
        LDOM_Text aText = (LDOM_Text&) aNode;
        LDOMString aData = aText.getData();
	#ifdef USE_STL_STREAM
        std::stringstream aStream (std::string(aData.GetString()));
	#else
        istrstream aStream (Standard_CString(aData.GetString()));
	#endif
        myShapeSet.Clear();
        myShapeSet.Read (aStream);
        break;
      }
    }
  }
}

//=======================================================================
//function : WriteShapeSection
//purpose  : 
//=======================================================================

void XmlMNaming_NamedShapeDriver::WriteShapeSection
                                        (XmlObjMgt_Element& theElement)
{
  //  Create "shapes" element and append it as child
  XmlObjMgt_Document aDoc     = theElement.getOwnerDocument();
  XmlObjMgt_Element anElement = aDoc.createElement (::ShapesString());
  theElement.appendChild (anElement);

  //  Add text to the "shapes" element
  if (myShapeSet.NbShapes() > 0) {
    myShapeSet.SetFormatNb(2);
    LDOM_OSStream aStream (1024);
//    ostrstream aStream;
//    aStream.rdbuf() -> setbuf (0, 16380);
    myShapeSet.Write (aStream);
    aStream << ends;
    char * aStr = (char *)aStream.str();
    LDOM_Text aText = aDoc.createTextNode (aStr);
    delete [] aStr;
    aText.SetValueClear();      // There are no characters '<' and '&' and like
//    aStream.rdbuf() -> freeze(0);                     // release the buffer
    anElement.appendChild (aText);
  // Clear the shape set to avoid appending to it on the next write
    BRepTools_ShapeSet& aShapeSet = (BRepTools_ShapeSet&) myShapeSet;
    aShapeSet.Clear();
  }
}

//=======================================================================
//function : Clear
//purpose  : 
//=======================================================================

void XmlMNaming_NamedShapeDriver::Clear()
{
  myShapeSet.Clear();
}