]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_ResultBody.cpp
Salome HOME
Make selection naming works wit hsketch faces (added sub-edges in naming structure)
[modules/shaper.git] / src / Model / Model_ResultBody.cpp
1 // File:        Model_ResultBody.cpp
2 // Created:     08 Jul 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_ResultBody.h>
6 #include <Model_Data.h>
7 #include <TNaming_Builder.hxx>
8 #include <TNaming_NamedShape.hxx>
9 #include <TopoDS_Shape.hxx>
10 #include <TDF_ChildIterator.hxx>
11 #include <TopTools_MapOfShape.hxx>
12 #include <TopExp_Explorer.hxx>
13 #include <TopTools_ListOfShape.hxx>
14 #include <TopTools_ListIteratorOfListOfShape.hxx>
15 #include <TopTools_DataMapOfShapeListOfShape.hxx>
16 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
17 #include <TopTools_MapIteratorOfMapOfShape.hxx>
18 #include <GeomAPI_Shape.h>
19 #include <GeomAlgoAPI_MakeShape.h>
20 // DEB
21 //#include <TCollection_AsciiString.hxx>
22 //#include <TDF_Tool.hxx>
23 Model_ResultBody::Model_ResultBody()
24 {
25   setIsConcealed(false);
26 }
27
28 void Model_ResultBody::store(const boost::shared_ptr<GeomAPI_Shape>& theShape)
29 {
30   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
31   if (aData) {
32     TDF_Label& aShapeLab = aData->shapeLab();
33     // clean builders
34     clean();   
35     // store the new shape as primitive
36     TNaming_Builder aBuilder(aShapeLab);
37     if (!theShape)
38       return;  // bad shape
39     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
40     if (aShape.IsNull())
41       return;  // null shape inside
42
43     aBuilder.Generated(aShape);
44   }
45 }
46
47 void Model_ResultBody::storeGenerated(const boost::shared_ptr<GeomAPI_Shape>& theFromShape,
48   const boost::shared_ptr<GeomAPI_Shape>& theToShape)
49 {
50   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
51   if (aData) {
52     TDF_Label& aShapeLab = aData->shapeLab();
53     // clean builders
54     clean();   
55     // store the new shape as primitive
56     TNaming_Builder aBuilder(aShapeLab);
57     if (!theFromShape || !theToShape)
58       return;  // bad shape
59     TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
60     if (aShapeBasis.IsNull())
61       return;  // null shape inside
62     TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
63     if (aShapeNew.IsNull())
64       return;  // null shape inside
65     aBuilder.Generated(aShapeBasis, aShapeNew);
66   }
67 }
68
69 void Model_ResultBody::storeModified(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
70   const boost::shared_ptr<GeomAPI_Shape>& theNewShape)
71 {
72   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
73   if (aData) {
74     TDF_Label& aShapeLab = aData->shapeLab();
75     // clean builders
76     clean();   
77     // store the new shape as primitive
78     TNaming_Builder aBuilder(aShapeLab);
79     if (!theOldShape || !theNewShape)
80       return;  // bad shape
81     TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
82     if (aShapeOld.IsNull())
83       return;  // null shape inside
84     TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
85     if (aShapeNew.IsNull())
86       return;  // null shape inside
87     aBuilder.Generated(aShapeOld, aShapeNew);
88   }
89 }
90
91 boost::shared_ptr<GeomAPI_Shape> Model_ResultBody::shape()
92 {
93   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
94   if (aData) {
95     TDF_Label& aShapeLab = aData->shapeLab();
96     Handle(TNaming_NamedShape) aName;
97     if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
98       TopoDS_Shape aShape = aName->Get();
99       if (!aShape.IsNull()) {
100         boost::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
101         aRes->setImpl(new TopoDS_Shape(aShape));
102         return aRes;
103       }
104     }
105   }
106   return boost::shared_ptr<GeomAPI_Shape>();
107 }
108
109 void Model_ResultBody::clean()
110 {
111   std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
112   for(; aBuilder != myBuilders.end(); aBuilder++)
113     delete *aBuilder;
114   myBuilders.clear();
115 }
116
117 Model_ResultBody::~Model_ResultBody()
118 {
119   clean();
120 }
121
122 TNaming_Builder* Model_ResultBody::builder(const int theTag)
123 {
124   if (myBuilders.size() <= (unsigned int)theTag) {
125     myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
126   }
127   if (!myBuilders[theTag]) {
128     boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
129     myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
130     //TCollection_AsciiString entry;//
131     //TDF_Tool::Entry(aData->shapeLab().FindChild(theTag), entry);
132     //cout << "Label = " <<entry.ToCString() <<endl;
133   }
134   return myBuilders[theTag];
135 }
136
137 void Model_ResultBody::generated(
138   const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
139 {
140   TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
141   builder(theTag)->Generated(aShape);
142 }
143
144 void Model_ResultBody::generated(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
145   const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
146 {
147   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
148   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
149   builder(theTag)->Generated(anOldShape, aNewShape);
150 }
151
152
153 void Model_ResultBody::modified(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
154   const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
155 {
156   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
157   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
158   builder(theTag)->Modify(anOldShape, aNewShape);
159 }
160
161 void Model_ResultBody::deleted(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
162   const int theTag)
163 {
164   TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
165   builder(theTag)->Delete(aShape);
166 }
167
168 void Model_ResultBody::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
169   boost::shared_ptr<GeomAPI_Shape>  theShapeIn,
170   const int  theKindOfShape,
171   const int  theTag)
172 {
173   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
174   TopTools_MapOfShape aView;
175   TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
176   for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
177     const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
178     if (!aView.Add(aRoot)) continue;
179     boost::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
180     aRShape->setImpl((new TopoDS_Shape(aRoot)));
181     if (theMS->isDeleted (aRShape)) {
182       builder(theTag)->Delete(aRoot);
183     }
184   }
185 }
186
187 void Model_ResultBody::loadAndOrientModifiedShapes (
188   GeomAlgoAPI_MakeShape* theMS,
189   boost::shared_ptr<GeomAPI_Shape>  theShapeIn,
190   const int  theKindOfShape,
191   const int  theTag,
192   GeomAPI_DataMapOfShapeShape& theSubShapes)
193 {
194   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
195   TopTools_MapOfShape aView;
196   TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
197   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
198     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
199     if (!aView.Add(aRoot)) continue;
200     ListOfShape aList;
201     boost::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
202     aRShape->setImpl((new TopoDS_Shape(aRoot)));
203         theMS->modified(aRShape, aList);
204     std::list<boost::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
205     for (; anIt != aLast; anIt++) {
206       TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();     
207       if (theSubShapes.isBound(*anIt)) {
208         boost::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
209         aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
210       }
211       if (!aRoot.IsSame (aNewShape)) 
212         builder(theTag)->Modify(aRoot,aNewShape);
213     }
214   }
215 }
216
217 void Model_ResultBody::loadAndOrientGeneratedShapes (
218   GeomAlgoAPI_MakeShape* theMS,
219   boost::shared_ptr<GeomAPI_Shape>  theShapeIn,
220   const int  theKindOfShape,
221   const int  theTag,
222   GeomAPI_DataMapOfShapeShape& theSubShapes)
223 {
224   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
225   TopTools_MapOfShape aView;
226   TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
227   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
228     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
229     if (!aView.Add(aRoot)) continue;
230     ListOfShape aList;
231     boost::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
232     aRShape->setImpl((new TopoDS_Shape(aRoot)));
233     theMS->generated(aRShape, aList);
234     std::list<boost::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
235     for (; anIt != aLast; anIt++) {
236       TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();     
237       if (theSubShapes.isBound(*anIt)) {
238         boost::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
239         aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
240       }
241       if (!aRoot.IsSame (aNewShape)) 
242         builder(theTag)->Generated(aRoot,aNewShape);
243     }
244   }
245 }
246
247 void Model_ResultBody::loadFirstLevel(boost::shared_ptr<GeomAPI_Shape> theShape, int&  theTag)
248 {
249
250 }
251
252 void Model_ResultBody::loadDisconnectedEdges(boost::shared_ptr<GeomAPI_Shape> theShape, int&  theTag)
253 {
254   if(theShape->isNull()) return;
255   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();  
256   TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
257   TopTools_ListOfShape empty;
258   TopExp_Explorer explF(aShape, TopAbs_FACE);
259   for (; explF.More(); explF.Next()) {
260     const TopoDS_Shape& aFace = explF.Current();
261     TopExp_Explorer explV(aFace, TopAbs_EDGE);
262     for (; explV.More(); explV.Next()) {
263       const TopoDS_Shape& anEdge = explV.Current();
264       if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
265       Standard_Boolean faceIsNew = Standard_True;
266       TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
267       for (; itrF.More(); itrF.Next()) {
268             if (itrF.Value().IsSame(aFace)) {
269             faceIsNew = Standard_False;
270             break;
271                 }
272           }
273       if (faceIsNew) 
274             edgeNaborFaces.ChangeFind(anEdge).Append(aFace);      
275         }
276   }
277   
278   TopTools_MapOfShape anEdgesToDelete;
279   TopExp_Explorer anEx(aShape,TopAbs_EDGE); 
280   for(;anEx.More();anEx.Next()) {
281     Standard_Boolean aC0 = Standard_False;
282     TopoDS_Shape anEdge1 = anEx.Current();
283     if (edgeNaborFaces.IsBound(anEdge1)) {
284       const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
285       if (aList1.Extent()<2) continue;
286       TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
287       for (; itr.More(); itr.Next()) {
288             TopoDS_Shape anEdge2 = itr.Key();
289             if(anEdgesToDelete.Contains(anEdge2)) continue;
290             if (anEdge1.IsSame(anEdge2)) continue;
291             const TopTools_ListOfShape& aList2 = itr.Value();
292             // compare lists of the neighbour faces of edge1 and edge2
293             if (aList1.Extent() == aList2.Extent()) {
294             Standard_Integer aMatches = 0;
295             for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
296               for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
297                 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
298                 if (aMatches == aList1.Extent()) {
299                   aC0=Standard_True;
300                           builder(++theTag)->Generated(anEdge2);              
301                   anEdgesToDelete.Add(anEdge2);
302                         }
303                 }
304           }      
305       TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
306       for(;itDelete.More();itDelete.Next()) 
307             edgeNaborFaces.UnBind(itDelete.Key());      
308       edgeNaborFaces.UnBind(anEdge1);
309         }
310     if (aC0) 
311           builder(++theTag)->Generated(anEdge1);                  
312   }
313 }
314
315 void Model_ResultBody::loadDisconnectedVertexes(boost::shared_ptr<GeomAPI_Shape> theShape, int&  theTag)
316 {
317   if(theShape->isNull()) return;
318   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();  
319   TopTools_DataMapOfShapeListOfShape vertexNaborFaces;
320   TopTools_ListOfShape empty;
321   TopExp_Explorer explF(aShape, TopAbs_FACE);
322   for (; explF.More(); explF.Next()) {
323     const TopoDS_Shape& aFace = explF.Current();
324     TopExp_Explorer explV(aFace, TopAbs_VERTEX);
325     for (; explV.More(); explV.Next()) {
326       const TopoDS_Shape& aVertex = explV.Current();
327       if (!vertexNaborFaces.IsBound(aVertex)) vertexNaborFaces.Bind(aVertex, empty);
328       Standard_Boolean faceIsNew = Standard_True;
329       TopTools_ListIteratorOfListOfShape itrF(vertexNaborFaces.Find(aVertex));
330       for (; itrF.More(); itrF.Next()) {
331         if (itrF.Value().IsSame(aFace)) {
332           faceIsNew = Standard_False;
333           break;
334         }
335       }
336       if (faceIsNew) {
337         vertexNaborFaces.ChangeFind(aVertex).Append(aFace);
338       }
339     }
340   }
341
342   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborFaces);
343   for (; itr.More(); itr.Next()) {
344     const TopTools_ListOfShape& naborFaces = itr.Value();
345     if (naborFaces.Extent() < 3) 
346                 builder(++theTag)->Generated(itr.Key());         
347   }
348 }