0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / XmlMDataStd / XmlMDataStd_RelationDriver.cxx
1 // Created on: 2001-09-12
2 // Created by: Julia DOROVSKIKH
3 // Copyright (c) 2001-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <Message_Messenger.hxx>
18 #include <Standard_Type.hxx>
19 #include <TDataStd_Relation.hxx>
20 #include <TDataStd_Variable.hxx>
21 #include <TDF_Attribute.hxx>
22 #include <TDF_ListIteratorOfAttributeList.hxx>
23 #include <XmlMDataStd_RelationDriver.hxx>
24 #include <XmlObjMgt.hxx>
25 #include <XmlObjMgt_Persistent.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataStd_RelationDriver,XmlMDF_ADriver)
28 IMPLEMENT_DOMSTRING (VariablesString, "variables")
29
30 //=======================================================================
31 //function : XmlMDataStd_RelationDriver
32 //purpose  : Constructor
33 //=======================================================================
34 XmlMDataStd_RelationDriver::XmlMDataStd_RelationDriver
35                         (const Handle(Message_Messenger)& theMsgDriver)
36       : XmlMDF_ADriver (theMsgDriver, NULL)
37 {}
38
39 //=======================================================================
40 //function : NewEmpty
41 //purpose  : 
42 //=======================================================================
43 Handle(TDF_Attribute) XmlMDataStd_RelationDriver::NewEmpty() const
44 {
45   return (new TDataStd_Relation());
46 }
47
48 //=======================================================================
49 //function : Paste
50 //purpose  : persistent -> transient (retrieve)
51 //=======================================================================
52 Standard_Boolean XmlMDataStd_RelationDriver::Paste
53                         (const XmlObjMgt_Persistent&  theSource,
54                          const Handle(TDF_Attribute)& theTarget,
55                          XmlObjMgt_RRelocationTable&  theRelocTable) const
56 {
57   Handle(TDataStd_Relation) aC = 
58     Handle(TDataStd_Relation)::DownCast(theTarget);
59   const XmlObjMgt_Element& anElem = theSource;
60
61   Standard_Integer aNb;
62   TCollection_ExtendedString aMsgString;
63
64   // expression
65   TCollection_ExtendedString aString;
66   if (!XmlObjMgt::GetExtendedString (theSource, aString))
67   {
68     myMessageDriver->Send("error retrieving ExtendedString for type TDataStd_Relation", Message_Fail);
69     return Standard_False;
70   }
71   aC->SetRelation(aString);
72
73   // variables
74   XmlObjMgt_DOMString aDOMStr = anElem.getAttribute(::VariablesString());
75   if (aDOMStr != NULL)
76   {
77     Standard_CString aVs = Standard_CString(aDOMStr.GetString());
78
79     // first variable
80     if (!XmlObjMgt::GetInteger(aVs, aNb))
81     {
82       aMsgString = TCollection_ExtendedString
83         ("XmlMDataStd_RelationDriver: Cannot retrieve reference on first variable from \"")
84           + aDOMStr + "\"";
85       myMessageDriver->Send (aMsgString, Message_Fail);
86       return Standard_False;
87     }
88     Standard_Integer i = 1;
89     while (aNb > 0)
90     {
91       Handle(TDF_Attribute) aV;
92       if (theRelocTable.IsBound(aNb))
93         aV = Handle(TDataStd_Variable)::DownCast(theRelocTable.Find(aNb));
94       else
95       {
96         aV = new TDataStd_Variable;
97         theRelocTable.Bind(aNb, aV);
98       }
99       aC->GetVariables().Append(aV);
100
101       // next variable
102       if (!XmlObjMgt::GetInteger(aVs, aNb)) aNb = 0;
103       i++;
104     }
105   }
106
107   return Standard_True;
108 }
109
110 //=======================================================================
111 //function : Paste
112 //purpose  : transient -> persistent (store)
113 //=======================================================================
114 void XmlMDataStd_RelationDriver::Paste
115                         (const Handle(TDF_Attribute)& theSource,
116                          XmlObjMgt_Persistent&        theTarget,
117                          XmlObjMgt_SRelocationTable&  theRelocTable) const
118 {
119   Handle(TDataStd_Relation) aC =
120     Handle(TDataStd_Relation)::DownCast(theSource);
121   XmlObjMgt_Element& anElem = theTarget;
122
123   Standard_Integer aNb;
124   Handle(TDF_Attribute) TV;   
125
126   // expression
127   XmlObjMgt::SetExtendedString (theTarget, aC->Name());
128
129   // variables
130   Standard_Integer nbvar = aC->GetVariables().Extent();
131   if (nbvar >= 1)
132   {
133     TCollection_AsciiString aGsStr;
134     TDF_ListIteratorOfAttributeList it;
135     Standard_Integer index = 0;
136     for (it.Initialize(aC->GetVariables()); it.More(); it.Next())
137     {
138       index++;
139       TV = it.Value(); 
140       if (!TV.IsNull())
141       {
142         aNb = theRelocTable.FindIndex(TV);
143         if (aNb == 0)
144         {
145           aNb = theRelocTable.Add(TV);
146         }
147         aGsStr += TCollection_AsciiString(aNb) + " ";
148       }
149       else aGsStr += "0 ";
150     }
151     anElem.setAttribute(::VariablesString(), aGsStr.ToCString());
152   }
153 }