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>
33 #include <TDataStd_ExtStringList.hxx>
35 #include <TopoDS_Face.hxx>
36 #include <TDF_ChildIterator.hxx>
37 #include <TDF_ChildIDIterator.hxx>
38 #include <TDF_Reference.hxx>
39 #include <TDF_Tool.hxx>
40 #include <TopTools_MapOfShape.hxx>
41 #include <TopExp_Explorer.hxx>
42 #include <TopTools_ListOfShape.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <TopTools_DataMapOfShapeListOfShape.hxx>
45 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
46 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
47 #include <TopTools_MapIteratorOfMapOfShape.hxx>
48 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <TopTools_DataMapOfShapeShape.hxx>
52 #include <BRepTools.hxx>
53 #include <BRep_Tool.hxx>
54 #include <BRepTools_History.hxx>
55 #include <GeomAPI_Shape.h>
56 #include <GeomAPI_ShapeExplorer.h>
57 #include <GeomAlgoAPI_MakeShape.h>
58 #include <GeomAlgoAPI_SortListOfShapes.h>
59 #include <Config_PropManager.h>
61 //#include <TCollection_AsciiString.hxx>
62 //#define DEB_IMPORT 1
64 /// reference to the shape in external document: sting list attribute identifier
65 static const Standard_GUID kEXTERNAL_SHAPE_REF("9aa5dd14-6d34-4a8d-8786-05842fd7bbbd");
67 static const int INVALID_TAG = -1;
68 static const int GENERATED_VERTICES_TAG = 1;
69 static const int GENERATED_EDGES_TAG = 2;
70 static const int GENERATED_FACES_TAG = 3;
71 static const int MODIFIED_VERTICES_TAG = 4;
72 static const int MODIFIED_EDGES_TAG = 5;
73 static const int MODIFIED_FACES_TAG = 6;
74 static const int DELETED_TAG = 7;
75 static const int PRIMITIVES_START_TAG = 11;
77 static int getGenerationTag(const TopoDS_Shape& theShape) {
78 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
80 case TopAbs_VERTEX: return GENERATED_VERTICES_TAG;
81 case TopAbs_EDGE: return GENERATED_EDGES_TAG;
82 case TopAbs_FACE: return GENERATED_FACES_TAG;
88 static int getModificationTag(const TopoDS_Shape& theShape) {
89 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
91 case TopAbs_VERTEX: return MODIFIED_VERTICES_TAG;
92 case TopAbs_EDGE: return MODIFIED_EDGES_TAG;
93 case TopAbs_FACE: return MODIFIED_FACES_TAG;
99 static bool isAlreadyStored(const TNaming_Builder* theBuilder,
100 const TopoDS_Shape& theOldShape,
101 const TopoDS_Shape& theNewShape)
103 for (TNaming_Iterator aNamingIt(theBuilder->NamedShape());
107 if (aNamingIt.NewShape().IsSame(theNewShape)
108 && aNamingIt.OldShape().IsSame(theOldShape))
117 Model_BodyBuilder::Model_BodyBuilder(ModelAPI_Object* theOwner)
118 : ModelAPI_BodyBuilder(theOwner),
119 myFreePrimitiveTag(PRIMITIVES_START_TAG)
123 /// Checks that shape is presented in the tree with not-selection evolution
124 /// In theOriginalLabel it returns label where NS of old sub-shape is stored
125 static bool isShapeInTree(const TDF_Label& theAccess1, const TDF_Label& theAccess2,
126 TopoDS_Shape theShape, TDF_Label& theOriginalLabel)
128 bool aResult = TNaming_Tool::HasLabel(theAccess1, theShape);
129 if (aResult) { //check evolution and a label of this shape
130 for(TNaming_SameShapeIterator aShapes(theShape, theAccess1); aShapes.More(); aShapes.Next())
132 static Handle(TNaming_NamedShape) aNS;
133 if (aShapes.Label().FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
134 if (aNS->Evolution() != TNaming_SELECTED) {
135 theOriginalLabel = aNS->Label();
141 if (!theAccess2.IsNull()) {
142 static const TDF_Label anEmpty;
143 return isShapeInTree(theAccess2, anEmpty, theShape, theOriginalLabel);
148 /// Stores entry to the external label in the entries list at this label
149 static void storeExternalReference(const TDF_Label& theExternal, const TDF_Label theThis)
151 // store information about the external document reference to restore old shape on open
152 if (!theExternal.IsNull() && !theExternal.Root().IsEqual(theThis.Root())) {
153 Handle(TDataStd_ExtStringList) anEntries;
154 if (!theThis.FindAttribute(kEXTERNAL_SHAPE_REF, anEntries)) {
155 anEntries = TDataStd_ExtStringList::Set(theThis, kEXTERNAL_SHAPE_REF);
157 TCollection_AsciiString anEntry;
158 TDF_Tool::Entry(theExternal, anEntry);
159 // check it already contains this entry
160 TDataStd_ListOfExtendedString::Iterator anIter(anEntries->List());
161 for(; anIter.More(); anIter.Next())
162 if (anIter.Value() == anEntry)
164 if (!anIter.More()) {
165 anEntries->Append(anEntry);
170 void Model_BodyBuilder::store(const GeomShapePtr& theShape,
171 const bool theIsStoreSameShapes)
173 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
175 TDF_Label aShapeLab = aData->shapeLab();
178 // store the new shape as primitive
179 TNaming_Builder aBuilder(aShapeLab);
182 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
184 return; // null shape inside
186 if(!theIsStoreSameShapes) {
187 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
188 // the last condition is for the issue 2751 : existing shape may be found in compound-NS
189 if(!aNS.IsNull() && !aNS->IsEmpty() && aNS->Get().IsSame(aShape)) {
190 // This shape is already in document, store reference instead of shape;
191 const TDF_Label aFoundLabel = aNS->Label();
192 TDF_Reference::Set(aShapeLab, aFoundLabel);
193 aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
198 aBuilder.Generated(aShape);
200 aShapeLab.ForgetAttribute(TDF_Reference::GetID());
201 if(!aBuilder.NamedShape()->IsEmpty()) {
202 Handle(TDataStd_Name) anAttr;
203 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
204 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
206 std::shared_ptr<Model_Document> aDoc =
207 std::dynamic_pointer_cast<Model_Document>(document());
208 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
215 void Model_BodyBuilder::storeGenerated(const GeomShapePtr& theFromShape,
216 const GeomShapePtr& theToShape, const bool theIsCleanStored)
218 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
220 TDF_Label aShapeLab = aData->shapeLab();
222 if (theIsCleanStored)
224 TNaming_Builder* aBuilder = builder(0);
225 if (!theFromShape || !theToShape)
227 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
228 if (aShapeBasis.IsNull())
229 return; // null shape inside
230 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
231 if (aShapeNew.IsNull())
232 return; // null shape inside
234 // There is no sense to write history if old shape does not exist in the document.
235 TDF_Label anAccess2 = std::dynamic_pointer_cast<Model_Document>(
236 ModelAPI_Session::get()->moduleDocument())->generalLabel();
237 TDF_Label anOriginalLabel;
238 if (!isShapeInTree(aData->shapeLab(), anAccess2, aShapeBasis, anOriginalLabel)) {
239 if (aBuilder->NamedShape()->Get().IsNull()) { // store as primitive if alone anyway
240 aBuilder->Generated(aShapeNew);
243 if (aBuilder->NamedShape()->Evolution() == TNaming_PRIMITIVE) { // erase primitive before
245 aBuilder = builder(0);
248 aBuilder->Generated(aShapeBasis, aShapeNew);
249 // store information about the external document reference to restore old shape on open
250 storeExternalReference(anOriginalLabel, aBuilder->NamedShape()->Label());
254 if(!aBuilder->NamedShape()->IsEmpty()) {
255 Handle(TDataStd_Name) anAttr;
256 if(aBuilder->NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
257 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
259 std::shared_ptr<Model_Document> aDoc =
260 std::dynamic_pointer_cast<Model_Document>(document());
261 aDoc->addNamingName(aBuilder->NamedShape()->Label(), aName);
268 void Model_BodyBuilder::storeGenerated(const std::list<GeomShapePtr>& theFromShapes,
269 const GeomShapePtr& theToShape, const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
271 bool aStored = false;
272 std::list<GeomShapePtr>::const_iterator anOldIter = theFromShapes.cbegin();
273 for(; anOldIter != theFromShapes.cend(); anOldIter++) {
274 ListOfShape aNews; // check this old really generates theToShape
275 theMakeShape->generated(*anOldIter, aNews);
276 ListOfShape::iterator aNewIter = aNews.begin();
277 for(; aNewIter != aNews.end(); aNewIter++) {
278 if (theToShape->isSame(*aNewIter))
281 if (aNewIter != aNews.end()) {
282 storeGenerated(*anOldIter, theToShape, !aStored);
283 TNaming_Builder* aBuilder = builder(0);
284 aStored = !aBuilder->NamedShape()->IsEmpty();
287 if (!aStored) { // store as PRIMITIVE, but clean in any way
293 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
295 std::map<int, TNaming_Builder*>::iterator aFind = myBuilders.find(theTag);
296 if (aFind == myBuilders.end()) {
297 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
298 myBuilders[theTag] = new TNaming_Builder(
299 theTag == 0 ? aData->shapeLab() : aData->shapeLab().FindChild(theTag));
300 aFind = myBuilders.find(theTag);
302 return aFind->second;
305 void Model_BodyBuilder::storeModified(const GeomShapePtr& theOldShape,
306 const GeomShapePtr& theNewShape,
307 const bool theIsCleanStored)
309 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
312 if (theIsCleanStored) clean();
313 TNaming_Builder* aBuilder = builder(0);
314 if (!theOldShape || !theNewShape)
316 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
317 if (aShapeOld.IsNull())
318 return; // null shape inside
319 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
320 if (aShapeNew.IsNull())
321 return; // null shape inside
323 // There is no sense to write history if old shape does not exist in the document.
324 TDF_Label anAccess2 = std::dynamic_pointer_cast<Model_Document>(
325 ModelAPI_Session::get()->moduleDocument())->generalLabel();
326 TDF_Label anOriginalLabel;
327 if (!isShapeInTree(aData->shapeLab(), anAccess2, aShapeOld, anOriginalLabel)) {
328 if (aBuilder->NamedShape()->Get().IsNull()) { // store as primitive if alone anyway
329 aBuilder->Generated(aShapeNew);
332 if (aBuilder->NamedShape()->Evolution() == TNaming_PRIMITIVE) { // erase primitive before
334 aBuilder = builder(0);
337 aBuilder->Modify(aShapeOld, aShapeNew);
338 // store information about the external document reference to restore old shape on open
339 storeExternalReference(anOriginalLabel, aBuilder->NamedShape()->Label());
342 if(!aBuilder->NamedShape()->IsEmpty()) {
343 Handle(TDataStd_Name) anAttr;
344 if(aBuilder->NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
345 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
347 std::shared_ptr<Model_Document> aDoc =
348 std::dynamic_pointer_cast<Model_Document>(document());
349 aDoc->addNamingName(aBuilder->NamedShape()->Label(), aName);
356 void Model_BodyBuilder::storeModified(const std::list<GeomShapePtr>& theOldShapes,
357 const GeomShapePtr& theNewShape, const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
359 bool aStored = false;
360 std::list<GeomShapePtr>::const_iterator anOldIter = theOldShapes.cbegin();
361 for(; anOldIter != theOldShapes.cend(); anOldIter++) {
362 ListOfShape aNews; // check this old really modifies theNewShape
363 theMakeShape->modified(*anOldIter, aNews);
364 ListOfShape::iterator aNewIter = aNews.begin();
365 for(; aNewIter != aNews.end(); aNewIter++) {
366 if (theNewShape->isSame(*aNewIter))
369 if (aNewIter != aNews.end()) {
370 storeModified(*anOldIter, theNewShape, !aStored);
371 TNaming_Builder* aBuilder = builder(0);
372 aStored = !aBuilder->NamedShape()->IsEmpty();
375 if (!aStored) { // store as PRIMITIVE, but clean in any way
381 void Model_BodyBuilder::clean()
383 TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>(data())->shapeLab();
386 std::map<int, TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
387 for(; aBuilder != myBuilders.end(); aBuilder++) {
388 Handle(TNaming_NamedShape) aNS = aBuilder->second->NamedShape();
389 delete aBuilder->second;
390 if (!aNS.IsNull() && !aNS->Label().IsNull())
391 aNS->Label().ForgetAttribute(TNaming_NamedShape::GetID());
394 myPrimitivesNamesIndexMap.clear();
395 // remove the old reference (if any)
396 aLab.ForgetAttribute(TDF_Reference::GetID());
397 myFreePrimitiveTag = PRIMITIVES_START_TAG;
398 TDF_ChildIDIterator anEntriesIter(aLab, kEXTERNAL_SHAPE_REF, true);
399 for(; anEntriesIter.More(); anEntriesIter.Next()) {
400 anEntriesIter.Value()->Label().ForgetAttribute(kEXTERNAL_SHAPE_REF);
404 void Model_BodyBuilder::cleanCash()
406 myPrimitivesNamesIndexMap.clear();
409 Model_BodyBuilder::~Model_BodyBuilder()
414 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
416 std::string aName = theName;
417 std::string aPrefix = "";
419 case GENERATED_VERTICES_TAG: aPrefix = aName.empty() ? "Generated_Vertex" : "GV:"; break;
420 case GENERATED_EDGES_TAG: aPrefix = aName.empty() ? "Generated_Edge" : "GE:"; break;
421 case GENERATED_FACES_TAG: aPrefix = aName.empty() ? "Generated_Face" : "GF:"; break;
422 case MODIFIED_VERTICES_TAG: aPrefix = aName.empty() ? "Modified_Vertex" : "MV:"; break;
423 case MODIFIED_EDGES_TAG: aPrefix = aName.empty() ? "Modified_Edge" : "ME:"; break;
424 case MODIFIED_FACES_TAG: aPrefix = aName.empty() ? "Modified_Face" : "MF:"; break;
426 aName.insert(0, aPrefix);
428 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(), aName.c_str());
430 bool Model_BodyBuilder::generated(const GeomShapePtr& theNewShape,
431 const std::string& theName,
432 const bool theCheckIsInResult)
434 GeomShapePtr aResultShape = shape();
435 if (theCheckIsInResult) {
436 bool aNewShapeIsNotInResultShape = !aResultShape->isSubShape(theNewShape, false);
437 if (aNewShapeIsNotInResultShape) {
442 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
443 builder(myFreePrimitiveTag)->Generated(aShape);
444 if (!theName.empty()) {
445 std::string aName = theName;
446 if (myPrimitivesNamesIndexMap.find(theName) != myPrimitivesNamesIndexMap.end()) {
447 IndexTags& anIndexTags = myPrimitivesNamesIndexMap.find(theName)->second;
448 aName += "_" + std::to_string(++(anIndexTags.index));
449 anIndexTags.tags.push_back(myFreePrimitiveTag);
450 if (anIndexTags.index == 2) {
451 buildName(anIndexTags.tags.front(), theName + "_1");
455 IndexTags anIndexTags;
456 anIndexTags.index = 1;
457 anIndexTags.tags.push_back(myFreePrimitiveTag);
458 myPrimitivesNamesIndexMap[theName] = anIndexTags;
461 buildName(myFreePrimitiveTag, aName);
463 ++myFreePrimitiveTag;
467 void Model_BodyBuilder::generated(const GeomShapePtr& theOldShape,
468 const GeomShapePtr& theNewShape,
469 const std::string& theName)
471 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
472 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
473 TopAbs_ShapeEnum aNewShapeType = aNewShape.ShapeType();
475 if (aNewShapeType == TopAbs_WIRE || aNewShapeType == TopAbs_SHELL) {
476 // TODO: This is a workaround. New shape should be only vertex, edge or face.
477 TopAbs_ShapeEnum aShapeTypeToExplore = aNewShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
478 aTag = TopAbs_WIRE ? GENERATED_EDGES_TAG : GENERATED_FACES_TAG;
479 for (TopExp_Explorer anExp(aNewShape, aShapeTypeToExplore); anExp.More(); anExp.Next()) {
480 builder(aTag)->Generated(anOldShape, anExp.Current());
482 buildName(aTag, theName);
484 aTag = getGenerationTag(aNewShape);
485 if (aTag == INVALID_TAG) return;
486 builder(aTag)->Generated(anOldShape, aNewShape);
487 buildName(aTag, theName);
491 void Model_BodyBuilder::modified(const GeomShapePtr& theOldShape,
492 const GeomShapePtr& theNewShape,
493 const std::string& theName)
495 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
496 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
497 int aTag = getModificationTag(aNewShape);
498 if (aTag == INVALID_TAG) return;
499 builder(aTag)->Modify(anOldShape, aNewShape);
500 buildName(aTag, theName);
503 void Model_BodyBuilder::loadDeletedShapes(const GeomMakeShapePtr& theAlgo,
504 const GeomShapePtr& theOldShape,
505 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
506 const GeomShapePtr& theShapesToExclude)
508 TopTools_MapOfShape anAlreadyProcessedShapes;
509 GeomShapePtr aResultShape = shape();
510 for (GeomAPI_ShapeExplorer anExp(theOldShape, theShapeTypeToExplore);
514 GeomShapePtr anOldSubShape = anExp.current();
515 const TopoDS_Shape& anOldSubShape_ = anOldSubShape->impl<TopoDS_Shape>();
516 if (!anAlreadyProcessedShapes.Add(anOldSubShape_)
517 || !theAlgo->isDeleted(anOldSubShape)
518 || aResultShape->isSubShape(anOldSubShape, false)
519 || (theShapesToExclude.get() && theShapesToExclude->isSubShape(anOldSubShape, false)))
524 ListOfShape aNewShapes;
525 if (BRepTools_History::IsSupportedType(anOldSubShape_)) { // to avoid crash in #2572
526 theAlgo->modified(anOldSubShape, aNewShapes);
529 if (aNewShapes.size() == 0
530 || (aNewShapes.size() == 1 && aNewShapes.front()->isSame(anOldSubShape))) {
531 builder(DELETED_TAG)->Delete(anOldSubShape_);
536 // Keep only the shapes with minimal shape type
537 static void keepTopLevelShapes(ListOfShape& theShapes,
538 const TopoDS_Shape& theRoot,
539 const GeomShapePtr& theResultShape = GeomShapePtr())
541 GeomAPI_Shape::ShapeType aKeepShapeType = GeomAPI_Shape::SHAPE;
542 ListOfShape::iterator anIt = theShapes.begin();
543 while (anIt != theShapes.end()) {
544 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
545 bool aSkip = aNewShape.IsNull() ||
546 (aNewShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aNewShape)));
547 if (aSkip || theRoot.IsSame(aNewShape) || (theResultShape &&
548 (!theResultShape->isSubShape(*anIt, false) || theResultShape->isSame(*anIt)))) {
549 ListOfShape::iterator aRemoveIt = anIt++;
550 theShapes.erase(aRemoveIt);
552 GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
553 if (aType < aKeepShapeType) {
554 // found a shape with lesser shape type => remove all previous shapes
555 aKeepShapeType = aType;
556 theShapes.erase(theShapes.begin(), anIt);
558 } else if (aType > aKeepShapeType) {
559 // shapes with greater shape type should be removed from the list
560 ListOfShape::iterator aRemoveIt = anIt++;
561 theShapes.erase(aRemoveIt);
568 void Model_BodyBuilder::loadModifiedShapes(const GeomMakeShapePtr& theAlgo,
569 const GeomShapePtr& theOldShape,
570 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
571 const std::string& theName)
573 GeomShapePtr aResultShape = shape();
574 GeomShapePtr aShapeToExplore = theOldShape;
575 if (theAlgo->isNewShapesCollected(theOldShape, theShapeTypeToExplore)) {
576 // use optimized set of old shapes for this
577 GeomShapePtr aCompound = theAlgo->oldShapesForNew(theOldShape,
579 theShapeTypeToExplore);
580 if (aCompound.get()) aShapeToExplore = aCompound;
583 TopTools_MapOfShape anAlreadyProcessedShapes;
584 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
585 for (GeomAPI_ShapeExplorer anOldShapeExp(aShapeToExplore, theShapeTypeToExplore);
586 anOldShapeExp.more();
587 anOldShapeExp.next())
589 GeomShapePtr anOldSubShape = anOldShapeExp.current();
590 const TopoDS_Shape& anOldSubShape_ = anOldSubShape->impl<TopoDS_Shape>();
592 // There is no sense to write history if shape already processed
593 // or old shape does not exist in the document.
594 bool anOldSubShapeAlreadyProcessed = !anAlreadyProcessedShapes.Add(anOldSubShape_);
595 TDF_Label anAccess2 = std::dynamic_pointer_cast<Model_Document>(
596 ModelAPI_Session::get()->moduleDocument())->generalLabel();
597 TDF_Label anOriginalLabel;
598 bool anOldSubShapeNotInTree =
599 !isShapeInTree(aData->shapeLab(), anAccess2, anOldSubShape_, anOriginalLabel);
600 if (anOldSubShapeAlreadyProcessed || anOldSubShapeNotInTree) {
605 ListOfShape aNewShapes;
606 theAlgo->modified(anOldSubShape, aNewShapes);
608 for (ListOfShape::const_iterator aNewShapesIt = aNewShapes.cbegin();
609 aNewShapesIt != aNewShapes.cend();
612 GeomShapePtr aNewShape = *aNewShapesIt;
613 const TopoDS_Shape& aNewShape_ = aNewShape->impl<TopoDS_Shape>();
614 bool isGenerated = anOldSubShape_.ShapeType() != aNewShape_.ShapeType();
616 bool aNewShapeIsSameAsOldShape = anOldSubShape->isSame(aNewShape);
617 bool aNewShapeIsNotInResultShape = !aResultShape->isSubShape(aNewShape, false);
618 if (aNewShapeIsSameAsOldShape || aNewShapeIsNotInResultShape)
621 if (aResultShape->isSame(aNewShape))
622 continue; // it is stored on the root level (2241 - history propagation issue)
624 int aTag = isGenerated ? getGenerationTag(aNewShape_) : getModificationTag(aNewShape_);
625 TNaming_Builder*aBuilder = builder(aTag);
626 if (isAlreadyStored(aBuilder, anOldSubShape_, aNewShape_))
627 continue; // new shape was already stored.
629 buildName(aTag, theName);
630 isGenerated ? aBuilder->Generated(anOldSubShape_, aNewShape_)
631 : aBuilder->Modify(anOldSubShape_, aNewShape_);
632 // store information about the external document reference to restore old shape on open
633 storeExternalReference(anOriginalLabel, aBuilder->NamedShape()->Label());
638 void Model_BodyBuilder::loadGeneratedShapes(const GeomMakeShapePtr& theAlgo,
639 const GeomShapePtr& theOldShape,
640 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
641 const std::string& theName)
643 GeomShapePtr aResultShape = shape();
644 TopTools_MapOfShape anAlreadyProcessedShapes;
645 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
646 for (GeomAPI_ShapeExplorer anOldShapeExp(theOldShape, theShapeTypeToExplore);
647 anOldShapeExp.more();
648 anOldShapeExp.next())
650 GeomShapePtr anOldSubShape = anOldShapeExp.current();
651 const TopoDS_Shape& anOldSubShape_ = anOldSubShape->impl<TopoDS_Shape>();
653 // There is no sense to write history if shape already processed
654 // or old shape does not exist in the document.
655 bool anOldSubShapeAlreadyProcessed = !anAlreadyProcessedShapes.Add(anOldSubShape_);
656 TDF_Label anAccess2 = std::dynamic_pointer_cast<Model_Document>(
657 ModelAPI_Session::get()->moduleDocument())->generalLabel();
658 TDF_Label anOriginalLabel;
659 bool anOldSubShapeNotInTree =
660 !isShapeInTree(aData->shapeLab(), anAccess2, anOldSubShape_, anOriginalLabel);
661 if (anOldSubShapeAlreadyProcessed || anOldSubShapeNotInTree) {
666 ListOfShape aNewShapes;
667 theAlgo->generated(anOldSubShape, aNewShapes);
669 keepTopLevelShapes(aNewShapes, anOldSubShape_);
671 for (ListOfShape::const_iterator aNewShapesIt = aNewShapes.cbegin();
672 aNewShapesIt != aNewShapes.cend();
675 GeomShapePtr aNewShape = *aNewShapesIt;
676 const TopoDS_Shape& aNewShape_ = aNewShape->impl<TopoDS_Shape>();
678 bool aNewShapeIsSameAsOldShape = anOldSubShape->isSame(aNewShape);
679 bool aNewShapeIsNotInResultShape = !aResultShape->isSubShape(aNewShape, false);
680 if (aNewShapeIsSameAsOldShape || aNewShapeIsNotInResultShape) {
684 TopAbs_ShapeEnum aNewShapeType = aNewShape_.ShapeType();
685 if (aNewShapeType == TopAbs_WIRE || aNewShapeType == TopAbs_SHELL) {
686 // TODO: This is a workaround. New shape should be only edge or face.
687 TopAbs_ShapeEnum aShapeTypeToExplore = aNewShapeType == TopAbs_WIRE ? TopAbs_EDGE
689 int aTag = TopAbs_WIRE ? GENERATED_EDGES_TAG : GENERATED_FACES_TAG;
690 for (TopExp_Explorer anExp(aNewShape_, aShapeTypeToExplore); anExp.More(); anExp.Next()) {
691 builder(aTag)->Generated(anOldSubShape_, anExp.Current());
692 // store information about the external document reference to restore old shape on open
693 storeExternalReference(anOriginalLabel, builder(aTag)->NamedShape()->Label());
695 buildName(aTag, theName);
697 int aTag = getGenerationTag(aNewShape_);
698 if (aTag == INVALID_TAG) return;
699 builder(aTag)->Generated(anOldSubShape_, aNewShape_);
700 buildName(aTag, theName);
701 // store information about the external document reference to restore old shape on open
702 storeExternalReference(anOriginalLabel, builder(aTag)->NamedShape()->Label());
709 //=======================================================================
710 int getDangleShapes(const TopoDS_Shape& theShapeIn,
711 const TopAbs_ShapeEnum theGeneratedFrom,
712 TopTools_DataMapOfShapeShape& theDangles)
715 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
716 TopAbs_ShapeEnum GeneratedTo;
717 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
718 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
719 else return Standard_False;
720 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
721 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
722 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
723 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
724 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
726 return theDangles.Extent();
729 //=======================================================================
730 void loadGeneratedDangleShapes(
731 const TopoDS_Shape& theShapeIn,
732 const TopAbs_ShapeEnum theGeneratedFrom,
733 TNaming_Builder * theBuilder)
735 TopTools_DataMapOfShapeShape dangles;
736 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
737 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
738 for (; itr.More(); itr.Next())
739 theBuilder->Generated(itr.Key(), itr.Value());
743 //=======================================================================
744 void Model_BodyBuilder::loadNextLevels(GeomShapePtr theShape,
745 const std::string& theName)
747 if(theShape->isNull()) return;
748 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
750 if (aShape.ShapeType() == TopAbs_SOLID) {
751 TopExp_Explorer expl(aShape, TopAbs_FACE);
752 for (; expl.More(); expl.Next()) {
753 builder(myFreePrimitiveTag)->Generated(expl.Current());
754 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
755 aName = theName + "_" + aStr.ToCString();
756 buildName(myFreePrimitiveTag, aName);
757 ++myFreePrimitiveTag;
760 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
761 // load faces and all the free edges
762 TopTools_IndexedMapOfShape Faces;
763 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
764 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
765 TopExp_Explorer expl(aShape, TopAbs_FACE);
766 for (; expl.More(); expl.Next()) {
767 builder(myFreePrimitiveTag)->Generated(expl.Current());
768 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
769 aName = theName + "_" + aStr.ToCString();
770 buildName(myFreePrimitiveTag, aName);
771 ++myFreePrimitiveTag;
774 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
775 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
776 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
778 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
779 if (aLL.Extent() < 2) {
780 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
782 builder(myFreePrimitiveTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
783 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
784 aName = theName + "_" + aStr.ToCString();
785 buildName(myFreePrimitiveTag, aName);
786 ++myFreePrimitiveTag;
788 TopTools_ListIteratorOfListOfShape anIter(aLL);
789 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
791 if(aFace.IsEqual(anIter.Value())) {
792 builder(myFreePrimitiveTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
793 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
794 aName = theName + "_" + aStr.ToCString();
795 buildName(myFreePrimitiveTag, aName);
796 ++myFreePrimitiveTag;
800 } else if (aShape.ShapeType() == TopAbs_WIRE) {
801 TopTools_IndexedMapOfShape Edges;
802 BRepTools::Map3DEdges(aShape, Edges);
803 if (Edges.Extent() == 1) {
804 builder(myFreePrimitiveTag++)->Generated(Edges.FindKey(1));
805 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
806 for (; expl.More(); expl.Next()) {
807 builder(myFreePrimitiveTag)->Generated(expl.Current());
808 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
809 aName = theName + "_" + aStr.ToCString();
810 buildName(myFreePrimitiveTag, aName);
811 ++myFreePrimitiveTag;
814 TopExp_Explorer expl(aShape, TopAbs_EDGE);
815 for (; expl.More(); expl.Next()) {
816 builder(myFreePrimitiveTag)->Generated(expl.Current());
817 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
818 aName = theName + "_" + aStr.ToCString();
819 buildName(myFreePrimitiveTag, aName);
820 ++myFreePrimitiveTag;
822 // and load generated vertices.
823 TopTools_DataMapOfShapeShape generated;
824 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
826 TNaming_Builder* pBuilder = builder(myFreePrimitiveTag++);
827 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
830 } else if (aShape.ShapeType() == TopAbs_EDGE) {
831 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
832 for (; expl.More(); expl.Next()) {
833 builder(myFreePrimitiveTag)->Generated(expl.Current());
834 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
835 aName = theName + "_" + aStr.ToCString();
836 buildName(myFreePrimitiveTag, aName);
837 ++myFreePrimitiveTag;
842 //=======================================================================
843 int findAmbiguities(const TopoDS_Shape& theShapeIn,
844 TopTools_ListOfShape& theList)
847 // edges -> ancestor faces list
848 TopTools_IndexedDataMapOfShapeListOfShape aSubShapeAndAncestors;
849 TopExp::MapShapesAndAncestors(theShapeIn, TopAbs_EDGE, TopAbs_FACE, aSubShapeAndAncestors);
850 // keeps the shapes which are already in the resulting list
851 TopTools_MapOfShape alreadyThere;
852 // map from faces identifier (combination of hash-codes) to list of edges produced such ID
853 NCollection_DataMap<int, NCollection_List<TopoDS_Shape> > aFacesIDs;
855 TopTools_IndexedDataMapOfShapeListOfShape::Iterator anAncestorsIter(aSubShapeAndAncestors);
856 for (; anAncestorsIter.More(); anAncestorsIter.Next()) {
857 const TopTools_ListOfShape& ancestors = anAncestorsIter.Value();
858 if (ancestors.Extent() < 2)
860 Standard_Integer anID = 0;
861 for(TopTools_ListIteratorOfListOfShape aFaceIt(ancestors); aFaceIt.More(); aFaceIt.Next()) {
862 anID ^= HashCode(aFaceIt.ChangeValue(), 1990657); // Pierpont prime
864 if (aFacesIDs.IsBound(anID)) { // there found same edge, check they really have same faces
865 const NCollection_List<TopoDS_Shape>& aSameFaces1 =
866 aSubShapeAndAncestors.FindFromKey(anAncestorsIter.Key());
867 NCollection_List<TopoDS_Shape>::Iterator aSameEdge(aFacesIDs.ChangeFind(anID));
868 for(; aSameEdge.More(); aSameEdge.Next()) {
869 const NCollection_List<TopoDS_Shape>& aSameFaces2 =
870 aSubShapeAndAncestors.FindFromKey(aSameEdge.Value());
871 if (aSameFaces2.Extent() != aSameFaces1.Extent()) // the number of faces is different
874 NCollection_List<TopoDS_Shape>::Iterator aFaceIter1(aSameFaces1);
875 for(; aFaceIter1.More(); aFaceIter1.Next()) {
876 NCollection_List<TopoDS_Shape>::Iterator aFaceIter2(aSameFaces2);
877 for(; aFaceIter2.More(); aFaceIter2.Next()) {
878 if (aFaceIter1.Value().IsSame(aFaceIter2.Value()))
881 if (!aFaceIter2.More()) // aFaceIter1 contains a face, which is not in aFaceIter2
884 if (!aFaceIter1.More()) { // all the faces are same => put to the result
885 if (alreadyThere.Add(aSameEdge.Value()))
886 theList.Append(aSameEdge.Value());
887 if (alreadyThere.Add(anAncestorsIter.Key()))
888 theList.Append(anAncestorsIter.Key());
891 } else { // ID is unique, just add this edge
892 aFacesIDs.Bind(anID, NCollection_List<TopoDS_Shape>());
894 aFacesIDs.ChangeFind(anID).Append(anAncestorsIter.Key()); // add to the list anyway
896 return theList.Extent();
899 //=======================================================================
900 void Model_BodyBuilder::loadFirstLevel(GeomShapePtr theShape, const std::string& theName)
902 if(theShape->isNull()) return;
903 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
905 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
906 TopoDS_Iterator itr(aShape);
907 for (; itr.More(); itr.Next()) {
908 builder(myFreePrimitiveTag)->Generated(itr.Value());
909 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
910 aName = theName + "_" + aStr.ToCString();
911 buildName(myFreePrimitiveTag, aName);
912 ++myFreePrimitiveTag;
913 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
914 itr.Value().ShapeType() == TopAbs_COMPSOLID)
916 GeomShapePtr itrShape(new GeomAPI_Shape());
917 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
918 loadFirstLevel(itrShape, theName);
920 GeomShapePtr itrShape(new GeomAPI_Shape());
921 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
922 loadNextLevels(itrShape, theName);
926 GeomShapePtr itrShape(new GeomAPI_Shape());
927 itrShape->setImpl(new TopoDS_Shape(aShape));
928 loadNextLevels(itrShape, theName);
930 TopTools_ListOfShape aList;
931 if(findAmbiguities(aShape, aList)) {
932 TopTools_ListIteratorOfListOfShape it(aList);
933 for (; it.More(); it.Next(), ++myFreePrimitiveTag) {
934 builder(myFreePrimitiveTag)->Generated(it.Value());
935 TCollection_AsciiString aStr(myFreePrimitiveTag - PRIMITIVES_START_TAG + 1);
936 aName = theName + "_" + aStr.ToCString();
937 buildName(myFreePrimitiveTag, aName);
942 GeomShapePtr Model_BodyBuilder::shape()
944 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
945 if (aData && aData->isValid()) {
946 TDF_Label aShapeLab = aData->shapeLab();
947 Handle(TDF_Reference) aRef;
948 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
949 aShapeLab = aRef->Get();
951 Handle(TNaming_NamedShape) aName;
952 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
953 TopoDS_Shape aShape = aName->Get();
954 if (!aShape.IsNull()) {
955 GeomShapePtr aRes(new GeomAPI_Shape);
956 aRes->setImpl(new TopoDS_Shape(aShape));
961 return GeomShapePtr();