1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_ResultBody.cpp
4 // Created: 08 Jul 2014
5 // Author: Mikhail PONIKAROV
7 #include <Model_BodyBuilder.h>
9 #include <Model_Data.h>
10 #include <Model_Document.h>
11 #include <ModelAPI_AttributeIntArray.h>
12 #include <TNaming_Builder.hxx>
13 #include <TNaming_NamedShape.hxx>
14 #include <TNaming_Iterator.hxx>
15 #include <TDataStd_Name.hxx>
16 #include <TDataStd_Integer.hxx>
18 #include <TopoDS_Face.hxx>
19 #include <TDF_ChildIterator.hxx>
20 #include <TopTools_MapOfShape.hxx>
21 #include <TopExp_Explorer.hxx>
22 #include <TopTools_ListOfShape.hxx>
23 #include <TopTools_ListIteratorOfListOfShape.hxx>
24 #include <TopTools_DataMapOfShapeListOfShape.hxx>
25 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
26 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
27 #include <TopTools_MapIteratorOfMapOfShape.hxx>
28 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
29 #include <TopTools_IndexedMapOfShape.hxx>
30 #include <TopTools_DataMapOfShapeShape.hxx>
32 #include <BRepTools.hxx>
33 #include <BRep_Tool.hxx>
34 #include <GeomAPI_Shape.h>
35 #include <GeomAlgoAPI_MakeShape.h>
36 #include <Config_PropManager.h>
38 //#include <TCollection_AsciiString.hxx>
39 //#include <TDF_Tool.hxx>
40 //#define DEB_IMPORT 1
42 Model_BodyBuilder::Model_BodyBuilder(ModelAPI_Object* theOwner)
43 : ModelAPI_BodyBuilder(theOwner)
47 // Converts evolution of naming shape to selection evelution and back to avoid
48 // naming support on the disabled results. Deeply in the labels tree, recursively.
49 static void EvolutionToSelection(TDF_Label theLab, const bool theFlag) {
50 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
51 Handle(TNaming_NamedShape) aName;
53 if (theLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
54 TNaming_Evolution aNSEvol = aName->Evolution();
55 if ((aNSEvol == TNaming_SELECTED && theFlag) ||
56 (aNSEvol != TNaming_SELECTED && !theFlag)) { // nothing to do, it is already correct
59 anEvolution = (int)(aNSEvol);
61 Handle(TDataStd_Integer) anAttrEvol;
62 if (theLab.FindAttribute(TDataStd_Integer::GetID(), anAttrEvol)) {
63 anEvolution = anAttrEvol->Get();
66 TDataStd_Integer::Set(theLab, anEvolution);
69 for(TNaming_Iterator anIter(aName); anIter.More(); anIter.Next()) {
70 aShapePairs.push_back(std::pair<TopoDS_Shape, TopoDS_Shape>
71 (anIter.OldShape(), anIter.NewShape()));
75 TNaming_Builder aBuilder(theLab);
76 TNaming_Evolution anEvol = (TNaming_Evolution)(anEvolution);
77 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter = aShapePairs.begin();
78 for(; aPairsIter != aShapePairs.end(); aPairsIter++) {
79 if (theFlag) { // disabled => make selection
80 aBuilder.Select(aPairsIter->first, aPairsIter->second);
81 } else if (anEvol == TNaming_GENERATED) {
82 aBuilder.Generated(aPairsIter->first, aPairsIter->second);
83 } else if (anEvol == TNaming_MODIFY) {
84 aBuilder.Modify(aPairsIter->first, aPairsIter->second);
85 } else if (anEvol == TNaming_DELETE) {
86 aBuilder.Delete(aPairsIter->first);
87 } else if (anEvol == TNaming_PRIMITIVE) {
88 aBuilder.Generated(aPairsIter->second);
91 // recursive call for all sub-labels
92 TDF_ChildIterator anIter(theLab, Standard_False);
93 for(; anIter.More(); anIter.Next()) {
94 EvolutionToSelection(anIter.Value(), theFlag);
98 void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape)
100 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
102 TDF_Label& aShapeLab = aData->shapeLab();
105 // store the new shape as primitive
106 TNaming_Builder aBuilder(aShapeLab);
109 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
111 return; // null shape inside
113 aBuilder.Generated(aShape);
115 if(!aBuilder.NamedShape()->IsEmpty()) {
116 Handle(TDataStd_Name) anAttr;
117 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
118 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
120 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
121 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
128 void Model_BodyBuilder::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
129 const std::shared_ptr<GeomAPI_Shape>& theToShape)
131 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
133 TDF_Label& aShapeLab = aData->shapeLab();
136 // store the new shape as primitive
137 TNaming_Builder aBuilder(aShapeLab);
138 if (!theFromShape || !theToShape)
140 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
141 if (aShapeBasis.IsNull())
142 return; // null shape inside
143 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
144 if (aShapeNew.IsNull())
145 return; // null shape inside
146 aBuilder.Generated(aShapeBasis, aShapeNew);
148 if(!aBuilder.NamedShape()->IsEmpty()) {
149 Handle(TDataStd_Name) anAttr;
150 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
151 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
153 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
154 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
161 void Model_BodyBuilder::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
162 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
164 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
166 TDF_Label& aShapeLab = aData->shapeLab();
169 // store the new shape as primitive
170 TNaming_Builder aBuilder(aShapeLab);
171 if (!theOldShape || !theNewShape)
173 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
174 if (aShapeOld.IsNull())
175 return; // null shape inside
176 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
177 if (aShapeNew.IsNull())
178 return; // null shape inside
179 aBuilder.Modify(aShapeOld, aShapeNew);
180 if (theDecomposeSolidsTag && aShapeNew.ShapeType() == TopAbs_COMPOUND) { // make sub elements as subs
182 // register name if it is possible
183 TCollection_AsciiString aName;
184 if(!aBuilder.NamedShape()->IsEmpty()) {
185 Handle(TDataStd_Name) anAttr;
186 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
187 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
191 TopoDS_Iterator aSubIter(aShapeNew);
192 for(int aTag = theDecomposeSolidsTag; aSubIter.More(); aSubIter.Next()) {
193 TNaming_Builder aSubBuilder(aShapeLab.FindChild(aTag++));
194 aSubBuilder.Generated(aSubIter.Value());
195 if(!aName.IsEmpty()) {
196 std::string aSolidName =
197 (aName + "_Solid_" + TCollection_AsciiString(aTag - theDecomposeSolidsTag)).ToCString();
198 std::shared_ptr<Model_Document> aDoc =
199 std::dynamic_pointer_cast<Model_Document>(document());
200 aDoc->addNamingName(aSubBuilder.NamedShape()->Label(), aSolidName);
201 TDataStd_Name::Set(aSubBuilder.NamedShape()->Label(), aSolidName.c_str());
207 void Model_BodyBuilder::clean()
209 std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
210 for(; aBuilder != myBuilders.end(); aBuilder++)
215 Model_BodyBuilder::~Model_BodyBuilder()
220 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
222 if (myBuilders.size() <= (unsigned int)theTag) {
223 myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
225 if (!myBuilders[theTag]) {
226 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
227 myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
228 //TCollection_AsciiString entry;//
229 //TDF_Tool::Entry(aData->shapeLab().FindChild(theTag), entry);
230 //cout << "Label = " <<entry.ToCString() <<endl;
232 return myBuilders[theTag];
235 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
237 std::string aName = data()->name() + "/" + theName;
238 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
239 aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), aName);
240 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(),aName.c_str());
242 void Model_BodyBuilder::generated(
243 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
245 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
246 builder(theTag)->Generated(aShape);
248 buildName(theTag, theName);
251 void Model_BodyBuilder::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
252 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
254 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
255 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
256 builder(theTag)->Generated(anOldShape, aNewShape);
258 buildName(theTag, theName);
262 void Model_BodyBuilder::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
263 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
265 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
266 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
267 builder(theTag)->Modify(anOldShape, aNewShape);
269 buildName(theTag, theName);
272 void Model_BodyBuilder::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
275 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
276 builder(theTag)->Delete(aShape);
279 void Model_BodyBuilder::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
280 std::shared_ptr<GeomAPI_Shape> theShapeIn,
281 const int theKindOfShape,
284 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
285 TopTools_MapOfShape aView;
286 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
287 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
288 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
289 if (!aView.Add(aRoot)) continue;
290 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
291 aRShape->setImpl((new TopoDS_Shape(aRoot)));
292 if (theMS->isDeleted (aRShape)) {
293 builder(theTag)->Delete(aRoot);
298 void Model_BodyBuilder::loadAndOrientModifiedShapes (
299 GeomAlgoAPI_MakeShape* theMS,
300 std::shared_ptr<GeomAPI_Shape> theShapeIn,
301 const int theKindOfShape,
303 const std::string& theName,
304 GeomAPI_DataMapOfShapeShape& theSubShapes)
306 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
307 TopTools_MapOfShape aView;
308 bool isBuilt = theName.empty();
309 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
310 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
311 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
312 if (!aView.Add(aRoot)) continue;
314 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
315 aRShape->setImpl((new TopoDS_Shape(aRoot)));
316 theMS->modified(aRShape, aList);
317 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
318 for (; anIt != aLast; anIt++) {
319 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
320 if (theSubShapes.isBound(*anIt)) {
321 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
322 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
324 if (!aRoot.IsSame (aNewShape)) {
325 builder(theTag)->Modify(aRoot,aNewShape);
327 buildName(theTag, theName);
333 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
334 GeomAlgoAPI_MakeShape* theMS,
335 std::shared_ptr<GeomAPI_Shape> theShapeIn,
336 const int theKindOfShape,
338 const std::string& theName,
339 GeomAPI_DataMapOfShapeShape& theSubShapes)
341 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
342 TopTools_MapOfShape aView;
343 bool isBuilt = theName.empty();
344 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
345 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
346 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
347 if (!aView.Add(aRoot)) continue;
349 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
350 aRShape->setImpl((new TopoDS_Shape(aRoot)));
351 theMS->generated(aRShape, aList);
352 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
353 for (; anIt != aLast; anIt++) {
354 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
355 if (theSubShapes.isBound(*anIt)) {
356 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
357 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
359 if (!aRoot.IsSame (aNewShape)) {
360 builder(theTag)->Generated(aRoot,aNewShape);
362 buildName(theTag, theName);
368 //=======================================================================
369 int getDangleShapes(const TopoDS_Shape& theShapeIn,
370 const TopAbs_ShapeEnum theGeneratedFrom,
371 TopTools_DataMapOfShapeShape& theDangles)
374 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
375 TopAbs_ShapeEnum GeneratedTo;
376 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
377 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
378 else return Standard_False;
379 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
380 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
381 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
382 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
383 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
385 return theDangles.Extent();
388 //=======================================================================
389 void loadGeneratedDangleShapes(
390 const TopoDS_Shape& theShapeIn,
391 const TopAbs_ShapeEnum theGeneratedFrom,
392 TNaming_Builder * theBuilder)
394 TopTools_DataMapOfShapeShape dangles;
395 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
396 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
397 for (; itr.More(); itr.Next())
398 theBuilder->Generated(itr.Key(), itr.Value());
401 //=======================================================================
402 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
403 const std::string& theName, int& theTag)
405 if(theShape->isNull()) return;
406 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
408 if (aShape.ShapeType() == TopAbs_SOLID) {
409 TopExp_Explorer expl(aShape, TopAbs_FACE);
410 for (; expl.More(); expl.Next()) {
411 builder(theTag)->Generated(expl.Current());
412 TCollection_AsciiString aStr(theTag);
413 aName = theName + aStr.ToCString();
414 buildName(theTag, aName);
418 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
419 // load faces and all the free edges
420 TopTools_IndexedMapOfShape Faces;
421 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
422 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
423 TopExp_Explorer expl(aShape, TopAbs_FACE);
424 for (; expl.More(); expl.Next()) {
425 builder(theTag)->Generated(expl.Current());
426 TCollection_AsciiString aStr(theTag);
427 aName = theName + aStr.ToCString();
428 buildName(theTag, aName);
432 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
433 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
434 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
436 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
437 if (aLL.Extent() < 2) {
438 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
440 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
441 TCollection_AsciiString aStr(theTag);
442 aName = theName + aStr.ToCString();
443 buildName(theTag, aName);
446 TopTools_ListIteratorOfListOfShape anIter(aLL);
447 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
449 if(aFace.IsEqual(anIter.Value())) {
450 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
451 TCollection_AsciiString aStr(theTag);
452 aName = theName + aStr.ToCString();
453 buildName(theTag, aName);
458 } else if (aShape.ShapeType() == TopAbs_WIRE) {
459 TopTools_IndexedMapOfShape Edges;
460 BRepTools::Map3DEdges(aShape, Edges);
461 if (Edges.Extent() == 1) {
462 builder(++theTag)->Generated(Edges.FindKey(1));
463 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
464 for (; expl.More(); expl.Next()) {
465 builder(theTag)->Generated(expl.Current());
466 TCollection_AsciiString aStr(theTag);
467 aName = theName + aStr.ToCString();
468 buildName(theTag, aName);
472 TopExp_Explorer expl(aShape, TopAbs_EDGE);
473 for (; expl.More(); expl.Next()) {
474 builder(theTag)->Generated(expl.Current());
475 TCollection_AsciiString aStr(theTag);
476 aName = theName + aStr.ToCString();
477 buildName(theTag, aName);
480 // and load generated vertices.
481 TopTools_DataMapOfShapeShape generated;
482 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
484 TNaming_Builder* pBuilder = builder(theTag++);
485 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
488 } else if (aShape.ShapeType() == TopAbs_EDGE) {
489 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
490 for (; expl.More(); expl.Next()) {
491 builder(theTag)->Generated(expl.Current());
492 TCollection_AsciiString aStr(theTag);
493 aName = theName + aStr.ToCString();
494 buildName(theTag, aName);
500 //=======================================================================
501 int findAmbiguities(const TopoDS_Shape& theShapeIn,
502 TopTools_ListOfShape& theList)
506 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
507 TopAbs_ShapeEnum aTS(TopAbs_EDGE);
508 TopAbs_ShapeEnum aTA(TopAbs_FACE);
509 TopTools_MapOfShape aMap1, aMap2; // map1 - for edge ancestors; map2 - for keys => edges
510 TopTools_ListOfShape aKeyList;
511 TopExp::MapShapesAndAncestors(theShapeIn, aTS, aTA, subShapeAndAncestors);
512 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
513 const TopoDS_Shape& aKeyEdge1 = subShapeAndAncestors.FindKey(i);
514 const TopTools_ListOfShape& ancestors1 = subShapeAndAncestors.FindFromIndex(i);
516 TopTools_ListIteratorOfListOfShape it(ancestors1);
517 for(;it.More();it.Next()) aMap1.Add(it.Value()); // fill map with key ancestors => aKey1
518 for (Standard_Integer j = 1; j <= subShapeAndAncestors.Extent(); j++) {
519 if (i == j) continue;
520 const TopoDS_Shape& aKeyEdge2 = subShapeAndAncestors.FindKey(j);
521 const TopTools_ListOfShape& ancestors2 = subShapeAndAncestors.FindFromIndex(j);
522 if(ancestors1.Extent() == ancestors2.Extent() && ancestors1.Extent() > 1) {
523 int aNum (ancestors2.Extent());
524 TopTools_ListIteratorOfListOfShape it(ancestors2);
525 for(;it.More();it.Next())
526 if(aMap1.Contains(it.Value())) aNum--;
528 if(aMap2.Add(aKeyEdge1))
529 aKeyList.Append(aKeyEdge1);
530 if(aMap2.Add(aKeyEdge2))
531 aKeyList.Append(aKeyEdge2);
534 } // at the end ==> List of edges to be named in addition
536 aNumEdges = aKeyList.Extent();
538 theList.Assign(aKeyList);
542 //=======================================================================
543 void Model_BodyBuilder::loadFirstLevel(
544 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
546 if(theShape->isNull()) return;
547 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
549 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
550 TopoDS_Iterator itr(aShape);
551 for (; itr.More(); itr.Next(),theTag++) {
552 builder(theTag)->Generated(itr.Value());
553 TCollection_AsciiString aStr(theTag);
554 aName = theName + aStr.ToCString();
555 buildName(theTag, aName);
556 if(!theName.empty()) buildName(theTag, aName);
557 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
558 itr.Value().ShapeType() == TopAbs_COMPSOLID)
560 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
561 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
562 loadFirstLevel(itrShape, theName, theTag);
564 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
565 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
566 loadNextLevels(itrShape, theName, theTag);
570 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
571 itrShape->setImpl(new TopoDS_Shape(aShape));
572 loadNextLevels(itrShape, theName, theTag);
574 TopTools_ListOfShape aList;
575 if(findAmbiguities(aShape, aList)) {
576 TopTools_ListIteratorOfListOfShape it(aList);
577 for (; it.More(); it.Next(),theTag++) {
578 builder(theTag)->Generated(it.Value());
579 TCollection_AsciiString aStr(theTag);
580 aName = theName + aStr.ToCString();
581 buildName(theTag, aName);
586 //=======================================================================
587 void Model_BodyBuilder::loadDisconnectedEdges(
588 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
590 if(theShape->isNull()) return;
591 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
592 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
593 TopTools_ListOfShape empty;
594 TopExp_Explorer explF(aShape, TopAbs_FACE);
595 for (; explF.More(); explF.Next()) {
596 const TopoDS_Shape& aFace = explF.Current();
597 TopExp_Explorer explV(aFace, TopAbs_EDGE);
598 for (; explV.More(); explV.Next()) {
599 const TopoDS_Shape& anEdge = explV.Current();
600 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
601 Standard_Boolean faceIsNew = Standard_True;
602 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
603 for (; itrF.More(); itrF.Next()) {
604 if (itrF.Value().IsSame(aFace)) {
605 faceIsNew = Standard_False;
610 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
614 /* TopTools_IndexedDataMapOfShapeListOfShape aDM;
615 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, aDM);
616 for(int i=1; i <= aDM.Extent(); i++) {
617 if(aDM.FindFromIndex(i).Extent() > 1) continue;
618 if (BRep_Tool::Degenerated(TopoDS::Edge(aDM.FindKey(i))))
620 builder(theTag)->Generated(aDM.FindKey(i));
621 TCollection_AsciiString aStr(theTag);
622 std::string aName = theName + aStr.ToCString();
623 buildName(theTag, aName);
626 BRepTools::Write(aDM.FindKey(i), aName.c_str());
631 TopTools_MapOfShape anEdgesToDelete;
632 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
634 for(;anEx.More();anEx.Next()) {
635 Standard_Boolean aC0 = Standard_False;
636 TopoDS_Shape anEdge1 = anEx.Current();
637 if (edgeNaborFaces.IsBound(anEdge1)) {
638 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
639 if (aList1.Extent()<2) continue;
640 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
641 for (; itr.More(); itr.Next()) {
642 TopoDS_Shape anEdge2 = itr.Key();
643 if(anEdgesToDelete.Contains(anEdge2)) continue;
644 if (anEdge1.IsSame(anEdge2)) continue;
645 const TopTools_ListOfShape& aList2 = itr.Value();
646 // compare lists of the neighbour faces of edge1 and edge2
647 if (aList1.Extent() == aList2.Extent()) {
648 Standard_Integer aMatches = 0;
649 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
650 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
651 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
652 if (aMatches == aList1.Extent()) {
654 builder(theTag)->Generated(anEdge2);
655 anEdgesToDelete.Add(anEdge2);
656 TCollection_AsciiString aStr(theTag);
657 aName = theName + aStr.ToCString();
658 buildName(theTag, aName);
663 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
664 for(;itDelete.More();itDelete.Next())
665 edgeNaborFaces.UnBind(itDelete.Key());
666 edgeNaborFaces.UnBind(anEdge1);
669 builder(theTag)->Generated(anEdge1);
670 TCollection_AsciiString aStr(theTag);
671 aName = theName + aStr.ToCString();
672 buildName(theTag, aName);
678 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
680 if(theShape->isNull()) return;
681 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
682 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
683 TopTools_ListOfShape empty;
684 TopExp_Explorer explF(aShape, TopAbs_EDGE);
685 for (; explF.More(); explF.Next()) {
686 const TopoDS_Shape& anEdge = explF.Current();
687 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
688 for (; explV.More(); explV.Next()) {
689 const TopoDS_Shape& aVertex = explV.Current();
690 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
691 Standard_Boolean faceIsNew = Standard_True;
692 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
693 for (; itrF.More(); itrF.Next()) {
694 if (itrF.Value().IsSame(anEdge)) {
695 faceIsNew = Standard_False;
700 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
705 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
706 for (; itr.More(); itr.Next()) {
707 const TopTools_ListOfShape& naborEdges = itr.Value();
708 if (naborEdges.Extent() < 2) {
709 builder(theTag)->Generated(itr.Key());
710 TCollection_AsciiString aStr(theTag);
711 aName = theName + aStr.ToCString();
712 buildName(theTag, aName);