1 // Created on: 2005-05-17
2 // Created by: Eugeny NAPALKOV
3 // Copyright (c) 2005-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <BinMDataStd.hxx>
18 #include <BinMXCAFDoc_LocationDriver.hxx>
19 #include <BinObjMgt_Persistent.hxx>
20 #include <BinTools_LocationSet.hxx>
21 #include <Message_Messenger.hxx>
23 #include <gp_Trsf.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>
31 IMPLEMENT_STANDARD_RTTIEXT(BinMXCAFDoc_LocationDriver,BinMDF_ADriver)
33 //#include <Precision.hxx>
34 //=======================================================================
37 //=======================================================================
38 BinMXCAFDoc_LocationDriver::BinMXCAFDoc_LocationDriver(const Handle(Message_Messenger)& theMsgDriver)
39 : BinMDF_ADriver(theMsgDriver, STANDARD_TYPE(XCAFDoc_Location)->Name())
43 //=======================================================================
46 //=======================================================================
47 Handle(TDF_Attribute) BinMXCAFDoc_LocationDriver::NewEmpty() const {
48 return new XCAFDoc_Location();
51 //=======================================================================
54 //=======================================================================
55 Standard_Boolean BinMXCAFDoc_LocationDriver::Paste(const BinObjMgt_Persistent& theSource,
56 const Handle(TDF_Attribute)& theTarget,
57 BinObjMgt_RRelocationTable& theRelocTable) const
59 Handle(XCAFDoc_Location) anAtt = Handle(XCAFDoc_Location)::DownCast(theTarget);
61 Standard_Boolean aRes = Translate(theSource, aLoc, theRelocTable);
66 //=======================================================================
69 //=======================================================================
70 void BinMXCAFDoc_LocationDriver::Paste(const Handle(TDF_Attribute)& theSource,
71 BinObjMgt_Persistent& theTarget,
72 BinObjMgt_SRelocationTable& theRelocTable) const
74 Handle(XCAFDoc_Location) anAtt = Handle(XCAFDoc_Location)::DownCast(theSource);
75 TopLoc_Location aLoc = anAtt->Get();
76 Translate(aLoc, theTarget, theRelocTable);
79 //=======================================================================
82 //=======================================================================
83 Standard_Boolean BinMXCAFDoc_LocationDriver::Translate(const BinObjMgt_Persistent& theSource,
84 TopLoc_Location& theLoc,
85 BinObjMgt_RRelocationTable& theMap) const
87 Standard_Integer anId = 0;
95 Standard_Integer aFileVer = BinMDataStd::DocumentVersion();
96 if( aFileVer > 5 && myLocations == 0 )
98 return Standard_False;
101 Standard_Integer aPower;
102 Handle(TopLoc_Datum3D) aDatum;
106 const TopLoc_Location& aLoc = myLocations->Location(anId);
107 aPower = aLoc.FirstPower();
108 aDatum = aLoc.FirstDatum();
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));
120 return Standard_False;
122 // read the datum's trasformation
125 Standard_Real aScaleFactor;
126 theSource >> aScaleFactor;
127 aTrsf.SetScaleFactor(aScaleFactor);
129 Standard_Integer aForm;
131 aTrsf.SetForm((gp_TrsfForm)aForm);
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++) {
139 aMat.SetValue(R, C, aVal);
142 Standard_Real x, y, z;
143 theSource >> x >> y >> z;
144 gp_XYZ& aLoc = (gp_XYZ&)aTrsf.TranslationPart();
149 aDatum = new TopLoc_Datum3D(aTrsf);
150 theMap.Bind(aDatumID, aDatum);
155 TopLoc_Location aNextLoc;
156 Translate(theSource, aNextLoc, theMap);
158 // Calculate the result
159 theLoc = aNextLoc * TopLoc_Location(aDatum).Powered(aPower);
160 return Standard_True;
163 //=======================================================================
166 //=======================================================================
167 void BinMXCAFDoc_LocationDriver::Translate(const TopLoc_Location& theLoc,
168 BinObjMgt_Persistent& theTarget,
169 BinObjMgt_SRelocationTable& theMap) const
171 if(theLoc.IsIdentity())
173 theTarget.PutInteger(0);
177 // The location is not identity
178 if( myLocations == 0 )
181 cout<<"Pointer to LocationSet is NULL\n";
186 Standard_Integer anId = myLocations->Add(theLoc);
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)
193 Handle(TopLoc_Datum3D) aDatum = theLoc.FirstDatum();
195 if(!theMap.Contains(aDatum)) {
200 Translate(theLoc.NextLocation(), theTarget, theMap);