1 // Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "StudyData_Object.h"
26 #include <TopoDS_Iterator.hxx>
27 #include <BRep_Builder.hxx>
28 #include <BRepTools.hxx>
29 #include <BRepBuilderAPI_Copy.hxx>
31 StudyData_Object::StudyData_Object(const std::string theFile)
33 std::istringstream streamBrep(theFile.c_str());
34 BRep_Builder aBuilder;
35 BRepTools::Read(myShape, streamBrep, aBuilder);
40 StudyData_Object::StudyData_Object()
42 myTick = 0; // when shape is defined, it will be increased to 1
45 int StudyData_Object::type() const
48 return 8; // GEOM.SHAPE
49 return (int) myShape.ShapeType();
52 std::string StudyData_Object::shapeStream() const
57 std::string StudyData_Object::oldShapeStream() const
59 return myOldStream.empty() ? myStream : myOldStream;
62 long long StudyData_Object::shape() const
64 return ((long long)(&myShape));
67 void StudyData_Object::updateShape(const std::string theFile)
69 if (myStream == theFile) { // absolutely identical shapes, no need to store
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) {
83 if (aFileStr>>aFileNum) {
84 if (std::abs(aMyNum - aFileNum) > 1.e-9)
85 break; // different numbers
87 break; // number and not number
89 } else if (aFileStr>>aFileNum) {
90 break; // number and not number
91 } else { // read two non-numbers
97 break; // strings are different
100 if (!aMyStr || !aFileStr) // both get to the end with equal content
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);
110 myOldStream = myStream;
114 int StudyData_Object::getTick() const
119 void StudyData_Object::setTick(const int theValue)
124 void StudyData_Object::SetShapeByPointer(const long long theShape)
126 myOldShape = myShape;
127 myShape = *((TopoDS_Shape*)theShape);
128 std::ostringstream aStreamBrep;
129 if (!myShape.IsNull()) {
130 BRepTools::Write(myShape, aStreamBrep);
132 myOldStream = myStream;
133 myStream = aStreamBrep.str();
137 long long StudyData_Object::groupShape(long long theMainShape, const std::list<long> theSelection)
139 if (myShape.IsNull()) { // compute the cashed shape
140 TopoDS_Shape* aShape = (TopoDS_Shape*)theMainShape;
141 TopTools_IndexedMapOfShape anIndices;
142 TopExp::MapShapes(*aShape, anIndices);
144 TopoDS_Compound aResult;
145 BRep_Builder aBuilder;
146 aBuilder.MakeCompound(aResult);
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);
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()))
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);
177 return (long long)(&myShape);