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 bool aSkip = aNewShape.IsNull() ||
389 (aNewShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aNewShape)));
390 if (aSkip || theRoot.IsSame(aNewShape) || (theResultShape &&
391 (!theResultShape->isSubShape(*anIt, false) || theResultShape->isSame(*anIt)))) {
392 ListOfShape::iterator aRemoveIt = anIt++;
393 theShapes.erase(aRemoveIt);
395 GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
396 if (aType < aKeepShapeType) {
397 // found a shape with lesser shape type => remove all previous shapes
398 aKeepShapeType = aType;
399 theShapes.erase(theShapes.begin(), anIt);
401 } else if (aType > aKeepShapeType) {
402 // shapes with greater shape type should be removed from the list
403 ListOfShape::iterator aRemoveIt = anIt++;
404 theShapes.erase(aRemoveIt);
411 // returns an ancestor shape-type thaty used for naming-definition of the sub-type
412 TopAbs_ShapeEnum typeOfAncestor(const TopAbs_ShapeEnum theSubType) {
413 if (theSubType == TopAbs_VERTEX)
415 if (theSubType == TopAbs_EDGE)
417 return TopAbs_VERTEX; // bad case
420 void Model_BodyBuilder::loadAndOrientModifiedShapes (
421 GeomAlgoAPI_MakeShape* theMS,
422 std::shared_ptr<GeomAPI_Shape> theShapeIn,
423 const int theKindOfShape,
425 const std::string& theName,
426 GeomAPI_DataMapOfShapeShape& theSubShapes,
427 const bool theIsStoreSeparate,
428 const bool theIsStoreAsGenerated)
432 bool isBuilt = !theName.empty();
433 std::string aName = theName;
434 std::ostringstream aStream;
435 GeomShapePtr aResultShape = shape();
436 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
437 TopTools_MapOfShape aView;
438 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
439 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
440 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
441 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
442 if (!aView.Add(aRoot)) continue;
445 TNaming_Tool::NamedShape(aRoot, aData->shapeLab()).IsNull();
446 if (aNotInTree && !theIsStoreSeparate) {
447 // there is no sense to write history if old shape does not exist in the document
448 continue; // but if it is stored separately, it will be builded as a primitive
451 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
452 aRShape->setImpl((new TopoDS_Shape(aRoot)));
453 theMS->modified(aRShape, aList);
454 if (!theIsStoreSeparate)
455 keepTopLevelShapes(aList, aRoot, aResultShape);
456 // sort the list of images before naming
457 GeomAlgoAPI_SortListOfShapes::sort(aList);
459 // to trace situation where several objects are produced by one parent (#2317)
460 int aSameParentShapes = -1;
461 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
462 anIt = aList.begin(), aLast = aList.end();
463 for (; anIt != aLast; anIt++) {
464 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
465 if (theSubShapes.isBound(*anIt)) {
466 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
467 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
469 GeomShapePtr aGeomNewShape(new GeomAPI_Shape());
470 aGeomNewShape->setImpl(new TopoDS_Shape(aNewShape));
471 if(!aRoot.IsSame(aNewShape) && aResultShape->isSubShape(aGeomNewShape, false) &&
472 !aResultShape->isSame(*anIt)) { // to avoid put of same shape on main label and sub
473 int aBuilderTag = aTag;
474 if (!theIsStoreSeparate) {
476 } else if (aNotInTree) { // check this new shape can not be represented as
477 // a sub-shape of higher level sub-shapes
478 TopAbs_ShapeEnum aNewType = aNewShape.ShapeType();
479 TopAbs_ShapeEnum anAncestorType = typeOfAncestor(aNewType);
480 if (anAncestorType != TopAbs_VERTEX) {
482 TopoDS_Shape aResultTShape = aResultShape->impl<TopoDS_Shape>();
483 TopExp_Explorer anAncestorExp(aResultTShape, anAncestorType);
484 for(; anAncestorExp.More() && !aFound; anAncestorExp.Next()) {
485 if (aResultTShape.IsSame(anAncestorExp.Current()))
487 TopExp_Explorer aSubExp(anAncestorExp.Current(), aNewType);
488 for(; aSubExp.More(); aSubExp.Next()) {
489 if (aNewShape.IsSame(aSubExp.Current())) {
496 continue; // not need to store this shape in the BRep structure
501 static const int THE_ANCHOR_TAG = 100000;
502 int aCurShapeType = (int)((*anIt)->shapeType());
503 bool needSuffix = false; // suffix for the name based on the shape type
504 if (aSameParentShapes > 0) { // store in other label
505 aBuilderTag = THE_ANCHOR_TAG - aSameParentShapes * 10 - aCurShapeType;
507 } else if (aCurShapeType != theKindOfShape) {
508 // modified shape has different type => set another tag
509 // to avoid shapes of different types on the same label
510 aBuilderTag = THE_ANCHOR_TAG - aCurShapeType;
515 switch (aCurShapeType) {
516 case GeomAPI_Shape::VERTEX: aSuffix = "_v"; break;
517 case GeomAPI_Shape::EDGE: aSuffix = "_e"; break;
518 case GeomAPI_Shape::FACE: aSuffix = "_f"; break;
523 if(theIsStoreAsGenerated) {
524 // Here we store shapes as generated, to avoid problem when one parent shape produce
525 // several child shapes. In this case naming could not determine which shape to select.
526 builder(aBuilderTag)->Generated(aRoot, aNewShape);
527 } else if (aNotInTree) {
528 // not in tree -> store as primitive (stored as separated)
529 builder(aBuilderTag)->Generated(aNewShape);
530 } else if (aNewShape.ShapeType() > aRoot.ShapeType()) {
531 // if lower-level type is produced, make it as generated
532 builder(aBuilderTag)->Generated(aRoot, aNewShape);
534 builder(aBuilderTag)->Modify(aRoot, aNewShape);
537 aStream.str(std::string());
540 if(theIsStoreSeparate)
541 aStream << "_" << anIndex++;
543 if (aSameParentShapes > 0) {
544 aStream.str(std::string());
546 aStream << aName << "_" << aSameParentShapes << "divided";
550 buildName(aBuilderTag, aStream.str());
552 if(theIsStoreSeparate) {
555 } else if (aResultShape->isSame(*anIt)) {
556 // keep the modification evolution on the root level (2241 - history propagation issue)
557 if(theIsStoreAsGenerated) {
558 builder(0)->Generated(aRoot, aNewShape);
560 builder(0)->Modify(aRoot, aNewShape);
567 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
568 GeomAlgoAPI_MakeShape* theMS,
569 std::shared_ptr<GeomAPI_Shape> theShapeIn,
570 const int theKindOfShape,
572 const std::string& theName,
573 GeomAPI_DataMapOfShapeShape& theSubShapes)
575 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
576 TopTools_MapOfShape aView;
577 bool isBuilt = !theName.empty();
578 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
579 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
580 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
581 if (!aView.Add(aRoot)) continue;
582 //if (TNaming_Tool::NamedShape(aRoot, builder(theTag)->NamedShape()->Label()).IsNull())
583 // continue; // there is no sense to write history if old shape does not exist in the document
585 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
586 aRShape->setImpl((new TopoDS_Shape(aRoot)));
587 theMS->generated(aRShape, aList);
588 keepTopLevelShapes(aList, aRoot);
589 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
590 anIt = aList.begin(), aLast = aList.end();
591 for (; anIt != aLast; anIt++) {
592 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
593 if (theSubShapes.isBound(*anIt)) {
594 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
595 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
597 if (!aRoot.IsSame (aNewShape)) {
598 builder(theTag)->Generated(aRoot,aNewShape);
600 buildName(theTag, theName);
602 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
603 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
604 TopAbs_ShapeEnum anExplodeShapeType =
605 aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
606 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
608 std::shared_ptr<Model_Document> aDoc =
609 std::dynamic_pointer_cast<Model_Document>(document());
610 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
611 TDF_Label aChildLabel = aLabel.FindChild(aTag);
612 TNaming_Builder aBuilder(aChildLabel);
613 aBuilder.Generated(aRoot, anExp.Current());
614 TCollection_AsciiString aChildName =
615 TCollection_AsciiString((theName + "_").c_str()) + aTag;
616 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
624 //=======================================================================
625 int getDangleShapes(const TopoDS_Shape& theShapeIn,
626 const TopAbs_ShapeEnum theGeneratedFrom,
627 TopTools_DataMapOfShapeShape& theDangles)
630 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
631 TopAbs_ShapeEnum GeneratedTo;
632 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
633 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
634 else return Standard_False;
635 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
636 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
637 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
638 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
639 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
641 return theDangles.Extent();
644 //=======================================================================
645 void loadGeneratedDangleShapes(
646 const TopoDS_Shape& theShapeIn,
647 const TopAbs_ShapeEnum theGeneratedFrom,
648 TNaming_Builder * theBuilder)
650 TopTools_DataMapOfShapeShape dangles;
651 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
652 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
653 for (; itr.More(); itr.Next())
654 theBuilder->Generated(itr.Key(), itr.Value());
657 //=======================================================================
658 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
659 const std::string& theName, int& theTag)
661 if(theShape->isNull()) return;
662 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
664 if (aShape.ShapeType() == TopAbs_SOLID) {
665 TopExp_Explorer expl(aShape, TopAbs_FACE);
666 for (; expl.More(); expl.Next()) {
667 builder(theTag)->Generated(expl.Current());
668 TCollection_AsciiString aStr(theTag);
669 aName = theName + aStr.ToCString();
670 buildName(theTag, aName);
674 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
675 // load faces and all the free edges
676 TopTools_IndexedMapOfShape Faces;
677 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
678 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
679 TopExp_Explorer expl(aShape, TopAbs_FACE);
680 for (; expl.More(); expl.Next()) {
681 builder(theTag)->Generated(expl.Current());
682 TCollection_AsciiString aStr(theTag);
683 aName = theName + aStr.ToCString();
684 buildName(theTag, aName);
688 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
689 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
690 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
692 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
693 if (aLL.Extent() < 2) {
694 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
696 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
697 TCollection_AsciiString aStr(theTag);
698 aName = theName + aStr.ToCString();
699 buildName(theTag, aName);
702 TopTools_ListIteratorOfListOfShape anIter(aLL);
703 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
705 if(aFace.IsEqual(anIter.Value())) {
706 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
707 TCollection_AsciiString aStr(theTag);
708 aName = theName + aStr.ToCString();
709 buildName(theTag, aName);
714 } else if (aShape.ShapeType() == TopAbs_WIRE) {
715 TopTools_IndexedMapOfShape Edges;
716 BRepTools::Map3DEdges(aShape, Edges);
717 if (Edges.Extent() == 1) {
718 builder(theTag++)->Generated(Edges.FindKey(1));
719 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
720 for (; expl.More(); expl.Next()) {
721 builder(theTag)->Generated(expl.Current());
722 TCollection_AsciiString aStr(theTag);
723 aName = theName + aStr.ToCString();
724 buildName(theTag, aName);
728 TopExp_Explorer expl(aShape, TopAbs_EDGE);
729 for (; expl.More(); expl.Next()) {
730 builder(theTag)->Generated(expl.Current());
731 TCollection_AsciiString aStr(theTag);
732 aName = theName + aStr.ToCString();
733 buildName(theTag, aName);
736 // and load generated vertices.
737 TopTools_DataMapOfShapeShape generated;
738 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
740 TNaming_Builder* pBuilder = builder(theTag++);
741 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
744 } else if (aShape.ShapeType() == TopAbs_EDGE) {
745 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
746 for (; expl.More(); expl.Next()) {
747 builder(theTag)->Generated(expl.Current());
748 TCollection_AsciiString aStr(theTag);
749 aName = theName + aStr.ToCString();
750 buildName(theTag, aName);
756 //=======================================================================
757 int findAmbiguities(const TopoDS_Shape& theShapeIn,
758 TopTools_ListOfShape& theList)
761 // edges -> ancestor faces list
762 TopTools_IndexedDataMapOfShapeListOfShape aSubShapeAndAncestors;
763 TopExp::MapShapesAndAncestors(theShapeIn, TopAbs_EDGE, TopAbs_FACE, aSubShapeAndAncestors);
764 // keeps the shapes which are already in the resulting list
765 TopTools_MapOfShape alreadyThere;
766 // map from faces identifier (combination of hash-codes) to list of edges produced such ID
767 NCollection_DataMap<int, NCollection_List<TopoDS_Shape> > aFacesIDs;
769 TopTools_IndexedDataMapOfShapeListOfShape::Iterator anAncestorsIter(aSubShapeAndAncestors);
770 for (; anAncestorsIter.More(); anAncestorsIter.Next()) {
771 const TopTools_ListOfShape& ancestors = anAncestorsIter.Value();
772 if (ancestors.Extent() < 2)
774 Standard_Integer anID = 0;
775 for(TopTools_ListIteratorOfListOfShape aFaceIt(ancestors); aFaceIt.More(); aFaceIt.Next()) {
776 anID ^= HashCode(aFaceIt.ChangeValue(), 1990657); // Pierpont prime
778 if (aFacesIDs.IsBound(anID)) { // there found same edge, check they really have same faces
779 const NCollection_List<TopoDS_Shape>& aSameFaces1 =
780 aSubShapeAndAncestors.FindFromKey(anAncestorsIter.Key());
781 NCollection_List<TopoDS_Shape>::Iterator aSameEdge(aFacesIDs.ChangeFind(anID));
782 for(; aSameEdge.More(); aSameEdge.Next()) {
783 const NCollection_List<TopoDS_Shape>& aSameFaces2 =
784 aSubShapeAndAncestors.FindFromKey(aSameEdge.Value());
785 if (aSameFaces2.Extent() != aSameFaces1.Extent()) // the number of faces is different
788 NCollection_List<TopoDS_Shape>::Iterator aFaceIter1(aSameFaces1);
789 for(; aFaceIter1.More(); aFaceIter1.Next()) {
790 NCollection_List<TopoDS_Shape>::Iterator aFaceIter2(aSameFaces2);
791 for(; aFaceIter2.More(); aFaceIter2.Next()) {
792 if (aFaceIter1.Value().IsSame(aFaceIter2.Value()))
795 if (!aFaceIter2.More()) // aFaceIter1 contains a face, which is not in aFaceIter2
798 if (!aFaceIter1.More()) { // all the faces are same => put to the result
799 if (alreadyThere.Add(aSameEdge.Value()))
800 theList.Append(aSameEdge.Value());
801 if (alreadyThere.Add(anAncestorsIter.Key()))
802 theList.Append(anAncestorsIter.Key());
805 } else { // ID is unique, just add this edge
806 aFacesIDs.Bind(anID, NCollection_List<TopoDS_Shape>());
808 aFacesIDs.ChangeFind(anID).Append(anAncestorsIter.Key()); // add to the list anyway
810 return theList.Extent();
813 //=======================================================================
814 void Model_BodyBuilder::loadFirstLevel(
815 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
817 if(theShape->isNull()) return;
818 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
820 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
821 TopoDS_Iterator itr(aShape);
822 for (; itr.More(); itr.Next()) {
823 builder(theTag)->Generated(itr.Value());
824 TCollection_AsciiString aStr(theTag);
825 aName = theName + aStr.ToCString();
826 buildName(theTag, aName);
827 if(!theName.empty()) buildName(theTag, aName);
829 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
830 itr.Value().ShapeType() == TopAbs_COMPSOLID)
832 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
833 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
834 loadFirstLevel(itrShape, theName, theTag);
836 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
837 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
838 loadNextLevels(itrShape, theName, theTag);
842 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
843 itrShape->setImpl(new TopoDS_Shape(aShape));
844 loadNextLevels(itrShape, theName, theTag);
846 TopTools_ListOfShape aList;
847 if(findAmbiguities(aShape, aList)) {
848 TopTools_ListIteratorOfListOfShape it(aList);
849 for (; it.More(); it.Next(),theTag++) {
850 builder(theTag)->Generated(it.Value());
851 TCollection_AsciiString aStr(theTag);
852 aName = theName + aStr.ToCString();
853 buildName(theTag, aName);
858 //=======================================================================
859 void Model_BodyBuilder::loadDisconnectedEdges(
860 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
862 if(theShape->isNull()) return;
863 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
864 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
865 TopTools_ListOfShape empty;
866 TopExp_Explorer explF(aShape, TopAbs_FACE);
867 for (; explF.More(); explF.Next()) {
868 const TopoDS_Shape& aFace = explF.Current();
869 TopExp_Explorer explV(aFace, TopAbs_EDGE);
870 for (; explV.More(); explV.Next()) {
871 const TopoDS_Shape& anEdge = explV.Current();
872 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
873 Standard_Boolean faceIsNew = Standard_True;
874 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
875 for (; itrF.More(); itrF.Next()) {
876 if (itrF.Value().IsSame(aFace)) {
877 faceIsNew = Standard_False;
882 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
886 TopTools_MapOfShape anEdgesToDelete;
887 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
889 for(;anEx.More();anEx.Next()) {
890 Standard_Boolean aC0 = Standard_False;
891 TopoDS_Shape anEdge1 = anEx.Current();
892 if (edgeNaborFaces.IsBound(anEdge1)) {
893 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
894 if (aList1.Extent()<2) continue;
895 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
896 for (; itr.More(); itr.Next()) {
897 TopoDS_Shape anEdge2 = itr.Key();
898 if(anEdgesToDelete.Contains(anEdge2)) continue;
899 if (anEdge1.IsSame(anEdge2)) continue;
900 const TopTools_ListOfShape& aList2 = itr.Value();
901 // compare lists of the neighbour faces of edge1 and edge2
902 if (aList1.Extent() == aList2.Extent()) {
903 Standard_Integer aMatches = 0;
904 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
905 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
906 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
907 if (aMatches == aList1.Extent()) {
909 builder(theTag)->Generated(anEdge2);
910 anEdgesToDelete.Add(anEdge2);
911 TCollection_AsciiString aStr(theTag);
912 aName = theName + aStr.ToCString();
913 buildName(theTag, aName);
918 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
919 for(;itDelete.More();itDelete.Next())
920 edgeNaborFaces.UnBind(itDelete.Key());
921 edgeNaborFaces.UnBind(anEdge1);
924 builder(theTag)->Generated(anEdge1);
925 TCollection_AsciiString aStr(theTag);
926 aName = theName + aStr.ToCString();
927 buildName(theTag, aName);
933 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape,
934 const std::string& theName, int& theTag)
936 if(theShape->isNull()) return;
937 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
938 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
939 TopTools_ListOfShape empty;
940 TopExp_Explorer explF(aShape, TopAbs_EDGE);
941 for (; explF.More(); explF.Next()) {
942 const TopoDS_Shape& anEdge = explF.Current();
943 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
944 for (; explV.More(); explV.Next()) {
945 const TopoDS_Shape& aVertex = explV.Current();
946 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
947 Standard_Boolean faceIsNew = Standard_True;
948 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
949 for (; itrF.More(); itrF.Next()) {
950 if (itrF.Value().IsSame(anEdge)) {
951 faceIsNew = Standard_False;
956 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
961 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
962 for (; itr.More(); itr.Next()) {
963 const TopTools_ListOfShape& naborEdges = itr.Value();
964 if (naborEdges.Extent() < 2) {
965 builder(theTag)->Generated(itr.Key());
966 TCollection_AsciiString aStr(theTag);
967 aName = theName + aStr.ToCString();
968 buildName(theTag, aName);
974 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
976 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
978 TDF_Label aShapeLab = aData->shapeLab();
979 Handle(TDF_Reference) aRef;
980 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
981 aShapeLab = aRef->Get();
983 Handle(TNaming_NamedShape) aName;
984 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
985 TopoDS_Shape aShape = aName->Get();
986 if (!aShape.IsNull()) {
987 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
988 aRes->setImpl(new TopoDS_Shape(aShape));
993 return std::shared_ptr<GeomAPI_Shape>();
996 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
998 if (theShape.get()) {
999 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
1000 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
1002 TDF_Label& aShapeLab = aData->shapeLab();
1003 Handle(TNaming_NamedShape) aName;
1004 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
1005 TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
1006 if (aLatest.IsNull())
1008 if (aLatest.IsEqual(aShape))
1010 // check sub-shapes for comp-solids:
1011 for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
1012 if (aLatest.IsEqual(anExp.Current()))