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 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
364 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
365 if (!aView.Add(aRoot)) continue;
366 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
367 aRShape->setImpl((new TopoDS_Shape(aRoot)));
368 if (theMS->isDeleted (aRShape)) {
369 builder(theTag)->Delete(aRoot);
374 // Keep only the shapes with minimal shape type
375 static void keepTopLevelShapes(ListOfShape& theShapes, const TopoDS_Shape& theRoot,
376 const GeomShapePtr& theResultShape = GeomShapePtr())
378 GeomAPI_Shape::ShapeType aKeepShapeType = GeomAPI_Shape::SHAPE;
379 ListOfShape::iterator anIt = theShapes.begin();
380 while (anIt != theShapes.end()) {
381 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
382 if (theRoot.IsSame(aNewShape) || (theResultShape &&
383 (!theResultShape->isSubShape(*anIt, false) || theResultShape->isSame(*anIt)))) {
384 ListOfShape::iterator aRemoveIt = anIt++;
385 theShapes.erase(aRemoveIt);
387 GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
388 if (aType < aKeepShapeType) {
389 // found a shape with lesser shape type => remove all previous shapes
390 aKeepShapeType = aType;
391 theShapes.erase(theShapes.begin(), anIt);
393 } else if (aType > aKeepShapeType) {
394 // shapes with greater shape type should be removed from the list
395 ListOfShape::iterator aRemoveIt = anIt++;
396 theShapes.erase(aRemoveIt);
403 // returns an ancestor shape-type thaty used for naming-definition of the sub-type
404 TopAbs_ShapeEnum typeOfAncestor(const TopAbs_ShapeEnum theSubType) {
405 if (theSubType == TopAbs_VERTEX)
407 if (theSubType == TopAbs_EDGE)
409 return TopAbs_VERTEX; // bad case
412 void Model_BodyBuilder::loadAndOrientModifiedShapes (
413 GeomAlgoAPI_MakeShape* theMS,
414 std::shared_ptr<GeomAPI_Shape> theShapeIn,
415 const int theKindOfShape,
417 const std::string& theName,
418 GeomAPI_DataMapOfShapeShape& theSubShapes,
419 const bool theIsStoreSeparate,
420 const bool theIsStoreAsGenerated)
424 bool isBuilt = !theName.empty();
425 std::string aName = theName;
426 std::ostringstream aStream;
427 GeomShapePtr aResultShape = shape();
428 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
429 TopTools_MapOfShape aView;
430 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
431 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
432 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
433 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
434 if (!aView.Add(aRoot)) continue;
437 TNaming_Tool::NamedShape(aRoot, aData->shapeLab()).IsNull();
438 if (aNotInTree && !theIsStoreSeparate) {
439 // there is no sense to write history if old shape does not exist in the document
440 continue; // but if it is stored separately, it will be builded as a primitive
443 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
444 aRShape->setImpl((new TopoDS_Shape(aRoot)));
445 theMS->modified(aRShape, aList);
446 if (!theIsStoreSeparate)
447 keepTopLevelShapes(aList, aRoot, aResultShape);
448 // sort the list of images before naming
449 GeomAlgoAPI_SortListOfShapes::sort(aList);
451 // to trace situation where several objects are produced by one parent (#2317)
452 int aSameParentShapes = -1;
453 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
454 anIt = aList.begin(), aLast = aList.end();
455 for (; anIt != aLast; anIt++) {
456 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
457 if (theSubShapes.isBound(*anIt)) {
458 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
459 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
461 GeomShapePtr aGeomNewShape(new GeomAPI_Shape());
462 aGeomNewShape->setImpl(new TopoDS_Shape(aNewShape));
463 if(!aRoot.IsSame(aNewShape) && aResultShape->isSubShape(aGeomNewShape, false) &&
464 !aResultShape->isSame(*anIt)) { // to avoid put of same shape on main label and sub
465 int aBuilderTag = aTag;
466 if (!theIsStoreSeparate) {
468 } else if (aNotInTree) { // check this new shape can not be represented as
469 // a sub-shape of higher level sub-shapes
470 TopAbs_ShapeEnum aNewType = aNewShape.ShapeType();
471 TopAbs_ShapeEnum anAncestorType = typeOfAncestor(aNewType);
472 if (anAncestorType != TopAbs_VERTEX) {
474 TopoDS_Shape aResultTShape = aResultShape->impl<TopoDS_Shape>();
475 TopExp_Explorer anAncestorExp(aResultTShape, anAncestorType);
476 for(; anAncestorExp.More() && !aFound; anAncestorExp.Next()) {
477 if (aResultTShape.IsSame(anAncestorExp.Current()))
479 TopExp_Explorer aSubExp(anAncestorExp.Current(), aNewType);
480 for(; aSubExp.More(); aSubExp.Next()) {
481 if (aNewShape.IsSame(aSubExp.Current())) {
488 continue; // not need to store this shape in the BRep structure
493 static const int THE_ANCHOR_TAG = 100000;
494 int aCurShapeType = (int)((*anIt)->shapeType());
495 bool needSuffix = false; // suffix for the name based on the shape type
496 if (aSameParentShapes > 0) { // store in other label
497 aBuilderTag = THE_ANCHOR_TAG - aSameParentShapes * 10 - aCurShapeType;
499 } else if (aCurShapeType != theKindOfShape) {
500 // modified shape has different type => set another tag
501 // to avoid shapes of different types on the same label
502 aBuilderTag = THE_ANCHOR_TAG - aCurShapeType;
507 switch (aCurShapeType) {
508 case GeomAPI_Shape::VERTEX: aSuffix = "_v"; break;
509 case GeomAPI_Shape::EDGE: aSuffix = "_e"; break;
510 case GeomAPI_Shape::FACE: aSuffix = "_f"; break;
515 if(theIsStoreAsGenerated) {
516 // Here we store shapes as generated, to avoid problem when one parent shape produce
517 // several child shapes. In this case naming could not determine which shape to select.
518 builder(aBuilderTag)->Generated(aRoot, aNewShape);
519 } else if (aNotInTree) {
520 // not in tree -> store as primitive (stored as separated)
521 builder(aBuilderTag)->Generated(aNewShape);
522 } else if (aNewShape.ShapeType() > aRoot.ShapeType()) {
523 // if lower-level type is produced, make it as generated
524 builder(aBuilderTag)->Generated(aRoot, aNewShape);
526 builder(aBuilderTag)->Modify(aRoot, aNewShape);
529 aStream.str(std::string());
532 if(theIsStoreSeparate)
533 aStream << "_" << anIndex++;
535 if (aSameParentShapes > 0) {
536 aStream.str(std::string());
538 aStream << aName << "_" << aSameParentShapes << "divided";
542 buildName(aBuilderTag, aStream.str());
544 if(theIsStoreSeparate) {
547 } else if (aResultShape->isSame(*anIt)) {
548 // keep the modification evolution on the root level (2241 - history propagation issue)
549 if(theIsStoreAsGenerated) {
550 builder(0)->Generated(aRoot, aNewShape);
552 builder(0)->Modify(aRoot, aNewShape);
559 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
560 GeomAlgoAPI_MakeShape* theMS,
561 std::shared_ptr<GeomAPI_Shape> theShapeIn,
562 const int theKindOfShape,
564 const std::string& theName,
565 GeomAPI_DataMapOfShapeShape& theSubShapes)
567 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
568 TopTools_MapOfShape aView;
569 bool isBuilt = !theName.empty();
570 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
571 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
572 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
573 if (!aView.Add(aRoot)) continue;
574 //if (TNaming_Tool::NamedShape(aRoot, builder(theTag)->NamedShape()->Label()).IsNull())
575 // continue; // there is no sense to write history if old shape does not exist in the document
577 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
578 aRShape->setImpl((new TopoDS_Shape(aRoot)));
579 theMS->generated(aRShape, aList);
580 keepTopLevelShapes(aList, aRoot);
581 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
582 anIt = aList.begin(), aLast = aList.end();
583 for (; anIt != aLast; anIt++) {
584 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
585 if (theSubShapes.isBound(*anIt)) {
586 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
587 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
589 if (!aRoot.IsSame (aNewShape)) {
590 builder(theTag)->Generated(aRoot,aNewShape);
592 buildName(theTag, theName);
594 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
595 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
596 TopAbs_ShapeEnum anExplodeShapeType =
597 aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
598 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
600 std::shared_ptr<Model_Document> aDoc =
601 std::dynamic_pointer_cast<Model_Document>(document());
602 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
603 TDF_Label aChildLabel = aLabel.FindChild(aTag);
604 TNaming_Builder aBuilder(aChildLabel);
605 aBuilder.Generated(aRoot, anExp.Current());
606 TCollection_AsciiString aChildName =
607 TCollection_AsciiString((theName + "_").c_str()) + aTag;
608 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
616 //=======================================================================
617 int getDangleShapes(const TopoDS_Shape& theShapeIn,
618 const TopAbs_ShapeEnum theGeneratedFrom,
619 TopTools_DataMapOfShapeShape& theDangles)
622 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
623 TopAbs_ShapeEnum GeneratedTo;
624 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
625 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
626 else return Standard_False;
627 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
628 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
629 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
630 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
631 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
633 return theDangles.Extent();
636 //=======================================================================
637 void loadGeneratedDangleShapes(
638 const TopoDS_Shape& theShapeIn,
639 const TopAbs_ShapeEnum theGeneratedFrom,
640 TNaming_Builder * theBuilder)
642 TopTools_DataMapOfShapeShape dangles;
643 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
644 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
645 for (; itr.More(); itr.Next())
646 theBuilder->Generated(itr.Key(), itr.Value());
649 //=======================================================================
650 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
651 const std::string& theName, int& theTag)
653 if(theShape->isNull()) return;
654 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
656 if (aShape.ShapeType() == TopAbs_SOLID) {
657 TopExp_Explorer expl(aShape, TopAbs_FACE);
658 for (; expl.More(); expl.Next()) {
659 builder(theTag)->Generated(expl.Current());
660 TCollection_AsciiString aStr(theTag);
661 aName = theName + aStr.ToCString();
662 buildName(theTag, aName);
666 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
667 // load faces and all the free edges
668 TopTools_IndexedMapOfShape Faces;
669 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
670 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
671 TopExp_Explorer expl(aShape, TopAbs_FACE);
672 for (; expl.More(); expl.Next()) {
673 builder(theTag)->Generated(expl.Current());
674 TCollection_AsciiString aStr(theTag);
675 aName = theName + aStr.ToCString();
676 buildName(theTag, aName);
680 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
681 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
682 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
684 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
685 if (aLL.Extent() < 2) {
686 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
688 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
689 TCollection_AsciiString aStr(theTag);
690 aName = theName + aStr.ToCString();
691 buildName(theTag, aName);
694 TopTools_ListIteratorOfListOfShape anIter(aLL);
695 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
697 if(aFace.IsEqual(anIter.Value())) {
698 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
699 TCollection_AsciiString aStr(theTag);
700 aName = theName + aStr.ToCString();
701 buildName(theTag, aName);
706 } else if (aShape.ShapeType() == TopAbs_WIRE) {
707 TopTools_IndexedMapOfShape Edges;
708 BRepTools::Map3DEdges(aShape, Edges);
709 if (Edges.Extent() == 1) {
710 builder(theTag++)->Generated(Edges.FindKey(1));
711 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
712 for (; expl.More(); expl.Next()) {
713 builder(theTag)->Generated(expl.Current());
714 TCollection_AsciiString aStr(theTag);
715 aName = theName + aStr.ToCString();
716 buildName(theTag, aName);
720 TopExp_Explorer expl(aShape, TopAbs_EDGE);
721 for (; expl.More(); expl.Next()) {
722 builder(theTag)->Generated(expl.Current());
723 TCollection_AsciiString aStr(theTag);
724 aName = theName + aStr.ToCString();
725 buildName(theTag, aName);
728 // and load generated vertices.
729 TopTools_DataMapOfShapeShape generated;
730 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
732 TNaming_Builder* pBuilder = builder(theTag++);
733 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
736 } else if (aShape.ShapeType() == TopAbs_EDGE) {
737 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
738 for (; expl.More(); expl.Next()) {
739 builder(theTag)->Generated(expl.Current());
740 TCollection_AsciiString aStr(theTag);
741 aName = theName + aStr.ToCString();
742 buildName(theTag, aName);
748 //=======================================================================
749 int findAmbiguities(const TopoDS_Shape& theShapeIn,
750 TopTools_ListOfShape& theList)
753 // edges -> ancestor faces list
754 TopTools_IndexedDataMapOfShapeListOfShape aSubShapeAndAncestors;
755 TopExp::MapShapesAndAncestors(theShapeIn, TopAbs_EDGE, TopAbs_FACE, aSubShapeAndAncestors);
756 // keeps the shapes which are already in the resulting list
757 TopTools_MapOfShape alreadyThere;
758 // map from faces identifier (combination of hash-codes) to list of edges produced such ID
759 NCollection_DataMap<int, NCollection_List<TopoDS_Shape> > aFacesIDs;
761 TopTools_IndexedDataMapOfShapeListOfShape::Iterator anAncestorsIter(aSubShapeAndAncestors);
762 for (; anAncestorsIter.More(); anAncestorsIter.Next()) {
763 const TopTools_ListOfShape& ancestors = anAncestorsIter.Value();
764 if (ancestors.Extent() < 2)
766 Standard_Integer anID = 0;
767 for(TopTools_ListIteratorOfListOfShape aFaceIt(ancestors); aFaceIt.More(); aFaceIt.Next()) {
768 anID ^= HashCode(aFaceIt.ChangeValue(), 1990657); // Pierpont prime
770 if (aFacesIDs.IsBound(anID)) { // there found same edge, check they really have same faces
771 const NCollection_List<TopoDS_Shape>& aSameFaces1 =
772 aSubShapeAndAncestors.FindFromKey(anAncestorsIter.Key());
773 NCollection_List<TopoDS_Shape>::Iterator aSameEdge(aFacesIDs.ChangeFind(anID));
774 for(; aSameEdge.More(); aSameEdge.Next()) {
775 const NCollection_List<TopoDS_Shape>& aSameFaces2 =
776 aSubShapeAndAncestors.FindFromKey(aSameEdge.Value());
777 if (aSameFaces2.Extent() != aSameFaces1.Extent()) // the number of faces is different
780 NCollection_List<TopoDS_Shape>::Iterator aFaceIter1(aSameFaces1);
781 for(; aFaceIter1.More(); aFaceIter1.Next()) {
782 NCollection_List<TopoDS_Shape>::Iterator aFaceIter2(aSameFaces2);
783 for(; aFaceIter2.More(); aFaceIter2.Next()) {
784 if (aFaceIter1.Value().IsSame(aFaceIter2.Value()))
787 if (!aFaceIter2.More()) // aFaceIter1 contains a face, which is not in aFaceIter2
790 if (!aFaceIter1.More()) { // all the faces are same => put to the result
791 if (alreadyThere.Add(aSameEdge.Value()))
792 theList.Append(aSameEdge.Value());
793 if (alreadyThere.Add(anAncestorsIter.Key()))
794 theList.Append(anAncestorsIter.Key());
797 } else { // ID is unique, just add this edge
798 aFacesIDs.Bind(anID, NCollection_List<TopoDS_Shape>());
800 aFacesIDs.ChangeFind(anID).Append(anAncestorsIter.Key()); // add to the list anyway
802 return theList.Extent();
805 //=======================================================================
806 void Model_BodyBuilder::loadFirstLevel(
807 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
809 if(theShape->isNull()) return;
810 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
812 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
813 TopoDS_Iterator itr(aShape);
814 for (; itr.More(); itr.Next()) {
815 builder(theTag)->Generated(itr.Value());
816 TCollection_AsciiString aStr(theTag);
817 aName = theName + aStr.ToCString();
818 buildName(theTag, aName);
819 if(!theName.empty()) buildName(theTag, aName);
821 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
822 itr.Value().ShapeType() == TopAbs_COMPSOLID)
824 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
825 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
826 loadFirstLevel(itrShape, theName, theTag);
828 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
829 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
830 loadNextLevels(itrShape, theName, theTag);
834 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
835 itrShape->setImpl(new TopoDS_Shape(aShape));
836 loadNextLevels(itrShape, theName, theTag);
838 TopTools_ListOfShape aList;
839 if(findAmbiguities(aShape, aList)) {
840 TopTools_ListIteratorOfListOfShape it(aList);
841 for (; it.More(); it.Next(),theTag++) {
842 builder(theTag)->Generated(it.Value());
843 TCollection_AsciiString aStr(theTag);
844 aName = theName + aStr.ToCString();
845 buildName(theTag, aName);
850 //=======================================================================
851 void Model_BodyBuilder::loadDisconnectedEdges(
852 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
854 if(theShape->isNull()) return;
855 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
856 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
857 TopTools_ListOfShape empty;
858 TopExp_Explorer explF(aShape, TopAbs_FACE);
859 for (; explF.More(); explF.Next()) {
860 const TopoDS_Shape& aFace = explF.Current();
861 TopExp_Explorer explV(aFace, TopAbs_EDGE);
862 for (; explV.More(); explV.Next()) {
863 const TopoDS_Shape& anEdge = explV.Current();
864 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
865 Standard_Boolean faceIsNew = Standard_True;
866 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
867 for (; itrF.More(); itrF.Next()) {
868 if (itrF.Value().IsSame(aFace)) {
869 faceIsNew = Standard_False;
874 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
878 TopTools_MapOfShape anEdgesToDelete;
879 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
881 for(;anEx.More();anEx.Next()) {
882 Standard_Boolean aC0 = Standard_False;
883 TopoDS_Shape anEdge1 = anEx.Current();
884 if (edgeNaborFaces.IsBound(anEdge1)) {
885 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
886 if (aList1.Extent()<2) continue;
887 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
888 for (; itr.More(); itr.Next()) {
889 TopoDS_Shape anEdge2 = itr.Key();
890 if(anEdgesToDelete.Contains(anEdge2)) continue;
891 if (anEdge1.IsSame(anEdge2)) continue;
892 const TopTools_ListOfShape& aList2 = itr.Value();
893 // compare lists of the neighbour faces of edge1 and edge2
894 if (aList1.Extent() == aList2.Extent()) {
895 Standard_Integer aMatches = 0;
896 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
897 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
898 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
899 if (aMatches == aList1.Extent()) {
901 builder(theTag)->Generated(anEdge2);
902 anEdgesToDelete.Add(anEdge2);
903 TCollection_AsciiString aStr(theTag);
904 aName = theName + aStr.ToCString();
905 buildName(theTag, aName);
910 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
911 for(;itDelete.More();itDelete.Next())
912 edgeNaborFaces.UnBind(itDelete.Key());
913 edgeNaborFaces.UnBind(anEdge1);
916 builder(theTag)->Generated(anEdge1);
917 TCollection_AsciiString aStr(theTag);
918 aName = theName + aStr.ToCString();
919 buildName(theTag, aName);
925 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape,
926 const std::string& theName, int& theTag)
928 if(theShape->isNull()) return;
929 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
930 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
931 TopTools_ListOfShape empty;
932 TopExp_Explorer explF(aShape, TopAbs_EDGE);
933 for (; explF.More(); explF.Next()) {
934 const TopoDS_Shape& anEdge = explF.Current();
935 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
936 for (; explV.More(); explV.Next()) {
937 const TopoDS_Shape& aVertex = explV.Current();
938 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
939 Standard_Boolean faceIsNew = Standard_True;
940 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
941 for (; itrF.More(); itrF.Next()) {
942 if (itrF.Value().IsSame(anEdge)) {
943 faceIsNew = Standard_False;
948 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
953 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
954 for (; itr.More(); itr.Next()) {
955 const TopTools_ListOfShape& naborEdges = itr.Value();
956 if (naborEdges.Extent() < 2) {
957 builder(theTag)->Generated(itr.Key());
958 TCollection_AsciiString aStr(theTag);
959 aName = theName + aStr.ToCString();
960 buildName(theTag, aName);
966 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
968 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
970 TDF_Label aShapeLab = aData->shapeLab();
971 Handle(TDF_Reference) aRef;
972 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
973 aShapeLab = aRef->Get();
975 Handle(TNaming_NamedShape) aName;
976 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
977 TopoDS_Shape aShape = aName->Get();
978 if (!aShape.IsNull()) {
979 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
980 aRes->setImpl(new TopoDS_Shape(aShape));
985 return std::shared_ptr<GeomAPI_Shape>();
988 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
990 if (theShape.get()) {
991 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
992 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
994 TDF_Label& aShapeLab = aData->shapeLab();
995 Handle(TNaming_NamedShape) aName;
996 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
997 TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
998 if (aLatest.IsNull())
1000 if (aLatest.IsEqual(aShape))
1002 // check sub-shapes for comp-solids:
1003 for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
1004 if (aLatest.IsEqual(anExp.Current()))