1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <Model_BodyBuilder.h>
23 #include <Model_Data.h>
24 #include <Model_Document.h>
25 #include <TNaming_Builder.hxx>
26 #include <TNaming_NamedShape.hxx>
27 #include <TNaming_Iterator.hxx>
28 #include <TNaming_Tool.hxx>
29 #include <TDataStd_Name.hxx>
30 #include <TDataStd_Integer.hxx>
32 #include <TopoDS_Face.hxx>
33 #include <TDF_ChildIterator.hxx>
34 #include <TDF_Reference.hxx>
35 #include <TopTools_MapOfShape.hxx>
36 #include <TopExp_Explorer.hxx>
37 #include <TopTools_ListOfShape.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39 #include <TopTools_DataMapOfShapeListOfShape.hxx>
40 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
41 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
42 #include <TopTools_MapIteratorOfMapOfShape.hxx>
43 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <TopTools_DataMapOfShapeShape.hxx>
47 #include <BRepTools.hxx>
48 #include <BRep_Tool.hxx>
49 #include <GeomAPI_Shape.h>
50 #include <GeomAlgoAPI_MakeShape.h>
51 #include <Config_PropManager.h>
53 //#include <TCollection_AsciiString.hxx>
54 //#include <TDF_Tool.hxx>
55 //#define DEB_IMPORT 1
57 Model_BodyBuilder::Model_BodyBuilder(ModelAPI_Object* theOwner)
58 : ModelAPI_BodyBuilder(theOwner)
62 // Converts evolution of naming shape to selection evelution and back to avoid
63 // naming support on the disabled results. Deeply in the labels tree, recursively.
64 static void evolutionToSelectionRec(TDF_Label theLab, const bool theFlag) {
65 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
66 Handle(TNaming_NamedShape) aName;
68 if (theLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
69 TNaming_Evolution aNSEvol = aName->Evolution();
70 if ((aNSEvol == TNaming_SELECTED && theFlag) ||
71 (aNSEvol != TNaming_SELECTED && !theFlag)) { // nothing to do, it is already correct
74 anEvolution = (int)(aNSEvol);
76 Handle(TDataStd_Integer) anAttrEvol;
77 if (theLab.FindAttribute(TDataStd_Integer::GetID(), anAttrEvol)) {
78 anEvolution = anAttrEvol->Get();
81 TDataStd_Integer::Set(theLab, anEvolution);
84 for(TNaming_Iterator anIter(aName); anIter.More(); anIter.Next()) {
85 // iterator goes in reversed order relatively to the Builder, to, make the list reversed
86 aShapePairs.push_front(std::pair<TopoDS_Shape, TopoDS_Shape>
87 (anIter.OldShape(), anIter.NewShape()));
91 TNaming_Builder aBuilder(theLab);
92 TNaming_Evolution anEvol = (TNaming_Evolution)(anEvolution);
93 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter = aShapePairs.begin();
94 for(; aPairsIter != aShapePairs.end(); aPairsIter++) {
95 if (theFlag) { // disabled => make selection
96 aBuilder.Select(aPairsIter->second, aPairsIter->first);
97 } else if (anEvol == TNaming_GENERATED) {
98 aBuilder.Generated(aPairsIter->first, aPairsIter->second);
99 } else if (anEvol == TNaming_MODIFY) {
100 aBuilder.Modify(aPairsIter->first, aPairsIter->second);
101 } else if (anEvol == TNaming_DELETE) {
102 aBuilder.Delete(aPairsIter->first);
103 } else if (anEvol == TNaming_PRIMITIVE) {
104 aBuilder.Generated(aPairsIter->second);
105 } else if (anEvol == TNaming_SELECTED) {
106 aBuilder.Select(aPairsIter->second, aPairsIter->first);
110 // recursive call for all sub-labels
111 TDF_ChildIterator anIter(theLab, Standard_False);
112 for(; anIter.More(); anIter.Next()) {
113 evolutionToSelectionRec(anIter.Value(), theFlag);
117 void Model_BodyBuilder::evolutionToSelection(const bool theFlag)
119 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
120 if (!aData) // unknown case
122 TDF_Label& aShapeLab = aData->shapeLab();
123 evolutionToSelectionRec(aShapeLab, theFlag);
126 void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape,
127 const bool theIsStoreSameShapes)
129 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
131 TDF_Label& aShapeLab = aData->shapeLab();
134 // store the new shape as primitive
135 TNaming_Builder aBuilder(aShapeLab);
138 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
140 return; // null shape inside
142 if(!theIsStoreSameShapes) {
143 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
144 if(!aNS.IsNull() && !aNS->IsEmpty()) {
145 // This shape is already in document, store reference instead of shape;
146 const TDF_Label aFoundLabel = aNS->Label();
147 TDF_Reference::Set(aShapeLab, aFoundLabel);
148 aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
153 aBuilder.Generated(aShape);
155 aShapeLab.ForgetAttribute(TDF_Reference::GetID());
156 if(!aBuilder.NamedShape()->IsEmpty()) {
157 Handle(TDataStd_Name) anAttr;
158 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
159 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
161 std::shared_ptr<Model_Document> aDoc =
162 std::dynamic_pointer_cast<Model_Document>(document());
163 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
170 void Model_BodyBuilder::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
171 const std::shared_ptr<GeomAPI_Shape>& theToShape)
173 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
175 TDF_Label& aShapeLab = aData->shapeLab();
178 // store the new shape as primitive
179 TNaming_Builder aBuilder(aShapeLab);
180 if (!theFromShape || !theToShape)
182 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
183 if (aShapeBasis.IsNull())
184 return; // null shape inside
185 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
186 if (aShapeNew.IsNull())
187 return; // null shape inside
188 aBuilder.Generated(aShapeBasis, aShapeNew);
190 if(!aBuilder.NamedShape()->IsEmpty()) {
191 Handle(TDataStd_Name) anAttr;
192 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
193 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
195 std::shared_ptr<Model_Document> aDoc =
196 std::dynamic_pointer_cast<Model_Document>(document());
197 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
204 void Model_BodyBuilder::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
205 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
207 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
209 TDF_Label& aShapeLab = aData->shapeLab();
212 // store the new shape as primitive
213 TNaming_Builder aBuilder(aShapeLab);
214 if (!theOldShape || !theNewShape)
216 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
217 if (aShapeOld.IsNull())
218 return; // null shape inside
219 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
220 if (aShapeNew.IsNull())
221 return; // null shape inside
222 aBuilder.Modify(aShapeOld, aShapeNew);
223 if(!aBuilder.NamedShape()->IsEmpty()) {
224 Handle(TDataStd_Name) anAttr;
225 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
226 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
228 std::shared_ptr<Model_Document> aDoc =
229 std::dynamic_pointer_cast<Model_Document>(document());
230 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
237 void Model_BodyBuilder::storeWithoutNaming(const std::shared_ptr<GeomAPI_Shape>& theShape)
239 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
244 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
246 return; // null shape inside
247 TNaming_Builder aBuilder(aData->shapeLab());
248 aBuilder.Select(aShape, aShape);
252 void Model_BodyBuilder::clean()
254 std::map<int, TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
255 for(; aBuilder != myBuilders.end(); aBuilder++)
256 delete aBuilder->second;
260 Model_BodyBuilder::~Model_BodyBuilder()
265 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
267 std::map<int, TNaming_Builder*>::iterator aFind = myBuilders.find(theTag);
268 if (aFind == myBuilders.end()) {
269 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
270 myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
271 aFind = myBuilders.find(theTag);
273 return aFind->second;
276 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
278 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
279 //aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), theName);
280 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(), theName.c_str());
282 void Model_BodyBuilder::generated(
283 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
285 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
286 builder(theTag)->Generated(aShape);
288 buildName(theTag, theName);
291 void Model_BodyBuilder::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
292 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
294 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
295 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
296 builder(theTag)->Generated(anOldShape, aNewShape);
298 buildName(theTag, theName);
299 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
300 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
301 TopAbs_ShapeEnum anExplodeShapeType = aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
302 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
304 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
305 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
306 TDF_Label aChildLabel = aLabel.FindChild(aTag);
307 TNaming_Builder aBuilder(aChildLabel);
308 aBuilder.Generated(anOldShape, anExp.Current());
309 TCollection_AsciiString aChildName = TCollection_AsciiString((theName + "_").c_str()) + aTag;
310 //aDoc->addNamingName(aChildLabel, aChildName.ToCString());
311 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
318 void Model_BodyBuilder::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
319 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
321 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
322 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
323 builder(theTag)->Modify(anOldShape, aNewShape);
325 buildName(theTag, theName);
328 void Model_BodyBuilder::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
331 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
332 builder(theTag)->Delete(aShape);
335 void Model_BodyBuilder::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
336 std::shared_ptr<GeomAPI_Shape> theShapeIn,
337 const int theKindOfShape,
340 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
341 TopTools_MapOfShape aView;
342 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
343 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
344 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
345 if (!aView.Add(aRoot)) continue;
346 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
347 aRShape->setImpl((new TopoDS_Shape(aRoot)));
348 if (theMS->isDeleted (aRShape)) {
349 builder(theTag)->Delete(aRoot);
354 void Model_BodyBuilder::loadAndOrientModifiedShapes (
355 GeomAlgoAPI_MakeShape* theMS,
356 std::shared_ptr<GeomAPI_Shape> theShapeIn,
357 const int theKindOfShape,
359 const std::string& theName,
360 GeomAPI_DataMapOfShapeShape& theSubShapes,
361 const bool theIsStoreSeparate,
362 const bool theIsStoreAsGenerated)
366 bool isBuilt = !theName.empty();
367 std::string aName = theName;
368 std::ostringstream aStream;
369 GeomShapePtr aResultShape = shape();
370 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
371 TopTools_MapOfShape aView;
372 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
373 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
374 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
375 if (!aView.Add(aRoot)) continue;
376 if (TNaming_Tool::NamedShape(aRoot, builder(theTag)->NamedShape()->Label()).IsNull())
377 continue; // there is no sence to write history is old shape does not persented in document
379 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
380 aRShape->setImpl((new TopoDS_Shape(aRoot)));
381 theMS->modified(aRShape, aList);
382 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
383 anIt = aList.begin(), aLast = aList.end();
384 for (; anIt != aLast; anIt++) {
385 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
386 if (theSubShapes.isBound(*anIt)) {
387 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
388 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
390 GeomShapePtr aGeomNewShape(new GeomAPI_Shape());
391 aGeomNewShape->setImpl(new TopoDS_Shape(aNewShape));
392 if(!aRoot.IsSame(aNewShape) && aResultShape->isSubShape(aGeomNewShape)) {
393 if(theIsStoreAsGenerated) {
394 // Here we store shapes as generated, to avoid problem when one parent shape produce
395 // several child shapes. In this case naming could not determine which shape to select.
396 builder(aTag)->Generated(aRoot,aNewShape);
398 builder(aTag)->Modify(aRoot,aNewShape);
401 if(theIsStoreSeparate) {
402 aStream.str(std::string());
404 aStream << theName << "_" << anIndex++;
405 aName = aStream.str();
407 buildName(aTag, aName);
409 if(theIsStoreSeparate) {
417 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
418 GeomAlgoAPI_MakeShape* theMS,
419 std::shared_ptr<GeomAPI_Shape> theShapeIn,
420 const int theKindOfShape,
422 const std::string& theName,
423 GeomAPI_DataMapOfShapeShape& theSubShapes)
425 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
426 TopTools_MapOfShape aView;
427 bool isBuilt = !theName.empty();
428 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
429 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
430 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
431 if (!aView.Add(aRoot)) continue;
432 if (TNaming_Tool::NamedShape(aRoot, builder(theTag)->NamedShape()->Label()).IsNull())
433 continue; // there is no sence to write history is old shape does not persented in document
435 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
436 aRShape->setImpl((new TopoDS_Shape(aRoot)));
437 theMS->generated(aRShape, aList);
438 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
439 anIt = aList.begin(), aLast = aList.end();
440 for (; anIt != aLast; anIt++) {
441 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
442 if (theSubShapes.isBound(*anIt)) {
443 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
444 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
446 if (!aRoot.IsSame (aNewShape)) {
447 builder(theTag)->Generated(aRoot,aNewShape);
449 buildName(theTag, theName);
451 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
452 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
453 TopAbs_ShapeEnum anExplodeShapeType =
454 aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
455 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
457 std::shared_ptr<Model_Document> aDoc =
458 std::dynamic_pointer_cast<Model_Document>(document());
459 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
460 TDF_Label aChildLabel = aLabel.FindChild(aTag);
461 TNaming_Builder aBuilder(aChildLabel);
462 aBuilder.Generated(aRoot, anExp.Current());
463 TCollection_AsciiString aChildName =
464 TCollection_AsciiString((theName + "_").c_str()) + aTag;
465 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
473 //=======================================================================
474 int getDangleShapes(const TopoDS_Shape& theShapeIn,
475 const TopAbs_ShapeEnum theGeneratedFrom,
476 TopTools_DataMapOfShapeShape& theDangles)
479 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
480 TopAbs_ShapeEnum GeneratedTo;
481 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
482 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
483 else return Standard_False;
484 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
485 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
486 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
487 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
488 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
490 return theDangles.Extent();
493 //=======================================================================
494 void loadGeneratedDangleShapes(
495 const TopoDS_Shape& theShapeIn,
496 const TopAbs_ShapeEnum theGeneratedFrom,
497 TNaming_Builder * theBuilder)
499 TopTools_DataMapOfShapeShape dangles;
500 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
501 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
502 for (; itr.More(); itr.Next())
503 theBuilder->Generated(itr.Key(), itr.Value());
506 //=======================================================================
507 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
508 const std::string& theName, int& theTag)
510 if(theShape->isNull()) return;
511 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
513 if (aShape.ShapeType() == TopAbs_SOLID) {
514 TopExp_Explorer expl(aShape, TopAbs_FACE);
515 for (; expl.More(); expl.Next()) {
516 builder(theTag)->Generated(expl.Current());
517 TCollection_AsciiString aStr(theTag);
518 aName = theName + aStr.ToCString();
519 buildName(theTag, aName);
523 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
524 // load faces and all the free edges
525 TopTools_IndexedMapOfShape Faces;
526 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
527 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
528 TopExp_Explorer expl(aShape, TopAbs_FACE);
529 for (; expl.More(); expl.Next()) {
530 builder(theTag)->Generated(expl.Current());
531 TCollection_AsciiString aStr(theTag);
532 aName = theName + aStr.ToCString();
533 buildName(theTag, aName);
537 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
538 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
539 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
541 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
542 if (aLL.Extent() < 2) {
543 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
545 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
546 TCollection_AsciiString aStr(theTag);
547 aName = theName + aStr.ToCString();
548 buildName(theTag, aName);
551 TopTools_ListIteratorOfListOfShape anIter(aLL);
552 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
554 if(aFace.IsEqual(anIter.Value())) {
555 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
556 TCollection_AsciiString aStr(theTag);
557 aName = theName + aStr.ToCString();
558 buildName(theTag, aName);
563 } else if (aShape.ShapeType() == TopAbs_WIRE) {
564 TopTools_IndexedMapOfShape Edges;
565 BRepTools::Map3DEdges(aShape, Edges);
566 if (Edges.Extent() == 1) {
567 builder(++theTag)->Generated(Edges.FindKey(1));
568 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
569 for (; expl.More(); expl.Next()) {
570 builder(theTag)->Generated(expl.Current());
571 TCollection_AsciiString aStr(theTag);
572 aName = theName + aStr.ToCString();
573 buildName(theTag, aName);
577 TopExp_Explorer expl(aShape, TopAbs_EDGE);
578 for (; expl.More(); expl.Next()) {
579 builder(theTag)->Generated(expl.Current());
580 TCollection_AsciiString aStr(theTag);
581 aName = theName + aStr.ToCString();
582 buildName(theTag, aName);
585 // and load generated vertices.
586 TopTools_DataMapOfShapeShape generated;
587 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
589 TNaming_Builder* pBuilder = builder(theTag++);
590 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
593 } else if (aShape.ShapeType() == TopAbs_EDGE) {
594 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
595 for (; expl.More(); expl.Next()) {
596 builder(theTag)->Generated(expl.Current());
597 TCollection_AsciiString aStr(theTag);
598 aName = theName + aStr.ToCString();
599 buildName(theTag, aName);
605 //=======================================================================
606 int findAmbiguities(const TopoDS_Shape& theShapeIn,
607 TopTools_ListOfShape& theList)
611 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
612 TopAbs_ShapeEnum aTS(TopAbs_EDGE);
613 TopAbs_ShapeEnum aTA(TopAbs_FACE);
614 TopTools_MapOfShape aMap1, aMap2; // map1 - for edge ancestors; map2 - for keys => edges
615 TopTools_ListOfShape aKeyList;
616 TopExp::MapShapesAndAncestors(theShapeIn, aTS, aTA, subShapeAndAncestors);
617 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
618 const TopoDS_Shape& aKeyEdge1 = subShapeAndAncestors.FindKey(i);
619 const TopTools_ListOfShape& ancestors1 = subShapeAndAncestors.FindFromIndex(i);
621 TopTools_ListIteratorOfListOfShape it(ancestors1);
622 for(;it.More();it.Next()) aMap1.Add(it.Value()); // fill map with key ancestors => aKey1
623 for (Standard_Integer j = 1; j <= subShapeAndAncestors.Extent(); j++) {
624 if (i == j) continue;
625 const TopoDS_Shape& aKeyEdge2 = subShapeAndAncestors.FindKey(j);
626 const TopTools_ListOfShape& ancestors2 = subShapeAndAncestors.FindFromIndex(j);
627 if(ancestors1.Extent() == ancestors2.Extent() && ancestors1.Extent() > 1) {
628 int aNum (ancestors2.Extent());
629 TopTools_ListIteratorOfListOfShape it(ancestors2);
630 for(;it.More();it.Next())
631 if(aMap1.Contains(it.Value())) aNum--;
633 if(aMap2.Add(aKeyEdge1))
634 aKeyList.Append(aKeyEdge1);
635 if(aMap2.Add(aKeyEdge2))
636 aKeyList.Append(aKeyEdge2);
639 } // at the end ==> List of edges to be named in addition
641 aNumEdges = aKeyList.Extent();
643 theList.Assign(aKeyList);
647 //=======================================================================
648 void Model_BodyBuilder::loadFirstLevel(
649 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
651 if(theShape->isNull()) return;
652 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
654 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
655 TopoDS_Iterator itr(aShape);
656 for (; itr.More(); itr.Next(),theTag++) {
657 builder(theTag)->Generated(itr.Value());
658 TCollection_AsciiString aStr(theTag);
659 aName = theName + aStr.ToCString();
660 buildName(theTag, aName);
661 if(!theName.empty()) buildName(theTag, aName);
662 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
663 itr.Value().ShapeType() == TopAbs_COMPSOLID)
665 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
666 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
667 loadFirstLevel(itrShape, theName, theTag);
669 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
670 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
671 loadNextLevels(itrShape, theName, theTag);
675 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
676 itrShape->setImpl(new TopoDS_Shape(aShape));
677 loadNextLevels(itrShape, theName, theTag);
679 TopTools_ListOfShape aList;
680 if(findAmbiguities(aShape, aList)) {
681 TopTools_ListIteratorOfListOfShape it(aList);
682 for (; it.More(); it.Next(),theTag++) {
683 builder(theTag)->Generated(it.Value());
684 TCollection_AsciiString aStr(theTag);
685 aName = theName + aStr.ToCString();
686 buildName(theTag, aName);
691 //=======================================================================
692 void Model_BodyBuilder::loadDisconnectedEdges(
693 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
695 if(theShape->isNull()) return;
696 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
697 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
698 TopTools_ListOfShape empty;
699 TopExp_Explorer explF(aShape, TopAbs_FACE);
700 for (; explF.More(); explF.Next()) {
701 const TopoDS_Shape& aFace = explF.Current();
702 TopExp_Explorer explV(aFace, TopAbs_EDGE);
703 for (; explV.More(); explV.Next()) {
704 const TopoDS_Shape& anEdge = explV.Current();
705 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
706 Standard_Boolean faceIsNew = Standard_True;
707 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
708 for (; itrF.More(); itrF.Next()) {
709 if (itrF.Value().IsSame(aFace)) {
710 faceIsNew = Standard_False;
715 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
719 TopTools_MapOfShape anEdgesToDelete;
720 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
722 for(;anEx.More();anEx.Next()) {
723 Standard_Boolean aC0 = Standard_False;
724 TopoDS_Shape anEdge1 = anEx.Current();
725 if (edgeNaborFaces.IsBound(anEdge1)) {
726 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
727 if (aList1.Extent()<2) continue;
728 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
729 for (; itr.More(); itr.Next()) {
730 TopoDS_Shape anEdge2 = itr.Key();
731 if(anEdgesToDelete.Contains(anEdge2)) continue;
732 if (anEdge1.IsSame(anEdge2)) continue;
733 const TopTools_ListOfShape& aList2 = itr.Value();
734 // compare lists of the neighbour faces of edge1 and edge2
735 if (aList1.Extent() == aList2.Extent()) {
736 Standard_Integer aMatches = 0;
737 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
738 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
739 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
740 if (aMatches == aList1.Extent()) {
742 builder(theTag)->Generated(anEdge2);
743 anEdgesToDelete.Add(anEdge2);
744 TCollection_AsciiString aStr(theTag);
745 aName = theName + aStr.ToCString();
746 buildName(theTag, aName);
751 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
752 for(;itDelete.More();itDelete.Next())
753 edgeNaborFaces.UnBind(itDelete.Key());
754 edgeNaborFaces.UnBind(anEdge1);
757 builder(theTag)->Generated(anEdge1);
758 TCollection_AsciiString aStr(theTag);
759 aName = theName + aStr.ToCString();
760 buildName(theTag, aName);
766 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape,
767 const std::string& theName, int& theTag)
769 if(theShape->isNull()) return;
770 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
771 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
772 TopTools_ListOfShape empty;
773 TopExp_Explorer explF(aShape, TopAbs_EDGE);
774 for (; explF.More(); explF.Next()) {
775 const TopoDS_Shape& anEdge = explF.Current();
776 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
777 for (; explV.More(); explV.Next()) {
778 const TopoDS_Shape& aVertex = explV.Current();
779 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
780 Standard_Boolean faceIsNew = Standard_True;
781 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
782 for (; itrF.More(); itrF.Next()) {
783 if (itrF.Value().IsSame(anEdge)) {
784 faceIsNew = Standard_False;
789 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
794 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
795 for (; itr.More(); itr.Next()) {
796 const TopTools_ListOfShape& naborEdges = itr.Value();
797 if (naborEdges.Extent() < 2) {
798 builder(theTag)->Generated(itr.Key());
799 TCollection_AsciiString aStr(theTag);
800 aName = theName + aStr.ToCString();
801 buildName(theTag, aName);
807 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
809 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
811 TDF_Label aShapeLab = aData->shapeLab();
812 Handle(TDF_Reference) aRef;
813 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
814 aShapeLab = aRef->Get();
816 Handle(TNaming_NamedShape) aName;
817 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
818 TopoDS_Shape aShape = aName->Get();
819 if (!aShape.IsNull()) {
820 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
821 aRes->setImpl(new TopoDS_Shape(aShape));
826 return std::shared_ptr<GeomAPI_Shape>();
829 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
831 if (theShape.get()) {
832 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
833 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
835 TDF_Label& aShapeLab = aData->shapeLab();
836 Handle(TNaming_NamedShape) aName;
837 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
838 TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
839 if (aLatest.IsNull())
841 if (aLatest.IsEqual(aShape))
843 // check sub-shapes for comp-solids:
844 for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
845 if (aLatest.IsEqual(anExp.Current()))