Salome HOME
Make comparison of updated shapes better.
[modules/shaper_study.git] / src / StudyData / StudyData_Object.cpp
1 // Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "StudyData_Object.h"
24
25 #include <TopExp.hxx>
26 #include <TopoDS_Iterator.hxx>
27 #include <BRep_Builder.hxx>
28 #include <BRepTools.hxx>
29 #include <BRepBuilderAPI_Copy.hxx>
30
31 StudyData_Object::StudyData_Object(const std::string theFile)
32 {
33   std::istringstream streamBrep(theFile.c_str());
34   BRep_Builder aBuilder;
35   BRepTools::Read(myShape, streamBrep, aBuilder);
36   myTick = 1;
37   myStream = theFile;
38 }
39
40 StudyData_Object::StudyData_Object()
41 {
42   myTick = 0; // when shape is defined, it will be increased to 1
43 }
44
45 int StudyData_Object::type() const
46 {
47   if (myShape.IsNull())
48     return 8; // GEOM.SHAPE
49   return (int) myShape.ShapeType();
50 }
51
52 std::string StudyData_Object::shapeStream() const
53 {
54   return myStream;
55 }
56
57 std::string StudyData_Object::oldShapeStream() const
58 {
59   return myOldStream.empty() ? myStream : myOldStream;
60 }
61
62 long long StudyData_Object::shape() const
63 {
64   return ((long long)(&myShape));
65 }
66
67 void StudyData_Object::updateShape(const std::string theFile)
68 {
69   if (myStream == theFile) { // absolutely identical shapes, no need to store
70     return;
71   }
72   size_t aDelta = myStream.size() - theFile.size();
73   aDelta = aDelta < 0 ? -aDelta : aDelta;
74   size_t aSum = myStream.size() + theFile.size();
75   if (double(aDelta) / aSum < 0.05) { // size-difference is less than 10%
76     // check numbers have the minimal differnce
77     std::istringstream aMyStr(myStream);
78     std::istringstream aFileStr(theFile);
79     double aMyNum, aFileNum;
80     std::string aBuf1, aBuf2;
81     while(aMyStr && aFileStr) {
82       if (aMyStr>>aMyNum) {
83         if (aFileStr>>aFileNum) {
84           if (std::abs(aMyNum - aFileNum) > 1.e-9)
85             break; // different numbers
86         } else {
87           break; // number and not number
88         }
89       } else if (aFileStr>>aFileNum) {
90         break; // number and not number
91       } else { // read two non-numbers
92         aMyStr.clear();
93         aMyStr>>aBuf1;
94         aFileStr.clear();
95         aFileStr>>aBuf2;
96         if (aBuf1 != aBuf2)
97           break; // strings are different
98       }
99     }
100     if (!aMyStr || !aFileStr) // both get to the end with equal content
101       return;
102   }
103
104   // update the current shape
105   std::istringstream streamBrep(theFile.c_str());
106   BRep_Builder aBuilder;
107   myOldShape = myShape;
108   BRepTools::Read(myShape, streamBrep, aBuilder);
109   myTick++;
110   myOldStream = myStream;
111   myStream = theFile;
112 }
113
114 int StudyData_Object::getTick() const
115 {
116   return myTick;
117 }
118
119 void StudyData_Object::setTick(const int theValue)
120 {
121   myTick = theValue;
122 }
123
124 void StudyData_Object::SetShapeByPointer(const long long theShape)
125 {
126   myOldShape = myShape;
127   myShape = *((TopoDS_Shape*)theShape);
128   std::ostringstream aStreamBrep;
129   if (!myShape.IsNull()) {
130     BRepTools::Write(myShape, aStreamBrep);
131   }
132   myOldStream = myStream;
133   myStream = aStreamBrep.str();
134   myTick++;
135 }
136
137 long long StudyData_Object::groupShape(long long theMainShape, const std::list<long> theSelection)
138 {
139   if (myShape.IsNull()) { // compute the cashed shape
140     TopoDS_Shape* aShape = (TopoDS_Shape*)theMainShape;
141     TopTools_IndexedMapOfShape anIndices;
142     TopExp::MapShapes(*aShape, anIndices);
143
144     TopoDS_Compound aResult;
145     BRep_Builder aBuilder;
146     aBuilder.MakeCompound(aResult);
147
148     std::list<long>::const_iterator aSelIter = theSelection.cbegin();
149     for(; aSelIter != theSelection.cend(); aSelIter++) {
150       TopoDS_Shape aSel = anIndices.FindKey(*aSelIter);
151       aBuilder.Add(aResult, aSel);
152     }
153     myShape = aResult;
154   } else { // check myShape equals to the new result
155     TopoDS_Shape* aShape = (TopoDS_Shape*)theMainShape;
156     TopTools_IndexedMapOfShape anIndices;
157     TopExp::MapShapes(*aShape, anIndices);
158     TopoDS_Iterator aMyIter(myShape);
159     std::list<long>::const_iterator aSelIter = theSelection.cbegin();
160     for(; aSelIter != theSelection.cend() && aMyIter.More(); aSelIter++, aMyIter.Next()) {
161       TopoDS_Shape aSel = anIndices.FindKey(*aSelIter);
162       if (!aSel.IsSame(aMyIter.Value()))
163         break;
164     }
165     if (aMyIter.More() || aSelIter != theSelection.cend()) { // recompute myShape
166       TopoDS_Compound aResult;
167       BRep_Builder aBuilder;
168       aBuilder.MakeCompound(aResult);
169       std::list<long>::const_iterator aSelIter = theSelection.cbegin();
170       for(; aSelIter != theSelection.cend(); aSelIter++) {
171         TopoDS_Shape aSel = anIndices.FindKey(*aSelIter);
172         aBuilder.Add(aResult, aSel);
173       }
174       myShape = aResult;
175     }
176   }
177   return (long long)(&myShape);
178 }