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 <TNaming_Tool.hxx>
16 #include <TDataStd_Name.hxx>
17 #include <TDataStd_Integer.hxx>
19 #include <TopoDS_Face.hxx>
20 #include <TDF_ChildIterator.hxx>
21 #include <TDF_Reference.hxx>
22 #include <TopTools_MapOfShape.hxx>
23 #include <TopExp_Explorer.hxx>
24 #include <TopTools_ListOfShape.hxx>
25 #include <TopTools_ListIteratorOfListOfShape.hxx>
26 #include <TopTools_DataMapOfShapeListOfShape.hxx>
27 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
28 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
29 #include <TopTools_MapIteratorOfMapOfShape.hxx>
30 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
31 #include <TopTools_IndexedMapOfShape.hxx>
32 #include <TopTools_DataMapOfShapeShape.hxx>
34 #include <BRepTools.hxx>
35 #include <BRep_Tool.hxx>
36 #include <GeomAPI_Shape.h>
37 #include <GeomAlgoAPI_MakeShape.h>
38 #include <Config_PropManager.h>
40 //#include <TCollection_AsciiString.hxx>
41 //#include <TDF_Tool.hxx>
42 //#define DEB_IMPORT 1
44 Model_BodyBuilder::Model_BodyBuilder(ModelAPI_Object* theOwner)
45 : ModelAPI_BodyBuilder(theOwner)
49 // Converts evolution of naming shape to selection evelution and back to avoid
50 // naming support on the disabled results. Deeply in the labels tree, recursively.
51 static void evolutionToSelectionRec(TDF_Label theLab, const bool theFlag) {
52 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
53 Handle(TNaming_NamedShape) aName;
55 if (theLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
56 TNaming_Evolution aNSEvol = aName->Evolution();
57 if ((aNSEvol == TNaming_SELECTED && theFlag) ||
58 (aNSEvol != TNaming_SELECTED && !theFlag)) { // nothing to do, it is already correct
61 anEvolution = (int)(aNSEvol);
63 Handle(TDataStd_Integer) anAttrEvol;
64 if (theLab.FindAttribute(TDataStd_Integer::GetID(), anAttrEvol)) {
65 anEvolution = anAttrEvol->Get();
68 TDataStd_Integer::Set(theLab, anEvolution);
71 for(TNaming_Iterator anIter(aName); anIter.More(); anIter.Next()) {
72 aShapePairs.push_back(std::pair<TopoDS_Shape, TopoDS_Shape>
73 (anIter.OldShape(), anIter.NewShape()));
77 TNaming_Builder aBuilder(theLab);
78 TNaming_Evolution anEvol = (TNaming_Evolution)(anEvolution);
79 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter = aShapePairs.begin();
80 for(; aPairsIter != aShapePairs.end(); aPairsIter++) {
81 if (theFlag) { // disabled => make selection
82 aBuilder.Select(aPairsIter->second, aPairsIter->first);
83 } else if (anEvol == TNaming_GENERATED) {
84 aBuilder.Generated(aPairsIter->first, aPairsIter->second);
85 } else if (anEvol == TNaming_MODIFY) {
86 aBuilder.Modify(aPairsIter->first, aPairsIter->second);
87 } else if (anEvol == TNaming_DELETE) {
88 aBuilder.Delete(aPairsIter->first);
89 } else if (anEvol == TNaming_PRIMITIVE) {
90 aBuilder.Generated(aPairsIter->second);
91 } else if (anEvol == TNaming_SELECTED) {
92 aBuilder.Select(aPairsIter->second, aPairsIter->first);
95 // recursive call for all sub-labels
96 TDF_ChildIterator anIter(theLab, Standard_False);
97 for(; anIter.More(); anIter.Next()) {
98 evolutionToSelectionRec(anIter.Value(), theFlag);
102 void Model_BodyBuilder::evolutionToSelection(const bool theFlag)
104 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
105 if (!aData) // unknown case
107 TDF_Label& aShapeLab = aData->shapeLab();
108 evolutionToSelectionRec(aShapeLab, theFlag);
111 void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape,
112 const bool theIsStoreSameShapes)
114 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
116 TDF_Label& aShapeLab = aData->shapeLab();
119 // store the new shape as primitive
120 TNaming_Builder aBuilder(aShapeLab);
123 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
125 return; // null shape inside
127 if(!theIsStoreSameShapes) {
128 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
129 if(!aNS.IsNull() && !aNS->IsEmpty()) {
130 // This shape is already in document, store reference instead of shape;
131 const TDF_Label aFoundLabel = aNS->Label();
132 TDF_Reference::Set(aShapeLab, aFoundLabel);
133 aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
138 aBuilder.Generated(aShape);
140 aShapeLab.ForgetAttribute(TDF_Reference::GetID());
141 if(!aBuilder.NamedShape()->IsEmpty()) {
142 Handle(TDataStd_Name) anAttr;
143 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
144 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
146 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
147 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
154 void Model_BodyBuilder::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
155 const std::shared_ptr<GeomAPI_Shape>& theToShape)
157 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
159 TDF_Label& aShapeLab = aData->shapeLab();
162 // store the new shape as primitive
163 TNaming_Builder aBuilder(aShapeLab);
164 if (!theFromShape || !theToShape)
166 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
167 if (aShapeBasis.IsNull())
168 return; // null shape inside
169 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
170 if (aShapeNew.IsNull())
171 return; // null shape inside
172 aBuilder.Generated(aShapeBasis, aShapeNew);
174 if(!aBuilder.NamedShape()->IsEmpty()) {
175 Handle(TDataStd_Name) anAttr;
176 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
177 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
179 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
180 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
187 void Model_BodyBuilder::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
188 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
190 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
192 TDF_Label& aShapeLab = aData->shapeLab();
195 // store the new shape as primitive
196 TNaming_Builder aBuilder(aShapeLab);
197 if (!theOldShape || !theNewShape)
199 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
200 if (aShapeOld.IsNull())
201 return; // null shape inside
202 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
203 if (aShapeNew.IsNull())
204 return; // null shape inside
205 aBuilder.Modify(aShapeOld, aShapeNew);
206 if (theDecomposeSolidsTag && aShapeNew.ShapeType() == TopAbs_COMPOUND) { // make sub elements as subs
208 // register name if it is possible
209 TCollection_AsciiString aName;
210 if(!aBuilder.NamedShape()->IsEmpty()) {
211 Handle(TDataStd_Name) anAttr;
212 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
213 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
217 TopoDS_Iterator aSubIter(aShapeNew);
218 for(int aTag = theDecomposeSolidsTag; aSubIter.More(); aSubIter.Next()) {
219 const TopoDS_Shape& aShape = aSubIter.Value();
220 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
221 if(!aNS.IsNull() && !aNS->IsEmpty()) {
222 // This shape is already in document, don't add it.
225 TNaming_Builder aSubBuilder(aShapeLab.FindChild(aTag++));
226 aSubBuilder.Generated(aSubIter.Value());
227 if(!aName.IsEmpty()) {
228 std::string aSolidName =
229 (aName + "_Solid_" + TCollection_AsciiString(aTag - theDecomposeSolidsTag)).ToCString();
230 std::shared_ptr<Model_Document> aDoc =
231 std::dynamic_pointer_cast<Model_Document>(document());
232 aDoc->addNamingName(aSubBuilder.NamedShape()->Label(), aSolidName);
233 TDataStd_Name::Set(aSubBuilder.NamedShape()->Label(), aSolidName.c_str());
240 void Model_BodyBuilder::storeWithoutNaming(const std::shared_ptr<GeomAPI_Shape>& theShape)
242 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
247 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
249 return; // null shape inside
250 TNaming_Builder aBuilder(aData->shapeLab());
251 aBuilder.Select(aShape, aShape);
255 void Model_BodyBuilder::clean()
257 std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
258 for(; aBuilder != myBuilders.end(); aBuilder++)
263 Model_BodyBuilder::~Model_BodyBuilder()
268 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
270 if (myBuilders.size() <= (unsigned int)theTag) {
271 myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
273 if (!myBuilders[theTag]) {
274 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
275 myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
276 //TCollection_AsciiString entry;//
277 //TDF_Tool::Entry(aData->shapeLab().FindChild(theTag), entry);
278 //cout << "Label = " <<entry.ToCString() <<endl;
280 return myBuilders[theTag];
283 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
285 std::string aName = data()->name() + "/" + theName;
286 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
287 aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), aName);
288 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(),aName.c_str());
290 void Model_BodyBuilder::generated(
291 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
293 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
294 builder(theTag)->Generated(aShape);
296 buildName(theTag, theName);
299 void Model_BodyBuilder::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
300 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
302 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
303 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
304 builder(theTag)->Generated(anOldShape, aNewShape);
306 buildName(theTag, theName);
310 void Model_BodyBuilder::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
311 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
313 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
314 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
315 builder(theTag)->Modify(anOldShape, aNewShape);
317 buildName(theTag, theName);
320 void Model_BodyBuilder::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
323 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
324 builder(theTag)->Delete(aShape);
327 void Model_BodyBuilder::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
328 std::shared_ptr<GeomAPI_Shape> theShapeIn,
329 const int theKindOfShape,
332 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
333 TopTools_MapOfShape aView;
334 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
335 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
336 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
337 if (!aView.Add(aRoot)) continue;
338 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
339 aRShape->setImpl((new TopoDS_Shape(aRoot)));
340 if (theMS->isDeleted (aRShape)) {
341 builder(theTag)->Delete(aRoot);
346 void Model_BodyBuilder::loadAndOrientModifiedShapes (
347 GeomAlgoAPI_MakeShape* theMS,
348 std::shared_ptr<GeomAPI_Shape> theShapeIn,
349 const int theKindOfShape,
351 const std::string& theName,
352 GeomAPI_DataMapOfShapeShape& theSubShapes,
353 const bool theIsStoreSeparate)
356 std::string aName = theName;
357 std::ostringstream aStream;
359 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
360 TopTools_MapOfShape aView;
361 bool isBuilt = theName.empty();
362 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
363 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
364 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
365 if (!aView.Add(aRoot)) continue;
367 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
368 aRShape->setImpl((new TopoDS_Shape(aRoot)));
369 theMS->modified(aRShape, aList);
370 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
371 for (; anIt != aLast; anIt++) {
372 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
373 if (theSubShapes.isBound(*anIt)) {
374 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
375 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
377 if (!aRoot.IsSame (aNewShape)) {
378 builder(aTag)->Modify(aRoot,aNewShape);
380 if(theIsStoreSeparate) {
381 aStream.str(std::string());
383 aStream << theName << "_" << anIndex++;
384 aName = aStream.str();
386 buildName(aTag, aName);
388 if(theIsStoreSeparate) {
396 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
397 GeomAlgoAPI_MakeShape* theMS,
398 std::shared_ptr<GeomAPI_Shape> theShapeIn,
399 const int theKindOfShape,
401 const std::string& theName,
402 GeomAPI_DataMapOfShapeShape& theSubShapes)
404 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
405 TopTools_MapOfShape aView;
406 bool isBuilt = theName.empty();
407 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
408 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
409 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
410 if (!aView.Add(aRoot)) continue;
412 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
413 aRShape->setImpl((new TopoDS_Shape(aRoot)));
414 theMS->generated(aRShape, aList);
415 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
416 for (; anIt != aLast; anIt++) {
417 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
418 if (theSubShapes.isBound(*anIt)) {
419 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
420 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
422 if (!aRoot.IsSame (aNewShape)) {
423 builder(theTag)->Generated(aRoot,aNewShape);
425 buildName(theTag, theName);
431 //=======================================================================
432 int getDangleShapes(const TopoDS_Shape& theShapeIn,
433 const TopAbs_ShapeEnum theGeneratedFrom,
434 TopTools_DataMapOfShapeShape& theDangles)
437 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
438 TopAbs_ShapeEnum GeneratedTo;
439 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
440 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
441 else return Standard_False;
442 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
443 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
444 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
445 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
446 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
448 return theDangles.Extent();
451 //=======================================================================
452 void loadGeneratedDangleShapes(
453 const TopoDS_Shape& theShapeIn,
454 const TopAbs_ShapeEnum theGeneratedFrom,
455 TNaming_Builder * theBuilder)
457 TopTools_DataMapOfShapeShape dangles;
458 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
459 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
460 for (; itr.More(); itr.Next())
461 theBuilder->Generated(itr.Key(), itr.Value());
464 //=======================================================================
465 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
466 const std::string& theName, int& theTag)
468 if(theShape->isNull()) return;
469 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
471 if (aShape.ShapeType() == TopAbs_SOLID) {
472 TopExp_Explorer expl(aShape, TopAbs_FACE);
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);
481 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
482 // load faces and all the free edges
483 TopTools_IndexedMapOfShape Faces;
484 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
485 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
486 TopExp_Explorer expl(aShape, TopAbs_FACE);
487 for (; expl.More(); expl.Next()) {
488 builder(theTag)->Generated(expl.Current());
489 TCollection_AsciiString aStr(theTag);
490 aName = theName + aStr.ToCString();
491 buildName(theTag, aName);
495 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
496 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
497 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
499 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
500 if (aLL.Extent() < 2) {
501 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
503 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
504 TCollection_AsciiString aStr(theTag);
505 aName = theName + aStr.ToCString();
506 buildName(theTag, aName);
509 TopTools_ListIteratorOfListOfShape anIter(aLL);
510 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
512 if(aFace.IsEqual(anIter.Value())) {
513 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
514 TCollection_AsciiString aStr(theTag);
515 aName = theName + aStr.ToCString();
516 buildName(theTag, aName);
521 } else if (aShape.ShapeType() == TopAbs_WIRE) {
522 TopTools_IndexedMapOfShape Edges;
523 BRepTools::Map3DEdges(aShape, Edges);
524 if (Edges.Extent() == 1) {
525 builder(++theTag)->Generated(Edges.FindKey(1));
526 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
527 for (; expl.More(); expl.Next()) {
528 builder(theTag)->Generated(expl.Current());
529 TCollection_AsciiString aStr(theTag);
530 aName = theName + aStr.ToCString();
531 buildName(theTag, aName);
535 TopExp_Explorer expl(aShape, TopAbs_EDGE);
536 for (; expl.More(); expl.Next()) {
537 builder(theTag)->Generated(expl.Current());
538 TCollection_AsciiString aStr(theTag);
539 aName = theName + aStr.ToCString();
540 buildName(theTag, aName);
543 // and load generated vertices.
544 TopTools_DataMapOfShapeShape generated;
545 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
547 TNaming_Builder* pBuilder = builder(theTag++);
548 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
551 } else if (aShape.ShapeType() == TopAbs_EDGE) {
552 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
553 for (; expl.More(); expl.Next()) {
554 builder(theTag)->Generated(expl.Current());
555 TCollection_AsciiString aStr(theTag);
556 aName = theName + aStr.ToCString();
557 buildName(theTag, aName);
563 //=======================================================================
564 int findAmbiguities(const TopoDS_Shape& theShapeIn,
565 TopTools_ListOfShape& theList)
569 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
570 TopAbs_ShapeEnum aTS(TopAbs_EDGE);
571 TopAbs_ShapeEnum aTA(TopAbs_FACE);
572 TopTools_MapOfShape aMap1, aMap2; // map1 - for edge ancestors; map2 - for keys => edges
573 TopTools_ListOfShape aKeyList;
574 TopExp::MapShapesAndAncestors(theShapeIn, aTS, aTA, subShapeAndAncestors);
575 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
576 const TopoDS_Shape& aKeyEdge1 = subShapeAndAncestors.FindKey(i);
577 const TopTools_ListOfShape& ancestors1 = subShapeAndAncestors.FindFromIndex(i);
579 TopTools_ListIteratorOfListOfShape it(ancestors1);
580 for(;it.More();it.Next()) aMap1.Add(it.Value()); // fill map with key ancestors => aKey1
581 for (Standard_Integer j = 1; j <= subShapeAndAncestors.Extent(); j++) {
582 if (i == j) continue;
583 const TopoDS_Shape& aKeyEdge2 = subShapeAndAncestors.FindKey(j);
584 const TopTools_ListOfShape& ancestors2 = subShapeAndAncestors.FindFromIndex(j);
585 if(ancestors1.Extent() == ancestors2.Extent() && ancestors1.Extent() > 1) {
586 int aNum (ancestors2.Extent());
587 TopTools_ListIteratorOfListOfShape it(ancestors2);
588 for(;it.More();it.Next())
589 if(aMap1.Contains(it.Value())) aNum--;
591 if(aMap2.Add(aKeyEdge1))
592 aKeyList.Append(aKeyEdge1);
593 if(aMap2.Add(aKeyEdge2))
594 aKeyList.Append(aKeyEdge2);
597 } // at the end ==> List of edges to be named in addition
599 aNumEdges = aKeyList.Extent();
601 theList.Assign(aKeyList);
605 //=======================================================================
606 void Model_BodyBuilder::loadFirstLevel(
607 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
609 if(theShape->isNull()) return;
610 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
612 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
613 TopoDS_Iterator itr(aShape);
614 for (; itr.More(); itr.Next(),theTag++) {
615 builder(theTag)->Generated(itr.Value());
616 TCollection_AsciiString aStr(theTag);
617 aName = theName + aStr.ToCString();
618 buildName(theTag, aName);
619 if(!theName.empty()) buildName(theTag, aName);
620 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
621 itr.Value().ShapeType() == TopAbs_COMPSOLID)
623 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
624 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
625 loadFirstLevel(itrShape, theName, theTag);
627 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
628 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
629 loadNextLevels(itrShape, theName, theTag);
633 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
634 itrShape->setImpl(new TopoDS_Shape(aShape));
635 loadNextLevels(itrShape, theName, theTag);
637 TopTools_ListOfShape aList;
638 if(findAmbiguities(aShape, aList)) {
639 TopTools_ListIteratorOfListOfShape it(aList);
640 for (; it.More(); it.Next(),theTag++) {
641 builder(theTag)->Generated(it.Value());
642 TCollection_AsciiString aStr(theTag);
643 aName = theName + aStr.ToCString();
644 buildName(theTag, aName);
649 //=======================================================================
650 void Model_BodyBuilder::loadDisconnectedEdges(
651 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
653 if(theShape->isNull()) return;
654 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
655 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
656 TopTools_ListOfShape empty;
657 TopExp_Explorer explF(aShape, TopAbs_FACE);
658 for (; explF.More(); explF.Next()) {
659 const TopoDS_Shape& aFace = explF.Current();
660 TopExp_Explorer explV(aFace, TopAbs_EDGE);
661 for (; explV.More(); explV.Next()) {
662 const TopoDS_Shape& anEdge = explV.Current();
663 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
664 Standard_Boolean faceIsNew = Standard_True;
665 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
666 for (; itrF.More(); itrF.Next()) {
667 if (itrF.Value().IsSame(aFace)) {
668 faceIsNew = Standard_False;
673 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
677 /* TopTools_IndexedDataMapOfShapeListOfShape aDM;
678 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, aDM);
679 for(int i=1; i <= aDM.Extent(); i++) {
680 if(aDM.FindFromIndex(i).Extent() > 1) continue;
681 if (BRep_Tool::Degenerated(TopoDS::Edge(aDM.FindKey(i))))
683 builder(theTag)->Generated(aDM.FindKey(i));
684 TCollection_AsciiString aStr(theTag);
685 std::string aName = theName + aStr.ToCString();
686 buildName(theTag, aName);
689 BRepTools::Write(aDM.FindKey(i), aName.c_str());
694 TopTools_MapOfShape anEdgesToDelete;
695 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
697 for(;anEx.More();anEx.Next()) {
698 Standard_Boolean aC0 = Standard_False;
699 TopoDS_Shape anEdge1 = anEx.Current();
700 if (edgeNaborFaces.IsBound(anEdge1)) {
701 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
702 if (aList1.Extent()<2) continue;
703 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
704 for (; itr.More(); itr.Next()) {
705 TopoDS_Shape anEdge2 = itr.Key();
706 if(anEdgesToDelete.Contains(anEdge2)) continue;
707 if (anEdge1.IsSame(anEdge2)) continue;
708 const TopTools_ListOfShape& aList2 = itr.Value();
709 // compare lists of the neighbour faces of edge1 and edge2
710 if (aList1.Extent() == aList2.Extent()) {
711 Standard_Integer aMatches = 0;
712 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
713 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
714 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
715 if (aMatches == aList1.Extent()) {
717 builder(theTag)->Generated(anEdge2);
718 anEdgesToDelete.Add(anEdge2);
719 TCollection_AsciiString aStr(theTag);
720 aName = theName + aStr.ToCString();
721 buildName(theTag, aName);
726 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
727 for(;itDelete.More();itDelete.Next())
728 edgeNaborFaces.UnBind(itDelete.Key());
729 edgeNaborFaces.UnBind(anEdge1);
732 builder(theTag)->Generated(anEdge1);
733 TCollection_AsciiString aStr(theTag);
734 aName = theName + aStr.ToCString();
735 buildName(theTag, aName);
741 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
743 if(theShape->isNull()) return;
744 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
745 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
746 TopTools_ListOfShape empty;
747 TopExp_Explorer explF(aShape, TopAbs_EDGE);
748 for (; explF.More(); explF.Next()) {
749 const TopoDS_Shape& anEdge = explF.Current();
750 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
751 for (; explV.More(); explV.Next()) {
752 const TopoDS_Shape& aVertex = explV.Current();
753 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
754 Standard_Boolean faceIsNew = Standard_True;
755 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
756 for (; itrF.More(); itrF.Next()) {
757 if (itrF.Value().IsSame(anEdge)) {
758 faceIsNew = Standard_False;
763 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
768 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
769 for (; itr.More(); itr.Next()) {
770 const TopTools_ListOfShape& naborEdges = itr.Value();
771 if (naborEdges.Extent() < 2) {
772 builder(theTag)->Generated(itr.Key());
773 TCollection_AsciiString aStr(theTag);
774 aName = theName + aStr.ToCString();
775 buildName(theTag, aName);
781 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
783 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
785 TDF_Label aShapeLab = aData->shapeLab();
786 Handle(TDF_Reference) aRef;
787 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
788 aShapeLab = aRef->Get();
790 Handle(TNaming_NamedShape) aName;
791 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
792 TopoDS_Shape aShape = aName->Get();
793 if (!aShape.IsNull()) {
794 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
795 aRes->setImpl(new TopoDS_Shape(aShape));
800 return std::shared_ptr<GeomAPI_Shape>();
803 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
805 if (theShape.get()) {
806 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
807 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
809 TDF_Label& aShapeLab = aData->shapeLab();
810 Handle(TNaming_NamedShape) aName;
811 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
812 TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
813 if (aLatest.IsNull())
815 if (aLatest.IsEqual(aShape))
817 // check sub-shapes for comp-solids:
818 for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
819 if (aLatest.IsEqual(anExp.Current()))