1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_ResultBody.cpp
4 // Created: 08 Jul 2014
5 // Author: Mikhail PONIKAROV
7 #include <Model_BodyBuilder.h>
9 #include <Model_Data.h>
10 #include <Model_Document.h>
11 #include <TNaming_Builder.hxx>
12 #include <TNaming_NamedShape.hxx>
13 #include <TNaming_Iterator.hxx>
14 #include <TNaming_Tool.hxx>
15 #include <TDataStd_Name.hxx>
16 #include <TDataStd_Integer.hxx>
18 #include <TopoDS_Face.hxx>
19 #include <TDF_ChildIterator.hxx>
20 #include <TDF_Reference.hxx>
21 #include <TopTools_MapOfShape.hxx>
22 #include <TopExp_Explorer.hxx>
23 #include <TopTools_ListOfShape.hxx>
24 #include <TopTools_ListIteratorOfListOfShape.hxx>
25 #include <TopTools_DataMapOfShapeListOfShape.hxx>
26 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
27 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
28 #include <TopTools_MapIteratorOfMapOfShape.hxx>
29 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
30 #include <TopTools_IndexedMapOfShape.hxx>
31 #include <TopTools_DataMapOfShapeShape.hxx>
33 #include <BRepTools.hxx>
34 #include <BRep_Tool.hxx>
35 #include <GeomAPI_Shape.h>
36 #include <GeomAlgoAPI_MakeShape.h>
37 #include <Config_PropManager.h>
39 //#include <TCollection_AsciiString.hxx>
40 //#include <TDF_Tool.hxx>
41 //#define DEB_IMPORT 1
43 Model_BodyBuilder::Model_BodyBuilder(ModelAPI_Object* theOwner)
44 : ModelAPI_BodyBuilder(theOwner)
48 // Converts evolution of naming shape to selection evelution and back to avoid
49 // naming support on the disabled results. Deeply in the labels tree, recursively.
50 static void evolutionToSelectionRec(TDF_Label theLab, const bool theFlag) {
51 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
52 Handle(TNaming_NamedShape) aName;
54 if (theLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
55 TNaming_Evolution aNSEvol = aName->Evolution();
56 if ((aNSEvol == TNaming_SELECTED && theFlag) ||
57 (aNSEvol != TNaming_SELECTED && !theFlag)) { // nothing to do, it is already correct
60 anEvolution = (int)(aNSEvol);
62 Handle(TDataStd_Integer) anAttrEvol;
63 if (theLab.FindAttribute(TDataStd_Integer::GetID(), anAttrEvol)) {
64 anEvolution = anAttrEvol->Get();
67 TDataStd_Integer::Set(theLab, anEvolution);
70 for(TNaming_Iterator anIter(aName); anIter.More(); anIter.Next()) {
71 // iterator goes in reversed order relatively to the Builder, to, make the list reversed
72 aShapePairs.push_front(std::pair<TopoDS_Shape, TopoDS_Shape>
73 (anIter.OldShape(), anIter.NewShape()));
77 TNaming_Builder aBuilder(theLab);
78 TNaming_Evolution anEvol = (TNaming_Evolution)(anEvolution);
79 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter = aShapePairs.begin();
80 for(; aPairsIter != aShapePairs.end(); aPairsIter++) {
81 if (theFlag) { // disabled => make selection
82 aBuilder.Select(aPairsIter->second, aPairsIter->first);
83 } else if (anEvol == TNaming_GENERATED) {
84 aBuilder.Generated(aPairsIter->first, aPairsIter->second);
85 } else if (anEvol == TNaming_MODIFY) {
86 aBuilder.Modify(aPairsIter->first, aPairsIter->second);
87 } else if (anEvol == TNaming_DELETE) {
88 aBuilder.Delete(aPairsIter->first);
89 } else if (anEvol == TNaming_PRIMITIVE) {
90 aBuilder.Generated(aPairsIter->second);
91 } else if (anEvol == TNaming_SELECTED) {
92 aBuilder.Select(aPairsIter->second, aPairsIter->first);
96 // recursive call for all sub-labels
97 TDF_ChildIterator anIter(theLab, Standard_False);
98 for(; anIter.More(); anIter.Next()) {
99 evolutionToSelectionRec(anIter.Value(), theFlag);
103 void Model_BodyBuilder::evolutionToSelection(const bool theFlag)
105 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
106 if (!aData) // unknown case
108 TDF_Label& aShapeLab = aData->shapeLab();
109 evolutionToSelectionRec(aShapeLab, theFlag);
112 void Model_BodyBuilder::store(const std::shared_ptr<GeomAPI_Shape>& theShape,
113 const bool theIsStoreSameShapes)
115 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
117 TDF_Label& aShapeLab = aData->shapeLab();
120 // store the new shape as primitive
121 TNaming_Builder aBuilder(aShapeLab);
124 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
126 return; // null shape inside
128 if(!theIsStoreSameShapes) {
129 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, aShapeLab);
130 if(!aNS.IsNull() && !aNS->IsEmpty()) {
131 // This shape is already in document, store reference instead of shape;
132 const TDF_Label aFoundLabel = aNS->Label();
133 TDF_Reference::Set(aShapeLab, aFoundLabel);
134 aShapeLab.ForgetAttribute(TNaming_NamedShape::GetID());
139 aBuilder.Generated(aShape);
141 aShapeLab.ForgetAttribute(TDF_Reference::GetID());
142 if(!aBuilder.NamedShape()->IsEmpty()) {
143 Handle(TDataStd_Name) anAttr;
144 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
145 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
147 std::shared_ptr<Model_Document> aDoc =
148 std::dynamic_pointer_cast<Model_Document>(document());
149 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
156 void Model_BodyBuilder::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
157 const std::shared_ptr<GeomAPI_Shape>& theToShape)
159 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
161 TDF_Label& aShapeLab = aData->shapeLab();
164 // store the new shape as primitive
165 TNaming_Builder aBuilder(aShapeLab);
166 if (!theFromShape || !theToShape)
168 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
169 if (aShapeBasis.IsNull())
170 return; // null shape inside
171 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
172 if (aShapeNew.IsNull())
173 return; // null shape inside
174 aBuilder.Generated(aShapeBasis, aShapeNew);
176 if(!aBuilder.NamedShape()->IsEmpty()) {
177 Handle(TDataStd_Name) anAttr;
178 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
179 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
181 std::shared_ptr<Model_Document> aDoc =
182 std::dynamic_pointer_cast<Model_Document>(document());
183 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
190 void Model_BodyBuilder::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
191 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
193 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
195 TDF_Label& aShapeLab = aData->shapeLab();
198 // store the new shape as primitive
199 TNaming_Builder aBuilder(aShapeLab);
200 if (!theOldShape || !theNewShape)
202 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
203 if (aShapeOld.IsNull())
204 return; // null shape inside
205 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
206 if (aShapeNew.IsNull())
207 return; // null shape inside
208 aBuilder.Modify(aShapeOld, aShapeNew);
209 if(!aBuilder.NamedShape()->IsEmpty()) {
210 Handle(TDataStd_Name) anAttr;
211 if(aBuilder.NamedShape()->Label().FindAttribute(TDataStd_Name::GetID(),anAttr)) {
212 std::string aName (TCollection_AsciiString(anAttr->Get()).ToCString());
214 std::shared_ptr<Model_Document> aDoc =
215 std::dynamic_pointer_cast<Model_Document>(document());
216 aDoc->addNamingName(aBuilder.NamedShape()->Label(), aName);
223 void Model_BodyBuilder::storeWithoutNaming(const std::shared_ptr<GeomAPI_Shape>& theShape)
225 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
230 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
232 return; // null shape inside
233 TNaming_Builder aBuilder(aData->shapeLab());
234 aBuilder.Select(aShape, aShape);
238 void Model_BodyBuilder::clean()
240 std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
241 for(; aBuilder != myBuilders.end(); aBuilder++)
246 Model_BodyBuilder::~Model_BodyBuilder()
251 TNaming_Builder* Model_BodyBuilder::builder(const int theTag)
253 if (myBuilders.size() <= (unsigned int)theTag) {
254 myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
256 if (!myBuilders[theTag]) {
257 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
258 myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
259 //TCollection_AsciiString entry;//
260 //TDF_Tool::Entry(aData->shapeLab().FindChild(theTag), entry);
261 //cout << "Label = " <<entry.ToCString() <<endl;
263 return myBuilders[theTag];
266 void Model_BodyBuilder::buildName(const int theTag, const std::string& theName)
268 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
269 //aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), theName);
270 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(), theName.c_str());
272 void Model_BodyBuilder::generated(
273 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
275 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
276 builder(theTag)->Generated(aShape);
278 buildName(theTag, theName);
281 void Model_BodyBuilder::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
282 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
284 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
285 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
286 builder(theTag)->Generated(anOldShape, aNewShape);
288 buildName(theTag, theName);
289 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
290 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
291 TopAbs_ShapeEnum anExplodeShapeType = aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
292 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
294 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
295 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
296 TDF_Label aChildLabel = aLabel.FindChild(aTag);
297 TNaming_Builder aBuilder(aChildLabel);
298 aBuilder.Generated(anOldShape, anExp.Current());
299 TCollection_AsciiString aChildName = TCollection_AsciiString((theName + "_").c_str()) + aTag;
300 //aDoc->addNamingName(aChildLabel, aChildName.ToCString());
301 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
308 void Model_BodyBuilder::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
309 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
311 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
312 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
313 builder(theTag)->Modify(anOldShape, aNewShape);
315 buildName(theTag, theName);
318 void Model_BodyBuilder::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
321 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
322 builder(theTag)->Delete(aShape);
325 void Model_BodyBuilder::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
326 std::shared_ptr<GeomAPI_Shape> theShapeIn,
327 const int theKindOfShape,
330 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
331 TopTools_MapOfShape aView;
332 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
333 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
334 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
335 if (!aView.Add(aRoot)) continue;
336 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
337 aRShape->setImpl((new TopoDS_Shape(aRoot)));
338 if (theMS->isDeleted (aRShape)) {
339 builder(theTag)->Delete(aRoot);
344 void Model_BodyBuilder::loadAndOrientModifiedShapes (
345 GeomAlgoAPI_MakeShape* theMS,
346 std::shared_ptr<GeomAPI_Shape> theShapeIn,
347 const int theKindOfShape,
349 const std::string& theName,
350 GeomAPI_DataMapOfShapeShape& theSubShapes,
351 const bool theIsStoreSeparate,
352 const bool theIsStoreAsGenerated)
356 bool isBuilt = !theName.empty();
357 std::string aName = theName;
358 std::ostringstream aStream;
359 GeomShapePtr aResultShape = shape();
360 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
361 TopTools_MapOfShape aView;
362 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
363 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
364 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
365 if (!aView.Add(aRoot)) continue;
367 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
368 aRShape->setImpl((new TopoDS_Shape(aRoot)));
369 theMS->modified(aRShape, aList);
370 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
371 anIt = aList.begin(), aLast = aList.end();
372 for (; anIt != aLast; anIt++) {
373 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
374 if (theSubShapes.isBound(*anIt)) {
375 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
376 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
378 GeomShapePtr aGeomNewShape(new GeomAPI_Shape());
379 aGeomNewShape->setImpl(new TopoDS_Shape(aNewShape));
380 if(!aRoot.IsSame(aNewShape) && aResultShape->isSubShape(aGeomNewShape)) {
381 if(theIsStoreAsGenerated) {
382 // Here we store shapes as generated, to avoid problem when one parent shape produce
383 // several child shapes. In this case naming could not determine which shape to select.
384 builder(aTag)->Generated(aRoot,aNewShape);
386 builder(aTag)->Modify(aRoot,aNewShape);
389 if(theIsStoreSeparate) {
390 aStream.str(std::string());
392 aStream << theName << "_" << anIndex++;
393 aName = aStream.str();
395 buildName(aTag, aName);
397 if(theIsStoreSeparate) {
405 void Model_BodyBuilder::loadAndOrientGeneratedShapes (
406 GeomAlgoAPI_MakeShape* theMS,
407 std::shared_ptr<GeomAPI_Shape> theShapeIn,
408 const int theKindOfShape,
410 const std::string& theName,
411 GeomAPI_DataMapOfShapeShape& theSubShapes)
413 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
414 TopTools_MapOfShape aView;
415 bool isBuilt = !theName.empty();
416 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
417 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
418 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
419 if (!aView.Add(aRoot)) continue;
421 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
422 aRShape->setImpl((new TopoDS_Shape(aRoot)));
423 theMS->generated(aRShape, aList);
424 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator
425 anIt = aList.begin(), aLast = aList.end();
426 for (; anIt != aLast; anIt++) {
427 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
428 if (theSubShapes.isBound(*anIt)) {
429 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
430 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
432 if (!aRoot.IsSame (aNewShape)) {
433 builder(theTag)->Generated(aRoot,aNewShape);
435 buildName(theTag, theName);
437 TopAbs_ShapeEnum aGenShapeType = aNewShape.ShapeType();
438 if(aGenShapeType == TopAbs_WIRE || aGenShapeType == TopAbs_SHELL) {
439 TopAbs_ShapeEnum anExplodeShapeType =
440 aGenShapeType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_FACE;
441 const TDF_Label aLabel = builder(theTag)->NamedShape()->Label();
443 std::shared_ptr<Model_Document> aDoc =
444 std::dynamic_pointer_cast<Model_Document>(document());
445 for(TopExp_Explorer anExp(aNewShape, anExplodeShapeType); anExp.More(); anExp.Next()) {
446 TDF_Label aChildLabel = aLabel.FindChild(aTag);
447 TNaming_Builder aBuilder(aChildLabel);
448 aBuilder.Generated(aRoot, anExp.Current());
449 TCollection_AsciiString aChildName =
450 TCollection_AsciiString((theName + "_").c_str()) + aTag;
451 TDataStd_Name::Set(aChildLabel, aChildName.ToCString());
459 //=======================================================================
460 int getDangleShapes(const TopoDS_Shape& theShapeIn,
461 const TopAbs_ShapeEnum theGeneratedFrom,
462 TopTools_DataMapOfShapeShape& theDangles)
465 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
466 TopAbs_ShapeEnum GeneratedTo;
467 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
468 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
469 else return Standard_False;
470 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
471 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
472 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
473 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
474 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
476 return theDangles.Extent();
479 //=======================================================================
480 void loadGeneratedDangleShapes(
481 const TopoDS_Shape& theShapeIn,
482 const TopAbs_ShapeEnum theGeneratedFrom,
483 TNaming_Builder * theBuilder)
485 TopTools_DataMapOfShapeShape dangles;
486 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
487 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
488 for (; itr.More(); itr.Next())
489 theBuilder->Generated(itr.Key(), itr.Value());
492 //=======================================================================
493 void Model_BodyBuilder::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
494 const std::string& theName, int& theTag)
496 if(theShape->isNull()) return;
497 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
499 if (aShape.ShapeType() == TopAbs_SOLID) {
500 TopExp_Explorer expl(aShape, TopAbs_FACE);
501 for (; expl.More(); expl.Next()) {
502 builder(theTag)->Generated(expl.Current());
503 TCollection_AsciiString aStr(theTag);
504 aName = theName + aStr.ToCString();
505 buildName(theTag, aName);
509 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
510 // load faces and all the free edges
511 TopTools_IndexedMapOfShape Faces;
512 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
513 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
514 TopExp_Explorer expl(aShape, TopAbs_FACE);
515 for (; expl.More(); expl.Next()) {
516 builder(theTag)->Generated(expl.Current());
517 TCollection_AsciiString aStr(theTag);
518 aName = theName + aStr.ToCString();
519 buildName(theTag, aName);
523 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
524 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
525 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
527 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
528 if (aLL.Extent() < 2) {
529 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeAndNeighbourFaces.FindKey(i))))
531 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
532 TCollection_AsciiString aStr(theTag);
533 aName = theName + aStr.ToCString();
534 buildName(theTag, aName);
537 TopTools_ListIteratorOfListOfShape anIter(aLL);
538 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
540 if(aFace.IsEqual(anIter.Value())) {
541 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
542 TCollection_AsciiString aStr(theTag);
543 aName = theName + aStr.ToCString();
544 buildName(theTag, aName);
549 } else if (aShape.ShapeType() == TopAbs_WIRE) {
550 TopTools_IndexedMapOfShape Edges;
551 BRepTools::Map3DEdges(aShape, Edges);
552 if (Edges.Extent() == 1) {
553 builder(++theTag)->Generated(Edges.FindKey(1));
554 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
555 for (; expl.More(); expl.Next()) {
556 builder(theTag)->Generated(expl.Current());
557 TCollection_AsciiString aStr(theTag);
558 aName = theName + aStr.ToCString();
559 buildName(theTag, aName);
563 TopExp_Explorer expl(aShape, TopAbs_EDGE);
564 for (; expl.More(); expl.Next()) {
565 builder(theTag)->Generated(expl.Current());
566 TCollection_AsciiString aStr(theTag);
567 aName = theName + aStr.ToCString();
568 buildName(theTag, aName);
571 // and load generated vertices.
572 TopTools_DataMapOfShapeShape generated;
573 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
575 TNaming_Builder* pBuilder = builder(theTag++);
576 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
579 } else if (aShape.ShapeType() == TopAbs_EDGE) {
580 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
581 for (; expl.More(); expl.Next()) {
582 builder(theTag)->Generated(expl.Current());
583 TCollection_AsciiString aStr(theTag);
584 aName = theName + aStr.ToCString();
585 buildName(theTag, aName);
591 //=======================================================================
592 int findAmbiguities(const TopoDS_Shape& theShapeIn,
593 TopTools_ListOfShape& theList)
597 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
598 TopAbs_ShapeEnum aTS(TopAbs_EDGE);
599 TopAbs_ShapeEnum aTA(TopAbs_FACE);
600 TopTools_MapOfShape aMap1, aMap2; // map1 - for edge ancestors; map2 - for keys => edges
601 TopTools_ListOfShape aKeyList;
602 TopExp::MapShapesAndAncestors(theShapeIn, aTS, aTA, subShapeAndAncestors);
603 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
604 const TopoDS_Shape& aKeyEdge1 = subShapeAndAncestors.FindKey(i);
605 const TopTools_ListOfShape& ancestors1 = subShapeAndAncestors.FindFromIndex(i);
607 TopTools_ListIteratorOfListOfShape it(ancestors1);
608 for(;it.More();it.Next()) aMap1.Add(it.Value()); // fill map with key ancestors => aKey1
609 for (Standard_Integer j = 1; j <= subShapeAndAncestors.Extent(); j++) {
610 if (i == j) continue;
611 const TopoDS_Shape& aKeyEdge2 = subShapeAndAncestors.FindKey(j);
612 const TopTools_ListOfShape& ancestors2 = subShapeAndAncestors.FindFromIndex(j);
613 if(ancestors1.Extent() == ancestors2.Extent() && ancestors1.Extent() > 1) {
614 int aNum (ancestors2.Extent());
615 TopTools_ListIteratorOfListOfShape it(ancestors2);
616 for(;it.More();it.Next())
617 if(aMap1.Contains(it.Value())) aNum--;
619 if(aMap2.Add(aKeyEdge1))
620 aKeyList.Append(aKeyEdge1);
621 if(aMap2.Add(aKeyEdge2))
622 aKeyList.Append(aKeyEdge2);
625 } // at the end ==> List of edges to be named in addition
627 aNumEdges = aKeyList.Extent();
629 theList.Assign(aKeyList);
633 //=======================================================================
634 void Model_BodyBuilder::loadFirstLevel(
635 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
637 if(theShape->isNull()) return;
638 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
640 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
641 TopoDS_Iterator itr(aShape);
642 for (; itr.More(); itr.Next(),theTag++) {
643 builder(theTag)->Generated(itr.Value());
644 TCollection_AsciiString aStr(theTag);
645 aName = theName + aStr.ToCString();
646 buildName(theTag, aName);
647 if(!theName.empty()) buildName(theTag, aName);
648 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
649 itr.Value().ShapeType() == TopAbs_COMPSOLID)
651 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
652 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
653 loadFirstLevel(itrShape, theName, theTag);
655 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
656 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
657 loadNextLevels(itrShape, theName, theTag);
661 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
662 itrShape->setImpl(new TopoDS_Shape(aShape));
663 loadNextLevels(itrShape, theName, theTag);
665 TopTools_ListOfShape aList;
666 if(findAmbiguities(aShape, aList)) {
667 TopTools_ListIteratorOfListOfShape it(aList);
668 for (; it.More(); it.Next(),theTag++) {
669 builder(theTag)->Generated(it.Value());
670 TCollection_AsciiString aStr(theTag);
671 aName = theName + aStr.ToCString();
672 buildName(theTag, aName);
677 //=======================================================================
678 void Model_BodyBuilder::loadDisconnectedEdges(
679 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
681 if(theShape->isNull()) return;
682 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
683 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
684 TopTools_ListOfShape empty;
685 TopExp_Explorer explF(aShape, TopAbs_FACE);
686 for (; explF.More(); explF.Next()) {
687 const TopoDS_Shape& aFace = explF.Current();
688 TopExp_Explorer explV(aFace, TopAbs_EDGE);
689 for (; explV.More(); explV.Next()) {
690 const TopoDS_Shape& anEdge = explV.Current();
691 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
692 Standard_Boolean faceIsNew = Standard_True;
693 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
694 for (; itrF.More(); itrF.Next()) {
695 if (itrF.Value().IsSame(aFace)) {
696 faceIsNew = Standard_False;
701 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
705 TopTools_MapOfShape anEdgesToDelete;
706 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
708 for(;anEx.More();anEx.Next()) {
709 Standard_Boolean aC0 = Standard_False;
710 TopoDS_Shape anEdge1 = anEx.Current();
711 if (edgeNaborFaces.IsBound(anEdge1)) {
712 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
713 if (aList1.Extent()<2) continue;
714 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
715 for (; itr.More(); itr.Next()) {
716 TopoDS_Shape anEdge2 = itr.Key();
717 if(anEdgesToDelete.Contains(anEdge2)) continue;
718 if (anEdge1.IsSame(anEdge2)) continue;
719 const TopTools_ListOfShape& aList2 = itr.Value();
720 // compare lists of the neighbour faces of edge1 and edge2
721 if (aList1.Extent() == aList2.Extent()) {
722 Standard_Integer aMatches = 0;
723 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
724 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
725 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
726 if (aMatches == aList1.Extent()) {
728 builder(theTag)->Generated(anEdge2);
729 anEdgesToDelete.Add(anEdge2);
730 TCollection_AsciiString aStr(theTag);
731 aName = theName + aStr.ToCString();
732 buildName(theTag, aName);
737 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
738 for(;itDelete.More();itDelete.Next())
739 edgeNaborFaces.UnBind(itDelete.Key());
740 edgeNaborFaces.UnBind(anEdge1);
743 builder(theTag)->Generated(anEdge1);
744 TCollection_AsciiString aStr(theTag);
745 aName = theName + aStr.ToCString();
746 buildName(theTag, aName);
752 void Model_BodyBuilder::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape,
753 const std::string& theName, int& theTag)
755 if(theShape->isNull()) return;
756 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
757 TopTools_DataMapOfShapeListOfShape vertexNaborEdges;
758 TopTools_ListOfShape empty;
759 TopExp_Explorer explF(aShape, TopAbs_EDGE);
760 for (; explF.More(); explF.Next()) {
761 const TopoDS_Shape& anEdge = explF.Current();
762 TopExp_Explorer explV(anEdge, TopAbs_VERTEX);
763 for (; explV.More(); explV.Next()) {
764 const TopoDS_Shape& aVertex = explV.Current();
765 if (!vertexNaborEdges.IsBound(aVertex)) vertexNaborEdges.Bind(aVertex, empty);
766 Standard_Boolean faceIsNew = Standard_True;
767 TopTools_ListIteratorOfListOfShape itrF(vertexNaborEdges.Find(aVertex));
768 for (; itrF.More(); itrF.Next()) {
769 if (itrF.Value().IsSame(anEdge)) {
770 faceIsNew = Standard_False;
775 vertexNaborEdges.ChangeFind(aVertex).Append(anEdge);
780 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborEdges);
781 for (; itr.More(); itr.Next()) {
782 const TopTools_ListOfShape& naborEdges = itr.Value();
783 if (naborEdges.Extent() < 2) {
784 builder(theTag)->Generated(itr.Key());
785 TCollection_AsciiString aStr(theTag);
786 aName = theName + aStr.ToCString();
787 buildName(theTag, aName);
793 std::shared_ptr<GeomAPI_Shape> Model_BodyBuilder::shape()
795 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
797 TDF_Label aShapeLab = aData->shapeLab();
798 Handle(TDF_Reference) aRef;
799 if (aShapeLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
800 aShapeLab = aRef->Get();
802 Handle(TNaming_NamedShape) aName;
803 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
804 TopoDS_Shape aShape = aName->Get();
805 if (!aShape.IsNull()) {
806 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
807 aRes->setImpl(new TopoDS_Shape(aShape));
812 return std::shared_ptr<GeomAPI_Shape>();
815 bool Model_BodyBuilder::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
817 if (theShape.get()) {
818 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
819 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
821 TDF_Label& aShapeLab = aData->shapeLab();
822 Handle(TNaming_NamedShape) aName;
823 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
824 TopoDS_Shape aLatest = TNaming_Tool::CurrentShape(aName);
825 if (aLatest.IsNull())
827 if (aLatest.IsEqual(aShape))
829 // check sub-shapes for comp-solids:
830 for (TopExp_Explorer anExp(aShape, aLatest.ShapeType()); anExp.More(); anExp.Next()) {
831 if (aLatest.IsEqual(anExp.Current()))