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();
219 // store the new shape as primitive
220 TNaming_Builder aBuilder(aShapeLab);
221 if (!theOldShape || !theNewShape)
223 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
224 if (aShapeOld.IsNull())
225 return; // null shape inside
226 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
227 if (aShapeNew.IsNull())
228 return; // null shape inside
229 aBuilder.Modify(aShapeOld, aShapeNew);
230 if(!aBuilder.NamedShape()->IsEmpty()) {
231 Handle(TDataStd_Name) anAttr;
232 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
233 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
235 std::shared_ptr<Model_Document> aDoc =
236 std::dynamic_pointer_cast<Model_Document>(document());
237 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
244 void Model_BodyBuilder::storeWithoutNaming(const std::shared_ptr<GeomAPI_Shape>& theShape)
246 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
251 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
253 return; // null shape inside
254 TNaming_Builder aBuilder(aData->shapeLab());
255 aBuilder.Select(aShape, aShape);
259 void Model_BodyBuilder::clean()
261 TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>(data())->shapeLab();
264 std::map<int, TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
265 for(; aBuilder != myBuilders.end(); aBuilder++) {
266 delete aBuilder->second;
267 // clear also shapes on cleaned sub-labels (#2241)
268 Handle(TNaming_NamedShape) aNS;
269 if (aLab.FindChild(aBuilder->first).FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
274 // remove the old reference (if any)
275 aLab.ForgetAttribute(TDF_Reference::GetID());
278 Model_BodyBuilder::~Model_BodyBuilder()
283 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
285 std::map<int, TNaming_Builder*>::iterator aFind = myBuilders.find(theTag);
286 if (aFind == myBuilders.end()) {
287 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
288 myBuilders[theTag] = new TNaming_Builder(
289 theTag == 0 ? aData->shapeLab() : aData->shapeLab().FindChild(theTag));
290 aFind = myBuilders.find(theTag);
292 return aFind->second;
295 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
297 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
298 //aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), theName);
299 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(), theName.c_str());
301 void Model_BodyBuilder::generated(
302 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
304 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
305 builder(theTag)->Generated(aShape);
307 buildName(theTag, theName);
310 void Model_BodyBuilder::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
311 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
313 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
314 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
315 builder(theTag)->Generated(anOldShape, aNewShape);
317 buildName(theTag, theName);
318 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
319 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
320 TopAbs_ShapeEnum anExplodeShapeType = aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
321 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
323 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
324 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
325 TDF_Label aChildLabel = aLabel.FindChild(aTag);
326 TNaming_Builder aBuilder(aChildLabel);
327 aBuilder.Generated(anOldShape, anExp.Current());
328 TCollection_AsciiString aChildName = TCollection_AsciiString((theName + "_").c_str()) + aTag;
329 //aDoc->addNamingName(aChildLabel, aChildName.ToCString());
330 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
337 void Model_BodyBuilder::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
338 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
340 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
341 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
342 builder(theTag)->Modify(anOldShape, aNewShape);
344 buildName(theTag, theName);
347 void Model_BodyBuilder::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
350 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
351 builder(theTag)->Delete(aShape);
354 void Model_BodyBuilder::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
355 std::shared_ptr<GeomAPI_Shape> theShapeIn,
356 const int theKindOfShape,
359 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
360 TopTools_MapOfShape aView;
361 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
362 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
363 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
364 if (!aView.Add(aRoot)) continue;
365 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
366 aRShape->setImpl((new TopoDS_Shape(aRoot)));
367 if (theMS->isDeleted (aRShape)) {
368 builder(theTag)->Delete(aRoot);
373 // Keep only the shapes with minimal shape type
374 static void keepTopLevelShapes(ListOfShape& theShapes, const TopoDS_Shape& theRoot,
375 const GeomShapePtr& theResultShape = GeomShapePtr())
377 GeomAPI_Shape::ShapeType aKeepShapeType = GeomAPI_Shape::SHAPE;
378 ListOfShape::iterator anIt = theShapes.begin();
379 while (anIt != theShapes.end()) {
380 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
381 if (theRoot.IsSame(aNewShape) || (theResultShape &&
382 (!theResultShape->isSubShape(*anIt, false) || theResultShape->isSame(*anIt)))) {
383 ListOfShape::iterator aRemoveIt = anIt++;
384 theShapes.erase(aRemoveIt);
386 GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
387 if (aType < aKeepShapeType) {
388 // found a shape with lesser shape type => remove all previous shapes
389 aKeepShapeType = aType;
390 theShapes.erase(theShapes.begin(), anIt);
392 } else if (aType > aKeepShapeType) {
393 // shapes with greater shape type should be removed from the list
394 ListOfShape::iterator aRemoveIt = anIt++;
395 theShapes.erase(aRemoveIt);
402 // returns an ancestor shape-type thaty used for naming-definition of the sub-type
403 TopAbs_ShapeEnum typeOfAncestor(const TopAbs_ShapeEnum theSubType) {
404 if (theSubType == TopAbs_VERTEX)
406 if (theSubType == TopAbs_EDGE)
408 return TopAbs_VERTEX; // bad case
411 void Model_BodyBuilder::loadAndOrientModifiedShapes (
412 GeomAlgoAPI_MakeShape* theMS,
413 std::shared_ptr<GeomAPI_Shape> theShapeIn,
414 const int theKindOfShape,
416 const std::string& theName,
417 GeomAPI_DataMapOfShapeShape& theSubShapes,
418 const bool theIsStoreSeparate,
419 const bool theIsStoreAsGenerated)
423 bool isBuilt = !theName.empty();
424 std::string aName = theName;
425 std::ostringstream aStream;
426 GeomShapePtr aResultShape = shape();
427 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
428 TopTools_MapOfShape aView;
429 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
430 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
431 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
432 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
433 if (!aView.Add(aRoot)) continue;
436 TNaming_Tool::NamedShape(aRoot, aData->shapeLab()).IsNull();
437 if (aNotInTree && !theIsStoreSeparate) {
438 // there is no sense to write history if old shape does not exist in the document
439 continue; // but if it is stored separately, it will be builded as a primitive
442 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
443 aRShape->setImpl((new TopoDS_Shape(aRoot)));
444 theMS->modified(aRShape, aList);
445 if (!theIsStoreSeparate)
446 keepTopLevelShapes(aList, aRoot, aResultShape);
447 // sort the list of images before naming
448 GeomAlgoAPI_SortListOfShapes::sort(aList);
450 // to trace situation where several objects are produced by one parent (#2317)
451 int aSameParentShapes = -1;
452 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
453 anIt = aList.begin(), aLast = aList.end();
454 for (; anIt != aLast; anIt++) {
455 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
456 if (theSubShapes.isBound(*anIt)) {
457 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
458 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
460 GeomShapePtr aGeomNewShape(new GeomAPI_Shape());
461 aGeomNewShape->setImpl(new TopoDS_Shape(aNewShape));
462 if(!aRoot.IsSame(aNewShape) && aResultShape->isSubShape(aGeomNewShape, false) &&
463 !aResultShape->isSame(*anIt)) { // to avoid put of same shape on main label and sub
464 int aBuilderTag = aTag;
465 if (!theIsStoreSeparate) {
467 } else if (aNotInTree) { // check this new shape can not be represented as
468 // a sub-shape of higher level sub-shapes
469 TopAbs_ShapeEnum aNewType = aNewShape.ShapeType();
470 TopAbs_ShapeEnum anAncestorType = typeOfAncestor(aNewType);
471 if (anAncestorType != TopAbs_VERTEX) {
473 TopoDS_Shape aResultTShape = aResultShape->impl<TopoDS_Shape>();
474 TopExp_Explorer anAncestorExp(aResultTShape, anAncestorType);
475 for(; anAncestorExp.More() && !aFound; anAncestorExp.Next()) {
476 if (aResultTShape.IsSame(anAncestorExp.Current()))
478 TopExp_Explorer aSubExp(anAncestorExp.Current(), aNewType);
479 for(; aSubExp.More(); aSubExp.Next()) {
480 if (aNewShape.IsSame(aSubExp.Current())) {
487 continue; // not need to store this shape in the BRep structure
492 static const int THE_ANCHOR_TAG = 100000;
493 int aCurShapeType = (int)((*anIt)->shapeType());
494 bool needSuffix = false; // suffix for the name based on the shape type
495 if (aSameParentShapes > 0) { // store in other label
496 aBuilderTag = THE_ANCHOR_TAG - aSameParentShapes * 10 - aCurShapeType;
498 } else if (aCurShapeType != theKindOfShape) {
499 // modified shape has different type => set another tag
500 // to avoid shapes of different types on the same label
501 aBuilderTag = THE_ANCHOR_TAG - aCurShapeType;
506 switch (aCurShapeType) {
507 case GeomAPI_Shape::VERTEX: aSuffix = "_v"; break;
508 case GeomAPI_Shape::EDGE: aSuffix = "_e"; break;
509 case GeomAPI_Shape::FACE: aSuffix = "_f"; break;
514 if(theIsStoreAsGenerated) {
515 // Here we store shapes as generated, to avoid problem when one parent shape produce
516 // several child shapes. In this case naming could not determine which shape to select.
517 builder(aBuilderTag)->Generated(aRoot, aNewShape);
518 } else if (aNotInTree) {
519 // not in tree -> store as primitive (stored as separated)
520 builder(aBuilderTag)->Generated(aNewShape);
522 builder(aBuilderTag)->Modify(aRoot, aNewShape);
525 aStream.str(std::string());
528 if(theIsStoreSeparate)
529 aStream << "_" << anIndex++;
531 if (aSameParentShapes > 0) {
532 aStream.str(std::string());
534 aStream << aName << "_" << aSameParentShapes << "divided";
538 buildName(aBuilderTag, aStream.str());
540 if(theIsStoreSeparate) {
543 } else if (aResultShape->isSame(*anIt)) {
544 // keep the modification evolution on the root level (2241 - history propagation issue)
545 if(theIsStoreAsGenerated) {
546 builder(0)->Generated(aRoot, aNewShape);
548 builder(0)->Modify(aRoot, aNewShape);
555 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
556 GeomAlgoAPI_MakeShape* theMS,
557 std::shared_ptr<GeomAPI_Shape> theShapeIn,
558 const int theKindOfShape,
560 const std::string& theName,
561 GeomAPI_DataMapOfShapeShape& theSubShapes)
563 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
564 TopTools_MapOfShape aView;
565 bool isBuilt = !theName.empty();
566 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
567 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
568 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
569 if (!aView.Add(aRoot)) continue;
570 //if (TNaming_Tool::NamedShape(aRoot, builder(theTag)->NamedShape()->Label()).IsNull())
571 // continue; // there is no sense to write history if old shape does not exist in the document
573 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
574 aRShape->setImpl((new TopoDS_Shape(aRoot)));
575 theMS->generated(aRShape, aList);
576 keepTopLevelShapes(aList, aRoot);
577 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
578 anIt = aList.begin(), aLast = aList.end();
579 for (; anIt != aLast; anIt++) {
580 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
581 if (theSubShapes.isBound(*anIt)) {
582 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
583 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
585 if (!aRoot.IsSame (aNewShape)) {
586 builder(theTag)->Generated(aRoot,aNewShape);
588 buildName(theTag, theName);
590 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
591 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
592 TopAbs_ShapeEnum anExplodeShapeType =
593 aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
594 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
596 std::shared_ptr<Model_Document> aDoc =
597 std::dynamic_pointer_cast<Model_Document>(document());
598 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
599 TDF_Label aChildLabel = aLabel.FindChild(aTag);
600 TNaming_Builder aBuilder(aChildLabel);
601 aBuilder.Generated(aRoot, anExp.Current());
602 TCollection_AsciiString aChildName =
603 TCollection_AsciiString((theName + "_").c_str()) + aTag;
604 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
612 //=======================================================================
613 int getDangleShapes(const TopoDS_Shape& theShapeIn,
614 const TopAbs_ShapeEnum theGeneratedFrom,
615 TopTools_DataMapOfShapeShape& theDangles)
618 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
619 TopAbs_ShapeEnum GeneratedTo;
620 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
621 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
622 else return Standard_False;
623 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
624 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
625 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
626 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
627 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
629 return theDangles.Extent();
632 //=======================================================================
633 void loadGeneratedDangleShapes(
634 const TopoDS_Shape& theShapeIn,
635 const TopAbs_ShapeEnum theGeneratedFrom,
636 TNaming_Builder * theBuilder)
638 TopTools_DataMapOfShapeShape dangles;
639 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
640 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
641 for (; itr.More(); itr.Next())
642 theBuilder->Generated(itr.Key(), itr.Value());
645 //=======================================================================
646 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
647 const std::string& theName, int& theTag)
649 if(theShape->isNull()) return;
650 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
652 if (aShape.ShapeType() == TopAbs_SOLID) {
653 TopExp_Explorer expl(aShape, TopAbs_FACE);
654 for (; expl.More(); expl.Next()) {
655 builder(theTag)->Generated(expl.Current());
656 TCollection_AsciiString aStr(theTag);
657 aName = theName + aStr.ToCString();
658 buildName(theTag, aName);
662 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
663 // load faces and all the free edges
664 TopTools_IndexedMapOfShape Faces;
665 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
666 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
667 TopExp_Explorer expl(aShape, TopAbs_FACE);
668 for (; expl.More(); expl.Next()) {
669 builder(theTag)->Generated(expl.Current());
670 TCollection_AsciiString aStr(theTag);
671 aName = theName + aStr.ToCString();
672 buildName(theTag, aName);
676 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
677 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
678 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
680 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
681 if (aLL.Extent() < 2) {
682 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
684 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
685 TCollection_AsciiString aStr(theTag);
686 aName = theName + aStr.ToCString();
687 buildName(theTag, aName);
690 TopTools_ListIteratorOfListOfShape anIter(aLL);
691 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
693 if(aFace.IsEqual(anIter.Value())) {
694 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
695 TCollection_AsciiString aStr(theTag);
696 aName = theName + aStr.ToCString();
697 buildName(theTag, aName);
702 } else if (aShape.ShapeType() == TopAbs_WIRE) {
703 TopTools_IndexedMapOfShape Edges;
704 BRepTools::Map3DEdges(aShape, Edges);
705 if (Edges.Extent() == 1) {
706 builder(theTag++)->Generated(Edges.FindKey(1));
707 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
708 for (; expl.More(); expl.Next()) {
709 builder(theTag)->Generated(expl.Current());
710 TCollection_AsciiString aStr(theTag);
711 aName = theName + aStr.ToCString();
712 buildName(theTag, aName);
716 TopExp_Explorer expl(aShape, TopAbs_EDGE);
717 for (; expl.More(); expl.Next()) {
718 builder(theTag)->Generated(expl.Current());
719 TCollection_AsciiString aStr(theTag);
720 aName = theName + aStr.ToCString();
721 buildName(theTag, aName);
724 // and load generated vertices.
725 TopTools_DataMapOfShapeShape generated;
726 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
728 TNaming_Builder* pBuilder = builder(theTag++);
729 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
732 } else if (aShape.ShapeType() == TopAbs_EDGE) {
733 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
734 for (; expl.More(); expl.Next()) {
735 builder(theTag)->Generated(expl.Current());
736 TCollection_AsciiString aStr(theTag);
737 aName = theName + aStr.ToCString();
738 buildName(theTag, aName);
744 //=======================================================================
745 int findAmbiguities(const TopoDS_Shape& theShapeIn,
746 TopTools_ListOfShape& theList)
749 // edges -> ancestor faces list
750 TopTools_IndexedDataMapOfShapeListOfShape aSubShapeAndAncestors;
751 TopExp::MapShapesAndAncestors(theShapeIn, TopAbs_EDGE, TopAbs_FACE, aSubShapeAndAncestors);
752 // keeps the shapes which are already in the resulting list
753 TopTools_MapOfShape alreadyThere;
754 // map from faces identifier (combination of hash-codes) to list of edges produced such ID
755 NCollection_DataMap<int, NCollection_List<TopoDS_Shape> > aFacesIDs;
757 TopTools_IndexedDataMapOfShapeListOfShape::Iterator anAncestorsIter(aSubShapeAndAncestors);
758 for (; anAncestorsIter.More(); anAncestorsIter.Next()) {
759 const TopTools_ListOfShape& ancestors = anAncestorsIter.Value();
760 if (ancestors.Extent() < 2)
762 Standard_Integer anID = 0;
763 for(TopTools_ListIteratorOfListOfShape aFaceIt(ancestors); aFaceIt.More(); aFaceIt.Next()) {
764 anID ^= HashCode(aFaceIt.ChangeValue(), 1990657); // Pierpont prime
766 if (aFacesIDs.IsBound(anID)) { // there found same edge, check they really have same faces
767 const NCollection_List<TopoDS_Shape>& aSameFaces1 =
768 aSubShapeAndAncestors.FindFromKey(anAncestorsIter.Key());
769 NCollection_List<TopoDS_Shape>::Iterator aSameEdge(aFacesIDs.ChangeFind(anID));
770 for(; aSameEdge.More(); aSameEdge.Next()) {
771 const NCollection_List<TopoDS_Shape>& aSameFaces2 =
772 aSubShapeAndAncestors.FindFromKey(aSameEdge.Value());
773 if (aSameFaces2.Extent() != aSameFaces1.Extent()) // the number of faces is different
776 NCollection_List<TopoDS_Shape>::Iterator aFaceIter1(aSameFaces1);
777 for(; aFaceIter1.More(); aFaceIter1.Next()) {
778 NCollection_List<TopoDS_Shape>::Iterator aFaceIter2(aSameFaces2);
779 for(; aFaceIter2.More(); aFaceIter2.Next()) {
780 if (aFaceIter1.Value().IsSame(aFaceIter2.Value()))
783 if (!aFaceIter2.More()) // aFaceIter1 contains a face, which is not in aFaceIter2
786 if (!aFaceIter1.More()) { // all the faces are same => put to the result
787 if (alreadyThere.Add(aSameEdge.Value()))
788 theList.Append(aSameEdge.Value());
789 if (alreadyThere.Add(anAncestorsIter.Key()))
790 theList.Append(anAncestorsIter.Key());
793 } else { // ID is unique, just add this edge
794 aFacesIDs.Bind(anID, NCollection_List<TopoDS_Shape>());
796 aFacesIDs.ChangeFind(anID).Append(anAncestorsIter.Key()); // add to the list anyway
798 return theList.Extent();
801 //=======================================================================
802 void Model_BodyBuilder::loadFirstLevel(
803 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
805 if(theShape->isNull()) return;
806 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
808 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
809 TopoDS_Iterator itr(aShape);
810 for (; itr.More(); itr.Next()) {
811 builder(theTag)->Generated(itr.Value());
812 TCollection_AsciiString aStr(theTag);
813 aName = theName + aStr.ToCString();
814 buildName(theTag, aName);
815 if(!theName.empty()) buildName(theTag, aName);
817 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
818 itr.Value().ShapeType() == TopAbs_COMPSOLID)
820 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
821 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
822 loadFirstLevel(itrShape, theName, theTag);
824 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
825 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
826 loadNextLevels(itrShape, theName, theTag);
830 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
831 itrShape->setImpl(new TopoDS_Shape(aShape));
832 loadNextLevels(itrShape, theName, theTag);
834 TopTools_ListOfShape aList;
835 if(findAmbiguities(aShape, aList)) {
836 TopTools_ListIteratorOfListOfShape it(aList);
837 for (; it.More(); it.Next(),theTag++) {
838 builder(theTag)->Generated(it.Value());
839 TCollection_AsciiString aStr(theTag);
840 aName = theName + aStr.ToCString();
841 buildName(theTag, aName);
846 //=======================================================================
847 void Model_BodyBuilder::loadDisconnectedEdges(
848 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
850 if(theShape->isNull()) return;
851 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
852 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
853 TopTools_ListOfShape empty;
854 TopExp_Explorer explF(aShape, TopAbs_FACE);
855 for (; explF.More(); explF.Next()) {
856 const TopoDS_Shape& aFace = explF.Current();
857 TopExp_Explorer explV(aFace, TopAbs_EDGE);
858 for (; explV.More(); explV.Next()) {
859 const TopoDS_Shape& anEdge = explV.Current();
860 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
861 Standard_Boolean faceIsNew = Standard_True;
862 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
863 for (; itrF.More(); itrF.Next()) {
864 if (itrF.Value().IsSame(aFace)) {
865 faceIsNew = Standard_False;
870 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
874 TopTools_MapOfShape anEdgesToDelete;
875 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
877 for(;anEx.More();anEx.Next()) {
878 Standard_Boolean aC0 = Standard_False;
879 TopoDS_Shape anEdge1 = anEx.Current();
880 if (edgeNaborFaces.IsBound(anEdge1)) {
881 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
882 if (aList1.Extent()<2) continue;
883 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
884 for (; itr.More(); itr.Next()) {
885 TopoDS_Shape anEdge2 = itr.Key();
886 if(anEdgesToDelete.Contains(anEdge2)) continue;
887 if (anEdge1.IsSame(anEdge2)) continue;
888 const TopTools_ListOfShape& aList2 = itr.Value();
889 // compare lists of the neighbour faces of edge1 and edge2
890 if (aList1.Extent() == aList2.Extent()) {
891 Standard_Integer aMatches = 0;
892 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
893 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
894 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
895 if (aMatches == aList1.Extent()) {
897 builder(theTag)->Generated(anEdge2);
898 anEdgesToDelete.Add(anEdge2);
899 TCollection_AsciiString aStr(theTag);
900 aName = theName + aStr.ToCString();
901 buildName(theTag, aName);
906 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
907 for(;itDelete.More();itDelete.Next())
908 edgeNaborFaces.UnBind(itDelete.Key());
909 edgeNaborFaces.UnBind(anEdge1);
912 builder(theTag)->Generated(anEdge1);
913 TCollection_AsciiString aStr(theTag);
914 aName = theName + aStr.ToCString();
915 buildName(theTag, aName);
921 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape,
922 const std::string& theName, int& theTag)
924 if(theShape->isNull()) return;
925 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
926 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
927 TopTools_ListOfShape empty;
928 TopExp_Explorer explF(aShape, TopAbs_EDGE);
929 for (; explF.More(); explF.Next()) {
930 const TopoDS_Shape& anEdge = explF.Current();
931 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
932 for (; explV.More(); explV.Next()) {
933 const TopoDS_Shape& aVertex = explV.Current();
934 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
935 Standard_Boolean faceIsNew = Standard_True;
936 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
937 for (; itrF.More(); itrF.Next()) {
938 if (itrF.Value().IsSame(anEdge)) {
939 faceIsNew = Standard_False;
944 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
949 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
950 for (; itr.More(); itr.Next()) {
951 const TopTools_ListOfShape& naborEdges = itr.Value();
952 if (naborEdges.Extent() < 2) {
953 builder(theTag)->Generated(itr.Key());
954 TCollection_AsciiString aStr(theTag);
955 aName = theName + aStr.ToCString();
956 buildName(theTag, aName);
962 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
964 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
966 TDF_Label aShapeLab = aData->shapeLab();
967 Handle(TDF_Reference) aRef;
968 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
969 aShapeLab = aRef->Get();
971 Handle(TNaming_NamedShape) aName;
972 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
973 TopoDS_Shape aShape = aName->Get();
974 if (!aShape.IsNull()) {
975 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
976 aRes->setImpl(new TopoDS_Shape(aShape));
981 return std::shared_ptr<GeomAPI_Shape>();
984 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
986 if (theShape.get()) {
987 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
988 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
990 TDF_Label& aShapeLab = aData->shapeLab();
991 Handle(TNaming_NamedShape) aName;
992 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
993 TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
994 if (aLatest.IsNull())
996 if (aLatest.IsEqual(aShape))
998 // check sub-shapes for comp-solids:
999 for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
1000 if (aLatest.IsEqual(anExp.Current()))