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 <BRepTools_History.hxx>
51 #include <GeomAPI_Shape.h>
52 #include <GeomAlgoAPI_MakeShape.h>
53 #include <GeomAlgoAPI_SortListOfShapes.h>
54 #include <Config_PropManager.h>
56 //#include <TCollection_AsciiString.hxx>
57 //#include <TDF_Tool.hxx>
58 //#define DEB_IMPORT 1
60 Model_BodyBuilder::Model_BodyBuilder(ModelAPI_Object* theOwner)
61 : ModelAPI_BodyBuilder(theOwner)
65 // Converts evolution of naming shape to selection evelution and back to avoid
66 // naming support on the disabled results. Deeply in the labels tree, recursively.
67 static void evolutionToSelectionRec(TDF_Label theLab, const bool theFlag) {
68 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
69 Handle(TNaming_NamedShape) aName;
71 if (theLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
72 TNaming_Evolution aNSEvol = aName->Evolution();
73 if ((aNSEvol == TNaming_SELECTED && theFlag) ||
74 (aNSEvol != TNaming_SELECTED && !theFlag)) { // nothing to do, it is already correct
77 anEvolution = (int)(aNSEvol);
79 Handle(TDataStd_Integer) anAttrEvol;
80 if (theLab.FindAttribute(TDataStd_Integer::GetID(), anAttrEvol)) {
81 anEvolution = anAttrEvol->Get();
84 TDataStd_Integer::Set(theLab, anEvolution);
87 for(TNaming_Iterator anIter(aName); anIter.More(); anIter.Next()) {
88 // iterator goes in reversed order relatively to the Builder, to, make the list reversed
89 aShapePairs.push_front(std::pair<TopoDS_Shape, TopoDS_Shape>
90 (anIter.OldShape(), anIter.NewShape()));
94 TNaming_Builder aBuilder(theLab);
95 TNaming_Evolution anEvol = (TNaming_Evolution)(anEvolution);
96 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter = aShapePairs.begin();
97 for(; aPairsIter != aShapePairs.end(); aPairsIter++) {
98 if (theFlag) { // disabled => make selection
99 if (anEvolution == TNaming_DELETE) // issue 2274 : don't put too many same null shapes
100 aBuilder.Select(aPairsIter->first, aPairsIter->first);
101 else if (anEvolution == TNaming_PRIMITIVE)
102 aBuilder.Select(aPairsIter->second, aPairsIter->second);
104 aBuilder.Select(aPairsIter->second, aPairsIter->first);
105 } else if (anEvol == TNaming_GENERATED) {
106 aBuilder.Generated(aPairsIter->first, aPairsIter->second);
107 } else if (anEvol == TNaming_MODIFY) {
108 aBuilder.Modify(aPairsIter->first, aPairsIter->second);
109 } else if (anEvol == TNaming_DELETE) {
110 aBuilder.Delete(aPairsIter->first);
111 } else if (anEvol == TNaming_PRIMITIVE) {
112 aBuilder.Generated(aPairsIter->second);
113 } else if (anEvol == TNaming_SELECTED) {
114 aBuilder.Select(aPairsIter->second, aPairsIter->first);
118 // recursive call for all sub-labels
119 TDF_ChildIterator anIter(theLab, Standard_False);
120 for(; anIter.More(); anIter.Next()) {
121 evolutionToSelectionRec(anIter.Value(), theFlag);
125 void Model_BodyBuilder::evolutionToSelection(const bool theFlag)
127 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
128 if (!aData) // unknown case
130 TDF_Label& aShapeLab = aData->shapeLab();
131 evolutionToSelectionRec(aShapeLab, theFlag);
134 void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape,
135 const bool theIsStoreSameShapes)
137 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
139 TDF_Label& aShapeLab = aData->shapeLab();
142 // store the new shape as primitive
143 TNaming_Builder aBuilder(aShapeLab);
146 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
148 return; // null shape inside
150 if(!theIsStoreSameShapes) {
151 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
152 if(!aNS.IsNull() && !aNS->IsEmpty()) {
153 // This shape is already in document, store reference instead of shape;
154 const TDF_Label aFoundLabel = aNS->Label();
155 TDF_Reference::Set(aShapeLab, aFoundLabel);
156 aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
161 aBuilder.Generated(aShape);
163 aShapeLab.ForgetAttribute(TDF_Reference::GetID());
164 if(!aBuilder.NamedShape()->IsEmpty()) {
165 Handle(TDataStd_Name) anAttr;
166 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
167 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
169 std::shared_ptr<Model_Document> aDoc =
170 std::dynamic_pointer_cast<Model_Document>(document());
171 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
178 void Model_BodyBuilder::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
179 const std::shared_ptr<GeomAPI_Shape>& theToShape)
181 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
183 TDF_Label& aShapeLab = aData->shapeLab();
186 // store the new shape as primitive
187 TNaming_Builder aBuilder(aShapeLab);
188 if (!theFromShape || !theToShape)
190 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
191 if (aShapeBasis.IsNull())
192 return; // null shape inside
193 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
194 if (aShapeNew.IsNull())
195 return; // null shape inside
196 aBuilder.Generated(aShapeBasis, aShapeNew);
198 if(!aBuilder.NamedShape()->IsEmpty()) {
199 Handle(TDataStd_Name) anAttr;
200 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
201 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
203 std::shared_ptr<Model_Document> aDoc =
204 std::dynamic_pointer_cast<Model_Document>(document());
205 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
212 void Model_BodyBuilder::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
213 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
215 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
217 TDF_Label& aShapeLab = aData->shapeLab();
219 if (theDecomposeSolidsTag != -2)
221 // store the new shape as primitive
222 TNaming_Builder aBuilder(aShapeLab);
223 if (!theOldShape || !theNewShape)
225 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
226 if (aShapeOld.IsNull())
227 return; // null shape inside
228 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
229 if (aShapeNew.IsNull())
230 return; // null shape inside
231 aBuilder.Modify(aShapeOld, aShapeNew);
232 if(!aBuilder.NamedShape()->IsEmpty()) {
233 Handle(TDataStd_Name) anAttr;
234 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
235 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
237 std::shared_ptr<Model_Document> aDoc =
238 std::dynamic_pointer_cast<Model_Document>(document());
239 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
246 void Model_BodyBuilder::storeWithoutNaming(const std::shared_ptr<GeomAPI_Shape>& theShape)
248 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
253 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
255 return; // null shape inside
256 TNaming_Builder aBuilder(aData->shapeLab());
257 aBuilder.Select(aShape, aShape);
261 void Model_BodyBuilder::clean()
263 TDF_Label aLab = std::dynamic_pointer_cast<Model_Data>(data())->shapeLab();
266 std::map<int, TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
267 for(; aBuilder != myBuilders.end(); aBuilder++) {
268 delete aBuilder->second;
269 // clear also shapes on cleaned sub-labels (#2241)
270 Handle(TNaming_NamedShape) aNS;
271 if (aLab.FindChild(aBuilder->first).FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
276 // remove the old reference (if any)
277 aLab.ForgetAttribute(TDF_Reference::GetID());
280 Model_BodyBuilder::~Model_BodyBuilder()
285 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
287 std::map<int, TNaming_Builder*>::iterator aFind = myBuilders.find(theTag);
288 if (aFind == myBuilders.end()) {
289 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
290 myBuilders[theTag] = new TNaming_Builder(
291 theTag == 0 ? aData->shapeLab() : aData->shapeLab().FindChild(theTag));
292 aFind = myBuilders.find(theTag);
294 return aFind->second;
297 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
299 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
300 //aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), theName);
301 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(), theName.c_str());
303 void Model_BodyBuilder::generated(
304 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
306 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
307 builder(theTag)->Generated(aShape);
309 buildName(theTag, theName);
312 void Model_BodyBuilder::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
313 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
315 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
316 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
317 builder(theTag)->Generated(anOldShape, aNewShape);
319 buildName(theTag, theName);
320 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
321 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
322 TopAbs_ShapeEnum anExplodeShapeType = aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
323 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
325 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
326 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
327 TDF_Label aChildLabel = aLabel.FindChild(aTag);
328 TNaming_Builder aBuilder(aChildLabel);
329 aBuilder.Generated(anOldShape, anExp.Current());
330 TCollection_AsciiString aChildName = TCollection_AsciiString((theName + "_").c_str()) + aTag;
331 //aDoc->addNamingName(aChildLabel, aChildName.ToCString());
332 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
339 void Model_BodyBuilder::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
340 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
342 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
343 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
344 builder(theTag)->Modify(anOldShape, aNewShape);
346 buildName(theTag, theName);
349 void Model_BodyBuilder::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
352 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
353 builder(theTag)->Delete(aShape);
356 void Model_BodyBuilder::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
357 std::shared_ptr<GeomAPI_Shape> theShapeIn,
358 const int theKindOfShape,
361 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
362 TopTools_MapOfShape aView;
363 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
364 GeomShapePtr aResultShape = shape();
365 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
366 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
367 if (!aView.Add(aRoot)) continue;
368 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
369 aRShape->setImpl((new TopoDS_Shape(aRoot)));
370 if (theMS->isDeleted (aRShape)) {
371 if (!aResultShape->isSubShape(aRShape, false)) {
373 if (BRepTools_History::IsSupportedType(aRoot)) // to avoid crash in #2572
374 theMS->modified(aRShape, aHist);
375 if (aHist.size() == 0 || (aHist.size() == 1 && aHist.front()->isSame(aRShape)))
376 builder(theTag)->Delete(aRoot);
382 // Keep only the shapes with minimal shape type
383 static void keepTopLevelShapes(ListOfShape& theShapes, const TopoDS_Shape& theRoot,
384 const GeomShapePtr& theResultShape = GeomShapePtr())
386 GeomAPI_Shape::ShapeType aKeepShapeType = GeomAPI_Shape::SHAPE;
387 ListOfShape::iterator anIt = theShapes.begin();
388 while (anIt != theShapes.end()) {
389 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
390 bool aSkip = aNewShape.IsNull() ||
391 (aNewShape.ShapeType() == TopAbs_EDGE && BRep_Tool::Degenerated(TopoDS::Edge(aNewShape)));
392 if (aSkip || theRoot.IsSame(aNewShape) || (theResultShape &&
393 (!theResultShape->isSubShape(*anIt, false) || theResultShape->isSame(*anIt)))) {
394 ListOfShape::iterator aRemoveIt = anIt++;
395 theShapes.erase(aRemoveIt);
397 GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
398 if (aType < aKeepShapeType) {
399 // found a shape with lesser shape type => remove all previous shapes
400 aKeepShapeType = aType;
401 theShapes.erase(theShapes.begin(), anIt);
403 } else if (aType > aKeepShapeType) {
404 // shapes with greater shape type should be removed from the list
405 ListOfShape::iterator aRemoveIt = anIt++;
406 theShapes.erase(aRemoveIt);
413 // returns an ancestor shape-type thaty used for naming-definition of the sub-type
414 TopAbs_ShapeEnum typeOfAncestor(const TopAbs_ShapeEnum theSubType) {
415 if (theSubType == TopAbs_VERTEX)
417 if (theSubType == TopAbs_EDGE)
419 return TopAbs_VERTEX; // bad case
422 void Model_BodyBuilder::loadAndOrientModifiedShapes (
423 GeomAlgoAPI_MakeShape* theMS,
424 std::shared_ptr<GeomAPI_Shape> theShapeIn,
425 const int theKindOfShape,
427 const std::string& theName,
428 GeomAPI_DataMapOfShapeShape& theSubShapes,
429 const bool theIsStoreSeparate,
430 const bool theIsStoreAsGenerated)
434 bool isBuilt = !theName.empty();
435 std::string aName = theName;
436 std::ostringstream aStream;
437 GeomShapePtr aResultShape = shape();
438 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
439 TopTools_MapOfShape aView;
440 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
441 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
442 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
443 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
444 if (!aView.Add(aRoot)) continue;
447 TNaming_Tool::NamedShape(aRoot, aData->shapeLab()).IsNull();
448 if (aNotInTree && !theIsStoreSeparate) {
449 // there is no sense to write history if old shape does not exist in the document
450 continue; // but if it is stored separately, it will be builded as a primitive
453 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
454 aRShape->setImpl((new TopoDS_Shape(aRoot)));
455 theMS->modified(aRShape, aList);
456 if (!theIsStoreSeparate)
457 keepTopLevelShapes(aList, aRoot, aResultShape);
458 // sort the list of images before naming
459 GeomAlgoAPI_SortListOfShapes::sort(aList);
461 // to trace situation where several objects are produced by one parent (#2317)
462 int aSameParentShapes = -1;
463 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
464 anIt = aList.begin(), aLast = aList.end();
465 for (; anIt != aLast; anIt++) {
466 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
467 if (theSubShapes.isBound(*anIt)) {
468 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
469 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
471 GeomShapePtr aGeomNewShape(new GeomAPI_Shape());
472 aGeomNewShape->setImpl(new TopoDS_Shape(aNewShape));
473 if(!aRoot.IsSame(aNewShape) && aResultShape->isSubShape(aGeomNewShape, false) &&
474 !aResultShape->isSame(*anIt)) { // to avoid put of same shape on main label and sub
475 int aBuilderTag = aTag;
476 if (!theIsStoreSeparate) {
478 } else if (aNotInTree) { // check this new shape can not be represented as
479 // a sub-shape of higher level sub-shapes
480 TopAbs_ShapeEnum aNewType = aNewShape.ShapeType();
481 TopAbs_ShapeEnum anAncestorType = typeOfAncestor(aNewType);
482 if (anAncestorType != TopAbs_VERTEX) {
484 TopoDS_Shape aResultTShape = aResultShape->impl<TopoDS_Shape>();
485 TopExp_Explorer anAncestorExp(aResultTShape, anAncestorType);
486 for(; anAncestorExp.More() && !aFound; anAncestorExp.Next()) {
487 if (aResultTShape.IsSame(anAncestorExp.Current()))
489 TopExp_Explorer aSubExp(anAncestorExp.Current(), aNewType);
490 for(; aSubExp.More(); aSubExp.Next()) {
491 if (aNewShape.IsSame(aSubExp.Current())) {
498 continue; // not need to store this shape in the BRep structure
503 static const int THE_ANCHOR_TAG = 100000;
504 int aCurShapeType = (int)((*anIt)->shapeType());
505 bool needSuffix = false; // suffix for the name based on the shape type
506 if (aSameParentShapes > 0) { // store in other label
507 aBuilderTag = THE_ANCHOR_TAG - aSameParentShapes * 10 - aCurShapeType;
509 } else if (aCurShapeType != theKindOfShape) {
510 // modified shape has different type => set another tag
511 // to avoid shapes of different types on the same label
512 aBuilderTag = THE_ANCHOR_TAG - aCurShapeType;
517 switch (aCurShapeType) {
518 case GeomAPI_Shape::VERTEX: aSuffix = "_v"; break;
519 case GeomAPI_Shape::EDGE: aSuffix = "_e"; break;
520 case GeomAPI_Shape::FACE: aSuffix = "_f"; break;
525 if(theIsStoreAsGenerated) {
526 // Here we store shapes as generated, to avoid problem when one parent shape produce
527 // several child shapes. In this case naming could not determine which shape to select.
528 builder(aBuilderTag)->Generated(aRoot, aNewShape);
529 } else if (aNotInTree) {
530 // not in tree -> store as primitive (stored as separated)
531 builder(aBuilderTag)->Generated(aNewShape);
532 } else if (aNewShape.ShapeType() > aRoot.ShapeType()) {
533 // if lower-level type is produced, make it as generated
534 builder(aBuilderTag)->Generated(aRoot, aNewShape);
536 builder(aBuilderTag)->Modify(aRoot, aNewShape);
539 aStream.str(std::string());
542 if(theIsStoreSeparate)
543 aStream << "_" << anIndex++;
545 if (aSameParentShapes > 0) {
546 aStream.str(std::string());
548 aStream << aName << "_" << aSameParentShapes << "divided";
552 buildName(aBuilderTag, aStream.str());
554 if(theIsStoreSeparate) {
557 } else if (aResultShape->isSame(*anIt)) {
558 // keep the modification evolution on the root level (2241 - history propagation issue)
559 if(theIsStoreAsGenerated) {
560 builder(0)->Generated(aRoot, aNewShape);
562 builder(0)->Modify(aRoot, aNewShape);
569 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
570 GeomAlgoAPI_MakeShape* theMS,
571 std::shared_ptr<GeomAPI_Shape> theShapeIn,
572 const int theKindOfShape,
574 const std::string& theName,
575 GeomAPI_DataMapOfShapeShape& theSubShapes)
577 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
578 TopTools_MapOfShape aView;
579 bool isBuilt = !theName.empty();
580 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
581 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
582 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
583 if (!aView.Add(aRoot)) continue;
584 //if (TNaming_Tool::NamedShape(aRoot, builder(theTag)->NamedShape()->Label()).IsNull())
585 // continue; // there is no sense to write history if old shape does not exist in the document
587 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
588 aRShape->setImpl((new TopoDS_Shape(aRoot)));
589 theMS->generated(aRShape, aList);
590 keepTopLevelShapes(aList, aRoot);
591 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
592 anIt = aList.begin(), aLast = aList.end();
593 for (; anIt != aLast; anIt++) {
594 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
595 if (theSubShapes.isBound(*anIt)) {
596 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
597 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
599 if (!aRoot.IsSame (aNewShape)) {
600 builder(theTag)->Generated(aRoot,aNewShape);
602 buildName(theTag, theName);
604 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
605 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
606 TopAbs_ShapeEnum anExplodeShapeType =
607 aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
608 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
610 std::shared_ptr<Model_Document> aDoc =
611 std::dynamic_pointer_cast<Model_Document>(document());
612 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
613 TDF_Label aChildLabel = aLabel.FindChild(aTag);
614 TNaming_Builder aBuilder(aChildLabel);
615 aBuilder.Generated(aRoot, anExp.Current());
616 TCollection_AsciiString aChildName =
617 TCollection_AsciiString((theName + "_").c_str()) + aTag;
618 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
626 //=======================================================================
627 int getDangleShapes(const TopoDS_Shape& theShapeIn,
628 const TopAbs_ShapeEnum theGeneratedFrom,
629 TopTools_DataMapOfShapeShape& theDangles)
632 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
633 TopAbs_ShapeEnum GeneratedTo;
634 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
635 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
636 else return Standard_False;
637 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
638 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
639 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
640 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
641 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
643 return theDangles.Extent();
646 //=======================================================================
647 void loadGeneratedDangleShapes(
648 const TopoDS_Shape& theShapeIn,
649 const TopAbs_ShapeEnum theGeneratedFrom,
650 TNaming_Builder * theBuilder)
652 TopTools_DataMapOfShapeShape dangles;
653 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
654 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
655 for (; itr.More(); itr.Next())
656 theBuilder->Generated(itr.Key(), itr.Value());
659 //=======================================================================
660 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
661 const std::string& theName, int& theTag)
663 if(theShape->isNull()) return;
664 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
666 if (aShape.ShapeType() == TopAbs_SOLID) {
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 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
677 // load faces and all the free edges
678 TopTools_IndexedMapOfShape Faces;
679 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
680 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
681 TopExp_Explorer expl(aShape, TopAbs_FACE);
682 for (; expl.More(); expl.Next()) {
683 builder(theTag)->Generated(expl.Current());
684 TCollection_AsciiString aStr(theTag);
685 aName = theName + aStr.ToCString();
686 buildName(theTag, aName);
690 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
691 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
692 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
694 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
695 if (aLL.Extent() < 2) {
696 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
698 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
699 TCollection_AsciiString aStr(theTag);
700 aName = theName + aStr.ToCString();
701 buildName(theTag, aName);
704 TopTools_ListIteratorOfListOfShape anIter(aLL);
705 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
707 if(aFace.IsEqual(anIter.Value())) {
708 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
709 TCollection_AsciiString aStr(theTag);
710 aName = theName + aStr.ToCString();
711 buildName(theTag, aName);
716 } else if (aShape.ShapeType() == TopAbs_WIRE) {
717 TopTools_IndexedMapOfShape Edges;
718 BRepTools::Map3DEdges(aShape, Edges);
719 if (Edges.Extent() == 1) {
720 builder(theTag++)->Generated(Edges.FindKey(1));
721 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
722 for (; expl.More(); expl.Next()) {
723 builder(theTag)->Generated(expl.Current());
724 TCollection_AsciiString aStr(theTag);
725 aName = theName + aStr.ToCString();
726 buildName(theTag, aName);
730 TopExp_Explorer expl(aShape, TopAbs_EDGE);
731 for (; expl.More(); expl.Next()) {
732 builder(theTag)->Generated(expl.Current());
733 TCollection_AsciiString aStr(theTag);
734 aName = theName + aStr.ToCString();
735 buildName(theTag, aName);
738 // and load generated vertices.
739 TopTools_DataMapOfShapeShape generated;
740 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
742 TNaming_Builder* pBuilder = builder(theTag++);
743 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
746 } else if (aShape.ShapeType() == TopAbs_EDGE) {
747 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
748 for (; expl.More(); expl.Next()) {
749 builder(theTag)->Generated(expl.Current());
750 TCollection_AsciiString aStr(theTag);
751 aName = theName + aStr.ToCString();
752 buildName(theTag, aName);
758 //=======================================================================
759 int findAmbiguities(const TopoDS_Shape& theShapeIn,
760 TopTools_ListOfShape& theList)
763 // edges -> ancestor faces list
764 TopTools_IndexedDataMapOfShapeListOfShape aSubShapeAndAncestors;
765 TopExp::MapShapesAndAncestors(theShapeIn, TopAbs_EDGE, TopAbs_FACE, aSubShapeAndAncestors);
766 // keeps the shapes which are already in the resulting list
767 TopTools_MapOfShape alreadyThere;
768 // map from faces identifier (combination of hash-codes) to list of edges produced such ID
769 NCollection_DataMap<int, NCollection_List<TopoDS_Shape> > aFacesIDs;
771 TopTools_IndexedDataMapOfShapeListOfShape::Iterator anAncestorsIter(aSubShapeAndAncestors);
772 for (; anAncestorsIter.More(); anAncestorsIter.Next()) {
773 const TopTools_ListOfShape& ancestors = anAncestorsIter.Value();
774 if (ancestors.Extent() < 2)
776 Standard_Integer anID = 0;
777 for(TopTools_ListIteratorOfListOfShape aFaceIt(ancestors); aFaceIt.More(); aFaceIt.Next()) {
778 anID ^= HashCode(aFaceIt.ChangeValue(), 1990657); // Pierpont prime
780 if (aFacesIDs.IsBound(anID)) { // there found same edge, check they really have same faces
781 const NCollection_List<TopoDS_Shape>& aSameFaces1 =
782 aSubShapeAndAncestors.FindFromKey(anAncestorsIter.Key());
783 NCollection_List<TopoDS_Shape>::Iterator aSameEdge(aFacesIDs.ChangeFind(anID));
784 for(; aSameEdge.More(); aSameEdge.Next()) {
785 const NCollection_List<TopoDS_Shape>& aSameFaces2 =
786 aSubShapeAndAncestors.FindFromKey(aSameEdge.Value());
787 if (aSameFaces2.Extent() != aSameFaces1.Extent()) // the number of faces is different
790 NCollection_List<TopoDS_Shape>::Iterator aFaceIter1(aSameFaces1);
791 for(; aFaceIter1.More(); aFaceIter1.Next()) {
792 NCollection_List<TopoDS_Shape>::Iterator aFaceIter2(aSameFaces2);
793 for(; aFaceIter2.More(); aFaceIter2.Next()) {
794 if (aFaceIter1.Value().IsSame(aFaceIter2.Value()))
797 if (!aFaceIter2.More()) // aFaceIter1 contains a face, which is not in aFaceIter2
800 if (!aFaceIter1.More()) { // all the faces are same => put to the result
801 if (alreadyThere.Add(aSameEdge.Value()))
802 theList.Append(aSameEdge.Value());
803 if (alreadyThere.Add(anAncestorsIter.Key()))
804 theList.Append(anAncestorsIter.Key());
807 } else { // ID is unique, just add this edge
808 aFacesIDs.Bind(anID, NCollection_List<TopoDS_Shape>());
810 aFacesIDs.ChangeFind(anID).Append(anAncestorsIter.Key()); // add to the list anyway
812 return theList.Extent();
815 //=======================================================================
816 void Model_BodyBuilder::loadFirstLevel(
817 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
819 if(theShape->isNull()) return;
820 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
822 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
823 TopoDS_Iterator itr(aShape);
824 for (; itr.More(); itr.Next()) {
825 builder(theTag)->Generated(itr.Value());
826 TCollection_AsciiString aStr(theTag);
827 aName = theName + aStr.ToCString();
828 buildName(theTag, aName);
829 if(!theName.empty()) buildName(theTag, aName);
831 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
832 itr.Value().ShapeType() == TopAbs_COMPSOLID)
834 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
835 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
836 loadFirstLevel(itrShape, theName, theTag);
838 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
839 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
840 loadNextLevels(itrShape, theName, theTag);
844 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
845 itrShape->setImpl(new TopoDS_Shape(aShape));
846 loadNextLevels(itrShape, theName, theTag);
848 TopTools_ListOfShape aList;
849 if(findAmbiguities(aShape, aList)) {
850 TopTools_ListIteratorOfListOfShape it(aList);
851 for (; it.More(); it.Next(),theTag++) {
852 builder(theTag)->Generated(it.Value());
853 TCollection_AsciiString aStr(theTag);
854 aName = theName + aStr.ToCString();
855 buildName(theTag, aName);
860 //=======================================================================
861 void Model_BodyBuilder::loadDisconnectedEdges(
862 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
864 if(theShape->isNull()) return;
865 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
866 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
867 TopTools_ListOfShape empty;
868 TopExp_Explorer explF(aShape, TopAbs_FACE);
869 for (; explF.More(); explF.Next()) {
870 const TopoDS_Shape& aFace = explF.Current();
871 TopExp_Explorer explV(aFace, TopAbs_EDGE);
872 for (; explV.More(); explV.Next()) {
873 const TopoDS_Shape& anEdge = explV.Current();
874 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
875 Standard_Boolean faceIsNew = Standard_True;
876 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
877 for (; itrF.More(); itrF.Next()) {
878 if (itrF.Value().IsSame(aFace)) {
879 faceIsNew = Standard_False;
884 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
888 TopTools_MapOfShape anEdgesToDelete;
889 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
891 for(;anEx.More();anEx.Next()) {
892 Standard_Boolean aC0 = Standard_False;
893 TopoDS_Shape anEdge1 = anEx.Current();
894 if (edgeNaborFaces.IsBound(anEdge1)) {
895 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
896 if (aList1.Extent()<2) continue;
897 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
898 for (; itr.More(); itr.Next()) {
899 TopoDS_Shape anEdge2 = itr.Key();
900 if(anEdgesToDelete.Contains(anEdge2)) continue;
901 if (anEdge1.IsSame(anEdge2)) continue;
902 const TopTools_ListOfShape& aList2 = itr.Value();
903 // compare lists of the neighbour faces of edge1 and edge2
904 if (aList1.Extent() == aList2.Extent()) {
905 Standard_Integer aMatches = 0;
906 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
907 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
908 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
909 if (aMatches == aList1.Extent()) {
911 builder(theTag)->Generated(anEdge2);
912 anEdgesToDelete.Add(anEdge2);
913 TCollection_AsciiString aStr(theTag);
914 aName = theName + aStr.ToCString();
915 buildName(theTag, aName);
920 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
921 for(;itDelete.More();itDelete.Next())
922 edgeNaborFaces.UnBind(itDelete.Key());
923 edgeNaborFaces.UnBind(anEdge1);
926 builder(theTag)->Generated(anEdge1);
927 TCollection_AsciiString aStr(theTag);
928 aName = theName + aStr.ToCString();
929 buildName(theTag, aName);
935 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape,
936 const std::string& theName, int& theTag)
938 if(theShape->isNull()) return;
939 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
940 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
941 TopTools_ListOfShape empty;
942 TopExp_Explorer explF(aShape, TopAbs_EDGE);
943 for (; explF.More(); explF.Next()) {
944 const TopoDS_Shape& anEdge = explF.Current();
945 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
946 for (; explV.More(); explV.Next()) {
947 const TopoDS_Shape& aVertex = explV.Current();
948 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
949 Standard_Boolean faceIsNew = Standard_True;
950 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
951 for (; itrF.More(); itrF.Next()) {
952 if (itrF.Value().IsSame(anEdge)) {
953 faceIsNew = Standard_False;
958 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
963 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
964 for (; itr.More(); itr.Next()) {
965 const TopTools_ListOfShape& naborEdges = itr.Value();
966 if (naborEdges.Extent() < 2) {
967 builder(theTag)->Generated(itr.Key());
968 TCollection_AsciiString aStr(theTag);
969 aName = theName + aStr.ToCString();
970 buildName(theTag, aName);
976 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
978 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
980 TDF_Label aShapeLab = aData->shapeLab();
981 Handle(TDF_Reference) aRef;
982 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
983 aShapeLab = aRef->Get();
985 Handle(TNaming_NamedShape) aName;
986 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
987 TopoDS_Shape aShape = aName->Get();
988 if (!aShape.IsNull()) {
989 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
990 aRes->setImpl(new TopoDS_Shape(aShape));
995 return std::shared_ptr<GeomAPI_Shape>();
998 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
1000 if (theShape.get()) {
1001 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
1002 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
1004 TDF_Label& aShapeLab = aData->shapeLab();
1005 Handle(TNaming_NamedShape) aName;
1006 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
1007 TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
1008 if (aLatest.IsNull())
1010 if (aLatest.IsEqual(aShape))
1012 // check sub-shapes for comp-solids:
1013 for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
1014 if (aLatest.IsEqual(anExp.Current()))