0028314: Application Framework - Display mode is not saved within XML OCAF document
[occt.git] / src / XmlMDataXtd / XmlMDataXtd_PresentationDriver.cxx
1 // Created on: 2001-09-04
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 #include <XmlMDataXtd_PresentationDriver.hxx>
17
18 #include <CDM_MessageDriver.hxx>
19 #include <Standard_Type.hxx>
20 #include <TDF_Attribute.hxx>
21 #include <XmlObjMgt.hxx>
22 #include <XmlObjMgt_Persistent.hxx>
23
24 #include <TDataXtd_Presentation.hxx>
25
26 IMPLEMENT_STANDARD_RTTIEXT(XmlMDataXtd_PresentationDriver,XmlMDF_ADriver)
27
28 IMPLEMENT_DOMSTRING (GuidString,        "guid")
29 IMPLEMENT_DOMSTRING (IsDisplayedString, "isdisplayed")
30 IMPLEMENT_DOMSTRING (ColorString,       "color")
31 IMPLEMENT_DOMSTRING (MaterialString,    "material")
32 IMPLEMENT_DOMSTRING (TransparencyString,"transparency")
33 IMPLEMENT_DOMSTRING (WidthString,       "width")
34 IMPLEMENT_DOMSTRING (ModeString,        "mode")
35
36 IMPLEMENT_DOMSTRING (DisplayedString,   "true")
37
38 //=======================================================================
39 //function : XmlMDataXtd_PresentationDriver
40 //purpose  : Constructor
41 //=======================================================================
42 XmlMDataXtd_PresentationDriver::XmlMDataXtd_PresentationDriver
43   (const Handle(CDM_MessageDriver)& theMsgDriver)
44   : XmlMDF_ADriver (theMsgDriver, NULL)
45 {}
46
47 //=======================================================================
48 //function : NewEmpty
49 //purpose  : 
50 //=======================================================================
51 Handle(TDF_Attribute) XmlMDataXtd_PresentationDriver::NewEmpty() const
52 {
53   return (new TDataXtd_Presentation());
54 }
55
56 //=======================================================================
57 //function : Paste
58 //purpose  : persistent -> transient (retrieve)
59 //=======================================================================
60 Standard_Boolean XmlMDataXtd_PresentationDriver::Paste
61   (const XmlObjMgt_Persistent&  theSource,
62   const Handle(TDF_Attribute)& theTarget,
63   XmlObjMgt_RRelocationTable&  ) const
64 {
65   TCollection_ExtendedString aMessageString;
66   XmlObjMgt_DOMString aDOMStr;
67
68   Handle(TDataXtd_Presentation) aTPrs =
69     Handle(TDataXtd_Presentation)::DownCast(theTarget);
70   const XmlObjMgt_Element& anElem = theSource;
71
72   //convert attribute value into GUID
73   aDOMStr = anElem.getAttribute(::GuidString());
74   if (aDOMStr == NULL)
75   {
76     WriteMessage("Cannot retrieve guid string from attribute");
77     return Standard_False;
78   }
79   Standard_CString aGuidStr = (Standard_CString) aDOMStr.GetString();
80   aTPrs->SetDriverGUID(aGuidStr);
81
82   // is displayed
83   aDOMStr = anElem.getAttribute(::IsDisplayedString());
84   aTPrs->SetDisplayed(aDOMStr != NULL);
85
86   Standard_Integer anIValue;
87
88   // color
89   aDOMStr = anElem.getAttribute(::ColorString());
90   if (aDOMStr != NULL)
91   {
92     if (!aDOMStr.GetInteger(anIValue))
93     {
94       aMessageString = TCollection_ExtendedString
95         ("Cannot retrieve Integer value from \"") + aDOMStr + "\"";
96       WriteMessage (aMessageString);
97       return Standard_False;
98     }
99     aTPrs->SetColor((Quantity_NameOfColor)anIValue);
100   }
101   else
102   {
103     aTPrs->UnsetColor();
104   }
105
106   // material
107   aDOMStr = anElem.getAttribute(::MaterialString());
108   if (aDOMStr != NULL)
109   {
110     if (!aDOMStr.GetInteger(anIValue))
111     {
112       aMessageString = TCollection_ExtendedString
113         ("Cannot retrieve Integer value from \"") + aDOMStr + "\"";
114       WriteMessage (aMessageString);
115       return Standard_False;
116     }
117     aTPrs->SetMaterialIndex(anIValue);
118   }
119   else
120   {
121     aTPrs->UnsetMaterial();
122   }
123
124   Standard_Real aValue;
125
126   // transparency
127   aDOMStr = anElem.getAttribute(::TransparencyString());
128   if (aDOMStr != NULL)
129   {
130     if (!XmlObjMgt::GetReal(aDOMStr, aValue))
131     {
132       aMessageString = TCollection_ExtendedString
133         ("Cannot retrieve Real value from \"") + aDOMStr + "\"";
134       WriteMessage (aMessageString);
135       return Standard_False;
136     }
137     aTPrs->SetTransparency(aValue);
138   }
139   else
140   {
141     aTPrs->UnsetTransparency();
142   }
143
144   // width
145   aDOMStr = anElem.getAttribute(::WidthString());
146   if (aDOMStr != NULL)
147   {
148     if (!XmlObjMgt::GetReal(aDOMStr, aValue))
149     {
150       aMessageString = TCollection_ExtendedString
151         ("Cannot retrieve Real value from \"") + aDOMStr + "\"";
152       WriteMessage (aMessageString);
153       return Standard_False;
154     }
155     aTPrs->SetWidth(aValue);
156   }
157   else
158   {
159     aTPrs->UnsetWidth();
160   }
161
162   // mode
163   aDOMStr = anElem.getAttribute(::ModeString());
164   if (aDOMStr != NULL)
165   {
166     if (!aDOMStr.GetInteger(anIValue))
167     {
168       aMessageString = TCollection_ExtendedString
169         ("Cannot retrieve Integer value from \"") + aDOMStr + "\"";
170       WriteMessage (aMessageString);
171       return Standard_False;
172     }
173     aTPrs->SetMode(anIValue);
174   }
175   else
176   {
177     aTPrs->UnsetMode();
178   }
179
180   return Standard_True;
181 }
182
183 //=======================================================================
184 //function : Paste
185 //purpose  : transient -> persistent (store)
186 //=======================================================================
187 void XmlMDataXtd_PresentationDriver::Paste
188                                   (const Handle(TDF_Attribute)& theSource,
189                                    XmlObjMgt_Persistent&        theTarget,
190                                    XmlObjMgt_SRelocationTable&) const
191 {
192   Handle(TDataXtd_Presentation) aTPrs =
193     Handle(TDataXtd_Presentation)::DownCast(theSource);
194   if (aTPrs.IsNull()) return;
195
196   //convert GUID into attribute value
197   Standard_Character aGuidStr [40];
198   Standard_PCharacter pGuidStr;
199   pGuidStr=aGuidStr;
200   aTPrs->GetDriverGUID().ToCString (pGuidStr);
201   theTarget.Element().setAttribute(::GuidString(), aGuidStr);
202
203   // is displayed
204   if (aTPrs->IsDisplayed())
205     theTarget.Element().setAttribute(::IsDisplayedString(), ::DisplayedString());
206
207   Standard_Integer aNb;
208
209   // color
210   if (aTPrs->HasOwnColor())
211   {
212     aNb = aTPrs->Color();
213     theTarget.Element().setAttribute(::ColorString(), aNb);
214   }
215
216   // material
217   if (aTPrs->HasOwnMaterial())
218   {
219     aNb = aTPrs->MaterialIndex();
220     theTarget.Element().setAttribute(::MaterialString(), aNb);
221   }
222
223   // transparency
224   if (aTPrs->HasOwnTransparency())
225   {
226     TCollection_AsciiString aRNbStr (aTPrs->Transparency());
227     theTarget.Element().setAttribute(::TransparencyString(), aRNbStr.ToCString());
228   }
229
230   // width
231   if (aTPrs->HasOwnWidth())
232   {
233     TCollection_AsciiString aRNbStr (aTPrs->Width());
234     theTarget.Element().setAttribute(::WidthString(), aRNbStr.ToCString());
235   }
236
237   // mode
238   if (aTPrs->HasOwnMode())
239   {
240     aNb = aTPrs->Mode();
241     theTarget.Element().setAttribute(::ModeString(), aNb);
242   }
243 }