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 <BinTools_ShapeReader.hxx>
22 #include <BinTools_ShapeWriter.hxx>
23 #include <Message_Messenger.hxx>
25 #include <gp_Trsf.hxx>
27 #include <Standard_Type.hxx>
28 #include <TDF_Attribute.hxx>
29 #include <TopLoc_Datum3D.hxx>
30 #include <TopLoc_Location.hxx>
31 #include <XCAFDoc_Location.hxx>
33 IMPLEMENT_STANDARD_RTTIEXT(BinMXCAFDoc_LocationDriver,BinMDF_ADriver)
35 //#include <Precision.hxx>
36 //=======================================================================
39 //=======================================================================
40 BinMXCAFDoc_LocationDriver::BinMXCAFDoc_LocationDriver(const Handle(Message_Messenger)& theMsgDriver)
41 : BinMDF_ADriver(theMsgDriver, STANDARD_TYPE(XCAFDoc_Location)->Name())
45 //=======================================================================
48 //=======================================================================
49 Handle(TDF_Attribute) BinMXCAFDoc_LocationDriver::NewEmpty() const {
50 return new XCAFDoc_Location();
53 //=======================================================================
56 //=======================================================================
57 Standard_Boolean BinMXCAFDoc_LocationDriver::Paste(const BinObjMgt_Persistent& theSource,
58 const Handle(TDF_Attribute)& theTarget,
59 BinObjMgt_RRelocationTable& theRelocTable) const
61 Handle(XCAFDoc_Location) anAtt = Handle(XCAFDoc_Location)::DownCast(theTarget);
63 Standard_Boolean aRes = Translate(theSource, aLoc, theRelocTable);
68 //=======================================================================
71 //=======================================================================
72 void BinMXCAFDoc_LocationDriver::Paste(const Handle(TDF_Attribute)& theSource,
73 BinObjMgt_Persistent& theTarget,
74 BinObjMgt_SRelocationTable& theRelocTable) const
76 Handle(XCAFDoc_Location) anAtt = Handle(XCAFDoc_Location)::DownCast(theSource);
77 TopLoc_Location aLoc = anAtt->Get();
78 Translate(aLoc, theTarget, theRelocTable);
81 //=======================================================================
84 //=======================================================================
85 Standard_Boolean BinMXCAFDoc_LocationDriver::Translate(const BinObjMgt_Persistent& theSource,
86 TopLoc_Location& theLoc,
87 BinObjMgt_RRelocationTable& theMap) const
89 if (!myNSDriver.IsNull() && myNSDriver->IsQuickPart())
91 BinTools_IStream aDirectStream (*(const_cast<BinObjMgt_Persistent*>(&theSource)->GetIStream()));
92 BinTools_ShapeReader* aReader = static_cast<BinTools_ShapeReader*>(myNSDriver->ShapeSet (Standard_True));
93 theLoc = *(aReader->ReadLocation (aDirectStream));
97 Standard_Integer anId = 0;
102 return Standard_True;
105 if (!myNSDriver.IsNull() && myNSDriver->IsQuickPart())
106 { // read directly from the stream
111 Standard_Integer aFileVer = theMap.GetHeaderData()->StorageVersion().IntegerValue();
112 if( aFileVer >= TDocStd_FormatVersion_VERSION_6 && myNSDriver.IsNull() )
114 return Standard_False;
117 Standard_Integer aPower (0);
118 Handle(TopLoc_Datum3D) aDatum;
120 if (aFileVer >= TDocStd_FormatVersion_VERSION_6)
122 const TopLoc_Location& aLoc = myNSDriver->GetShapesLocations().Location (anId);
123 aPower = aLoc.FirstPower();
124 aDatum = aLoc.FirstDatum();
128 Standard_Integer aDatumID = -1;
129 Standard_Integer aReadDatum = -1;
130 theSource >> aReadDatum;
131 theSource >> aDatumID;
132 if(aReadDatum != -1) {
133 if(theMap.IsBound(aDatumID)) {
134 aDatum = Handle(TopLoc_Datum3D)::DownCast(theMap.Find(aDatumID));
136 return Standard_False;
138 // read the datum's transformation
141 Standard_Real aScaleFactor;
142 theSource >> aScaleFactor;
143 aTrsf.SetScaleFactor(aScaleFactor);
145 Standard_Integer aForm;
147 aTrsf.SetForm((gp_TrsfForm)aForm);
149 Standard_Integer R, C;
150 gp_Mat& aMat = (gp_Mat&)aTrsf.HVectorialPart();
151 for(R = 1; R <= 3; R++)
152 for(C = 1; C <= 3; C++) {
155 aMat.SetValue(R, C, aVal);
158 Standard_Real x, y, z;
159 theSource >> x >> y >> z;
160 gp_XYZ& aLoc = (gp_XYZ&)aTrsf.TranslationPart();
165 aDatum = new TopLoc_Datum3D(aTrsf);
166 theMap.Bind(aDatumID, aDatum);
171 TopLoc_Location aNextLoc;
172 Translate(theSource, aNextLoc, theMap);
174 // Calculate the result
175 theLoc = aNextLoc * TopLoc_Location(aDatum).Powered(aPower);
176 return Standard_True;
179 //=======================================================================
182 //=======================================================================
183 void BinMXCAFDoc_LocationDriver::Translate(const TopLoc_Location& theLoc,
184 BinObjMgt_Persistent& theTarget,
185 BinObjMgt_SRelocationTable& theMap) const
187 if (!myNSDriver.IsNull() && myNSDriver->IsQuickPart())
188 { // write directly to the stream
189 Standard_OStream* aDirectStream = theTarget.GetOStream();
190 BinTools_ShapeWriter* aWriter = static_cast<BinTools_ShapeWriter*>(myNSDriver->ShapeSet (Standard_False));
191 BinTools_OStream aStream(*aDirectStream);
192 aWriter->WriteLocation (aStream, theLoc);
195 if (theLoc.IsIdentity())
197 theTarget.PutInteger(0);
201 // The location is not identity
202 if (myNSDriver.IsNull())
205 std::cout << "NamedShape Driver is NULL\n";
210 Standard_Integer anId = myNSDriver->GetShapesLocations().Add (theLoc);
213 // In earlier version of this driver a datums from location stored in
214 // the relocation table, but now it's not necessary
215 // (try to uncomment it if some problems appear)
217 Handle(TopLoc_Datum3D) aDatum = theLoc.FirstDatum();
219 if(!theMap.Contains(aDatum)) {
224 Translate(theLoc.NextLocation(), theTarget, theMap);