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 <GeomAlgoAPI_SortListOfShapes.h>
53 #include <Config_PropManager.h>
55 //#include <TCollection_AsciiString.hxx>
56 //#include <TDF_Tool.hxx>
57 //#define DEB_IMPORT 1
59 Model_BodyBuilder::Model_BodyBuilder(ModelAPI_Object* theOwner)
60 : ModelAPI_BodyBuilder(theOwner)
64 // Converts evolution of naming shape to selection evelution and back to avoid
65 // naming support on the disabled results. Deeply in the labels tree, recursively.
66 static void evolutionToSelectionRec(TDF_Label theLab, const bool theFlag) {
67 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
68 Handle(TNaming_NamedShape) aName;
70 if (theLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
71 TNaming_Evolution aNSEvol = aName->Evolution();
72 if ((aNSEvol == TNaming_SELECTED && theFlag) ||
73 (aNSEvol != TNaming_SELECTED && !theFlag)) { // nothing to do, it is already correct
76 anEvolution = (int)(aNSEvol);
78 Handle(TDataStd_Integer) anAttrEvol;
79 if (theLab.FindAttribute(TDataStd_Integer::GetID(), anAttrEvol)) {
80 anEvolution = anAttrEvol->Get();
83 TDataStd_Integer::Set(theLab, anEvolution);
86 for(TNaming_Iterator anIter(aName); anIter.More(); anIter.Next()) {
87 // iterator goes in reversed order relatively to the Builder, to, make the list reversed
88 aShapePairs.push_front(std::pair<TopoDS_Shape, TopoDS_Shape>
89 (anIter.OldShape(), anIter.NewShape()));
93 TNaming_Builder aBuilder(theLab);
94 TNaming_Evolution anEvol = (TNaming_Evolution)(anEvolution);
95 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter = aShapePairs.begin();
96 for(; aPairsIter != aShapePairs.end(); aPairsIter++) {
97 if (theFlag) { // disabled => make selection
98 if (anEvolution == TNaming_DELETE) // issue 2274 : don't put too many same null shapes
99 aBuilder.Select(aPairsIter->first, aPairsIter->first);
100 else if (anEvolution == TNaming_PRIMITIVE)
101 aBuilder.Select(aPairsIter->second, aPairsIter->second);
103 aBuilder.Select(aPairsIter->second, aPairsIter->first);
104 } else if (anEvol == TNaming_GENERATED) {
105 aBuilder.Generated(aPairsIter->first, aPairsIter->second);
106 } else if (anEvol == TNaming_MODIFY) {
107 aBuilder.Modify(aPairsIter->first, aPairsIter->second);
108 } else if (anEvol == TNaming_DELETE) {
109 aBuilder.Delete(aPairsIter->first);
110 } else if (anEvol == TNaming_PRIMITIVE) {
111 aBuilder.Generated(aPairsIter->second);
112 } else if (anEvol == TNaming_SELECTED) {
113 aBuilder.Select(aPairsIter->second, aPairsIter->first);
117 // recursive call for all sub-labels
118 TDF_ChildIterator anIter(theLab, Standard_False);
119 for(; anIter.More(); anIter.Next()) {
120 evolutionToSelectionRec(anIter.Value(), theFlag);
124 void Model_BodyBuilder::evolutionToSelection(const bool theFlag)
126 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
127 if (!aData) // unknown case
129 TDF_Label& aShapeLab = aData->shapeLab();
130 evolutionToSelectionRec(aShapeLab, theFlag);
133 void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape,
134 const bool theIsStoreSameShapes)
136 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
138 TDF_Label& aShapeLab = aData->shapeLab();
141 // store the new shape as primitive
142 TNaming_Builder aBuilder(aShapeLab);
145 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
147 return; // null shape inside
149 if(!theIsStoreSameShapes) {
150 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
151 if(!aNS.IsNull() && !aNS->IsEmpty()) {
152 // This shape is already in document, store reference instead of shape;
153 const TDF_Label aFoundLabel = aNS->Label();
154 TDF_Reference::Set(aShapeLab, aFoundLabel);
155 aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
160 aBuilder.Generated(aShape);
162 aShapeLab.ForgetAttribute(TDF_Reference::GetID());
163 if(!aBuilder.NamedShape()->IsEmpty()) {
164 Handle(TDataStd_Name) anAttr;
165 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
166 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
168 std::shared_ptr<Model_Document> aDoc =
169 std::dynamic_pointer_cast<Model_Document>(document());
170 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
177 void Model_BodyBuilder::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
178 const std::shared_ptr<GeomAPI_Shape>& theToShape)
180 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
182 TDF_Label& aShapeLab = aData->shapeLab();
185 // store the new shape as primitive
186 TNaming_Builder aBuilder(aShapeLab);
187 if (!theFromShape || !theToShape)
189 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
190 if (aShapeBasis.IsNull())
191 return; // null shape inside
192 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
193 if (aShapeNew.IsNull())
194 return; // null shape inside
195 aBuilder.Generated(aShapeBasis, aShapeNew);
197 if(!aBuilder.NamedShape()->IsEmpty()) {
198 Handle(TDataStd_Name) anAttr;
199 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
200 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
202 std::shared_ptr<Model_Document> aDoc =
203 std::dynamic_pointer_cast<Model_Document>(document());
204 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
211 void Model_BodyBuilder::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
212 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
214 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
216 TDF_Label& aShapeLab = aData->shapeLab();
218 if (theDecomposeSolidsTag != -2)
220 // store the new shape as primitive
221 TNaming_Builder aBuilder(aShapeLab);
222 if (!theOldShape || !theNewShape)
224 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
225 if (aShapeOld.IsNull())
226 return; // null shape inside
227 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
228 if (aShapeNew.IsNull())
229 return; // null shape inside
230 aBuilder.Modify(aShapeOld, aShapeNew);
231 if(!aBuilder.NamedShape()->IsEmpty()) {
232 Handle(TDataStd_Name) anAttr;
233 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
234 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
236 std::shared_ptr<Model_Document> aDoc =
237 std::dynamic_pointer_cast<Model_Document>(document());
238 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
245 void Model_BodyBuilder::storeWithoutNaming(const std::shared_ptr<GeomAPI_Shape>& theShape)
247 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
252 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
254 return; // null shape inside
255 TNaming_Builder aBuilder(aData->shapeLab());
256 aBuilder.Select(aShape, aShape);
260 void Model_BodyBuilder::clean()
262 TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>(data())->shapeLab();
265 std::map<int, TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
266 for(; aBuilder != myBuilders.end(); aBuilder++) {
267 delete aBuilder->second;
268 // clear also shapes on cleaned sub-labels (#2241)
269 Handle(TNaming_NamedShape) aNS;
270 if (aLab.FindChild(aBuilder->first).FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
275 // remove the old reference (if any)
276 aLab.ForgetAttribute(TDF_Reference::GetID());
279 Model_BodyBuilder::~Model_BodyBuilder()
284 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
286 std::map<int, TNaming_Builder*>::iterator aFind = myBuilders.find(theTag);
287 if (aFind == myBuilders.end()) {
288 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
289 myBuilders[theTag] = new TNaming_Builder(
290 theTag == 0 ? aData->shapeLab() : aData->shapeLab().FindChild(theTag));
291 aFind = myBuilders.find(theTag);
293 return aFind->second;
296 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
298 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
299 //aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), theName);
300 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(), theName.c_str());
302 void Model_BodyBuilder::generated(
303 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
305 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
306 builder(theTag)->Generated(aShape);
308 buildName(theTag, theName);
311 void Model_BodyBuilder::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
312 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
314 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
315 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
316 builder(theTag)->Generated(anOldShape, aNewShape);
318 buildName(theTag, theName);
319 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
320 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
321 TopAbs_ShapeEnum anExplodeShapeType = aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
322 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
324 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
325 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
326 TDF_Label aChildLabel = aLabel.FindChild(aTag);
327 TNaming_Builder aBuilder(aChildLabel);
328 aBuilder.Generated(anOldShape, anExp.Current());
329 TCollection_AsciiString aChildName = TCollection_AsciiString((theName + "_").c_str()) + aTag;
330 //aDoc->addNamingName(aChildLabel, aChildName.ToCString());
331 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
338 void Model_BodyBuilder::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
339 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
341 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
342 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
343 builder(theTag)->Modify(anOldShape, aNewShape);
345 buildName(theTag, theName);
348 void Model_BodyBuilder::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
351 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
352 builder(theTag)->Delete(aShape);
355 void Model_BodyBuilder::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
356 std::shared_ptr<GeomAPI_Shape> theShapeIn,
357 const int theKindOfShape,
360 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
361 TopTools_MapOfShape aView;
362 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
363 GeomShapePtr aResultShape = shape();
364 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
365 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
366 if (!aView.Add(aRoot)) continue;
367 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
368 aRShape->setImpl((new TopoDS_Shape(aRoot)));
369 if (theMS->isDeleted (aRShape)) {
370 if (!aResultShape->isSubShape(aRShape, false)) {
372 theMS->modified(aRShape, aHist);
373 if (aHist.size() == 0 || (aHist.size() == 1 && aHist.front()->isSame(aRShape)))
374 builder(theTag)->Delete(aRoot);
380 // Keep only the shapes with minimal shape type
381 static void keepTopLevelShapes(ListOfShape& theShapes, const TopoDS_Shape& theRoot,
382 const GeomShapePtr& theResultShape = GeomShapePtr())
384 GeomAPI_Shape::ShapeType aKeepShapeType = GeomAPI_Shape::SHAPE;
385 ListOfShape::iterator anIt = theShapes.begin();
386 while (anIt != theShapes.end()) {
387 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
388 if (theRoot.IsSame(aNewShape) || (theResultShape &&
389 (!theResultShape->isSubShape(*anIt, false) || theResultShape->isSame(*anIt)))) {
390 ListOfShape::iterator aRemoveIt = anIt++;
391 theShapes.erase(aRemoveIt);
393 GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
394 if (aType < aKeepShapeType) {
395 // found a shape with lesser shape type => remove all previous shapes
396 aKeepShapeType = aType;
397 theShapes.erase(theShapes.begin(), anIt);
399 } else if (aType > aKeepShapeType) {
400 // shapes with greater shape type should be removed from the list
401 ListOfShape::iterator aRemoveIt = anIt++;
402 theShapes.erase(aRemoveIt);
409 // returns an ancestor shape-type thaty used for naming-definition of the sub-type
410 TopAbs_ShapeEnum typeOfAncestor(const TopAbs_ShapeEnum theSubType) {
411 if (theSubType == TopAbs_VERTEX)
413 if (theSubType == TopAbs_EDGE)
415 return TopAbs_VERTEX; // bad case
418 void Model_BodyBuilder::loadAndOrientModifiedShapes (
419 GeomAlgoAPI_MakeShape* theMS,
420 std::shared_ptr<GeomAPI_Shape> theShapeIn,
421 const int theKindOfShape,
423 const std::string& theName,
424 GeomAPI_DataMapOfShapeShape& theSubShapes,
425 const bool theIsStoreSeparate,
426 const bool theIsStoreAsGenerated)
430 bool isBuilt = !theName.empty();
431 std::string aName = theName;
432 std::ostringstream aStream;
433 GeomShapePtr aResultShape = shape();
434 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
435 TopTools_MapOfShape aView;
436 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
437 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
438 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
439 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
440 if (!aView.Add(aRoot)) continue;
443 TNaming_Tool::NamedShape(aRoot, aData->shapeLab()).IsNull();
444 if (aNotInTree && !theIsStoreSeparate) {
445 // there is no sense to write history if old shape does not exist in the document
446 continue; // but if it is stored separately, it will be builded as a primitive
449 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
450 aRShape->setImpl((new TopoDS_Shape(aRoot)));
451 theMS->modified(aRShape, aList);
452 if (!theIsStoreSeparate)
453 keepTopLevelShapes(aList, aRoot, aResultShape);
454 // sort the list of images before naming
455 GeomAlgoAPI_SortListOfShapes::sort(aList);
457 // to trace situation where several objects are produced by one parent (#2317)
458 int aSameParentShapes = -1;
459 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
460 anIt = aList.begin(), aLast = aList.end();
461 for (; anIt != aLast; anIt++) {
462 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
463 if (theSubShapes.isBound(*anIt)) {
464 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
465 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
467 GeomShapePtr aGeomNewShape(new GeomAPI_Shape());
468 aGeomNewShape->setImpl(new TopoDS_Shape(aNewShape));
469 if(!aRoot.IsSame(aNewShape) && aResultShape->isSubShape(aGeomNewShape, false) &&
470 !aResultShape->isSame(*anIt)) { // to avoid put of same shape on main label and sub
471 int aBuilderTag = aTag;
472 if (!theIsStoreSeparate) {
474 } else if (aNotInTree) { // check this new shape can not be represented as
475 // a sub-shape of higher level sub-shapes
476 TopAbs_ShapeEnum aNewType = aNewShape.ShapeType();
477 TopAbs_ShapeEnum anAncestorType = typeOfAncestor(aNewType);
478 if (anAncestorType != TopAbs_VERTEX) {
480 TopoDS_Shape aResultTShape = aResultShape->impl<TopoDS_Shape>();
481 TopExp_Explorer anAncestorExp(aResultTShape, anAncestorType);
482 for(; anAncestorExp.More() && !aFound; anAncestorExp.Next()) {
483 if (aResultTShape.IsSame(anAncestorExp.Current()))
485 TopExp_Explorer aSubExp(anAncestorExp.Current(), aNewType);
486 for(; aSubExp.More(); aSubExp.Next()) {
487 if (aNewShape.IsSame(aSubExp.Current())) {
494 continue; // not need to store this shape in the BRep structure
499 static const int THE_ANCHOR_TAG = 100000;
500 int aCurShapeType = (int)((*anIt)->shapeType());
501 bool needSuffix = false; // suffix for the name based on the shape type
502 if (aSameParentShapes > 0) { // store in other label
503 aBuilderTag = THE_ANCHOR_TAG - aSameParentShapes * 10 - aCurShapeType;
505 } else if (aCurShapeType != theKindOfShape) {
506 // modified shape has different type => set another tag
507 // to avoid shapes of different types on the same label
508 aBuilderTag = THE_ANCHOR_TAG - aCurShapeType;
513 switch (aCurShapeType) {
514 case GeomAPI_Shape::VERTEX: aSuffix = "_v"; break;
515 case GeomAPI_Shape::EDGE: aSuffix = "_e"; break;
516 case GeomAPI_Shape::FACE: aSuffix = "_f"; break;
521 if(theIsStoreAsGenerated) {
522 // Here we store shapes as generated, to avoid problem when one parent shape produce
523 // several child shapes. In this case naming could not determine which shape to select.
524 builder(aBuilderTag)->Generated(aRoot, aNewShape);
525 } else if (aNotInTree) {
526 // not in tree -> store as primitive (stored as separated)
527 builder(aBuilderTag)->Generated(aNewShape);
528 } else if (aNewShape.ShapeType() > aRoot.ShapeType()) {
529 // if lower-level type is produced, make it as generated
530 builder(aBuilderTag)->Generated(aRoot, aNewShape);
532 builder(aBuilderTag)->Modify(aRoot, aNewShape);
535 aStream.str(std::string());
538 if(theIsStoreSeparate)
539 aStream << "_" << anIndex++;
541 if (aSameParentShapes > 0) {
542 aStream.str(std::string());
544 aStream << aName << "_" << aSameParentShapes << "divided";
548 buildName(aBuilderTag, aStream.str());
550 if(theIsStoreSeparate) {
553 } else if (aResultShape->isSame(*anIt)) {
554 // keep the modification evolution on the root level (2241 - history propagation issue)
555 if(theIsStoreAsGenerated) {
556 builder(0)->Generated(aRoot, aNewShape);
558 builder(0)->Modify(aRoot, aNewShape);
565 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
566 GeomAlgoAPI_MakeShape* theMS,
567 std::shared_ptr<GeomAPI_Shape> theShapeIn,
568 const int theKindOfShape,
570 const std::string& theName,
571 GeomAPI_DataMapOfShapeShape& theSubShapes)
573 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
574 TopTools_MapOfShape aView;
575 bool isBuilt = !theName.empty();
576 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
577 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
578 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
579 if (!aView.Add(aRoot)) continue;
580 //if (TNaming_Tool::NamedShape(aRoot, builder(theTag)->NamedShape()->Label()).IsNull())
581 // continue; // there is no sense to write history if old shape does not exist in the document
583 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
584 aRShape->setImpl((new TopoDS_Shape(aRoot)));
585 theMS->generated(aRShape, aList);
586 keepTopLevelShapes(aList, aRoot);
587 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
588 anIt = aList.begin(), aLast = aList.end();
589 for (; anIt != aLast; anIt++) {
590 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
591 if (theSubShapes.isBound(*anIt)) {
592 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
593 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
595 if (!aRoot.IsSame (aNewShape)) {
596 builder(theTag)->Generated(aRoot,aNewShape);
598 buildName(theTag, theName);
600 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
601 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
602 TopAbs_ShapeEnum anExplodeShapeType =
603 aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
604 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
606 std::shared_ptr<Model_Document> aDoc =
607 std::dynamic_pointer_cast<Model_Document>(document());
608 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
609 TDF_Label aChildLabel = aLabel.FindChild(aTag);
610 TNaming_Builder aBuilder(aChildLabel);
611 aBuilder.Generated(aRoot, anExp.Current());
612 TCollection_AsciiString aChildName =
613 TCollection_AsciiString((theName + "_").c_str()) + aTag;
614 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
622 //=======================================================================
623 int getDangleShapes(const TopoDS_Shape& theShapeIn,
624 const TopAbs_ShapeEnum theGeneratedFrom,
625 TopTools_DataMapOfShapeShape& theDangles)
628 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
629 TopAbs_ShapeEnum GeneratedTo;
630 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
631 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
632 else return Standard_False;
633 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
634 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
635 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
636 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
637 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
639 return theDangles.Extent();
642 //=======================================================================
643 void loadGeneratedDangleShapes(
644 const TopoDS_Shape& theShapeIn,
645 const TopAbs_ShapeEnum theGeneratedFrom,
646 TNaming_Builder * theBuilder)
648 TopTools_DataMapOfShapeShape dangles;
649 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
650 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
651 for (; itr.More(); itr.Next())
652 theBuilder->Generated(itr.Key(), itr.Value());
655 //=======================================================================
656 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
657 const std::string& theName, int& theTag)
659 if(theShape->isNull()) return;
660 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
662 if (aShape.ShapeType() == TopAbs_SOLID) {
663 TopExp_Explorer expl(aShape, TopAbs_FACE);
664 for (; expl.More(); expl.Next()) {
665 builder(theTag)->Generated(expl.Current());
666 TCollection_AsciiString aStr(theTag);
667 aName = theName + aStr.ToCString();
668 buildName(theTag, aName);
672 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
673 // load faces and all the free edges
674 TopTools_IndexedMapOfShape Faces;
675 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
676 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
677 TopExp_Explorer expl(aShape, TopAbs_FACE);
678 for (; expl.More(); expl.Next()) {
679 builder(theTag)->Generated(expl.Current());
680 TCollection_AsciiString aStr(theTag);
681 aName = theName + aStr.ToCString();
682 buildName(theTag, aName);
686 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
687 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
688 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
690 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
691 if (aLL.Extent() < 2) {
692 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
694 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
695 TCollection_AsciiString aStr(theTag);
696 aName = theName + aStr.ToCString();
697 buildName(theTag, aName);
700 TopTools_ListIteratorOfListOfShape anIter(aLL);
701 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
703 if(aFace.IsEqual(anIter.Value())) {
704 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
705 TCollection_AsciiString aStr(theTag);
706 aName = theName + aStr.ToCString();
707 buildName(theTag, aName);
712 } else if (aShape.ShapeType() == TopAbs_WIRE) {
713 TopTools_IndexedMapOfShape Edges;
714 BRepTools::Map3DEdges(aShape, Edges);
715 if (Edges.Extent() == 1) {
716 builder(theTag++)->Generated(Edges.FindKey(1));
717 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
718 for (; expl.More(); expl.Next()) {
719 builder(theTag)->Generated(expl.Current());
720 TCollection_AsciiString aStr(theTag);
721 aName = theName + aStr.ToCString();
722 buildName(theTag, aName);
726 TopExp_Explorer expl(aShape, TopAbs_EDGE);
727 for (; expl.More(); expl.Next()) {
728 builder(theTag)->Generated(expl.Current());
729 TCollection_AsciiString aStr(theTag);
730 aName = theName + aStr.ToCString();
731 buildName(theTag, aName);
734 // and load generated vertices.
735 TopTools_DataMapOfShapeShape generated;
736 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
738 TNaming_Builder* pBuilder = builder(theTag++);
739 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
742 } else if (aShape.ShapeType() == TopAbs_EDGE) {
743 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
744 for (; expl.More(); expl.Next()) {
745 builder(theTag)->Generated(expl.Current());
746 TCollection_AsciiString aStr(theTag);
747 aName = theName + aStr.ToCString();
748 buildName(theTag, aName);
754 //=======================================================================
755 int findAmbiguities(const TopoDS_Shape& theShapeIn,
756 TopTools_ListOfShape& theList)
759 // edges -> ancestor faces list
760 TopTools_IndexedDataMapOfShapeListOfShape aSubShapeAndAncestors;
761 TopExp::MapShapesAndAncestors(theShapeIn, TopAbs_EDGE, TopAbs_FACE, aSubShapeAndAncestors);
762 // keeps the shapes which are already in the resulting list
763 TopTools_MapOfShape alreadyThere;
764 // map from faces identifier (combination of hash-codes) to list of edges produced such ID
765 NCollection_DataMap<int, NCollection_List<TopoDS_Shape> > aFacesIDs;
767 TopTools_IndexedDataMapOfShapeListOfShape::Iterator anAncestorsIter(aSubShapeAndAncestors);
768 for (; anAncestorsIter.More(); anAncestorsIter.Next()) {
769 const TopTools_ListOfShape& ancestors = anAncestorsIter.Value();
770 if (ancestors.Extent() < 2)
772 Standard_Integer anID = 0;
773 for(TopTools_ListIteratorOfListOfShape aFaceIt(ancestors); aFaceIt.More(); aFaceIt.Next()) {
774 anID ^= HashCode(aFaceIt.ChangeValue(), 1990657); // Pierpont prime
776 if (aFacesIDs.IsBound(anID)) { // there found same edge, check they really have same faces
777 const NCollection_List<TopoDS_Shape>& aSameFaces1 =
778 aSubShapeAndAncestors.FindFromKey(anAncestorsIter.Key());
779 NCollection_List<TopoDS_Shape>::Iterator aSameEdge(aFacesIDs.ChangeFind(anID));
780 for(; aSameEdge.More(); aSameEdge.Next()) {
781 const NCollection_List<TopoDS_Shape>& aSameFaces2 =
782 aSubShapeAndAncestors.FindFromKey(aSameEdge.Value());
783 if (aSameFaces2.Extent() != aSameFaces1.Extent()) // the number of faces is different
786 NCollection_List<TopoDS_Shape>::Iterator aFaceIter1(aSameFaces1);
787 for(; aFaceIter1.More(); aFaceIter1.Next()) {
788 NCollection_List<TopoDS_Shape>::Iterator aFaceIter2(aSameFaces2);
789 for(; aFaceIter2.More(); aFaceIter2.Next()) {
790 if (aFaceIter1.Value().IsSame(aFaceIter2.Value()))
793 if (!aFaceIter2.More()) // aFaceIter1 contains a face, which is not in aFaceIter2
796 if (!aFaceIter1.More()) { // all the faces are same => put to the result
797 if (alreadyThere.Add(aSameEdge.Value()))
798 theList.Append(aSameEdge.Value());
799 if (alreadyThere.Add(anAncestorsIter.Key()))
800 theList.Append(anAncestorsIter.Key());
803 } else { // ID is unique, just add this edge
804 aFacesIDs.Bind(anID, NCollection_List<TopoDS_Shape>());
806 aFacesIDs.ChangeFind(anID).Append(anAncestorsIter.Key()); // add to the list anyway
808 return theList.Extent();
811 //=======================================================================
812 void Model_BodyBuilder::loadFirstLevel(
813 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
815 if(theShape->isNull()) return;
816 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
818 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
819 TopoDS_Iterator itr(aShape);
820 for (; itr.More(); itr.Next()) {
821 builder(theTag)->Generated(itr.Value());
822 TCollection_AsciiString aStr(theTag);
823 aName = theName + aStr.ToCString();
824 buildName(theTag, aName);
825 if(!theName.empty()) buildName(theTag, aName);
827 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
828 itr.Value().ShapeType() == TopAbs_COMPSOLID)
830 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
831 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
832 loadFirstLevel(itrShape, theName, theTag);
834 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
835 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
836 loadNextLevels(itrShape, theName, theTag);
840 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
841 itrShape->setImpl(new TopoDS_Shape(aShape));
842 loadNextLevels(itrShape, theName, theTag);
844 TopTools_ListOfShape aList;
845 if(findAmbiguities(aShape, aList)) {
846 TopTools_ListIteratorOfListOfShape it(aList);
847 for (; it.More(); it.Next(),theTag++) {
848 builder(theTag)->Generated(it.Value());
849 TCollection_AsciiString aStr(theTag);
850 aName = theName + aStr.ToCString();
851 buildName(theTag, aName);
856 //=======================================================================
857 void Model_BodyBuilder::loadDisconnectedEdges(
858 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
860 if(theShape->isNull()) return;
861 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
862 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
863 TopTools_ListOfShape empty;
864 TopExp_Explorer explF(aShape, TopAbs_FACE);
865 for (; explF.More(); explF.Next()) {
866 const TopoDS_Shape& aFace = explF.Current();
867 TopExp_Explorer explV(aFace, TopAbs_EDGE);
868 for (; explV.More(); explV.Next()) {
869 const TopoDS_Shape& anEdge = explV.Current();
870 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
871 Standard_Boolean faceIsNew = Standard_True;
872 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
873 for (; itrF.More(); itrF.Next()) {
874 if (itrF.Value().IsSame(aFace)) {
875 faceIsNew = Standard_False;
880 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
884 TopTools_MapOfShape anEdgesToDelete;
885 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
887 for(;anEx.More();anEx.Next()) {
888 Standard_Boolean aC0 = Standard_False;
889 TopoDS_Shape anEdge1 = anEx.Current();
890 if (edgeNaborFaces.IsBound(anEdge1)) {
891 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
892 if (aList1.Extent()<2) continue;
893 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
894 for (; itr.More(); itr.Next()) {
895 TopoDS_Shape anEdge2 = itr.Key();
896 if(anEdgesToDelete.Contains(anEdge2)) continue;
897 if (anEdge1.IsSame(anEdge2)) continue;
898 const TopTools_ListOfShape& aList2 = itr.Value();
899 // compare lists of the neighbour faces of edge1 and edge2
900 if (aList1.Extent() == aList2.Extent()) {
901 Standard_Integer aMatches = 0;
902 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
903 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
904 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
905 if (aMatches == aList1.Extent()) {
907 builder(theTag)->Generated(anEdge2);
908 anEdgesToDelete.Add(anEdge2);
909 TCollection_AsciiString aStr(theTag);
910 aName = theName + aStr.ToCString();
911 buildName(theTag, aName);
916 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
917 for(;itDelete.More();itDelete.Next())
918 edgeNaborFaces.UnBind(itDelete.Key());
919 edgeNaborFaces.UnBind(anEdge1);
922 builder(theTag)->Generated(anEdge1);
923 TCollection_AsciiString aStr(theTag);
924 aName = theName + aStr.ToCString();
925 buildName(theTag, aName);
931 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape,
932 const std::string& theName, int& theTag)
934 if(theShape->isNull()) return;
935 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
936 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
937 TopTools_ListOfShape empty;
938 TopExp_Explorer explF(aShape, TopAbs_EDGE);
939 for (; explF.More(); explF.Next()) {
940 const TopoDS_Shape& anEdge = explF.Current();
941 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
942 for (; explV.More(); explV.Next()) {
943 const TopoDS_Shape& aVertex = explV.Current();
944 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
945 Standard_Boolean faceIsNew = Standard_True;
946 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
947 for (; itrF.More(); itrF.Next()) {
948 if (itrF.Value().IsSame(anEdge)) {
949 faceIsNew = Standard_False;
954 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
959 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
960 for (; itr.More(); itr.Next()) {
961 const TopTools_ListOfShape& naborEdges = itr.Value();
962 if (naborEdges.Extent() < 2) {
963 builder(theTag)->Generated(itr.Key());
964 TCollection_AsciiString aStr(theTag);
965 aName = theName + aStr.ToCString();
966 buildName(theTag, aName);
972 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
974 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
976 TDF_Label aShapeLab = aData->shapeLab();
977 Handle(TDF_Reference) aRef;
978 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
979 aShapeLab = aRef->Get();
981 Handle(TNaming_NamedShape) aName;
982 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
983 TopoDS_Shape aShape = aName->Get();
984 if (!aShape.IsNull()) {
985 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
986 aRes->setImpl(new TopoDS_Shape(aShape));
991 return std::shared_ptr<GeomAPI_Shape>();
994 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
996 if (theShape.get()) {
997 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
998 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
1000 TDF_Label& aShapeLab = aData->shapeLab();
1001 Handle(TNaming_NamedShape) aName;
1002 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
1003 TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
1004 if (aLatest.IsNull())
1006 if (aLatest.IsEqual(aShape))
1008 // check sub-shapes for comp-solids:
1009 for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
1010 if (aLatest.IsEqual(anExp.Current()))