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_ChildIDIterator.hxx>
35 #include <TDF_Reference.hxx>
36 #include <TopTools_MapOfShape.hxx>
37 #include <TopExp_Explorer.hxx>
38 #include <TopTools_ListOfShape.hxx>
39 #include <TopTools_ListIteratorOfListOfShape.hxx>
40 #include <TopTools_DataMapOfShapeListOfShape.hxx>
41 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
42 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
43 #include <TopTools_MapIteratorOfMapOfShape.hxx>
44 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
46 #include <TopTools_DataMapOfShapeShape.hxx>
48 #include <BRepTools.hxx>
49 #include <BRep_Tool.hxx>
50 #include <GeomAPI_Shape.h>
51 #include <GeomAlgoAPI_MakeShape.h>
52 #include <Config_PropManager.h>
54 //#include <TCollection_AsciiString.hxx>
55 //#include <TDF_Tool.hxx>
56 //#define DEB_IMPORT 1
58 Model_BodyBuilder::Model_BodyBuilder(ModelAPI_Object* theOwner)
59 : ModelAPI_BodyBuilder(theOwner)
63 // Converts evolution of naming shape to selection evelution and back to avoid
64 // naming support on the disabled results. Deeply in the labels tree, recursively.
65 static void evolutionToSelectionRec(TDF_Label theLab, const bool theFlag) {
66 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
67 Handle(TNaming_NamedShape) aName;
69 if (theLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
70 TNaming_Evolution aNSEvol = aName->Evolution();
71 if ((aNSEvol == TNaming_SELECTED && theFlag) ||
72 (aNSEvol != TNaming_SELECTED && !theFlag)) { // nothing to do, it is already correct
75 anEvolution = (int)(aNSEvol);
77 Handle(TDataStd_Integer) anAttrEvol;
78 if (theLab.FindAttribute(TDataStd_Integer::GetID(), anAttrEvol)) {
79 anEvolution = anAttrEvol->Get();
82 TDataStd_Integer::Set(theLab, anEvolution);
85 for(TNaming_Iterator anIter(aName); anIter.More(); anIter.Next()) {
86 // iterator goes in reversed order relatively to the Builder, to, make the list reversed
87 aShapePairs.push_front(std::pair<TopoDS_Shape, TopoDS_Shape>
88 (anIter.OldShape(), anIter.NewShape()));
92 TNaming_Builder aBuilder(theLab);
93 TNaming_Evolution anEvol = (TNaming_Evolution)(anEvolution);
94 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter = aShapePairs.begin();
95 for(; aPairsIter != aShapePairs.end(); aPairsIter++) {
96 if (theFlag) { // disabled => make selection
97 if (anEvolution == TNaming_DELETE) // issue 2274 : don't put too many same null shapes
98 aBuilder.Select(aPairsIter->first, aPairsIter->first);
99 else if (anEvolution == TNaming_PRIMITIVE)
100 aBuilder.Select(aPairsIter->second, aPairsIter->second);
102 aBuilder.Select(aPairsIter->second, aPairsIter->first);
103 } else if (anEvol == TNaming_GENERATED) {
104 aBuilder.Generated(aPairsIter->first, aPairsIter->second);
105 } else if (anEvol == TNaming_MODIFY) {
106 aBuilder.Modify(aPairsIter->first, aPairsIter->second);
107 } else if (anEvol == TNaming_DELETE) {
108 aBuilder.Delete(aPairsIter->first);
109 } else if (anEvol == TNaming_PRIMITIVE) {
110 aBuilder.Generated(aPairsIter->second);
111 } else if (anEvol == TNaming_SELECTED) {
112 aBuilder.Select(aPairsIter->second, aPairsIter->first);
116 // recursive call for all sub-labels
117 TDF_ChildIterator anIter(theLab, Standard_False);
118 for(; anIter.More(); anIter.Next()) {
119 evolutionToSelectionRec(anIter.Value(), theFlag);
123 void Model_BodyBuilder::evolutionToSelection(const bool theFlag)
125 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
126 if (!aData) // unknown case
128 TDF_Label& aShapeLab = aData->shapeLab();
129 evolutionToSelectionRec(aShapeLab, theFlag);
132 void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape,
133 const bool theIsStoreSameShapes)
135 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
137 TDF_Label& aShapeLab = aData->shapeLab();
140 // store the new shape as primitive
141 TNaming_Builder aBuilder(aShapeLab);
144 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
146 return; // null shape inside
148 if(!theIsStoreSameShapes) {
149 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
150 if(!aNS.IsNull() && !aNS->IsEmpty()) {
151 // This shape is already in document, store reference instead of shape;
152 const TDF_Label aFoundLabel = aNS->Label();
153 TDF_Reference::Set(aShapeLab, aFoundLabel);
154 aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
159 aBuilder.Generated(aShape);
161 aShapeLab.ForgetAttribute(TDF_Reference::GetID());
162 if(!aBuilder.NamedShape()->IsEmpty()) {
163 Handle(TDataStd_Name) anAttr;
164 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
165 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
167 std::shared_ptr<Model_Document> aDoc =
168 std::dynamic_pointer_cast<Model_Document>(document());
169 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
176 void Model_BodyBuilder::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
177 const std::shared_ptr<GeomAPI_Shape>& theToShape)
179 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
181 TDF_Label& aShapeLab = aData->shapeLab();
184 // store the new shape as primitive
185 TNaming_Builder aBuilder(aShapeLab);
186 if (!theFromShape || !theToShape)
188 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
189 if (aShapeBasis.IsNull())
190 return; // null shape inside
191 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
192 if (aShapeNew.IsNull())
193 return; // null shape inside
194 aBuilder.Generated(aShapeBasis, aShapeNew);
196 if(!aBuilder.NamedShape()->IsEmpty()) {
197 Handle(TDataStd_Name) anAttr;
198 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
199 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
201 std::shared_ptr<Model_Document> aDoc =
202 std::dynamic_pointer_cast<Model_Document>(document());
203 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
210 void Model_BodyBuilder::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
211 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
213 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
215 TDF_Label& aShapeLab = aData->shapeLab();
218 // store the new shape as primitive
219 TNaming_Builder aBuilder(aShapeLab);
220 if (!theOldShape || !theNewShape)
222 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
223 if (aShapeOld.IsNull())
224 return; // null shape inside
225 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
226 if (aShapeNew.IsNull())
227 return; // null shape inside
228 aBuilder.Modify(aShapeOld, aShapeNew);
229 if(!aBuilder.NamedShape()->IsEmpty()) {
230 Handle(TDataStd_Name) anAttr;
231 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
232 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
234 std::shared_ptr<Model_Document> aDoc =
235 std::dynamic_pointer_cast<Model_Document>(document());
236 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
243 void Model_BodyBuilder::storeWithoutNaming(const std::shared_ptr<GeomAPI_Shape>& theShape)
245 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
250 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
252 return; // null shape inside
253 TNaming_Builder aBuilder(aData->shapeLab());
254 aBuilder.Select(aShape, aShape);
258 void Model_BodyBuilder::clean()
260 TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>(data())->shapeLab();
263 std::map<int, TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
264 for(; aBuilder != myBuilders.end(); aBuilder++) {
265 delete aBuilder->second;
266 // clear also shapes on cleaned sub-labels (#2241)
267 Handle(TNaming_NamedShape) aNS;
268 if (aLab.FindChild(aBuilder->first).FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
275 Model_BodyBuilder::~Model_BodyBuilder()
280 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
282 std::map<int, TNaming_Builder*>::iterator aFind = myBuilders.find(theTag);
283 if (aFind == myBuilders.end()) {
284 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
285 myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
286 aFind = myBuilders.find(theTag);
288 return aFind->second;
291 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
293 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
294 //aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), theName);
295 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(), theName.c_str());
297 void Model_BodyBuilder::generated(
298 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
300 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
301 builder(theTag)->Generated(aShape);
303 buildName(theTag, theName);
306 void Model_BodyBuilder::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
307 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
309 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
310 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
311 builder(theTag)->Generated(anOldShape, aNewShape);
313 buildName(theTag, theName);
314 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
315 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
316 TopAbs_ShapeEnum anExplodeShapeType = aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
317 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
319 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
320 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
321 TDF_Label aChildLabel = aLabel.FindChild(aTag);
322 TNaming_Builder aBuilder(aChildLabel);
323 aBuilder.Generated(anOldShape, anExp.Current());
324 TCollection_AsciiString aChildName = TCollection_AsciiString((theName + "_").c_str()) + aTag;
325 //aDoc->addNamingName(aChildLabel, aChildName.ToCString());
326 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
333 void Model_BodyBuilder::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
334 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
336 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
337 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
338 builder(theTag)->Modify(anOldShape, aNewShape);
340 buildName(theTag, theName);
343 void Model_BodyBuilder::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
346 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
347 builder(theTag)->Delete(aShape);
350 void Model_BodyBuilder::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
351 std::shared_ptr<GeomAPI_Shape> theShapeIn,
352 const int theKindOfShape,
355 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
356 TopTools_MapOfShape aView;
357 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
358 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
359 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
360 if (!aView.Add(aRoot)) continue;
361 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
362 aRShape->setImpl((new TopoDS_Shape(aRoot)));
363 if (theMS->isDeleted (aRShape)) {
364 builder(theTag)->Delete(aRoot);
369 void Model_BodyBuilder::loadAndOrientModifiedShapes (
370 GeomAlgoAPI_MakeShape* theMS,
371 std::shared_ptr<GeomAPI_Shape> theShapeIn,
372 const int theKindOfShape,
374 const std::string& theName,
375 GeomAPI_DataMapOfShapeShape& theSubShapes,
376 const bool theIsStoreSeparate,
377 const bool theIsStoreAsGenerated)
381 bool isBuilt = !theName.empty();
382 std::string aName = theName;
383 std::ostringstream aStream;
384 GeomShapePtr aResultShape = shape();
385 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
386 TopTools_MapOfShape aView;
387 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
388 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
389 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
390 if (!aView.Add(aRoot)) continue;
391 if (TNaming_Tool::NamedShape(aRoot, builder(theTag)->NamedShape()->Label()).IsNull())
392 continue; // there is no sence to write history is old shape does not persented in document
394 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
395 aRShape->setImpl((new TopoDS_Shape(aRoot)));
396 theMS->modified(aRShape, aList);
397 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
398 anIt = aList.begin(), aLast = aList.end();
399 for (; anIt != aLast; anIt++) {
400 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
401 if (theSubShapes.isBound(*anIt)) {
402 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
403 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
405 GeomShapePtr aGeomNewShape(new GeomAPI_Shape());
406 aGeomNewShape->setImpl(new TopoDS_Shape(aNewShape));
407 if(!aRoot.IsSame(aNewShape) && aResultShape->isSubShape(aGeomNewShape, false)) {
408 if(theIsStoreAsGenerated) {
409 // Here we store shapes as generated, to avoid problem when one parent shape produce
410 // several child shapes. In this case naming could not determine which shape to select.
411 builder(aTag)->Generated(aRoot,aNewShape);
413 builder(aTag)->Modify(aRoot,aNewShape);
416 if(theIsStoreSeparate) {
417 aStream.str(std::string());
419 aStream << theName << "_" << anIndex++;
420 aName = aStream.str();
422 buildName(aTag, aName);
424 if(theIsStoreSeparate) {
432 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
433 GeomAlgoAPI_MakeShape* theMS,
434 std::shared_ptr<GeomAPI_Shape> theShapeIn,
435 const int theKindOfShape,
437 const std::string& theName,
438 GeomAPI_DataMapOfShapeShape& theSubShapes)
440 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
441 TopTools_MapOfShape aView;
442 bool isBuilt = !theName.empty();
443 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
444 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
445 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
446 if (!aView.Add(aRoot)) continue;
447 if (TNaming_Tool::NamedShape(aRoot, builder(theTag)->NamedShape()->Label()).IsNull())
448 continue; // there is no sence to write history is old shape does not persented in document
450 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
451 aRShape->setImpl((new TopoDS_Shape(aRoot)));
452 theMS->generated(aRShape, aList);
453 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
454 anIt = aList.begin(), aLast = aList.end();
455 for (; anIt != aLast; anIt++) {
456 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
457 if (theSubShapes.isBound(*anIt)) {
458 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
459 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
461 if (!aRoot.IsSame (aNewShape)) {
462 builder(theTag)->Generated(aRoot,aNewShape);
464 buildName(theTag, theName);
466 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
467 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
468 TopAbs_ShapeEnum anExplodeShapeType =
469 aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
470 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
472 std::shared_ptr<Model_Document> aDoc =
473 std::dynamic_pointer_cast<Model_Document>(document());
474 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
475 TDF_Label aChildLabel = aLabel.FindChild(aTag);
476 TNaming_Builder aBuilder(aChildLabel);
477 aBuilder.Generated(aRoot, anExp.Current());
478 TCollection_AsciiString aChildName =
479 TCollection_AsciiString((theName + "_").c_str()) + aTag;
480 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
488 //=======================================================================
489 int getDangleShapes(const TopoDS_Shape& theShapeIn,
490 const TopAbs_ShapeEnum theGeneratedFrom,
491 TopTools_DataMapOfShapeShape& theDangles)
494 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
495 TopAbs_ShapeEnum GeneratedTo;
496 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
497 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
498 else return Standard_False;
499 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
500 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
501 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
502 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
503 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
505 return theDangles.Extent();
508 //=======================================================================
509 void loadGeneratedDangleShapes(
510 const TopoDS_Shape& theShapeIn,
511 const TopAbs_ShapeEnum theGeneratedFrom,
512 TNaming_Builder * theBuilder)
514 TopTools_DataMapOfShapeShape dangles;
515 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
516 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
517 for (; itr.More(); itr.Next())
518 theBuilder->Generated(itr.Key(), itr.Value());
521 //=======================================================================
522 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
523 const std::string& theName, int& theTag)
525 if(theShape->isNull()) return;
526 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
528 if (aShape.ShapeType() == TopAbs_SOLID) {
529 TopExp_Explorer expl(aShape, TopAbs_FACE);
530 for (; expl.More(); expl.Next()) {
531 builder(theTag)->Generated(expl.Current());
532 TCollection_AsciiString aStr(theTag);
533 aName = theName + aStr.ToCString();
534 buildName(theTag, aName);
538 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
539 // load faces and all the free edges
540 TopTools_IndexedMapOfShape Faces;
541 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
542 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
543 TopExp_Explorer expl(aShape, TopAbs_FACE);
544 for (; expl.More(); expl.Next()) {
545 builder(theTag)->Generated(expl.Current());
546 TCollection_AsciiString aStr(theTag);
547 aName = theName + aStr.ToCString();
548 buildName(theTag, aName);
552 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
553 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
554 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
556 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
557 if (aLL.Extent() < 2) {
558 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
560 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
561 TCollection_AsciiString aStr(theTag);
562 aName = theName + aStr.ToCString();
563 buildName(theTag, aName);
566 TopTools_ListIteratorOfListOfShape anIter(aLL);
567 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
569 if(aFace.IsEqual(anIter.Value())) {
570 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
571 TCollection_AsciiString aStr(theTag);
572 aName = theName + aStr.ToCString();
573 buildName(theTag, aName);
578 } else if (aShape.ShapeType() == TopAbs_WIRE) {
579 TopTools_IndexedMapOfShape Edges;
580 BRepTools::Map3DEdges(aShape, Edges);
581 if (Edges.Extent() == 1) {
582 builder(theTag++)->Generated(Edges.FindKey(1));
583 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
584 for (; expl.More(); expl.Next()) {
585 builder(theTag)->Generated(expl.Current());
586 TCollection_AsciiString aStr(theTag);
587 aName = theName + aStr.ToCString();
588 buildName(theTag, aName);
592 TopExp_Explorer expl(aShape, TopAbs_EDGE);
593 for (; expl.More(); expl.Next()) {
594 builder(theTag)->Generated(expl.Current());
595 TCollection_AsciiString aStr(theTag);
596 aName = theName + aStr.ToCString();
597 buildName(theTag, aName);
600 // and load generated vertices.
601 TopTools_DataMapOfShapeShape generated;
602 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
604 TNaming_Builder* pBuilder = builder(theTag++);
605 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
608 } else if (aShape.ShapeType() == TopAbs_EDGE) {
609 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
610 for (; expl.More(); expl.Next()) {
611 builder(theTag)->Generated(expl.Current());
612 TCollection_AsciiString aStr(theTag);
613 aName = theName + aStr.ToCString();
614 buildName(theTag, aName);
620 //=======================================================================
621 int findAmbiguities(const TopoDS_Shape& theShapeIn,
622 TopTools_ListOfShape& theList)
625 // edges -> ancestor faces list
626 TopTools_IndexedDataMapOfShapeListOfShape aSubShapeAndAncestors;
627 TopExp::MapShapesAndAncestors(theShapeIn, TopAbs_EDGE, TopAbs_FACE, aSubShapeAndAncestors);
628 // keeps the shapes which are already in the resulting list
629 TopTools_MapOfShape alreadyThere;
630 // map from faces identifier (combination of hash-codes) to list of edges produced such ID
631 NCollection_DataMap<int, NCollection_List<TopoDS_Shape> > aFacesIDs;
633 TopTools_IndexedDataMapOfShapeListOfShape::Iterator anAncestorsIter(aSubShapeAndAncestors);
634 for (; anAncestorsIter.More(); anAncestorsIter.Next()) {
635 const TopTools_ListOfShape& ancestors = anAncestorsIter.Value();
636 if (ancestors.Extent() < 2)
638 Standard_Integer anID = 0;
639 for(TopTools_ListIteratorOfListOfShape aFaceIt(ancestors); aFaceIt.More(); aFaceIt.Next()) {
640 anID ^= HashCode(aFaceIt.ChangeValue(), 1990657); // Pierpont prime
642 if (aFacesIDs.IsBound(anID)) { // there found same edge, check they really have same faces
643 const NCollection_List<TopoDS_Shape>& aSameFaces1 =
644 aSubShapeAndAncestors.FindFromKey(anAncestorsIter.Key());
645 NCollection_List<TopoDS_Shape>::Iterator aSameEdge(aFacesIDs.ChangeFind(anID));
646 for(; aSameEdge.More(); aSameEdge.Next()) {
647 const NCollection_List<TopoDS_Shape>& aSameFaces2 =
648 aSubShapeAndAncestors.FindFromKey(aSameEdge.Value());
649 if (aSameFaces2.Extent() != aSameFaces1.Extent()) // the number of faces is different
652 NCollection_List<TopoDS_Shape>::Iterator aFaceIter1(aSameFaces1);
653 for(; aFaceIter1.More(); aFaceIter1.Next()) {
654 NCollection_List<TopoDS_Shape>::Iterator aFaceIter2(aSameFaces2);
655 for(; aFaceIter2.More(); aFaceIter2.Next()) {
656 if (aFaceIter1.Value().IsSame(aFaceIter2.Value()))
659 if (!aFaceIter2.More()) // aFaceIter1 contains a face, which is not in aFaceIter2
662 if (!aFaceIter1.More()) { // all the faces are same => put to the result
663 if (alreadyThere.Add(aSameEdge.Value()))
664 theList.Append(aSameEdge.Value());
665 if (alreadyThere.Add(anAncestorsIter.Key()))
666 theList.Append(anAncestorsIter.Key());
669 } else { // ID is unique, just add this edge
670 aFacesIDs.Bind(anID, NCollection_List<TopoDS_Shape>());
672 aFacesIDs.ChangeFind(anID).Append(anAncestorsIter.Key()); // add to the list anyway
674 return theList.Extent();
677 //=======================================================================
678 void Model_BodyBuilder::loadFirstLevel(
679 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
681 if(theShape->isNull()) return;
682 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
684 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
685 TopoDS_Iterator itr(aShape);
686 for (; itr.More(); itr.Next()) {
687 builder(theTag)->Generated(itr.Value());
688 TCollection_AsciiString aStr(theTag);
689 aName = theName + aStr.ToCString();
690 buildName(theTag, aName);
691 if(!theName.empty()) buildName(theTag, aName);
693 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
694 itr.Value().ShapeType() == TopAbs_COMPSOLID)
696 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
697 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
698 loadFirstLevel(itrShape, theName, theTag);
700 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
701 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
702 loadNextLevels(itrShape, theName, theTag);
706 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
707 itrShape->setImpl(new TopoDS_Shape(aShape));
708 loadNextLevels(itrShape, theName, theTag);
710 TopTools_ListOfShape aList;
711 if(findAmbiguities(aShape, aList)) {
712 TopTools_ListIteratorOfListOfShape it(aList);
713 for (; it.More(); it.Next(),theTag++) {
714 builder(theTag)->Generated(it.Value());
715 TCollection_AsciiString aStr(theTag);
716 aName = theName + aStr.ToCString();
717 buildName(theTag, aName);
722 //=======================================================================
723 void Model_BodyBuilder::loadDisconnectedEdges(
724 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
726 if(theShape->isNull()) return;
727 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
728 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
729 TopTools_ListOfShape empty;
730 TopExp_Explorer explF(aShape, TopAbs_FACE);
731 for (; explF.More(); explF.Next()) {
732 const TopoDS_Shape& aFace = explF.Current();
733 TopExp_Explorer explV(aFace, TopAbs_EDGE);
734 for (; explV.More(); explV.Next()) {
735 const TopoDS_Shape& anEdge = explV.Current();
736 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
737 Standard_Boolean faceIsNew = Standard_True;
738 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
739 for (; itrF.More(); itrF.Next()) {
740 if (itrF.Value().IsSame(aFace)) {
741 faceIsNew = Standard_False;
746 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
750 TopTools_MapOfShape anEdgesToDelete;
751 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
753 for(;anEx.More();anEx.Next()) {
754 Standard_Boolean aC0 = Standard_False;
755 TopoDS_Shape anEdge1 = anEx.Current();
756 if (edgeNaborFaces.IsBound(anEdge1)) {
757 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
758 if (aList1.Extent()<2) continue;
759 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
760 for (; itr.More(); itr.Next()) {
761 TopoDS_Shape anEdge2 = itr.Key();
762 if(anEdgesToDelete.Contains(anEdge2)) continue;
763 if (anEdge1.IsSame(anEdge2)) continue;
764 const TopTools_ListOfShape& aList2 = itr.Value();
765 // compare lists of the neighbour faces of edge1 and edge2
766 if (aList1.Extent() == aList2.Extent()) {
767 Standard_Integer aMatches = 0;
768 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
769 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
770 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
771 if (aMatches == aList1.Extent()) {
773 builder(theTag)->Generated(anEdge2);
774 anEdgesToDelete.Add(anEdge2);
775 TCollection_AsciiString aStr(theTag);
776 aName = theName + aStr.ToCString();
777 buildName(theTag, aName);
782 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
783 for(;itDelete.More();itDelete.Next())
784 edgeNaborFaces.UnBind(itDelete.Key());
785 edgeNaborFaces.UnBind(anEdge1);
788 builder(theTag)->Generated(anEdge1);
789 TCollection_AsciiString aStr(theTag);
790 aName = theName + aStr.ToCString();
791 buildName(theTag, aName);
797 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape,
798 const std::string& theName, int& theTag)
800 if(theShape->isNull()) return;
801 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
802 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
803 TopTools_ListOfShape empty;
804 TopExp_Explorer explF(aShape, TopAbs_EDGE);
805 for (; explF.More(); explF.Next()) {
806 const TopoDS_Shape& anEdge = explF.Current();
807 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
808 for (; explV.More(); explV.Next()) {
809 const TopoDS_Shape& aVertex = explV.Current();
810 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
811 Standard_Boolean faceIsNew = Standard_True;
812 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
813 for (; itrF.More(); itrF.Next()) {
814 if (itrF.Value().IsSame(anEdge)) {
815 faceIsNew = Standard_False;
820 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
825 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
826 for (; itr.More(); itr.Next()) {
827 const TopTools_ListOfShape& naborEdges = itr.Value();
828 if (naborEdges.Extent() < 2) {
829 builder(theTag)->Generated(itr.Key());
830 TCollection_AsciiString aStr(theTag);
831 aName = theName + aStr.ToCString();
832 buildName(theTag, aName);
838 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
840 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
842 TDF_Label aShapeLab = aData->shapeLab();
843 Handle(TDF_Reference) aRef;
844 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
845 aShapeLab = aRef->Get();
847 Handle(TNaming_NamedShape) aName;
848 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
849 TopoDS_Shape aShape = aName->Get();
850 if (!aShape.IsNull()) {
851 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
852 aRes->setImpl(new TopoDS_Shape(aShape));
857 return std::shared_ptr<GeomAPI_Shape>();
860 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
862 if (theShape.get()) {
863 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
864 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
866 TDF_Label& aShapeLab = aData->shapeLab();
867 Handle(TNaming_NamedShape) aName;
868 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
869 TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
870 if (aLatest.IsNull())
872 if (aLatest.IsEqual(aShape))
874 // check sub-shapes for comp-solids:
875 for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
876 if (aLatest.IsEqual(anExp.Current()))