0030686: Visualization, SelectMgr_ViewerSelector - sorting issues of transformation...
[occt.git] / src / BinMXCAFDoc / BinMXCAFDoc_LocationDriver.cxx
1 // Created on: 2005-05-17
2 // Created by: Eugeny NAPALKOV
3 // Copyright (c) 2005-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 <BinMDataStd.hxx>
18 #include <BinMXCAFDoc_LocationDriver.hxx>
19 #include <BinObjMgt_Persistent.hxx>
20 #include <BinTools_LocationSet.hxx>
21 #include <Message_Messenger.hxx>
22 #include <gp_Mat.hxx>
23 #include <gp_Trsf.hxx>
24 #include <gp_XYZ.hxx>
25 #include <Standard_Type.hxx>
26 #include <TDF_Attribute.hxx>
27 #include <TopLoc_Datum3D.hxx>
28 #include <TopLoc_Location.hxx>
29 #include <XCAFDoc_Location.hxx>
30
31 IMPLEMENT_STANDARD_RTTIEXT(BinMXCAFDoc_LocationDriver,BinMDF_ADriver)
32
33 //#include <Precision.hxx>
34 //=======================================================================
35 //function :
36 //purpose  : 
37 //=======================================================================
38 BinMXCAFDoc_LocationDriver::BinMXCAFDoc_LocationDriver(const Handle(Message_Messenger)& theMsgDriver)
39      : BinMDF_ADriver(theMsgDriver, STANDARD_TYPE(XCAFDoc_Location)->Name())
40      , myLocations(0) {
41 }
42
43 //=======================================================================
44 //function :
45 //purpose  : 
46 //=======================================================================
47 Handle(TDF_Attribute) BinMXCAFDoc_LocationDriver::NewEmpty() const {
48   return new XCAFDoc_Location();
49 }
50
51 //=======================================================================
52 //function :
53 //purpose  : 
54 //=======================================================================
55 Standard_Boolean BinMXCAFDoc_LocationDriver::Paste(const BinObjMgt_Persistent& theSource,
56                                                    const Handle(TDF_Attribute)& theTarget,
57                                                    BinObjMgt_RRelocationTable& theRelocTable) const
58 {
59   Handle(XCAFDoc_Location) anAtt = Handle(XCAFDoc_Location)::DownCast(theTarget);
60   TopLoc_Location aLoc;
61   Standard_Boolean aRes = Translate(theSource, aLoc, theRelocTable);
62   anAtt->Set(aLoc);
63   return aRes;
64 }
65
66 //=======================================================================
67 //function :
68 //purpose  : 
69 //=======================================================================
70 void BinMXCAFDoc_LocationDriver::Paste(const Handle(TDF_Attribute)& theSource,
71                                        BinObjMgt_Persistent& theTarget,
72                                        BinObjMgt_SRelocationTable& theRelocTable) const
73 {
74   Handle(XCAFDoc_Location) anAtt = Handle(XCAFDoc_Location)::DownCast(theSource);
75   TopLoc_Location aLoc = anAtt->Get();
76   Translate(aLoc, theTarget, theRelocTable);
77 }
78
79 //=======================================================================
80 //function :
81 //purpose  : 
82 //=======================================================================
83 Standard_Boolean BinMXCAFDoc_LocationDriver::Translate(const BinObjMgt_Persistent& theSource,
84                                                        TopLoc_Location& theLoc,
85                                                        BinObjMgt_RRelocationTable& theMap) const
86 {
87   Standard_Integer anId = 0;
88   theSource >> anId;
89   
90   if(anId == 0)
91   {
92     return Standard_True;
93   }
94   
95   Standard_Integer aFileVer = theMap.GetHeaderData()->StorageVersion().IntegerValue();
96   if( aFileVer > 5 && myLocations == 0 )
97   {
98     return Standard_False;
99   }
100   
101   Standard_Integer aPower;
102   Handle(TopLoc_Datum3D) aDatum;
103   
104   if( aFileVer > 5 )
105   {
106     const TopLoc_Location& aLoc = myLocations->Location(anId);
107     aPower = aLoc.FirstPower();
108     aDatum = aLoc.FirstDatum();
109   } else {
110     theSource >> aPower;
111
112     Standard_Integer aDatumID = -1;
113     Standard_Integer aReadDatum = -1;
114     theSource >> aReadDatum;
115     theSource >> aDatumID;
116     if(aReadDatum != -1) {
117       if(theMap.IsBound(aDatumID)) {
118         aDatum = Handle(TopLoc_Datum3D)::DownCast(theMap.Find(aDatumID));
119       } else
120         return Standard_False;
121     } else {
122       // read the datum's trasformation
123       gp_Trsf aTrsf;
124
125       Standard_Real aScaleFactor;
126       theSource >> aScaleFactor;
127       aTrsf.SetScaleFactor(aScaleFactor);
128
129       Standard_Integer aForm;
130       theSource >> aForm;
131       aTrsf.SetForm((gp_TrsfForm)aForm);
132
133       Standard_Integer R, C;
134       gp_Mat& aMat = (gp_Mat&)aTrsf.HVectorialPart();
135       for(R = 1; R <= 3; R++)
136         for(C = 1; C <= 3; C++) {
137           Standard_Real aVal;
138           theSource >> aVal;
139           aMat.SetValue(R, C, aVal);
140         }
141
142       Standard_Real x, y, z;
143       theSource >> x >> y >> z;
144       gp_XYZ& aLoc = (gp_XYZ&)aTrsf.TranslationPart();
145       aLoc.SetX(x);
146       aLoc.SetY(y);
147       aLoc.SetZ(z);
148
149       aDatum = new TopLoc_Datum3D(aTrsf);
150       theMap.Bind(aDatumID, aDatum);
151     }
152   }
153   
154   //  Get Next Location
155   TopLoc_Location aNextLoc;
156   Translate(theSource, aNextLoc, theMap);
157   
158   //  Calculate the result
159   theLoc = aNextLoc * TopLoc_Location(aDatum).Powered(aPower);
160   return Standard_True;
161 }
162
163 //=======================================================================
164 //function :
165 //purpose  : 
166 //=======================================================================
167 void BinMXCAFDoc_LocationDriver::Translate(const TopLoc_Location& theLoc,
168                                            BinObjMgt_Persistent& theTarget,
169                                            BinObjMgt_SRelocationTable& theMap) const
170 {
171   if(theLoc.IsIdentity()) 
172   {
173     theTarget.PutInteger(0);
174     return;
175   }
176   
177   // The location is not identity  
178   if( myLocations == 0 )
179   {
180 #ifdef OCCT_DEBUG
181     cout<<"Pointer to LocationSet is NULL\n";
182 #endif
183     return;
184   }
185   
186   Standard_Integer anId = myLocations->Add(theLoc);
187   theTarget << anId;
188   
189   // In earlier version of this driver a datums from location stored in 
190   // the relocation table, but now it's not necessary
191   // (try to uncomment it if some problems appear)
192   /*
193   Handle(TopLoc_Datum3D) aDatum = theLoc.FirstDatum();
194   
195   if(!theMap.Contains(aDatum)) {
196     theMap.Add(aDatum);
197   }
198   */
199   
200   Translate(theLoc.NextLocation(), theTarget, theMap);
201 }
202