Salome HOME
Implementation of color as integer array attribute
[modules/shaper.git] / src / Model / Model_ResultBody.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_ResultBody.cpp
4 // Created:     08 Jul 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_ResultBody.h>
8 #include <Model_Data.h>
9 #include <Model_Document.h>
10 #include <ModelAPI_AttributeIntArray.h>
11 #include <TNaming_Builder.hxx>
12 #include <TNaming_NamedShape.hxx>
13 #include <TDataStd_Name.hxx>
14 #include <TopoDS.hxx>
15 #include <TopoDS_Shape.hxx>
16 #include <TopoDS_Face.hxx>
17 #include <TDF_ChildIterator.hxx>
18 #include <TopTools_MapOfShape.hxx>
19 #include <TopExp_Explorer.hxx>
20 #include <TopTools_ListOfShape.hxx>
21 #include <TopTools_ListIteratorOfListOfShape.hxx>
22 #include <TopTools_DataMapOfShapeListOfShape.hxx>
23 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
24 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
25 #include <TopTools_MapIteratorOfMapOfShape.hxx>
26 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
27 #include <TopTools_IndexedMapOfShape.hxx>
28 #include <TopTools_DataMapOfShapeShape.hxx>
29 #include <TopExp.hxx>
30 #include <BRepTools.hxx>
31 #include <BRep_Tool.hxx>
32 #include <GeomAPI_Shape.h>
33 #include <GeomAlgoAPI_MakeShape.h>
34 #include <Config_PropManager.h>
35 // DEB
36 //#include <TCollection_AsciiString.hxx>
37 //#include <TDF_Tool.hxx>
38 //#define DEB_IMPORT 1
39
40 #define RESULT_BODY_COLOR "#E0A01B"
41
42 Model_ResultBody::Model_ResultBody()
43 {
44   setIsConcealed(false);
45 }
46
47 void Model_ResultBody::initAttributes()
48 {
49   // append the color attribute
50   DataPtr aData = data();
51   aData->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::type());
52   AttributeIntArrayPtr aColorAttr = aData->intArray(COLOR_ID());
53   std::vector<int> aRGB;
54   aRGB = Config_PropManager::color("Visualization", "result_body_color", RESULT_BODY_COLOR);
55   aColorAttr->setSize(3);
56   aColorAttr->setValue(0, aRGB[0]);
57   aColorAttr->setValue(1, aRGB[1]);
58   aColorAttr->setValue(2, aRGB[2]);
59 }
60
61 void Model_ResultBody::store(const std::shared_ptr<GeomAPI_Shape>& theShape)
62 {
63   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
64   if (aData) {
65     TDF_Label& aShapeLab = aData->shapeLab();
66     // clean builders
67     clean();   
68     // store the new shape as primitive
69     TNaming_Builder aBuilder(aShapeLab);
70     if (!theShape)
71       return;  // bad shape
72     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
73     if (aShape.IsNull())
74       return;  // null shape inside
75
76     aBuilder.Generated(aShape); 
77         // register name
78         if(!aBuilder.NamedShape()->IsEmpty()) {
79           Handle(TDataStd_Name) anAttr;
80           if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
81                 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
82                 if(!aName.empty()) {
83           std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
84           aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
85                 }
86           }
87         }
88   }
89 }
90
91 void Model_ResultBody::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
92   const std::shared_ptr<GeomAPI_Shape>& theToShape)
93 {
94   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
95   if (aData) {
96     TDF_Label& aShapeLab = aData->shapeLab();
97     // clean builders
98     clean();   
99     // store the new shape as primitive
100     TNaming_Builder aBuilder(aShapeLab);
101     if (!theFromShape || !theToShape)
102       return;  // bad shape
103     TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
104     if (aShapeBasis.IsNull())
105       return;  // null shape inside
106     TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
107     if (aShapeNew.IsNull())
108       return;  // null shape inside
109     aBuilder.Generated(aShapeBasis, aShapeNew);
110                 // register name
111         if(!aBuilder.NamedShape()->IsEmpty()) {
112           Handle(TDataStd_Name) anAttr;
113           if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
114                 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
115                 if(!aName.empty()) {
116           std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
117           aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
118                 }
119           }
120         }
121   }
122 }
123
124 void Model_ResultBody::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
125   const std::shared_ptr<GeomAPI_Shape>& theNewShape)
126 {
127   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
128   if (aData) {
129     TDF_Label& aShapeLab = aData->shapeLab();
130     // clean builders
131     clean();   
132     // store the new shape as primitive
133     TNaming_Builder aBuilder(aShapeLab);
134     if (!theOldShape || !theNewShape)
135       return;  // bad shape
136     TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
137     if (aShapeOld.IsNull())
138       return;  // null shape inside
139     TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
140     if (aShapeNew.IsNull())
141       return;  // null shape inside
142         aBuilder.Modify(aShapeOld, aShapeNew);
143   }
144 }
145
146 std::shared_ptr<GeomAPI_Shape> Model_ResultBody::shape()
147 {
148   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
149   if (aData) {
150     TDF_Label& aShapeLab = aData->shapeLab();
151     Handle(TNaming_NamedShape) aName;
152     if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
153       TopoDS_Shape aShape = aName->Get();
154       if (!aShape.IsNull()) {
155         std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
156         aRes->setImpl(new TopoDS_Shape(aShape));
157         return aRes;
158       }
159     }
160   }
161   return std::shared_ptr<GeomAPI_Shape>();
162 }
163
164 void Model_ResultBody::clean()
165 {
166   std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
167   for(; aBuilder != myBuilders.end(); aBuilder++)
168     delete *aBuilder;
169   myBuilders.clear();
170 }
171
172 Model_ResultBody::~Model_ResultBody()
173 {
174   clean();
175 }
176
177 TNaming_Builder* Model_ResultBody::builder(const int theTag)
178 {
179   if (myBuilders.size() <= (unsigned int)theTag) {
180     myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
181   }
182   if (!myBuilders[theTag]) {
183     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
184     myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
185     //TCollection_AsciiString entry;//
186     //TDF_Tool::Entry(aData->shapeLab().FindChild(theTag), entry);
187     //cout << "Label = " <<entry.ToCString() <<endl;
188   }
189   return myBuilders[theTag];
190 }
191
192 void Model_ResultBody::buildName(const int theTag, const std::string& theName)
193 {
194   std::string aName = data()->name() + "/" + theName; 
195   std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
196   aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), aName);
197   TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(),aName.c_str());
198 }
199 void Model_ResultBody::generated(
200   const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
201 {
202   TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
203   builder(theTag)->Generated(aShape);
204   if(!theName.empty()) 
205     buildName(theTag, theName);
206 }
207
208 void Model_ResultBody::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
209   const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
210 {
211   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
212   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
213   builder(theTag)->Generated(anOldShape, aNewShape);
214   if(!theName.empty()) 
215     buildName(theTag, theName);
216 }
217
218
219 void Model_ResultBody::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
220   const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
221 {
222   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
223   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
224   builder(theTag)->Modify(anOldShape, aNewShape);
225   if(!theName.empty()) 
226     buildName(theTag, theName);
227 }
228
229 void Model_ResultBody::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
230   const int theTag)
231 {
232   TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
233   builder(theTag)->Delete(aShape);
234 }
235
236 void Model_ResultBody::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
237   std::shared_ptr<GeomAPI_Shape>  theShapeIn,
238   const int  theKindOfShape,
239   const int  theTag)
240 {
241   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
242   TopTools_MapOfShape aView;
243   TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
244   for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
245     const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
246     if (!aView.Add(aRoot)) continue;
247     std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
248     aRShape->setImpl((new TopoDS_Shape(aRoot)));
249     if (theMS->isDeleted (aRShape)) {
250       builder(theTag)->Delete(aRoot);
251     }
252   }
253 }
254
255 void Model_ResultBody::loadAndOrientModifiedShapes (
256   GeomAlgoAPI_MakeShape* theMS,
257   std::shared_ptr<GeomAPI_Shape>  theShapeIn,
258   const int  theKindOfShape,
259   const int  theTag,
260   const std::string& theName,
261   GeomAPI_DataMapOfShapeShape& theSubShapes)
262 {
263   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
264   TopTools_MapOfShape aView;
265   bool isBuilt = theName.empty();
266   TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
267   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
268     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
269     if (!aView.Add(aRoot)) continue;
270     ListOfShape aList;
271     std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
272     aRShape->setImpl((new TopoDS_Shape(aRoot)));
273         theMS->modified(aRShape, aList);
274     std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
275     for (; anIt != aLast; anIt++) {
276       TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();     
277       if (theSubShapes.isBound(*anIt)) {
278         std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
279         aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
280       }
281       if (!aRoot.IsSame (aNewShape)) {
282         builder(theTag)->Modify(aRoot,aNewShape);
283                 if(!isBuilt) 
284                   buildName(theTag, theName);           
285           }
286     }
287   }
288 }
289
290 void Model_ResultBody::loadAndOrientGeneratedShapes (
291   GeomAlgoAPI_MakeShape* theMS,
292   std::shared_ptr<GeomAPI_Shape>  theShapeIn,
293   const int  theKindOfShape,
294   const int  theTag,
295   const std::string& theName,
296   GeomAPI_DataMapOfShapeShape& theSubShapes)
297 {
298   TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
299   TopTools_MapOfShape aView;
300   bool isBuilt = theName.empty();
301   TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
302   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
303     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
304     if (!aView.Add(aRoot)) continue;
305     ListOfShape aList;
306     std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
307     aRShape->setImpl((new TopoDS_Shape(aRoot)));
308     theMS->generated(aRShape, aList);
309     std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
310     for (; anIt != aLast; anIt++) {
311       TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();     
312       if (theSubShapes.isBound(*anIt)) {
313         std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
314         aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
315       }
316       if (!aRoot.IsSame (aNewShape)) {
317         builder(theTag)->Generated(aRoot,aNewShape);
318                 if(!isBuilt) 
319                   buildName(theTag, theName);   
320           }
321     }
322   }
323 }
324
325 //=======================================================================
326 int getDangleShapes(const TopoDS_Shape&           theShapeIn, 
327                                         const TopAbs_ShapeEnum        theGeneratedFrom,
328                                     TopTools_DataMapOfShapeShape& theDangles) 
329 {
330   theDangles.Clear();
331   TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
332   TopAbs_ShapeEnum GeneratedTo;
333   if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
334   else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
335   else return Standard_False;
336   TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
337   for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
338     const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
339     const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
340     if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
341   }
342   return theDangles.Extent();
343 }
344
345 //=======================================================================
346 void loadGeneratedDangleShapes(
347                                                            const TopoDS_Shape&      theShapeIn,
348                                                const TopAbs_ShapeEnum   theGeneratedFrom,
349                                                TNaming_Builder *        theBuilder)
350 {
351   TopTools_DataMapOfShapeShape dangles;
352   if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
353   TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
354   for (; itr.More(); itr.Next()) 
355         theBuilder->Generated(itr.Key(), itr.Value());
356 }
357
358 //=======================================================================
359 void Model_ResultBody::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape, 
360                                           const std::string& theName, int&  theTag)
361 {
362   if(theShape->isNull()) return;
363   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();    
364   std::string aName;
365   if (aShape.ShapeType() == TopAbs_SOLID) {                 
366     TopExp_Explorer expl(aShape, TopAbs_FACE);
367     for (; expl.More(); expl.Next()) {  
368           builder(theTag)->Generated(expl.Current()); 
369           TCollection_AsciiString aStr(theTag);
370           aName = theName + aStr.ToCString();
371           buildName(theTag, aName);
372           theTag++;
373         }
374   }
375   else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
376     // load faces and all the free edges
377     TopTools_IndexedMapOfShape Faces;
378     TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
379     if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
380       TopExp_Explorer expl(aShape, TopAbs_FACE);
381       for (; expl.More(); expl.Next()) {
382                   builder(theTag)->Generated(expl.Current());          
383                   TCollection_AsciiString aStr(theTag);
384               aName = theName + aStr.ToCString();
385               buildName(theTag, aName);
386                   theTag++;
387           }
388         }
389     TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
390     TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
391     for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++) 
392         {
393       const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
394       if (aLL.Extent() < 2) {
395                   if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
396           continue;
397             builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
398                 TCollection_AsciiString aStr(theTag);
399             aName = theName + aStr.ToCString();
400             buildName(theTag, aName);
401                 theTag++;
402       } else {
403           TopTools_ListIteratorOfListOfShape anIter(aLL);
404           const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
405           anIter.Next();
406           if(aFace.IsEqual(anIter.Value())) {
407                 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
408                 TCollection_AsciiString aStr(theTag);
409             aName = theName + aStr.ToCString();
410             buildName(theTag, aName);
411             theTag++;
412           }
413           }
414         }
415   } else if (aShape.ShapeType() == TopAbs_WIRE) {
416     TopTools_IndexedMapOfShape Edges;
417     BRepTools::Map3DEdges(aShape, Edges);
418     if (Edges.Extent() == 1) {
419           builder(++theTag)->Generated(Edges.FindKey(1));
420       TopExp_Explorer expl(aShape, TopAbs_VERTEX);
421       for (; expl.More(); expl.Next()) {
422             builder(theTag)->Generated(expl.Current());
423                 TCollection_AsciiString aStr(theTag);
424             aName = theName + aStr.ToCString();
425             buildName(theTag, aName);
426             theTag++;
427           }
428         } else {
429       TopExp_Explorer expl(aShape, TopAbs_EDGE); 
430       for (; expl.More(); expl.Next()) {        
431                 builder(theTag)->Generated(expl.Current());
432                 TCollection_AsciiString aStr(theTag);
433             aName = theName + aStr.ToCString();
434             buildName(theTag, aName);
435                 theTag++;
436           }   
437       // and load generated vertices.
438       TopTools_DataMapOfShapeShape generated;
439       if (getDangleShapes(aShape, TopAbs_EDGE, generated)) 
440           {
441                 TNaming_Builder* pBuilder = builder(theTag++);
442                 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);  
443           }
444         }
445   } else if (aShape.ShapeType() == TopAbs_EDGE) {
446     TopExp_Explorer expl(aShape, TopAbs_VERTEX);
447     for (; expl.More(); expl.Next()) {      
448                 builder(theTag)->Generated(expl.Current());
449                 TCollection_AsciiString aStr(theTag);
450             aName = theName + aStr.ToCString();
451             buildName(theTag, aName);
452                 theTag++;
453         }
454   }
455 }
456
457 //=======================================================================
458 int findAmbiguities(const TopoDS_Shape&           theShapeIn,                                   
459                                           TopTools_ListOfShape&   theList) 
460 {
461   int aNumEdges(0);
462   theList.Clear();
463   TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
464   TopAbs_ShapeEnum aTS(TopAbs_EDGE);
465   TopAbs_ShapeEnum aTA(TopAbs_FACE);
466   TopTools_MapOfShape aMap1, aMap2; // map1 - for edge ancestors; map2 - for keys => edges
467   TopTools_ListOfShape aKeyList;
468   TopExp::MapShapesAndAncestors(theShapeIn, aTS, aTA, subShapeAndAncestors);
469   for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
470     const TopoDS_Shape& aKeyEdge1 = subShapeAndAncestors.FindKey(i);
471     const TopTools_ListOfShape& ancestors1 = subShapeAndAncestors.FindFromIndex(i);
472         aMap1.Clear();
473     TopTools_ListIteratorOfListOfShape it(ancestors1);
474         for(;it.More();it.Next()) aMap1.Add(it.Value()); // fill map with key ancestors => aKey1
475         for (Standard_Integer j = 1; j <= subShapeAndAncestors.Extent(); j++) {
476           if (i == j) continue;
477       const TopoDS_Shape& aKeyEdge2 = subShapeAndAncestors.FindKey(j);
478       const TopTools_ListOfShape& ancestors2 = subShapeAndAncestors.FindFromIndex(j);
479           if(ancestors1.Extent() == ancestors2.Extent() && ancestors1.Extent() > 1) {
480                 int aNum (ancestors2.Extent());
481             TopTools_ListIteratorOfListOfShape it(ancestors2);
482             for(;it.More();it.Next()) 
483                         if(aMap1.Contains(it.Value())) aNum--;
484                 if(aNum == 0) {
485                   if(aMap2.Add(aKeyEdge1)) 
486                         aKeyList.Append(aKeyEdge1);
487                   if(aMap2.Add(aKeyEdge2))
488                         aKeyList.Append(aKeyEdge2);
489                 }
490           }
491         } // at the end ==> List of edges to be named in addition       
492   }
493   aNumEdges = aKeyList.Extent();
494   if(aNumEdges)
495         theList.Assign(aKeyList);       
496   return aNumEdges; 
497 }
498
499 //=======================================================================
500 void Model_ResultBody::loadFirstLevel(
501                      std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int&  theTag)
502 {
503   if(theShape->isNull()) return;
504   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>(); 
505   std::string aName;
506   if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
507     TopoDS_Iterator itr(aShape);
508     for (; itr.More(); itr.Next(),theTag++) {
509           builder(theTag)->Generated(itr.Value());
510           TCollection_AsciiString aStr(theTag);
511           aName = theName + aStr.ToCString();
512           buildName(theTag, aName);
513           if(!theName.empty()) buildName(theTag, aName);
514       if (itr.Value().ShapeType() == TopAbs_COMPOUND || 
515                   itr.Value().ShapeType() == TopAbs_COMPSOLID) 
516           {
517                 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
518         itrShape->setImpl(new TopoDS_Shape(itr.Value()));
519             loadFirstLevel(itrShape, theName, theTag);
520       } else {
521                 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
522         itrShape->setImpl(new TopoDS_Shape(itr.Value()));               
523                 loadNextLevels(itrShape, theName, theTag);
524           }
525     }
526   } else {
527     std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
528     itrShape->setImpl(new TopoDS_Shape(aShape));
529         loadNextLevels(itrShape, theName, theTag); 
530   }
531   TopTools_ListOfShape   aList;
532   if(findAmbiguities(aShape, aList)) {
533         TopTools_ListIteratorOfListOfShape it(aList);
534     for (; it.More(); it.Next(),theTag++) {
535           builder(theTag)->Generated(it.Value());
536           TCollection_AsciiString aStr(theTag);
537           aName = theName + aStr.ToCString();
538           buildName(theTag, aName);
539         }
540   }
541 }
542
543 //=======================================================================
544 void Model_ResultBody::loadDisconnectedEdges(
545                      std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int&  theTag)
546 {
547   if(theShape->isNull()) return;
548   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();  
549   TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
550   TopTools_ListOfShape empty;
551   TopExp_Explorer explF(aShape, TopAbs_FACE);
552   for (; explF.More(); explF.Next()) {
553     const TopoDS_Shape& aFace = explF.Current();
554     TopExp_Explorer explV(aFace, TopAbs_EDGE);
555     for (; explV.More(); explV.Next()) {
556       const TopoDS_Shape& anEdge = explV.Current();
557       if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
558       Standard_Boolean faceIsNew = Standard_True;
559       TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
560       for (; itrF.More(); itrF.Next()) {
561             if (itrF.Value().IsSame(aFace)) {
562             faceIsNew = Standard_False;
563             break;
564                 }
565           }
566       if (faceIsNew) 
567             edgeNaborFaces.ChangeFind(anEdge).Append(aFace);      
568         }
569   }
570
571 /*  TopTools_IndexedDataMapOfShapeListOfShape aDM;
572   TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, aDM);
573   for(int i=1; i <= aDM.Extent(); i++) {
574         if(aDM.FindFromIndex(i).Extent() > 1) continue;
575         if (BRep_Tool::Degenerated(TopoDS::Edge(aDM.FindKey(i))))
576      continue;
577         builder(theTag)->Generated(aDM.FindKey(i));
578     TCollection_AsciiString aStr(theTag);
579         std::string aName = theName + aStr.ToCString();
580         buildName(theTag, aName);
581 #ifdef DEB_IMPORT
582         aName +=  + ".brep";
583         BRepTools::Write(aDM.FindKey(i), aName.c_str());
584 #endif
585         theTag++;
586   }
587 */
588   TopTools_MapOfShape anEdgesToDelete;
589   TopExp_Explorer anEx(aShape,TopAbs_EDGE); 
590   std::string aName;
591   for(;anEx.More();anEx.Next()) {
592     Standard_Boolean aC0 = Standard_False;
593     TopoDS_Shape anEdge1 = anEx.Current();
594     if (edgeNaborFaces.IsBound(anEdge1)) {
595       const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
596       if (aList1.Extent()<2) continue;
597       TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
598       for (; itr.More(); itr.Next()) {
599             TopoDS_Shape anEdge2 = itr.Key();
600             if(anEdgesToDelete.Contains(anEdge2)) continue;
601             if (anEdge1.IsSame(anEdge2)) continue;
602             const TopTools_ListOfShape& aList2 = itr.Value();
603             // compare lists of the neighbour faces of edge1 and edge2
604             if (aList1.Extent() == aList2.Extent()) {
605             Standard_Integer aMatches = 0;
606             for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
607               for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
608                 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
609                 if (aMatches == aList1.Extent()) {
610                   aC0=Standard_True;
611                           builder(theTag)->Generated(anEdge2);
612                   anEdgesToDelete.Add(anEdge2);
613                           TCollection_AsciiString aStr(theTag);
614                           aName = theName + aStr.ToCString();
615                   buildName(theTag, aName);
616                           theTag++;
617                         }
618                 }
619           }      
620       TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
621       for(;itDelete.More();itDelete.Next()) 
622             edgeNaborFaces.UnBind(itDelete.Key());      
623       edgeNaborFaces.UnBind(anEdge1);
624         }
625     if (aC0) {
626           builder(theTag)->Generated(anEdge1);
627           TCollection_AsciiString aStr(theTag);
628           aName = theName + aStr.ToCString();
629           buildName(theTag, aName);      
630           theTag++;
631         }
632   }  
633 }
634
635 void Model_ResultBody::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int&  theTag)
636 {
637   if(theShape->isNull()) return;
638   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();  
639   TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
640   TopTools_ListOfShape empty;
641   TopExp_Explorer explF(aShape, TopAbs_EDGE);
642   for (; explF.More(); explF.Next()) {
643     const TopoDS_Shape& anEdge = explF.Current();
644     TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
645     for (; explV.More(); explV.Next()) {
646       const TopoDS_Shape& aVertex = explV.Current();
647       if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
648       Standard_Boolean faceIsNew = Standard_True;
649       TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
650       for (; itrF.More(); itrF.Next()) {
651         if (itrF.Value().IsSame(anEdge)) {
652           faceIsNew = Standard_False;
653           break;
654         }
655       }
656       if (faceIsNew) {
657         vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
658       }
659     }
660   }
661   std::string aName;
662   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
663   for (; itr.More(); itr.Next()) {
664     const TopTools_ListOfShape& naborEdges = itr.Value();
665     if (naborEdges.Extent() < 2) {              
666                 builder(theTag)->Generated(itr.Key());
667                 TCollection_AsciiString aStr(theTag);
668             aName = theName + aStr.ToCString();
669             buildName(theTag, aName);    
670                 theTag++;
671         }
672   }
673 }