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_ResultBody.h>
8 #include <Model_Data.h>
9 #include <Model_Document.h>
10 #include <TNaming_Builder.hxx>
11 #include <TNaming_NamedShape.hxx>
12 #include <TDataStd_Name.hxx>
14 #include <TopoDS_Shape.hxx>
15 #include <TopoDS_Face.hxx>
16 #include <TDF_ChildIterator.hxx>
17 #include <TopTools_MapOfShape.hxx>
18 #include <TopExp_Explorer.hxx>
19 #include <TopTools_ListOfShape.hxx>
20 #include <TopTools_ListIteratorOfListOfShape.hxx>
21 #include <TopTools_DataMapOfShapeListOfShape.hxx>
22 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
23 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
24 #include <TopTools_MapIteratorOfMapOfShape.hxx>
25 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
26 #include <TopTools_IndexedMapOfShape.hxx>
27 #include <TopTools_DataMapOfShapeShape.hxx>
29 #include <BRepTools.hxx>
30 #include <GeomAPI_Shape.h>
31 #include <GeomAlgoAPI_MakeShape.h>
33 //#include <TCollection_AsciiString.hxx>
34 //#include <TDF_Tool.hxx>
35 Model_ResultBody::Model_ResultBody()
37 setIsConcealed(false);
40 void Model_ResultBody::store(const std::shared_ptr<GeomAPI_Shape>& theShape)
42 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
44 TDF_Label& aShapeLab = aData->shapeLab();
47 // store the new shape as primitive
48 TNaming_Builder aBuilder(aShapeLab);
51 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
53 return; // null shape inside
55 aBuilder.Generated(aShape);
59 void Model_ResultBody::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
60 const std::shared_ptr<GeomAPI_Shape>& theToShape)
62 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
64 TDF_Label& aShapeLab = aData->shapeLab();
67 // store the new shape as primitive
68 TNaming_Builder aBuilder(aShapeLab);
69 if (!theFromShape || !theToShape)
71 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
72 if (aShapeBasis.IsNull())
73 return; // null shape inside
74 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
75 if (aShapeNew.IsNull())
76 return; // null shape inside
77 aBuilder.Generated(aShapeBasis, aShapeNew);
81 void Model_ResultBody::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
82 const std::shared_ptr<GeomAPI_Shape>& theNewShape)
84 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
86 TDF_Label& aShapeLab = aData->shapeLab();
89 // store the new shape as primitive
90 TNaming_Builder aBuilder(aShapeLab);
91 if (!theOldShape || !theNewShape)
93 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
94 if (aShapeOld.IsNull())
95 return; // null shape inside
96 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
97 if (aShapeNew.IsNull())
98 return; // null shape inside
99 aBuilder.Modify(aShapeOld, aShapeNew);
103 std::shared_ptr<GeomAPI_Shape> Model_ResultBody::shape()
105 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
107 TDF_Label& aShapeLab = aData->shapeLab();
108 Handle(TNaming_NamedShape) aName;
109 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
110 TopoDS_Shape aShape = aName->Get();
111 if (!aShape.IsNull()) {
112 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
113 aRes->setImpl(new TopoDS_Shape(aShape));
118 return std::shared_ptr<GeomAPI_Shape>();
121 void Model_ResultBody::clean()
123 std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
124 for(; aBuilder != myBuilders.end(); aBuilder++)
129 Model_ResultBody::~Model_ResultBody()
134 TNaming_Builder* Model_ResultBody::builder(const int theTag)
136 if (myBuilders.size() <= (unsigned int)theTag) {
137 myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
139 if (!myBuilders[theTag]) {
140 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
141 myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
142 //TCollection_AsciiString entry;//
143 //TDF_Tool::Entry(aData->shapeLab().FindChild(theTag), entry);
144 //cout << "Label = " <<entry.ToCString() <<endl;
146 return myBuilders[theTag];
149 void Model_ResultBody::buildName(const int theTag, const std::string& theName)
151 std::string aName = data()->name() + "/" + theName;
152 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(document());
153 aDoc->addNamingName(builder(theTag)->NamedShape()->Label(), aName);
154 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(),aName.c_str());
156 void Model_ResultBody::generated(
157 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
159 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
160 builder(theTag)->Generated(aShape);
162 buildName(theTag, theName);
165 void Model_ResultBody::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
166 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
168 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
169 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
170 builder(theTag)->Generated(anOldShape, aNewShape);
172 buildName(theTag, theName);
176 void Model_ResultBody::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
177 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
179 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
180 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
181 builder(theTag)->Modify(anOldShape, aNewShape);
183 buildName(theTag, theName);
186 void Model_ResultBody::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
189 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
190 builder(theTag)->Delete(aShape);
193 void Model_ResultBody::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
194 std::shared_ptr<GeomAPI_Shape> theShapeIn,
195 const int theKindOfShape,
198 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
199 TopTools_MapOfShape aView;
200 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
201 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
202 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
203 if (!aView.Add(aRoot)) continue;
204 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
205 aRShape->setImpl((new TopoDS_Shape(aRoot)));
206 if (theMS->isDeleted (aRShape)) {
207 builder(theTag)->Delete(aRoot);
212 void Model_ResultBody::loadAndOrientModifiedShapes (
213 GeomAlgoAPI_MakeShape* theMS,
214 std::shared_ptr<GeomAPI_Shape> theShapeIn,
215 const int theKindOfShape,
217 const std::string& theName,
218 GeomAPI_DataMapOfShapeShape& theSubShapes)
220 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
221 TopTools_MapOfShape aView;
222 bool isBuilt = theName.empty();
223 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
224 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
225 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
226 if (!aView.Add(aRoot)) continue;
228 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
229 aRShape->setImpl((new TopoDS_Shape(aRoot)));
230 theMS->modified(aRShape, aList);
231 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
232 for (; anIt != aLast; anIt++) {
233 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
234 if (theSubShapes.isBound(*anIt)) {
235 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
236 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
238 if (!aRoot.IsSame (aNewShape)) {
239 builder(theTag)->Modify(aRoot,aNewShape);
241 buildName(theTag, theName);
247 void Model_ResultBody::loadAndOrientGeneratedShapes (
248 GeomAlgoAPI_MakeShape* theMS,
249 std::shared_ptr<GeomAPI_Shape> theShapeIn,
250 const int theKindOfShape,
252 const std::string& theName,
253 GeomAPI_DataMapOfShapeShape& theSubShapes)
255 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
256 TopTools_MapOfShape aView;
257 bool isBuilt = theName.empty();
258 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
259 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
260 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
261 if (!aView.Add(aRoot)) continue;
263 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
264 aRShape->setImpl((new TopoDS_Shape(aRoot)));
265 theMS->generated(aRShape, aList);
266 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
267 for (; anIt != aLast; anIt++) {
268 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
269 if (theSubShapes.isBound(*anIt)) {
270 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
271 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
273 if (!aRoot.IsSame (aNewShape)) {
274 builder(theTag)->Generated(aRoot,aNewShape);
276 buildName(theTag, theName);
282 //=======================================================================
283 int getDangleShapes(const TopoDS_Shape& theShapeIn,
284 const TopAbs_ShapeEnum theGeneratedFrom,
285 TopTools_DataMapOfShapeShape& theDangles)
288 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
289 TopAbs_ShapeEnum GeneratedTo;
290 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
291 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
292 else return Standard_False;
293 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
294 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
295 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
296 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
297 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
299 return theDangles.Extent();
302 //=======================================================================
303 void loadGeneratedDangleShapes(
304 const TopoDS_Shape& theShapeIn,
305 const TopAbs_ShapeEnum theGeneratedFrom,
306 TNaming_Builder * theBuilder)
308 TopTools_DataMapOfShapeShape dangles;
309 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
310 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
311 for (; itr.More(); itr.Next())
312 theBuilder->Generated(itr.Key(), itr.Value());
315 //=======================================================================
316 void Model_ResultBody::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
317 const std::string& theName, int& theTag)
319 if(theShape->isNull()) return;
320 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
322 if (aShape.ShapeType() == TopAbs_SOLID) {
323 TopExp_Explorer expl(aShape, TopAbs_FACE);
324 for (; expl.More(); expl.Next()) {
325 builder(theTag)->Generated(expl.Current());
326 TCollection_AsciiString aStr(theTag);
327 aName = theName + aStr.ToCString();
328 buildName(theTag, aName);
332 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
333 // load faces and all the free edges
334 TopTools_IndexedMapOfShape Faces;
335 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
336 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
337 TopExp_Explorer expl(aShape, TopAbs_FACE);
338 for (; expl.More(); expl.Next()) {
339 builder(theTag)->Generated(expl.Current());
340 TCollection_AsciiString aStr(theTag);
341 aName = theName + aStr.ToCString();
342 buildName(theTag, aName);
346 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
347 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
348 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
350 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
351 if (aLL.Extent() < 2) {
352 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
353 TCollection_AsciiString aStr(theTag);
354 aName = theName + aStr.ToCString();
355 buildName(theTag, aName);
358 TopTools_ListIteratorOfListOfShape anIter(aLL);
359 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
361 if(aFace.IsEqual(anIter.Value())) {
362 builder(theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
363 TCollection_AsciiString aStr(theTag);
364 aName = theName + aStr.ToCString();
365 buildName(theTag, aName);
370 } else if (aShape.ShapeType() == TopAbs_WIRE) {
371 TopTools_IndexedMapOfShape Edges;
372 BRepTools::Map3DEdges(aShape, Edges);
373 if (Edges.Extent() == 1) {
374 builder(++theTag)->Generated(Edges.FindKey(1));
375 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
376 for (; expl.More(); expl.Next()) {
377 builder(theTag)->Generated(expl.Current());
378 TCollection_AsciiString aStr(theTag);
379 aName = theName + aStr.ToCString();
380 buildName(theTag, aName);
384 TopExp_Explorer expl(aShape, TopAbs_EDGE);
385 for (; expl.More(); expl.Next()) {
386 builder(theTag)->Generated(expl.Current());
387 TCollection_AsciiString aStr(theTag);
388 aName = theName + aStr.ToCString();
389 buildName(theTag, aName);
392 // and load generated vertices.
393 TopTools_DataMapOfShapeShape generated;
394 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
396 TNaming_Builder* pBuilder = builder(theTag++);
397 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
400 } else if (aShape.ShapeType() == TopAbs_EDGE) {
401 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
402 for (; expl.More(); expl.Next()) {
403 builder(theTag)->Generated(expl.Current());
404 TCollection_AsciiString aStr(theTag);
405 aName = theName + aStr.ToCString();
406 buildName(theTag, aName);
411 //=======================================================================
412 void Model_ResultBody::loadFirstLevel(
413 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
415 if(theShape->isNull()) return;
416 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
418 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
419 TopoDS_Iterator itr(aShape);
420 for (; itr.More(); itr.Next(),theTag++) {
421 builder(theTag)->Generated(itr.Value());
422 TCollection_AsciiString aStr(theTag);
423 aName = theName + aStr.ToCString();
424 buildName(theTag, aName);
425 if(!theName.empty()) buildName(theTag, aName);
426 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
427 itr.Value().ShapeType() == TopAbs_COMPSOLID)
429 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
430 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
431 loadFirstLevel(itrShape, theName, theTag);
433 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
434 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
435 loadNextLevels(itrShape, theName, theTag);
439 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
440 itrShape->setImpl(new TopoDS_Shape(aShape));
441 loadNextLevels(itrShape, theName, theTag);
445 //=======================================================================
446 void Model_ResultBody::loadDisconnectedEdges(
447 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
449 if(theShape->isNull()) return;
450 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
451 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
452 TopTools_ListOfShape empty;
453 TopExp_Explorer explF(aShape, TopAbs_FACE);
454 for (; explF.More(); explF.Next()) {
455 const TopoDS_Shape& aFace = explF.Current();
456 TopExp_Explorer explV(aFace, TopAbs_EDGE);
457 for (; explV.More(); explV.Next()) {
458 const TopoDS_Shape& anEdge = explV.Current();
459 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
460 Standard_Boolean faceIsNew = Standard_True;
461 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
462 for (; itrF.More(); itrF.Next()) {
463 if (itrF.Value().IsSame(aFace)) {
464 faceIsNew = Standard_False;
469 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
473 TopTools_MapOfShape anEdgesToDelete;
474 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
476 for(;anEx.More();anEx.Next()) {
477 Standard_Boolean aC0 = Standard_False;
478 TopoDS_Shape anEdge1 = anEx.Current();
479 if (edgeNaborFaces.IsBound(anEdge1)) {
480 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
481 if (aList1.Extent()<2) continue;
482 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
483 for (; itr.More(); itr.Next()) {
484 TopoDS_Shape anEdge2 = itr.Key();
485 if(anEdgesToDelete.Contains(anEdge2)) continue;
486 if (anEdge1.IsSame(anEdge2)) continue;
487 const TopTools_ListOfShape& aList2 = itr.Value();
488 // compare lists of the neighbour faces of edge1 and edge2
489 if (aList1.Extent() == aList2.Extent()) {
490 Standard_Integer aMatches = 0;
491 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
492 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
493 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
494 if (aMatches == aList1.Extent()) {
496 builder(theTag)->Generated(anEdge2);
497 anEdgesToDelete.Add(anEdge2);
498 TCollection_AsciiString aStr(theTag);
499 aName = theName + aStr.ToCString();
500 buildName(theTag, aName);
505 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
506 for(;itDelete.More();itDelete.Next())
507 edgeNaborFaces.UnBind(itDelete.Key());
508 edgeNaborFaces.UnBind(anEdge1);
511 builder(theTag)->Generated(anEdge1);
512 TCollection_AsciiString aStr(theTag);
513 aName = theName + aStr.ToCString();
514 buildName(theTag, aName);
520 void Model_ResultBody::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
522 if(theShape->isNull()) return;
523 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
524 TopTools_DataMapOfShapeListOfShape vertexNaborFaces;
525 TopTools_ListOfShape empty;
526 TopExp_Explorer explF(aShape, TopAbs_FACE);
527 for (; explF.More(); explF.Next()) {
528 const TopoDS_Shape& aFace = explF.Current();
529 TopExp_Explorer explV(aFace, TopAbs_VERTEX);
530 for (; explV.More(); explV.Next()) {
531 const TopoDS_Shape& aVertex = explV.Current();
532 if (!vertexNaborFaces.IsBound(aVertex)) vertexNaborFaces.Bind(aVertex, empty);
533 Standard_Boolean faceIsNew = Standard_True;
534 TopTools_ListIteratorOfListOfShape itrF(vertexNaborFaces.Find(aVertex));
535 for (; itrF.More(); itrF.Next()) {
536 if (itrF.Value().IsSame(aFace)) {
537 faceIsNew = Standard_False;
542 vertexNaborFaces.ChangeFind(aVertex).Append(aFace);
547 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborFaces);
548 for (; itr.More(); itr.Next()) {
549 const TopTools_ListOfShape& naborFaces = itr.Value();
550 if (naborFaces.Extent() < 1) {
551 builder(theTag)->Generated(itr.Key());
552 TCollection_AsciiString aStr(theTag);
553 aName = theName + aStr.ToCString();
554 buildName(theTag, aName);