0025266: Debug statements in the source are getting flushed on to the console
[occt.git] / src / XmlMXCAFDoc / XmlMXCAFDoc_LocationDriver.cxx
1 // Created on: 2001-09-11
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 <XmlMXCAFDoc_LocationDriver.ixx>
17
18 #include <XCAFDoc_Location.hxx>
19 #include <XmlObjMgt_Document.hxx>
20 #include <XmlObjMgt_DOMString.hxx>
21 #include <XmlObjMgt_Persistent.hxx>
22 #include <XmlObjMgt.hxx>
23 #include <XmlObjMgt_GP.hxx>
24 #include <TopLoc_Datum3D.hxx>
25
26 #include <XmlMNaming.hxx>
27 #include <TopTools_LocationSet.hxx>
28
29 IMPLEMENT_DOMSTRING (DatumString,    "datum")
30 IMPLEMENT_DOMSTRING (LocationString, "location")
31 IMPLEMENT_DOMSTRING (PowerString,    "power")
32 IMPLEMENT_DOMSTRING (TrsfString,     "trsf")
33 IMPLEMENT_DOMSTRING (LocIdString,    "locId")
34
35 //=======================================================================
36 //function : XmlMXCAFDoc_LocationDriver
37 //purpose  : Constructor
38 //=======================================================================
39 XmlMXCAFDoc_LocationDriver::XmlMXCAFDoc_LocationDriver
40                         (const Handle(CDM_MessageDriver)& theMsgDriver)
41       : XmlMDF_ADriver (theMsgDriver, "xcaf", "Location")
42       , myLocations(0)
43 {}
44
45 //=======================================================================
46 //function : NewEmpty
47 //purpose  : 
48 //=======================================================================
49 Handle(TDF_Attribute) XmlMXCAFDoc_LocationDriver::NewEmpty() const
50 {
51   return (new XCAFDoc_Location());
52 }
53
54 //=======================================================================
55 //function : Paste
56 //purpose  : 
57 //=======================================================================
58 Standard_Boolean XmlMXCAFDoc_LocationDriver::Paste
59                               (const XmlObjMgt_Persistent&  theSource,
60                                const Handle(TDF_Attribute)& theTarget,
61                                XmlObjMgt_RRelocationTable&  theRelocTable) const
62 {
63   TopLoc_Location aLoc;
64   Translate (theSource.Element(), aLoc, theRelocTable);
65
66   Handle(XCAFDoc_Location) aT = Handle(XCAFDoc_Location)::DownCast (theTarget);
67   aT->Set(aLoc);
68
69   return Standard_True;
70 }
71
72 //=======================================================================
73 //function : Paste
74 //purpose  : 
75 //=======================================================================
76 void XmlMXCAFDoc_LocationDriver::Paste
77                               (const Handle(TDF_Attribute)& theSource,
78                                XmlObjMgt_Persistent&        theTarget,
79                                XmlObjMgt_SRelocationTable&  theRelocTable) const
80 {
81   Handle(XCAFDoc_Location) aS = Handle(XCAFDoc_Location)::DownCast(theSource);
82   XmlObjMgt_Element anElem = theTarget.Element();
83   Translate (aS->Get(), anElem, theRelocTable);
84 }
85
86 //=======================================================================
87 //function : Translate
88 //purpose  : .. from Transient to Persistent
89 //=======================================================================
90 void XmlMXCAFDoc_LocationDriver::Translate (const TopLoc_Location&      theLoc,
91                                      XmlObjMgt_Element&          theParent,
92                                      XmlObjMgt_SRelocationTable& theMap) const
93 {
94   if (theLoc.IsIdentity())
95   {
96     return;
97   }
98   
99   // The location is not identity  
100   if( myLocations == 0 )
101   {
102 #ifdef XMLMXCAFDOC_DEB
103     cout<<"Pointer to LocationSet is NULL\n";
104 #endif
105     return;
106   }
107   
108   //  Create Location element
109   XmlObjMgt_Document aDoc = theParent.getOwnerDocument();
110   XmlObjMgt_Element aLocElem = aDoc.createElement(::LocationString());
111   
112   Standard_Integer anId = myLocations->Add(theLoc);
113   
114   aLocElem.setAttribute (::LocIdString(), anId);
115   theParent.appendChild (aLocElem);
116
117   
118   // In earlier version of this driver a datums from location stored in 
119   // the relocation table, but now it's not necessary
120   // (try to uncomment it if some problems appear)
121   /*
122   Handle(TopLoc_Datum3D) aDatum = theLoc.FirstDatum();
123   
124   if(!theMap.Contains(aDatum)) {
125     theMap.Add(aDatum);
126   }
127   */
128
129   //  Add next Location from the list
130   Translate (theLoc.NextLocation(), aLocElem, theMap);
131 }
132
133 //=======================================================================
134 //function : Translate
135 //purpose  : .. from Persistent to Transient
136 //=======================================================================
137 Standard_Boolean XmlMXCAFDoc_LocationDriver::Translate
138                                         (const XmlObjMgt_Element&    theParent,
139                                          TopLoc_Location&            theLoc,
140                                          XmlObjMgt_RRelocationTable& theMap) const
141 {
142   XmlObjMgt_Element aLocElem =
143     XmlObjMgt::FindChildByName (theParent, ::LocationString());
144   if (aLocElem == NULL)
145     return Standard_False;
146   
147   Standard_Integer aFileVer = XmlMNaming::DocumentVersion();
148   if( aFileVer > 5 && myLocations == 0 )
149   {
150     return Standard_False;
151   }
152   
153   Standard_Integer aPower;
154   Handle(TopLoc_Datum3D) aDatum;
155   
156   if( aFileVer > 5 )
157   {
158     //  Get Location ID
159     Standard_Integer anId;
160     aLocElem.getAttribute (::LocIdString()).GetInteger (anId);
161     
162     const TopLoc_Location& aLoc = myLocations->Location(anId);
163     aPower = aLoc.FirstPower();
164     aDatum = aLoc.FirstDatum();
165   } else
166   {
167     //  Get Power
168     aLocElem.getAttribute (::PowerString()).GetInteger (aPower);
169
170     //  get datum
171     XmlObjMgt_Persistent aPD (aLocElem, ::DatumString());
172     if (aPD.Id() <= 0) {
173       Standard_Integer aDatumID;
174       aLocElem.getAttribute (::DatumString()).GetInteger (aDatumID);
175       if (aDatumID > 0 && theMap.IsBound (aDatumID))
176         aDatum = (Handle(TopLoc_Datum3D)&) theMap.Find (aDatumID);
177       else
178         return Standard_False;
179     }else{
180       gp_Trsf aTrsf;
181       XmlObjMgt_GP::Translate (aPD.Element().getAttribute(::TrsfString()), aTrsf);
182       aDatum = new TopLoc_Datum3D (aTrsf);
183       theMap.Bind (aPD.Id(), aDatum);
184     }
185   }
186   
187   //  Get Next Location
188   TopLoc_Location aNextLoc;
189   Translate (aLocElem, aNextLoc, theMap);
190   
191   //  Calculate the result
192   theLoc = aNextLoc * TopLoc_Location (aDatum).Powered (aPower);
193   return Standard_True;
194 }