b311480e |
1 | // Created on: 2008-12-10 |
2 | // Created by: Pavel TELKOV |
973c2be1 |
3 | // Copyright (c) 2008-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. |
7fd59977 |
15 | |
16 | #include <XmlMXCAFDoc_DimTolDriver.ixx> |
17 | #include <XmlObjMgt.hxx> |
18 | #include <XCAFDoc_DimTol.hxx> |
19 | #include <TCollection_HAsciiString.hxx> |
20 | #include <TColStd_HArray1OfReal.hxx> |
21 | |
22 | IMPLEMENT_DOMSTRING (NameIndexString, "name") |
23 | IMPLEMENT_DOMSTRING (DescrIndexString, "descr") |
24 | IMPLEMENT_DOMSTRING (FirstIndexString, "first") |
25 | IMPLEMENT_DOMSTRING (LastIndexString, "last") |
26 | IMPLEMENT_DOMSTRING (ValueIndexString, "values") |
27 | |
28 | //======================================================================= |
29 | //function : XmlMXCAFDoc_DimTolDriver |
30 | //purpose : Constructor |
31 | //======================================================================= |
32 | XmlMXCAFDoc_DimTolDriver::XmlMXCAFDoc_DimTolDriver |
33 | (const Handle(CDM_MessageDriver)& theMsgDriver) |
34 | : XmlMDF_ADriver (theMsgDriver, "xcaf", "DimTol") |
35 | {} |
36 | |
37 | //======================================================================= |
38 | //function : NewEmpty |
39 | //purpose : |
40 | //======================================================================= |
41 | Handle(TDF_Attribute) XmlMXCAFDoc_DimTolDriver::NewEmpty() const |
42 | { |
43 | return (new XCAFDoc_DimTol()); |
44 | } |
45 | |
46 | //======================================================================= |
47 | //function : Paste |
48 | //purpose : persistent -> transient (retrieve) |
49 | //======================================================================= |
50 | Standard_Boolean XmlMXCAFDoc_DimTolDriver::Paste |
51 | (const XmlObjMgt_Persistent& theSource, |
52 | const Handle(TDF_Attribute)& theTarget, |
53 | XmlObjMgt_RRelocationTable& ) const |
54 | { |
55 | Standard_Integer aKind; |
56 | XmlObjMgt_DOMString anIntStr = XmlObjMgt::GetStringValue(theSource); |
57 | |
58 | if (anIntStr.GetInteger(aKind) == Standard_False) { |
59 | TCollection_ExtendedString aMessageString = |
60 | TCollection_ExtendedString("Cannot retrieve DimTol attribute kind from \"") |
61 | + anIntStr + "\""; |
62 | WriteMessage (aMessageString); |
63 | return Standard_False; |
64 | } |
65 | |
66 | const XmlObjMgt_Element& anElement = theSource; |
67 | XmlObjMgt_DOMString aNameStr = anElement.getAttribute(::NameIndexString()); |
68 | XmlObjMgt_DOMString aDescrStr = anElement.getAttribute(::DescrIndexString()); |
69 | if ( aNameStr == NULL || aDescrStr == NULL ) { |
70 | TCollection_ExtendedString aMessageString |
71 | ("Cannot retrieve DimTol attribute name or description"); |
72 | WriteMessage (aMessageString); |
73 | return Standard_False; |
74 | } |
75 | |
76 | Handle(TCollection_HAsciiString) aName = |
77 | new TCollection_HAsciiString( aNameStr.GetString() ); |
78 | Handle(TCollection_HAsciiString) aDescr = |
79 | new TCollection_HAsciiString( aDescrStr.GetString() ); |
80 | |
81 | Standard_Integer aFirstInd, aLastInd; |
82 | XmlObjMgt_DOMString aFirstIndex = anElement.getAttribute(::FirstIndexString()); |
83 | if (aFirstIndex == NULL) |
84 | aFirstInd = 1; |
85 | else if (!aFirstIndex.GetInteger(aFirstInd)) { |
86 | TCollection_ExtendedString aMessageString |
87 | ("Cannot retrieve the DimTol first index for real array "); |
88 | WriteMessage (aMessageString); |
89 | return Standard_False; |
90 | } |
91 | XmlObjMgt_DOMString aLastIndex = anElement.getAttribute(::LastIndexString()); |
92 | if (aLastIndex == NULL) |
93 | aLastInd = 0; |
94 | else if (!aLastIndex.GetInteger(aLastInd)) { |
95 | TCollection_ExtendedString aMessageString |
96 | ("Cannot retrieve the DimTol last index for real array "); |
97 | WriteMessage (aMessageString); |
98 | return Standard_False; |
99 | } |
100 | |
101 | const Standard_Integer aLength = aLastInd - aFirstInd + 1; |
102 | Handle(TColStd_HArray1OfReal) aHArr; |
103 | if ( aLength > 0 ) { |
104 | // read real array |
105 | Standard_Real aValue = 0.; |
106 | const XmlObjMgt_DOMString& aString = anElement.getAttribute(::ValueIndexString()); |
107 | aHArr = new TColStd_HArray1OfReal( aFirstInd, aLastInd ); |
108 | Standard_CString aValueStr = Standard_CString(aString.GetString()); |
109 | for (Standard_Integer ind = aFirstInd; ind <= aLastInd; ind++) |
110 | { |
111 | if (!XmlObjMgt::GetReal(aValueStr, aValue)) { |
112 | TCollection_ExtendedString aMessageString = |
113 | TCollection_ExtendedString("Cannot retrieve real member" |
114 | " for real array \"") |
115 | + aValueStr + "\""; |
116 | WriteMessage (aMessageString); |
117 | return Standard_False; |
118 | } |
119 | aHArr->SetValue(ind, aValue); |
120 | } |
121 | } |
122 | |
123 | Handle(XCAFDoc_DimTol) anAtt = Handle(XCAFDoc_DimTol)::DownCast(theTarget); |
124 | anAtt->Set(aKind, aHArr, aName, aDescr); |
125 | |
126 | return Standard_True; |
127 | } |
128 | |
129 | //======================================================================= |
130 | //function : Paste |
131 | //purpose : transient -> persistent (store) |
132 | //======================================================================= |
133 | void XmlMXCAFDoc_DimTolDriver::Paste (const Handle(TDF_Attribute)& theSource, |
134 | XmlObjMgt_Persistent& theTarget, |
135 | XmlObjMgt_SRelocationTable& ) const |
136 | { |
137 | Handle(XCAFDoc_DimTol) anAtt = Handle(XCAFDoc_DimTol)::DownCast(theSource); |
138 | |
139 | XmlObjMgt_DOMString aNameString, aDescrString; |
140 | if ( !anAtt->GetName().IsNull() ) |
141 | aNameString = anAtt->GetName()->String().ToCString(); |
142 | if ( !anAtt->GetDescription().IsNull() ) |
143 | aDescrString = anAtt->GetDescription()->String().ToCString(); |
144 | |
145 | XmlObjMgt::SetStringValue (theTarget, anAtt->GetKind()); |
146 | theTarget.Element().setAttribute(::NameIndexString(), aNameString); |
147 | theTarget.Element().setAttribute(::DescrIndexString(),aDescrString); |
148 | |
149 | Handle(TColStd_HArray1OfReal) aHArr = anAtt->GetVal(); |
150 | Standard_Integer aFirstInd = 1, aLastInd = 0; |
151 | if ( !aHArr.IsNull() ) |
152 | { |
153 | aFirstInd = aHArr->Lower(); |
154 | aLastInd = aHArr->Upper(); |
155 | } |
156 | theTarget.Element().setAttribute(::FirstIndexString(), aFirstInd); |
157 | theTarget.Element().setAttribute(::LastIndexString(), aLastInd); |
158 | if ( aLastInd >= aFirstInd ) |
159 | { |
160 | TCollection_AsciiString aValueStr; |
161 | for ( Standard_Integer i = aFirstInd; i <= aLastInd; i++ ) |
162 | { |
163 | char aValueChar[256]; |
91322f44 |
164 | Sprintf(aValueChar, "%.15g", aHArr->Value(i)); |
7fd59977 |
165 | aValueStr += aValueChar; |
166 | if ( i < aLastInd ) |
167 | aValueStr += ' '; |
168 | } |
169 | theTarget.Element().setAttribute(::ValueIndexString(), aValueStr.ToCString()); |
170 | } |
171 | } |