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 <ModelAPI_Session.h>
26 #include <TNaming_Builder.hxx>
27 #include <TNaming_NamedShape.hxx>
28 #include <TNaming_Iterator.hxx>
29 #include <TNaming_Tool.hxx>
30 #include <TNaming_SameShapeIterator.hxx>
31 #include <TDataStd_Name.hxx>
32 #include <TDataStd_Integer.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TDF_ChildIterator.hxx>
36 #include <TDF_ChildIDIterator.hxx>
37 #include <TDF_Reference.hxx>
38 #include <TopTools_MapOfShape.hxx>
39 #include <TopExp_Explorer.hxx>
40 #include <TopTools_ListOfShape.hxx>
41 #include <TopTools_ListIteratorOfListOfShape.hxx>
42 #include <TopTools_DataMapOfShapeListOfShape.hxx>
43 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
44 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
45 #include <TopTools_MapIteratorOfMapOfShape.hxx>
46 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48 #include <TopTools_DataMapOfShapeShape.hxx>
50 #include <BRepTools.hxx>
51 #include <BRep_Tool.hxx>
52 #include <BRepTools_History.hxx>
53 #include <GeomAPI_Shape.h>
54 #include <GeomAPI_ShapeExplorer.h>
55 #include <GeomAlgoAPI_MakeShape.h>
56 #include <GeomAlgoAPI_SortListOfShapes.h>
57 #include <Config_PropManager.h>
59 //#include <TCollection_AsciiString.hxx>
60 //#include <TDF_Tool.hxx>
61 //#define DEB_IMPORT 1
63 static const int INVALID_TAG = -1;
64 static const int GENERATED_VERTICES_TAG = 1;
65 static const int GENERATED_EDGES_TAG = 2;
66 static const int GENERATED_FACES_TAG = 3;
67 static const int MODIFIED_VERTICES_TAG = 4;
68 static const int MODIFIED_EDGES_TAG = 5;
69 static const int MODIFIED_FACES_TAG = 6;
70 static const int DELETED_TAG = 7;
71 static const int PRIMITIVES_START_TAG = 11;
73 static int getGenerationTag(const TopoDS_Shape& theShape) {
74 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
76 case TopAbs_VERTEX: return GENERATED_VERTICES_TAG;
77 case TopAbs_EDGE: return GENERATED_EDGES_TAG;
78 case TopAbs_FACE: return GENERATED_FACES_TAG;
84 static int getModificationTag(const TopoDS_Shape& theShape) {
85 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
87 case TopAbs_VERTEX: return MODIFIED_VERTICES_TAG;
88 case TopAbs_EDGE: return MODIFIED_EDGES_TAG;
89 case TopAbs_FACE: return MODIFIED_FACES_TAG;
95 static TopAbs_ShapeEnum convertShapeType(const GeomAPI_Shape::ShapeType theType) {
97 case GeomAPI_Shape::VERTEX: return TopAbs_VERTEX;
98 case GeomAPI_Shape::EDGE: return TopAbs_EDGE;
99 case GeomAPI_Shape::WIRE: return TopAbs_WIRE;
100 case GeomAPI_Shape::FACE: return TopAbs_FACE;
101 case GeomAPI_Shape::SHELL: return TopAbs_SHELL;
102 case GeomAPI_Shape::SOLID: return TopAbs_SOLID;
103 case GeomAPI_Shape::COMPSOLID: return TopAbs_COMPSOLID;
104 case GeomAPI_Shape::COMPOUND: return TopAbs_COMPOUND;
105 case GeomAPI_Shape::SHAPE: return TopAbs_SHAPE;
110 static bool isAlreadyStored(const TNaming_Builder* theBuilder,
111 const TopoDS_Shape& theOldShape,
112 const TopoDS_Shape& theNewShape)
114 for (TNaming_Iterator aNamingIt(theBuilder->NamedShape());
118 if (aNamingIt.NewShape().IsSame(theNewShape)
119 && aNamingIt.OldShape().IsSame(theOldShape))
128 Model_BodyBuilder::Model_BodyBuilder(ModelAPI_Object* theOwner)
129 : ModelAPI_BodyBuilder(theOwner),
130 myFreePrimitiveTag(PRIMITIVES_START_TAG)
134 void Model_BodyBuilder::store(const GeomShapePtr& theShape,
135 const bool theIsStoreSameShapes)
137 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
139 TDF_Label aShapeLab = aData->shapeLab();
142 // store the new shape as primitive
143 TNaming_Builder aBuilder(aShapeLab);
146 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
148 return; // null shape inside
150 if(!theIsStoreSameShapes) {
151 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
152 if(!aNS.IsNull() && !aNS->IsEmpty()) {
153 // This shape is already in document, store reference instead of shape;
154 const TDF_Label aFoundLabel = aNS->Label();
155 TDF_Reference::Set(aShapeLab, aFoundLabel);
156 aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
161 aBuilder.Generated(aShape);
163 aShapeLab.ForgetAttribute(TDF_Reference::GetID());
164 if(!aBuilder.NamedShape()->IsEmpty()) {
165 Handle(TDataStd_Name) anAttr;
166 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
167 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
169 std::shared_ptr<Model_Document> aDoc =
170 std::dynamic_pointer_cast<Model_Document>(document());
171 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
178 void Model_BodyBuilder::storeGenerated(const GeomShapePtr& theFromShape,
179 const GeomShapePtr& theToShape)
181 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
183 TDF_Label aShapeLab = aData->shapeLab();
186 // store the new shape as primitive
187 TNaming_Builder aBuilder(aShapeLab);
188 if (!theFromShape || !theToShape)
190 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
191 if (aShapeBasis.IsNull())
192 return; // null shape inside
193 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
194 if (aShapeNew.IsNull())
195 return; // null shape inside
196 aBuilder.Generated(aShapeBasis, aShapeNew);
198 if(!aBuilder.NamedShape()->IsEmpty()) {
199 Handle(TDataStd_Name) anAttr;
200 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
201 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
203 std::shared_ptr<Model_Document> aDoc =
204 std::dynamic_pointer_cast<Model_Document>(document());
205 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
212 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
214 std::map<int, TNaming_Builder*>::iterator aFind = myBuilders.find(theTag);
215 if (aFind == myBuilders.end()) {
216 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
217 myBuilders[theTag] = new TNaming_Builder(
218 theTag == 0 ? aData->shapeLab() : aData->shapeLab().FindChild(theTag));
219 aFind = myBuilders.find(theTag);
221 return aFind->second;
224 void Model_BodyBuilder::storeModified(const GeomShapePtr& theOldShape,
225 const GeomShapePtr& theNewShape,
226 const bool theIsCleanStored)
228 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
231 if (theIsCleanStored) clean();
232 // store the new shape as primitive
233 TNaming_Builder* aBuilder = builder(0);
234 if (!theOldShape || !theNewShape)
236 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
237 if (aShapeOld.IsNull())
238 return; // null shape inside
239 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
240 if (aShapeNew.IsNull())
241 return; // null shape inside
242 aBuilder->Modify(aShapeOld, aShapeNew);
243 if(!aBuilder->NamedShape()->IsEmpty()) {
244 Handle(TDataStd_Name) anAttr;
245 if(aBuilder->NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
246 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
248 std::shared_ptr<Model_Document> aDoc =
249 std::dynamic_pointer_cast<Model_Document>(document());
250 aDoc->addNamingName(aBuilder->NamedShape()->Label(), aName);
257 void Model_BodyBuilder::storeWithoutNaming(const GeomShapePtr& theShape)
259 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
264 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
266 return; // null shape inside
267 TNaming_Builder aBuilder(aData->shapeLab());
268 aBuilder.Select(aShape, aShape);
272 void Model_BodyBuilder::clean()
274 TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>(data())->shapeLab();
277 std::map<int, TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
278 for(; aBuilder != myBuilders.end(); aBuilder++) {
279 delete aBuilder->second;
280 // clear also shapes on cleaned sub-labels (#2241)
281 Handle(TNaming_NamedShape) aNS;
282 if (aLab.FindChild(aBuilder->first).FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
287 myPrimitivesNamesIndexMap.clear();
288 // remove the old reference (if any)
289 aLab.ForgetAttribute(TDF_Reference::GetID());
290 myFreePrimitiveTag = PRIMITIVES_START_TAG;
293 Model_BodyBuilder::~Model_BodyBuilder()
298 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
300 std::string aName = theName;
301 std::string aPrefix = "";
303 case GENERATED_VERTICES_TAG: aPrefix = aName.empty() ? "Generated_Vertex" : "GV:"; break;
304 case GENERATED_EDGES_TAG: aPrefix = aName.empty() ? "Generated_Edge" : "GE:"; break;
305 case GENERATED_FACES_TAG: aPrefix = aName.empty() ? "Generated_Face" : "GF:"; break;
306 case MODIFIED_VERTICES_TAG: aPrefix = aName.empty() ? "Modified_Vertex" : "MV:"; break;
307 case MODIFIED_EDGES_TAG: aPrefix = aName.empty() ? "Modified_Edge" : "ME:"; break;
308 case MODIFIED_FACES_TAG: aPrefix = aName.empty() ? "Modified_Face" : "MF:"; break;
310 aName.insert(0, aPrefix);
312 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(), aName.c_str());
314 void Model_BodyBuilder::generated(const GeomShapePtr& theNewShape,
315 const std::string& theName)
317 GeomShapePtr aResultShape = shape();
319 bool aNewShapeIsNotInResultShape = !aResultShape->isSubShape(theNewShape, false);
320 if (aNewShapeIsNotInResultShape) {
324 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
325 builder(myFreePrimitiveTag)->Generated(aShape);
326 if (!theName.empty()) {
327 std::string aName = theName;
328 if (myPrimitivesNamesIndexMap.find(theName) != myPrimitivesNamesIndexMap.end()) {
329 IndexTags& anIndexTags = myPrimitivesNamesIndexMap.find(theName)->second;
330 aName += "_" + std::to_string(++(anIndexTags.index));
331 anIndexTags.tags.push_back(myFreePrimitiveTag);
332 if (anIndexTags.index == 2) {
333 buildName(anIndexTags.tags.front(), theName + "_1");
337 IndexTags anIndexTags;
338 anIndexTags.index = 1;
339 anIndexTags.tags.push_back(myFreePrimitiveTag);
340 myPrimitivesNamesIndexMap[theName] = anIndexTags;
343 buildName(myFreePrimitiveTag, aName);
345 ++myFreePrimitiveTag;
348 void Model_BodyBuilder::generated(const GeomShapePtr& theOldShape,
349 const GeomShapePtr& theNewShape,
350 const std::string& theName)
352 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
353 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
354 TopAbs_ShapeEnum aNewShapeType = aNewShape.ShapeType();
356 if (aNewShapeType == TopAbs_WIRE || aNewShapeType == TopAbs_SHELL) {
357 // TODO: This is a workaround. New shape should be only vertex, edge or face.
358 TopAbs_ShapeEnum aShapeTypeToExplore = aNewShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
359 aTag = TopAbs_WIRE ? GENERATED_EDGES_TAG : GENERATED_FACES_TAG;
360 for (TopExp_Explorer anExp(aNewShape, aShapeTypeToExplore); anExp.More(); anExp.Next()) {
361 builder(aTag)->Generated(anOldShape, anExp.Current());
363 buildName(aTag, theName);
365 aTag = getGenerationTag(aNewShape);
366 if (aTag == INVALID_TAG) return;
367 builder(aTag)->Generated(anOldShape, aNewShape);
368 buildName(aTag, theName);
372 void Model_BodyBuilder::modified(const GeomShapePtr& theOldShape,
373 const GeomShapePtr& theNewShape,
374 const std::string& theName)
376 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
377 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
378 int aTag = getModificationTag(aNewShape);
379 if (aTag == INVALID_TAG) return;
380 builder(aTag)->Modify(anOldShape, aNewShape);
381 buildName(aTag, theName);
384 void Model_BodyBuilder::deleted(const GeomShapePtr& theOldShape)
386 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
387 builder(DELETED_TAG)->Delete(aShape);
390 void Model_BodyBuilder::loadDeletedShapes(const GeomMakeShapePtr& theAlgo,
391 const GeomShapePtr& theOldShape,
392 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
393 const GeomShapePtr& theShapesToExclude)
395 TopTools_MapOfShape anAlreadyProcessedShapes;
396 GeomShapePtr aResultShape = shape();
397 for (GeomAPI_ShapeExplorer anExp(theOldShape, theShapeTypeToExplore);
401 GeomShapePtr anOldSubShape = anExp.current();
402 const TopoDS_Shape& anOldSubShape_ = anOldSubShape->impl<TopoDS_Shape>();
403 if (!anAlreadyProcessedShapes.Add(anOldSubShape_)
404 || !theAlgo->isDeleted(anOldSubShape)
405 || aResultShape->isSubShape(anOldSubShape, false)
406 || (theShapesToExclude.get() && theShapesToExclude->isSubShape(anOldSubShape, false)))
411 ListOfShape aNewShapes;
412 if (BRepTools_History::IsSupportedType(anOldSubShape_)) { // to avoid crash in #2572
413 theAlgo->modified(anOldSubShape, aNewShapes);
416 if (aNewShapes.size() == 0
417 || (aNewShapes.size() == 1 && aNewShapes.front()->isSame(anOldSubShape))) {
418 builder(DELETED_TAG)->Delete(anOldSubShape_);
423 static void removeBadShapes(ListOfShape& theShapes)
425 ListOfShape::iterator anIt = theShapes.begin();
426 while (anIt != theShapes.end()) {
427 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
428 bool aSkip = aNewShape.IsNull()
429 || (aNewShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aNewShape)));
431 ListOfShape::iterator aRemoveIt = anIt++;
432 theShapes.erase(aRemoveIt);
439 // Keep only the shapes with minimal shape type
440 static void keepTopLevelShapes(ListOfShape& theShapes,
441 const TopoDS_Shape& theRoot,
442 const GeomShapePtr& theResultShape = GeomShapePtr())
444 GeomAPI_Shape::ShapeType aKeepShapeType = GeomAPI_Shape::SHAPE;
445 ListOfShape::iterator anIt = theShapes.begin();
446 while (anIt != theShapes.end()) {
447 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
448 bool aSkip = aNewShape.IsNull() ||
449 (aNewShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aNewShape)));
450 if (aSkip || theRoot.IsSame(aNewShape) || (theResultShape &&
451 (!theResultShape->isSubShape(*anIt, false) || theResultShape->isSame(*anIt)))) {
452 ListOfShape::iterator aRemoveIt = anIt++;
453 theShapes.erase(aRemoveIt);
455 GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
456 if (aType < aKeepShapeType) {
457 // found a shape with lesser shape type => remove all previous shapes
458 aKeepShapeType = aType;
459 theShapes.erase(theShapes.begin(), anIt);
461 } else if (aType > aKeepShapeType) {
462 // shapes with greater shape type should be removed from the list
463 ListOfShape::iterator aRemoveIt = anIt++;
464 theShapes.erase(aRemoveIt);
471 // returns an ancestor shape-type that used for naming-definition of the sub-type
472 TopAbs_ShapeEnum typeOfAncestor(const TopAbs_ShapeEnum theSubType) {
473 if (theSubType == TopAbs_VERTEX)
475 if (theSubType == TopAbs_EDGE)
477 return TopAbs_VERTEX; // bad case
480 /// Checks that shape is presented in the tree with not-selection evolution
481 static bool isShapeInTree(const TDF_Label& theAccess1, const TDF_Label& theAccess2,
482 TopoDS_Shape theShape)
484 bool aResult = TNaming_Tool::HasLabel(theAccess1, theShape);
485 if (aResult) { //check evolution and a label of this shape
486 for(TNaming_SameShapeIterator aShapes(theShape, theAccess1); aShapes.More(); aShapes.Next())
488 static Handle(TNaming_NamedShape) aNS;
489 if (aShapes.Label().FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
490 if (aNS->Evolution() != TNaming_SELECTED) {
496 if (!theAccess2.IsNull()) {
497 static const TDF_Label anEmpty;
498 return isShapeInTree(theAccess2, anEmpty, theShape);
503 void Model_BodyBuilder::loadModifiedShapes(const GeomMakeShapePtr& theAlgo,
504 const GeomShapePtr& theOldShape,
505 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
506 const std::string& theName)
508 GeomShapePtr aResultShape = shape();
509 GeomShapePtr aShapeToExplore = theOldShape;
510 if (theAlgo->isNewShapesCollected(theOldShape, theShapeTypeToExplore)) {
511 // use optimized set of old shapes for this
512 GeomShapePtr aCompound = theAlgo->oldShapesForNew(theOldShape,
514 theShapeTypeToExplore);
515 if (aCompound.get()) aShapeToExplore = aCompound;
518 TopTools_MapOfShape anAlreadyProcessedShapes;
519 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
520 for (GeomAPI_ShapeExplorer anOldShapeExp(aShapeToExplore, theShapeTypeToExplore);
521 anOldShapeExp.more();
522 anOldShapeExp.next())
524 GeomShapePtr anOldSubShape = anOldShapeExp.current();
525 const TopoDS_Shape& anOldSubShape_ = anOldSubShape->impl<TopoDS_Shape>();
527 // There is no sense to write history if shape already processed
528 // or old shape does not exist in the document.
529 bool anOldSubShapeAlreadyProcessed = !anAlreadyProcessedShapes.Add(anOldSubShape_);
530 TDF_Label anAccess2 = std::dynamic_pointer_cast<Model_Document>(
531 ModelAPI_Session::get()->moduleDocument())->generalLabel();
532 bool anOldSubShapeNotInTree = !isShapeInTree(aData->shapeLab(), anAccess2, anOldSubShape_);
533 if (anOldSubShapeAlreadyProcessed || anOldSubShapeNotInTree) {
538 ListOfShape aNewShapes;
539 theAlgo->modified(anOldSubShape, aNewShapes);
541 for (ListOfShape::const_iterator aNewShapesIt = aNewShapes.cbegin();
542 aNewShapesIt != aNewShapes.cend();
545 GeomShapePtr aNewShape = *aNewShapesIt;
546 const TopoDS_Shape& aNewShape_ = aNewShape->impl<TopoDS_Shape>();
547 bool isGenerated = anOldSubShape_.ShapeType() != aNewShape_.ShapeType();
549 bool aNewShapeIsSameAsOldShape = anOldSubShape->isSame(aNewShape);
550 bool aNewShapeIsNotInResultShape = !aResultShape->isSubShape(aNewShape, false);
551 if (aNewShapeIsSameAsOldShape
552 || aNewShapeIsNotInResultShape)
557 TNaming_Builder* aBuilder;
558 if (aResultShape->isSame(aNewShape)) {
559 // keep the modification evolution on the root level (2241 - history propagation issue)
560 aBuilder = builder(0);
561 TDF_Label aShapeLab = aBuilder->NamedShape()->Label();
562 Handle(TDF_Reference) aRef;
563 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
564 // Store only in case if it does not have reference.
568 // Check if new shape was already stored.
569 if (isAlreadyStored(aBuilder, anOldSubShape_, aNewShape_)) continue;
571 if (!aBuilder->NamedShape().IsNull() &&
572 ((isGenerated && aBuilder->NamedShape()->Evolution() != TNaming_GENERATED)
573 || (!isGenerated && aBuilder->NamedShape()->Evolution() != TNaming_MODIFY)))
575 myBuilders.erase(0); // clear old builder to avoid different evolutions crash
576 aBuilder = builder(0);
579 int aTag = isGenerated ? getGenerationTag(aNewShape_)
580 : getModificationTag(aNewShape_);
581 aBuilder = builder(aTag);
583 // Check if new shape was already stored.
584 if (isAlreadyStored(aBuilder, anOldSubShape_, aNewShape_)) continue;
586 buildName(aTag, theName);
589 isGenerated ? aBuilder->Generated(anOldSubShape_, aNewShape_)
590 : aBuilder->Modify(anOldSubShape_, aNewShape_);
595 void Model_BodyBuilder::loadGeneratedShapes(const GeomMakeShapePtr& theAlgo,
596 const GeomShapePtr& theOldShape,
597 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
598 const std::string& theName)
600 GeomShapePtr aResultShape = shape();
601 TopTools_MapOfShape anAlreadyProcessedShapes;
602 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
603 for (GeomAPI_ShapeExplorer anOldShapeExp(theOldShape, theShapeTypeToExplore);
604 anOldShapeExp.more();
605 anOldShapeExp.next())
607 GeomShapePtr anOldSubShape = anOldShapeExp.current();
608 const TopoDS_Shape& anOldSubShape_ = anOldSubShape->impl<TopoDS_Shape>();
610 // There is no sense to write history if shape already processed
611 // or old shape does not exist in the document.
612 bool anOldSubShapeAlreadyProcessed = !anAlreadyProcessedShapes.Add(anOldSubShape_);
613 TDF_Label anAccess2 = std::dynamic_pointer_cast<Model_Document>(
614 ModelAPI_Session::get()->moduleDocument())->generalLabel();
615 bool anOldSubShapeNotInTree = !isShapeInTree(aData->shapeLab(), anAccess2, anOldSubShape_);
616 if (anOldSubShapeAlreadyProcessed || anOldSubShapeNotInTree) {
621 ListOfShape aNewShapes;
622 theAlgo->generated(anOldSubShape, aNewShapes);
624 keepTopLevelShapes(aNewShapes, anOldSubShape_);
626 for (ListOfShape::const_iterator aNewShapesIt = aNewShapes.cbegin();
627 aNewShapesIt != aNewShapes.cend();
630 GeomShapePtr aNewShape = *aNewShapesIt;
631 const TopoDS_Shape& aNewShape_ = aNewShape->impl<TopoDS_Shape>();
633 bool aNewShapeIsSameAsOldShape = anOldSubShape->isSame(aNewShape);
634 bool aNewShapeIsNotInResultShape = !aResultShape->isSubShape(aNewShape, false);
635 if (aNewShapeIsSameAsOldShape
636 || aNewShapeIsNotInResultShape)
641 TopAbs_ShapeEnum aNewShapeType = aNewShape_.ShapeType();
642 if (aNewShapeType == TopAbs_WIRE || aNewShapeType == TopAbs_SHELL) {
643 // TODO: This is a workaround. New shape should be only edge or face.
644 TopAbs_ShapeEnum aShapeTypeToExplore = aNewShapeType == TopAbs_WIRE ? TopAbs_EDGE
646 int aTag = TopAbs_WIRE ? GENERATED_EDGES_TAG : GENERATED_FACES_TAG;
647 for (TopExp_Explorer anExp(aNewShape_, aShapeTypeToExplore); anExp.More(); anExp.Next()) {
648 builder(aTag)->Generated(anOldSubShape_, anExp.Current());
650 buildName(aTag, theName);
653 int aTag = getGenerationTag(aNewShape_);
654 if (aTag == INVALID_TAG) return;
655 builder(aTag)->Generated(anOldSubShape_, aNewShape_);
656 buildName(aTag, theName);
662 //=======================================================================
663 int getDangleShapes(const TopoDS_Shape& theShapeIn,
664 const TopAbs_ShapeEnum theGeneratedFrom,
665 TopTools_DataMapOfShapeShape& theDangles)
668 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
669 TopAbs_ShapeEnum GeneratedTo;
670 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
671 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
672 else return Standard_False;
673 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
674 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
675 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
676 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
677 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
679 return theDangles.Extent();
682 //=======================================================================
683 void loadGeneratedDangleShapes(
684 const TopoDS_Shape& theShapeIn,
685 const TopAbs_ShapeEnum theGeneratedFrom,
686 TNaming_Builder * theBuilder)
688 TopTools_DataMapOfShapeShape dangles;
689 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
690 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
691 for (; itr.More(); itr.Next())
692 theBuilder->Generated(itr.Key(), itr.Value());
695 //=======================================================================
696 void Model_BodyBuilder::loadNextLevels(GeomShapePtr theShape,
697 const std::string& theName)
699 if(theShape->isNull()) return;
700 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
702 if (aShape.ShapeType() == TopAbs_SOLID) {
703 TopExp_Explorer expl(aShape, TopAbs_FACE);
704 for (; expl.More(); expl.Next()) {
705 builder(myFreePrimitiveTag)->Generated(expl.Current());
706 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
707 aName = theName + "_" + aStr.ToCString();
708 buildName(myFreePrimitiveTag, aName);
709 ++myFreePrimitiveTag;
712 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
713 // load faces and all the free edges
714 TopTools_IndexedMapOfShape Faces;
715 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
716 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
717 TopExp_Explorer expl(aShape, TopAbs_FACE);
718 for (; expl.More(); expl.Next()) {
719 builder(myFreePrimitiveTag)->Generated(expl.Current());
720 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
721 aName = theName + "_" + aStr.ToCString();
722 buildName(myFreePrimitiveTag, aName);
723 ++myFreePrimitiveTag;
726 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
727 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
728 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
730 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
731 if (aLL.Extent() < 2) {
732 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
734 builder(myFreePrimitiveTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
735 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
736 aName = theName + "_" + aStr.ToCString();
737 buildName(myFreePrimitiveTag, aName);
738 ++myFreePrimitiveTag;
740 TopTools_ListIteratorOfListOfShape anIter(aLL);
741 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
743 if(aFace.IsEqual(anIter.Value())) {
744 builder(myFreePrimitiveTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
745 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
746 aName = theName + "_" + aStr.ToCString();
747 buildName(myFreePrimitiveTag, aName);
748 ++myFreePrimitiveTag;
752 } else if (aShape.ShapeType() == TopAbs_WIRE) {
753 TopTools_IndexedMapOfShape Edges;
754 BRepTools::Map3DEdges(aShape, Edges);
755 if (Edges.Extent() == 1) {
756 builder(myFreePrimitiveTag++)->Generated(Edges.FindKey(1));
757 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
758 for (; expl.More(); expl.Next()) {
759 builder(myFreePrimitiveTag)->Generated(expl.Current());
760 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
761 aName = theName + "_" + aStr.ToCString();
762 buildName(myFreePrimitiveTag, aName);
763 ++myFreePrimitiveTag;
766 TopExp_Explorer expl(aShape, TopAbs_EDGE);
767 for (; expl.More(); expl.Next()) {
768 builder(myFreePrimitiveTag)->Generated(expl.Current());
769 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
770 aName = theName + "_" + aStr.ToCString();
771 buildName(myFreePrimitiveTag, aName);
772 ++myFreePrimitiveTag;
774 // and load generated vertices.
775 TopTools_DataMapOfShapeShape generated;
776 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
778 TNaming_Builder* pBuilder = builder(myFreePrimitiveTag++);
779 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
782 } else if (aShape.ShapeType() == TopAbs_EDGE) {
783 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
784 for (; expl.More(); expl.Next()) {
785 builder(myFreePrimitiveTag)->Generated(expl.Current());
786 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
787 aName = theName + "_" + aStr.ToCString();
788 buildName(myFreePrimitiveTag, aName);
789 ++myFreePrimitiveTag;
794 //=======================================================================
795 int findAmbiguities(const TopoDS_Shape& theShapeIn,
796 TopTools_ListOfShape& theList)
799 // edges -> ancestor faces list
800 TopTools_IndexedDataMapOfShapeListOfShape aSubShapeAndAncestors;
801 TopExp::MapShapesAndAncestors(theShapeIn, TopAbs_EDGE, TopAbs_FACE, aSubShapeAndAncestors);
802 // keeps the shapes which are already in the resulting list
803 TopTools_MapOfShape alreadyThere;
804 // map from faces identifier (combination of hash-codes) to list of edges produced such ID
805 NCollection_DataMap<int, NCollection_List<TopoDS_Shape> > aFacesIDs;
807 TopTools_IndexedDataMapOfShapeListOfShape::Iterator anAncestorsIter(aSubShapeAndAncestors);
808 for (; anAncestorsIter.More(); anAncestorsIter.Next()) {
809 const TopTools_ListOfShape& ancestors = anAncestorsIter.Value();
810 if (ancestors.Extent() < 2)
812 Standard_Integer anID = 0;
813 for(TopTools_ListIteratorOfListOfShape aFaceIt(ancestors); aFaceIt.More(); aFaceIt.Next()) {
814 anID ^= HashCode(aFaceIt.ChangeValue(), 1990657); // Pierpont prime
816 if (aFacesIDs.IsBound(anID)) { // there found same edge, check they really have same faces
817 const NCollection_List<TopoDS_Shape>& aSameFaces1 =
818 aSubShapeAndAncestors.FindFromKey(anAncestorsIter.Key());
819 NCollection_List<TopoDS_Shape>::Iterator aSameEdge(aFacesIDs.ChangeFind(anID));
820 for(; aSameEdge.More(); aSameEdge.Next()) {
821 const NCollection_List<TopoDS_Shape>& aSameFaces2 =
822 aSubShapeAndAncestors.FindFromKey(aSameEdge.Value());
823 if (aSameFaces2.Extent() != aSameFaces1.Extent()) // the number of faces is different
826 NCollection_List<TopoDS_Shape>::Iterator aFaceIter1(aSameFaces1);
827 for(; aFaceIter1.More(); aFaceIter1.Next()) {
828 NCollection_List<TopoDS_Shape>::Iterator aFaceIter2(aSameFaces2);
829 for(; aFaceIter2.More(); aFaceIter2.Next()) {
830 if (aFaceIter1.Value().IsSame(aFaceIter2.Value()))
833 if (!aFaceIter2.More()) // aFaceIter1 contains a face, which is not in aFaceIter2
836 if (!aFaceIter1.More()) { // all the faces are same => put to the result
837 if (alreadyThere.Add(aSameEdge.Value()))
838 theList.Append(aSameEdge.Value());
839 if (alreadyThere.Add(anAncestorsIter.Key()))
840 theList.Append(anAncestorsIter.Key());
843 } else { // ID is unique, just add this edge
844 aFacesIDs.Bind(anID, NCollection_List<TopoDS_Shape>());
846 aFacesIDs.ChangeFind(anID).Append(anAncestorsIter.Key()); // add to the list anyway
848 return theList.Extent();
851 //=======================================================================
852 void Model_BodyBuilder::loadFirstLevel(GeomShapePtr theShape, const std::string& theName)
854 if(theShape->isNull()) return;
855 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
857 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
858 TopoDS_Iterator itr(aShape);
859 for (; itr.More(); itr.Next()) {
860 builder(myFreePrimitiveTag)->Generated(itr.Value());
861 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
862 aName = theName + "_" + aStr.ToCString();
863 buildName(myFreePrimitiveTag, aName);
864 ++myFreePrimitiveTag;
865 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
866 itr.Value().ShapeType() == TopAbs_COMPSOLID)
868 GeomShapePtr itrShape(new GeomAPI_Shape());
869 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
870 loadFirstLevel(itrShape, theName);
872 GeomShapePtr itrShape(new GeomAPI_Shape());
873 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
874 loadNextLevels(itrShape, theName);
878 GeomShapePtr itrShape(new GeomAPI_Shape());
879 itrShape->setImpl(new TopoDS_Shape(aShape));
880 loadNextLevels(itrShape, theName);
882 TopTools_ListOfShape aList;
883 if(findAmbiguities(aShape, aList)) {
884 TopTools_ListIteratorOfListOfShape it(aList);
885 for (; it.More(); it.Next(), ++myFreePrimitiveTag) {
886 builder(myFreePrimitiveTag)->Generated(it.Value());
887 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
888 aName = theName + "_" + aStr.ToCString();
889 buildName(myFreePrimitiveTag, aName);
894 //=======================================================================
895 void Model_BodyBuilder::loadDisconnectedEdges(GeomShapePtr theShape, const std::string& theName)
897 if(theShape->isNull()) return;
898 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
899 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
900 TopTools_ListOfShape empty;
901 TopExp_Explorer explF(aShape, TopAbs_FACE);
902 for (; explF.More(); explF.Next()) {
903 const TopoDS_Shape& aFace = explF.Current();
904 TopExp_Explorer explV(aFace, TopAbs_EDGE);
905 for (; explV.More(); explV.Next()) {
906 const TopoDS_Shape& anEdge = explV.Current();
907 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
908 Standard_Boolean faceIsNew = Standard_True;
909 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
910 for (; itrF.More(); itrF.Next()) {
911 if (itrF.Value().IsSame(aFace)) {
912 faceIsNew = Standard_False;
917 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
921 TopTools_MapOfShape anEdgesToDelete;
922 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
924 for(;anEx.More();anEx.Next()) {
925 Standard_Boolean aC0 = Standard_False;
926 TopoDS_Shape anEdge1 = anEx.Current();
927 if (edgeNaborFaces.IsBound(anEdge1)) {
928 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
929 if (aList1.Extent()<2) continue;
930 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
931 for (; itr.More(); itr.Next()) {
932 TopoDS_Shape anEdge2 = itr.Key();
933 if(anEdgesToDelete.Contains(anEdge2)) continue;
934 if (anEdge1.IsSame(anEdge2)) continue;
935 const TopTools_ListOfShape& aList2 = itr.Value();
936 // compare lists of the neighbor faces of edge1 and edge2
937 if (aList1.Extent() == aList2.Extent()) {
938 Standard_Integer aMatches = 0;
939 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
940 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
941 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
942 if (aMatches == aList1.Extent()) {
944 builder(myFreePrimitiveTag)->Generated(anEdge2);
945 anEdgesToDelete.Add(anEdge2);
946 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
947 aName = theName + "_" + aStr.ToCString();
948 buildName(myFreePrimitiveTag, aName);
949 ++myFreePrimitiveTag;
953 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
954 for(;itDelete.More();itDelete.Next())
955 edgeNaborFaces.UnBind(itDelete.Key());
956 edgeNaborFaces.UnBind(anEdge1);
959 builder(myFreePrimitiveTag)->Generated(anEdge1);
960 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
961 aName = theName + "_" + aStr.ToCString();
962 buildName(myFreePrimitiveTag, aName);
963 ++myFreePrimitiveTag;
968 void Model_BodyBuilder::loadDisconnectedVertexes(GeomShapePtr theShape,
969 const std::string& theName)
971 if(theShape->isNull()) return;
972 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
973 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
974 TopTools_ListOfShape empty;
975 TopExp_Explorer explF(aShape, TopAbs_EDGE);
976 for (; explF.More(); explF.Next()) {
977 const TopoDS_Shape& anEdge = explF.Current();
978 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
979 for (; explV.More(); explV.Next()) {
980 const TopoDS_Shape& aVertex = explV.Current();
981 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
982 Standard_Boolean faceIsNew = Standard_True;
983 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
984 for (; itrF.More(); itrF.Next()) {
985 if (itrF.Value().IsSame(anEdge)) {
986 faceIsNew = Standard_False;
991 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
996 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
997 for (; itr.More(); itr.Next()) {
998 const TopTools_ListOfShape& naborEdges = itr.Value();
999 if (naborEdges.Extent() < 2) {
1000 builder(myFreePrimitiveTag)->Generated(itr.Key());
1001 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
1002 aName = theName + "_" + aStr.ToCString();
1003 buildName(myFreePrimitiveTag, aName);
1004 ++myFreePrimitiveTag;
1009 GeomShapePtr Model_BodyBuilder::shape()
1011 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
1012 if (aData && aData->isValid()) {
1013 TDF_Label aShapeLab = aData->shapeLab();
1014 Handle(TDF_Reference) aRef;
1015 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
1016 aShapeLab = aRef->Get();
1018 Handle(TNaming_NamedShape) aName;
1019 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
1020 TopoDS_Shape aShape = aName->Get();
1021 if (!aShape.IsNull()) {
1022 GeomShapePtr aRes(new GeomAPI_Shape);
1023 aRes->setImpl(new TopoDS_Shape(aShape));
1028 return GeomShapePtr();
1031 bool Model_BodyBuilder::isLatestEqual(const GeomShapePtr& theShape)
1033 if (theShape.get()) {
1034 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
1035 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
1037 TDF_Label aShapeLab = aData->shapeLab();
1038 Handle(TNaming_NamedShape) aName;
1039 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
1040 TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
1041 if (aLatest.IsNull())
1043 if (aLatest.IsEqual(aShape))
1045 // check sub-shapes for comp-solids:
1046 for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
1047 if (aLatest.IsEqual(anExp.Current()))