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