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)) {
273 // remove the old reference (if any)
274 aLab.ForgetAttribute(TDF_Reference::GetID());
277 Model_BodyBuilder::~Model_BodyBuilder()
282 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
284 std::map<int, TNaming_Builder*>::iterator aFind = myBuilders.find(theTag);
285 if (aFind == myBuilders.end()) {
286 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
287 myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
288 aFind = myBuilders.find(theTag);
290 return aFind->second;
293 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
295 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
296 //aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), theName);
297 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(), theName.c_str());
299 void Model_BodyBuilder::generated(
300 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
302 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
303 builder(theTag)->Generated(aShape);
305 buildName(theTag, theName);
308 void Model_BodyBuilder::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
309 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
311 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
312 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
313 builder(theTag)->Generated(anOldShape, aNewShape);
315 buildName(theTag, theName);
316 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
317 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
318 TopAbs_ShapeEnum anExplodeShapeType = aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
319 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
321 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
322 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
323 TDF_Label aChildLabel = aLabel.FindChild(aTag);
324 TNaming_Builder aBuilder(aChildLabel);
325 aBuilder.Generated(anOldShape, anExp.Current());
326 TCollection_AsciiString aChildName = TCollection_AsciiString((theName + "_").c_str()) + aTag;
327 //aDoc->addNamingName(aChildLabel, aChildName.ToCString());
328 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
335 void Model_BodyBuilder::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
336 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
338 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
339 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
340 builder(theTag)->Modify(anOldShape, aNewShape);
342 buildName(theTag, theName);
345 void Model_BodyBuilder::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
348 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
349 builder(theTag)->Delete(aShape);
352 void Model_BodyBuilder::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
353 std::shared_ptr<GeomAPI_Shape> theShapeIn,
354 const int theKindOfShape,
357 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
358 TopTools_MapOfShape aView;
359 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
360 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
361 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
362 if (!aView.Add(aRoot)) continue;
363 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
364 aRShape->setImpl((new TopoDS_Shape(aRoot)));
365 if (theMS->isDeleted (aRShape)) {
366 builder(theTag)->Delete(aRoot);
371 void Model_BodyBuilder::loadAndOrientModifiedShapes (
372 GeomAlgoAPI_MakeShape* theMS,
373 std::shared_ptr<GeomAPI_Shape> theShapeIn,
374 const int theKindOfShape,
376 const std::string& theName,
377 GeomAPI_DataMapOfShapeShape& theSubShapes,
378 const bool theIsStoreSeparate,
379 const bool theIsStoreAsGenerated)
383 bool isBuilt = !theName.empty();
384 std::string aName = theName;
385 std::ostringstream aStream;
386 GeomShapePtr aResultShape = shape();
387 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
388 TopTools_MapOfShape aView;
389 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
390 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
391 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
392 if (!aView.Add(aRoot)) continue;
393 if (TNaming_Tool::NamedShape(aRoot, builder(theTag)->NamedShape()->Label()).IsNull())
394 continue; // there is no sense to write history if old shape does not exist in the document
396 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
397 aRShape->setImpl((new TopoDS_Shape(aRoot)));
398 theMS->modified(aRShape, aList);
399 // to trace situation where several objects are produced by one parent (#2317)
400 int aSameParentShapes = -1;
401 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
402 anIt = aList.begin(), aLast = aList.end();
403 for (; anIt != aLast; anIt++) {
404 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
405 if (theSubShapes.isBound(*anIt)) {
406 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
407 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
409 GeomShapePtr aGeomNewShape(new GeomAPI_Shape());
410 aGeomNewShape->setImpl(new TopoDS_Shape(aNewShape));
411 if(!aRoot.IsSame(aNewShape) && aResultShape->isSubShape(aGeomNewShape, false)) {
412 int aBuilderTag = aTag;
413 if (!theIsStoreSeparate)
415 if (aSameParentShapes > 0) { // store in other label (
416 aBuilderTag = 100000 - aSameParentShapes * 10 - aTag;
418 if(theIsStoreAsGenerated) {
419 // Here we store shapes as generated, to avoid problem when one parent shape produce
420 // several child shapes. In this case naming could not determine which shape to select.
421 builder(aBuilderTag)->Generated(aRoot,aNewShape);
423 builder(aBuilderTag)->Modify(aRoot,aNewShape);
426 if(theIsStoreSeparate) {
427 aStream.str(std::string());
429 aStream << theName << "_" << anIndex++;
430 aName = aStream.str();
432 if (aSameParentShapes > 0) {
433 aStream.str(std::string());
435 aStream << aName << "_" << aSameParentShapes << "divided";
436 std::string aNameDiv = aStream.str();
437 buildName(aBuilderTag, aNameDiv);
439 buildName(aBuilderTag, aName);
442 if(theIsStoreSeparate) {
450 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
451 GeomAlgoAPI_MakeShape* theMS,
452 std::shared_ptr<GeomAPI_Shape> theShapeIn,
453 const int theKindOfShape,
455 const std::string& theName,
456 GeomAPI_DataMapOfShapeShape& theSubShapes)
458 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
459 TopTools_MapOfShape aView;
460 bool isBuilt = !theName.empty();
461 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
462 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
463 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
464 if (!aView.Add(aRoot)) continue;
465 //if (TNaming_Tool::NamedShape(aRoot, builder(theTag)->NamedShape()->Label()).IsNull())
466 // continue; // there is no sense to write history if old shape does not exist in the document
468 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
469 aRShape->setImpl((new TopoDS_Shape(aRoot)));
470 theMS->generated(aRShape, aList);
471 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
472 anIt = aList.begin(), aLast = aList.end();
473 for (; anIt != aLast; anIt++) {
474 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
475 if (theSubShapes.isBound(*anIt)) {
476 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
477 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
479 if (!aRoot.IsSame (aNewShape)) {
480 builder(theTag)->Generated(aRoot,aNewShape);
482 buildName(theTag, theName);
484 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
485 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
486 TopAbs_ShapeEnum anExplodeShapeType =
487 aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
488 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
490 std::shared_ptr<Model_Document> aDoc =
491 std::dynamic_pointer_cast<Model_Document>(document());
492 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
493 TDF_Label aChildLabel = aLabel.FindChild(aTag);
494 TNaming_Builder aBuilder(aChildLabel);
495 aBuilder.Generated(aRoot, anExp.Current());
496 TCollection_AsciiString aChildName =
497 TCollection_AsciiString((theName + "_").c_str()) + aTag;
498 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
506 //=======================================================================
507 int getDangleShapes(const TopoDS_Shape& theShapeIn,
508 const TopAbs_ShapeEnum theGeneratedFrom,
509 TopTools_DataMapOfShapeShape& theDangles)
512 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
513 TopAbs_ShapeEnum GeneratedTo;
514 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
515 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
516 else return Standard_False;
517 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
518 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
519 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
520 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
521 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
523 return theDangles.Extent();
526 //=======================================================================
527 void loadGeneratedDangleShapes(
528 const TopoDS_Shape& theShapeIn,
529 const TopAbs_ShapeEnum theGeneratedFrom,
530 TNaming_Builder * theBuilder)
532 TopTools_DataMapOfShapeShape dangles;
533 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
534 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
535 for (; itr.More(); itr.Next())
536 theBuilder->Generated(itr.Key(), itr.Value());
539 //=======================================================================
540 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
541 const std::string& theName, int& theTag)
543 if(theShape->isNull()) return;
544 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
546 if (aShape.ShapeType() == TopAbs_SOLID) {
547 TopExp_Explorer expl(aShape, TopAbs_FACE);
548 for (; expl.More(); expl.Next()) {
549 builder(theTag)->Generated(expl.Current());
550 TCollection_AsciiString aStr(theTag);
551 aName = theName + aStr.ToCString();
552 buildName(theTag, aName);
556 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
557 // load faces and all the free edges
558 TopTools_IndexedMapOfShape Faces;
559 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
560 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
561 TopExp_Explorer expl(aShape, TopAbs_FACE);
562 for (; expl.More(); expl.Next()) {
563 builder(theTag)->Generated(expl.Current());
564 TCollection_AsciiString aStr(theTag);
565 aName = theName + aStr.ToCString();
566 buildName(theTag, aName);
570 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
571 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
572 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
574 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
575 if (aLL.Extent() < 2) {
576 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
578 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
579 TCollection_AsciiString aStr(theTag);
580 aName = theName + aStr.ToCString();
581 buildName(theTag, aName);
584 TopTools_ListIteratorOfListOfShape anIter(aLL);
585 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
587 if(aFace.IsEqual(anIter.Value())) {
588 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
589 TCollection_AsciiString aStr(theTag);
590 aName = theName + aStr.ToCString();
591 buildName(theTag, aName);
596 } else if (aShape.ShapeType() == TopAbs_WIRE) {
597 TopTools_IndexedMapOfShape Edges;
598 BRepTools::Map3DEdges(aShape, Edges);
599 if (Edges.Extent() == 1) {
600 builder(theTag++)->Generated(Edges.FindKey(1));
601 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
602 for (; expl.More(); expl.Next()) {
603 builder(theTag)->Generated(expl.Current());
604 TCollection_AsciiString aStr(theTag);
605 aName = theName + aStr.ToCString();
606 buildName(theTag, aName);
610 TopExp_Explorer expl(aShape, TopAbs_EDGE);
611 for (; expl.More(); expl.Next()) {
612 builder(theTag)->Generated(expl.Current());
613 TCollection_AsciiString aStr(theTag);
614 aName = theName + aStr.ToCString();
615 buildName(theTag, aName);
618 // and load generated vertices.
619 TopTools_DataMapOfShapeShape generated;
620 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
622 TNaming_Builder* pBuilder = builder(theTag++);
623 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
626 } else if (aShape.ShapeType() == TopAbs_EDGE) {
627 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
628 for (; expl.More(); expl.Next()) {
629 builder(theTag)->Generated(expl.Current());
630 TCollection_AsciiString aStr(theTag);
631 aName = theName + aStr.ToCString();
632 buildName(theTag, aName);
638 //=======================================================================
639 int findAmbiguities(const TopoDS_Shape& theShapeIn,
640 TopTools_ListOfShape& theList)
643 // edges -> ancestor faces list
644 TopTools_IndexedDataMapOfShapeListOfShape aSubShapeAndAncestors;
645 TopExp::MapShapesAndAncestors(theShapeIn, TopAbs_EDGE, TopAbs_FACE, aSubShapeAndAncestors);
646 // keeps the shapes which are already in the resulting list
647 TopTools_MapOfShape alreadyThere;
648 // map from faces identifier (combination of hash-codes) to list of edges produced such ID
649 NCollection_DataMap<int, NCollection_List<TopoDS_Shape> > aFacesIDs;
651 TopTools_IndexedDataMapOfShapeListOfShape::Iterator anAncestorsIter(aSubShapeAndAncestors);
652 for (; anAncestorsIter.More(); anAncestorsIter.Next()) {
653 const TopTools_ListOfShape& ancestors = anAncestorsIter.Value();
654 if (ancestors.Extent() < 2)
656 Standard_Integer anID = 0;
657 for(TopTools_ListIteratorOfListOfShape aFaceIt(ancestors); aFaceIt.More(); aFaceIt.Next()) {
658 anID ^= HashCode(aFaceIt.ChangeValue(), 1990657); // Pierpont prime
660 if (aFacesIDs.IsBound(anID)) { // there found same edge, check they really have same faces
661 const NCollection_List<TopoDS_Shape>& aSameFaces1 =
662 aSubShapeAndAncestors.FindFromKey(anAncestorsIter.Key());
663 NCollection_List<TopoDS_Shape>::Iterator aSameEdge(aFacesIDs.ChangeFind(anID));
664 for(; aSameEdge.More(); aSameEdge.Next()) {
665 const NCollection_List<TopoDS_Shape>& aSameFaces2 =
666 aSubShapeAndAncestors.FindFromKey(aSameEdge.Value());
667 if (aSameFaces2.Extent() != aSameFaces1.Extent()) // the number of faces is different
670 NCollection_List<TopoDS_Shape>::Iterator aFaceIter1(aSameFaces1);
671 for(; aFaceIter1.More(); aFaceIter1.Next()) {
672 NCollection_List<TopoDS_Shape>::Iterator aFaceIter2(aSameFaces2);
673 for(; aFaceIter2.More(); aFaceIter2.Next()) {
674 if (aFaceIter1.Value().IsSame(aFaceIter2.Value()))
677 if (!aFaceIter2.More()) // aFaceIter1 contains a face, which is not in aFaceIter2
680 if (!aFaceIter1.More()) { // all the faces are same => put to the result
681 if (alreadyThere.Add(aSameEdge.Value()))
682 theList.Append(aSameEdge.Value());
683 if (alreadyThere.Add(anAncestorsIter.Key()))
684 theList.Append(anAncestorsIter.Key());
687 } else { // ID is unique, just add this edge
688 aFacesIDs.Bind(anID, NCollection_List<TopoDS_Shape>());
690 aFacesIDs.ChangeFind(anID).Append(anAncestorsIter.Key()); // add to the list anyway
692 return theList.Extent();
695 //=======================================================================
696 void Model_BodyBuilder::loadFirstLevel(
697 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
699 if(theShape->isNull()) return;
700 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
702 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
703 TopoDS_Iterator itr(aShape);
704 for (; itr.More(); itr.Next()) {
705 builder(theTag)->Generated(itr.Value());
706 TCollection_AsciiString aStr(theTag);
707 aName = theName + aStr.ToCString();
708 buildName(theTag, aName);
709 if(!theName.empty()) buildName(theTag, aName);
711 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
712 itr.Value().ShapeType() == TopAbs_COMPSOLID)
714 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
715 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
716 loadFirstLevel(itrShape, theName, theTag);
718 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
719 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
720 loadNextLevels(itrShape, theName, theTag);
724 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
725 itrShape->setImpl(new TopoDS_Shape(aShape));
726 loadNextLevels(itrShape, theName, theTag);
728 TopTools_ListOfShape aList;
729 if(findAmbiguities(aShape, aList)) {
730 TopTools_ListIteratorOfListOfShape it(aList);
731 for (; it.More(); it.Next(),theTag++) {
732 builder(theTag)->Generated(it.Value());
733 TCollection_AsciiString aStr(theTag);
734 aName = theName + aStr.ToCString();
735 buildName(theTag, aName);
740 //=======================================================================
741 void Model_BodyBuilder::loadDisconnectedEdges(
742 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
744 if(theShape->isNull()) return;
745 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
746 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
747 TopTools_ListOfShape empty;
748 TopExp_Explorer explF(aShape, TopAbs_FACE);
749 for (; explF.More(); explF.Next()) {
750 const TopoDS_Shape& aFace = explF.Current();
751 TopExp_Explorer explV(aFace, TopAbs_EDGE);
752 for (; explV.More(); explV.Next()) {
753 const TopoDS_Shape& anEdge = explV.Current();
754 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
755 Standard_Boolean faceIsNew = Standard_True;
756 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
757 for (; itrF.More(); itrF.Next()) {
758 if (itrF.Value().IsSame(aFace)) {
759 faceIsNew = Standard_False;
764 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
768 TopTools_MapOfShape anEdgesToDelete;
769 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
771 for(;anEx.More();anEx.Next()) {
772 Standard_Boolean aC0 = Standard_False;
773 TopoDS_Shape anEdge1 = anEx.Current();
774 if (edgeNaborFaces.IsBound(anEdge1)) {
775 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
776 if (aList1.Extent()<2) continue;
777 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
778 for (; itr.More(); itr.Next()) {
779 TopoDS_Shape anEdge2 = itr.Key();
780 if(anEdgesToDelete.Contains(anEdge2)) continue;
781 if (anEdge1.IsSame(anEdge2)) continue;
782 const TopTools_ListOfShape& aList2 = itr.Value();
783 // compare lists of the neighbour faces of edge1 and edge2
784 if (aList1.Extent() == aList2.Extent()) {
785 Standard_Integer aMatches = 0;
786 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
787 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
788 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
789 if (aMatches == aList1.Extent()) {
791 builder(theTag)->Generated(anEdge2);
792 anEdgesToDelete.Add(anEdge2);
793 TCollection_AsciiString aStr(theTag);
794 aName = theName + aStr.ToCString();
795 buildName(theTag, aName);
800 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
801 for(;itDelete.More();itDelete.Next())
802 edgeNaborFaces.UnBind(itDelete.Key());
803 edgeNaborFaces.UnBind(anEdge1);
806 builder(theTag)->Generated(anEdge1);
807 TCollection_AsciiString aStr(theTag);
808 aName = theName + aStr.ToCString();
809 buildName(theTag, aName);
815 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape,
816 const std::string& theName, int& theTag)
818 if(theShape->isNull()) return;
819 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
820 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
821 TopTools_ListOfShape empty;
822 TopExp_Explorer explF(aShape, TopAbs_EDGE);
823 for (; explF.More(); explF.Next()) {
824 const TopoDS_Shape& anEdge = explF.Current();
825 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
826 for (; explV.More(); explV.Next()) {
827 const TopoDS_Shape& aVertex = explV.Current();
828 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
829 Standard_Boolean faceIsNew = Standard_True;
830 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
831 for (; itrF.More(); itrF.Next()) {
832 if (itrF.Value().IsSame(anEdge)) {
833 faceIsNew = Standard_False;
838 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
843 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
844 for (; itr.More(); itr.Next()) {
845 const TopTools_ListOfShape& naborEdges = itr.Value();
846 if (naborEdges.Extent() < 2) {
847 builder(theTag)->Generated(itr.Key());
848 TCollection_AsciiString aStr(theTag);
849 aName = theName + aStr.ToCString();
850 buildName(theTag, aName);
856 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
858 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
860 TDF_Label aShapeLab = aData->shapeLab();
861 Handle(TDF_Reference) aRef;
862 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
863 aShapeLab = aRef->Get();
865 Handle(TNaming_NamedShape) aName;
866 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
867 TopoDS_Shape aShape = aName->Get();
868 if (!aShape.IsNull()) {
869 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
870 aRes->setImpl(new TopoDS_Shape(aShape));
875 return std::shared_ptr<GeomAPI_Shape>();
878 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
880 if (theShape.get()) {
881 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
882 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
884 TDF_Label& aShapeLab = aData->shapeLab();
885 Handle(TNaming_NamedShape) aName;
886 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
887 TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
888 if (aLatest.IsNull())
890 if (aLatest.IsEqual(aShape))
892 // check sub-shapes for comp-solids:
893 for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
894 if (aLatest.IsEqual(anExp.Current()))