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 <TNaming_Builder.hxx>
10 #include <TNaming_NamedShape.hxx>
11 #include <TDataStd_Name.hxx>
13 #include <TopoDS_Shape.hxx>
14 #include <TopoDS_Face.hxx>
15 #include <TDF_ChildIterator.hxx>
16 #include <TopTools_MapOfShape.hxx>
17 #include <TopExp_Explorer.hxx>
18 #include <TopTools_ListOfShape.hxx>
19 #include <TopTools_ListIteratorOfListOfShape.hxx>
20 #include <TopTools_DataMapOfShapeListOfShape.hxx>
21 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
22 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
23 #include <TopTools_MapIteratorOfMapOfShape.hxx>
24 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
25 #include <TopTools_IndexedMapOfShape.hxx>
26 #include <TopTools_DataMapOfShapeShape.hxx>
28 #include <BRepTools.hxx>
29 #include <GeomAPI_Shape.h>
30 #include <GeomAlgoAPI_MakeShape.h>
32 //#include <TCollection_AsciiString.hxx>
33 //#include <TDF_Tool.hxx>
34 Model_ResultBody::Model_ResultBody()
36 setIsConcealed(false);
39 void Model_ResultBody::store(const std::shared_ptr<GeomAPI_Shape>& theShape)
41 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
43 TDF_Label& aShapeLab = aData->shapeLab();
46 // store the new shape as primitive
47 TNaming_Builder aBuilder(aShapeLab);
50 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
52 return; // null shape inside
54 aBuilder.Generated(aShape);
58 void Model_ResultBody::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
59 const std::shared_ptr<GeomAPI_Shape>& theToShape)
61 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
63 TDF_Label& aShapeLab = aData->shapeLab();
66 // store the new shape as primitive
67 TNaming_Builder aBuilder(aShapeLab);
68 if (!theFromShape || !theToShape)
70 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
71 if (aShapeBasis.IsNull())
72 return; // null shape inside
73 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
74 if (aShapeNew.IsNull())
75 return; // null shape inside
76 aBuilder.Generated(aShapeBasis, aShapeNew);
80 void Model_ResultBody::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
81 const std::shared_ptr<GeomAPI_Shape>& theNewShape)
83 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
85 TDF_Label& aShapeLab = aData->shapeLab();
88 // store the new shape as primitive
89 TNaming_Builder aBuilder(aShapeLab);
90 if (!theOldShape || !theNewShape)
92 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
93 if (aShapeOld.IsNull())
94 return; // null shape inside
95 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
96 if (aShapeNew.IsNull())
97 return; // null shape inside
98 aBuilder.Modify(aShapeOld, aShapeNew);
102 std::shared_ptr<GeomAPI_Shape> Model_ResultBody::shape()
104 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
106 TDF_Label& aShapeLab = aData->shapeLab();
107 Handle(TNaming_NamedShape) aName;
108 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
109 TopoDS_Shape aShape = aName->Get();
110 if (!aShape.IsNull()) {
111 std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
112 aRes->setImpl(new TopoDS_Shape(aShape));
117 return std::shared_ptr<GeomAPI_Shape>();
120 void Model_ResultBody::clean()
122 std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
123 for(; aBuilder != myBuilders.end(); aBuilder++)
128 Model_ResultBody::~Model_ResultBody()
133 TNaming_Builder* Model_ResultBody::builder(const int theTag)
135 if (myBuilders.size() <= (unsigned int)theTag) {
136 myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
138 if (!myBuilders[theTag]) {
139 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(data());
140 myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
141 //TCollection_AsciiString entry;//
142 //TDF_Tool::Entry(aData->shapeLab().FindChild(theTag), entry);
143 //cout << "Label = " <<entry.ToCString() <<endl;
145 return myBuilders[theTag];
148 void Model_ResultBody::buildName(const int theTag, const std::string& theName)
150 std::string aName = data()->name() + "/" + theName;
151 TDataStd_Name::Set(builder(theTag)->NamedShape()->Label(),aName.c_str());
153 void Model_ResultBody::generated(
154 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
156 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
157 builder(theTag)->Generated(aShape);
159 buildName(theTag, theName);
162 void Model_ResultBody::generated(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
163 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
165 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
166 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
167 builder(theTag)->Generated(anOldShape, aNewShape);
169 buildName(theTag, theName);
173 void Model_ResultBody::modified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
174 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const std::string& theName, const int theTag)
176 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
177 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
178 builder(theTag)->Modify(anOldShape, aNewShape);
180 buildName(theTag, theName);
183 void Model_ResultBody::deleted(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
186 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
187 builder(theTag)->Delete(aShape);
190 void Model_ResultBody::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
191 std::shared_ptr<GeomAPI_Shape> theShapeIn,
192 const int theKindOfShape,
195 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
196 TopTools_MapOfShape aView;
197 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
198 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
199 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
200 if (!aView.Add(aRoot)) continue;
201 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
202 aRShape->setImpl((new TopoDS_Shape(aRoot)));
203 if (theMS->isDeleted (aRShape)) {
204 builder(theTag)->Delete(aRoot);
209 void Model_ResultBody::loadAndOrientModifiedShapes (
210 GeomAlgoAPI_MakeShape* theMS,
211 std::shared_ptr<GeomAPI_Shape> theShapeIn,
212 const int theKindOfShape,
214 const std::string& theName,
215 GeomAPI_DataMapOfShapeShape& theSubShapes)
217 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
218 TopTools_MapOfShape aView;
219 bool isBuilt = theName.empty();
220 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
221 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
222 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
223 if (!aView.Add(aRoot)) continue;
225 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
226 aRShape->setImpl((new TopoDS_Shape(aRoot)));
227 theMS->modified(aRShape, aList);
228 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
229 for (; anIt != aLast; anIt++) {
230 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
231 if (theSubShapes.isBound(*anIt)) {
232 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
233 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
235 if (!aRoot.IsSame (aNewShape)) {
236 builder(theTag)->Modify(aRoot,aNewShape);
238 buildName(theTag, theName);
244 void Model_ResultBody::loadAndOrientGeneratedShapes (
245 GeomAlgoAPI_MakeShape* theMS,
246 std::shared_ptr<GeomAPI_Shape> theShapeIn,
247 const int theKindOfShape,
249 const std::string& theName,
250 GeomAPI_DataMapOfShapeShape& theSubShapes)
252 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
253 TopTools_MapOfShape aView;
254 bool isBuilt = theName.empty();
255 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
256 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
257 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
258 if (!aView.Add(aRoot)) continue;
260 std::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
261 aRShape->setImpl((new TopoDS_Shape(aRoot)));
262 theMS->generated(aRShape, aList);
263 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
264 for (; anIt != aLast; anIt++) {
265 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
266 if (theSubShapes.isBound(*anIt)) {
267 std::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
268 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
270 if (!aRoot.IsSame (aNewShape)) {
271 builder(theTag)->Generated(aRoot,aNewShape);
273 buildName(theTag, theName);
279 //=======================================================================
280 int getDangleShapes(const TopoDS_Shape& theShapeIn,
281 const TopAbs_ShapeEnum theGeneratedFrom,
282 TopTools_DataMapOfShapeShape& theDangles)
285 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
286 TopAbs_ShapeEnum GeneratedTo;
287 if (theGeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
288 else if (theGeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
289 else return Standard_False;
290 TopExp::MapShapesAndAncestors(theShapeIn, GeneratedTo, theGeneratedFrom, subShapeAndAncestors);
291 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
292 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
293 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
294 if (ancestors.Extent() == 1) theDangles.Bind(ancestors.First(), mayBeDangle);
296 return theDangles.Extent();
299 //=======================================================================
300 void loadGeneratedDangleShapes(
301 const TopoDS_Shape& theShapeIn,
302 const TopAbs_ShapeEnum theGeneratedFrom,
303 TNaming_Builder * theBuilder)
305 TopTools_DataMapOfShapeShape dangles;
306 if (!getDangleShapes(theShapeIn, theGeneratedFrom, dangles)) return;
307 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
308 for (; itr.More(); itr.Next())
309 theBuilder->Generated(itr.Key(), itr.Value());
312 //=======================================================================
313 void Model_ResultBody::loadNextLevels(std::shared_ptr<GeomAPI_Shape> theShape,
314 const std::string& theName, int& theTag)
316 if(theShape->isNull()) return;
317 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
319 if (aShape.ShapeType() == TopAbs_SOLID) {
320 TopExp_Explorer expl(aShape, TopAbs_FACE);
321 for (; expl.More(); expl.Next())
322 builder(++theTag)->Generated(expl.Current());
323 TCollection_AsciiString aStr(theTag);
324 aName = theName + aStr.ToCString();
325 buildName(theTag, aName);
327 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
328 // load faces and all the free edges
329 TopTools_IndexedMapOfShape Faces;
330 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
331 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
332 TopExp_Explorer expl(aShape, TopAbs_FACE);
333 for (; expl.More(); expl.Next()) {
334 builder(++theTag)->Generated(expl.Current());
335 TCollection_AsciiString aStr(theTag);
336 aName = theName + aStr.ToCString();
337 buildName(theTag, aName);
340 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
341 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
342 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
344 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
345 if (aLL.Extent() < 2) {
346 builder(++theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
347 TCollection_AsciiString aStr(theTag);
348 aName = theName + aStr.ToCString();
349 buildName(theTag, aName);
351 TopTools_ListIteratorOfListOfShape anIter(aLL);
352 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
354 if(aFace.IsEqual(anIter.Value())) {
355 builder(++theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
356 TCollection_AsciiString aStr(theTag);
357 aName = theName + aStr.ToCString();
358 buildName(theTag, aName);
362 } else if (aShape.ShapeType() == TopAbs_WIRE) {
363 TopTools_IndexedMapOfShape Edges;
364 BRepTools::Map3DEdges(aShape, Edges);
365 if (Edges.Extent() == 1) {
366 builder(++theTag)->Generated(Edges.FindKey(1));
367 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
368 for (; expl.More(); expl.Next()) {
369 builder(++theTag)->Generated(expl.Current());
370 TCollection_AsciiString aStr(theTag);
371 aName = theName + aStr.ToCString();
372 buildName(theTag, aName);
375 TopExp_Explorer expl(aShape, TopAbs_EDGE);
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);
382 // and load generated vertices.
383 TopTools_DataMapOfShapeShape generated;
384 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
386 TNaming_Builder* pBuilder = builder(++theTag);
387 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
390 } else if (aShape.ShapeType() == TopAbs_EDGE) {
391 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
392 for (; expl.More(); expl.Next()) {
393 builder(++theTag)->Generated(expl.Current());
394 TCollection_AsciiString aStr(theTag);
395 aName = theName + aStr.ToCString();
396 buildName(theTag, aName);
400 //=======================================================================
401 void Model_ResultBody::loadFirstLevel(
402 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
404 if(theShape->isNull()) return;
405 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
407 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
408 TopoDS_Iterator itr(aShape);
409 for (; itr.More(); itr.Next()) {
410 builder(++theTag)->Generated(itr.Value());
411 TCollection_AsciiString aStr(theTag);
412 aName = theName + aStr.ToCString();
413 buildName(theTag, aName);
414 if(!theName.empty()) buildName(theTag, aName);
415 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
416 itr.Value().ShapeType() == TopAbs_COMPSOLID)
418 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
419 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
420 loadFirstLevel(itrShape, theName, theTag);
422 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
423 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
424 loadNextLevels(itrShape, theName, theTag);
428 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
429 itrShape->setImpl(new TopoDS_Shape(aShape));
430 loadNextLevels(itrShape, theName, theTag);
434 //=======================================================================
435 void Model_ResultBody::loadDisconnectedEdges(
436 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
438 if(theShape->isNull()) return;
439 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
440 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
441 TopTools_ListOfShape empty;
442 TopExp_Explorer explF(aShape, TopAbs_FACE);
443 for (; explF.More(); explF.Next()) {
444 const TopoDS_Shape& aFace = explF.Current();
445 TopExp_Explorer explV(aFace, TopAbs_EDGE);
446 for (; explV.More(); explV.Next()) {
447 const TopoDS_Shape& anEdge = explV.Current();
448 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
449 Standard_Boolean faceIsNew = Standard_True;
450 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
451 for (; itrF.More(); itrF.Next()) {
452 if (itrF.Value().IsSame(aFace)) {
453 faceIsNew = Standard_False;
458 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
462 TopTools_MapOfShape anEdgesToDelete;
463 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
465 for(;anEx.More();anEx.Next()) {
466 Standard_Boolean aC0 = Standard_False;
467 TopoDS_Shape anEdge1 = anEx.Current();
468 if (edgeNaborFaces.IsBound(anEdge1)) {
469 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
470 if (aList1.Extent()<2) continue;
471 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
472 for (; itr.More(); itr.Next()) {
473 TopoDS_Shape anEdge2 = itr.Key();
474 if(anEdgesToDelete.Contains(anEdge2)) continue;
475 if (anEdge1.IsSame(anEdge2)) continue;
476 const TopTools_ListOfShape& aList2 = itr.Value();
477 // compare lists of the neighbour faces of edge1 and edge2
478 if (aList1.Extent() == aList2.Extent()) {
479 Standard_Integer aMatches = 0;
480 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
481 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
482 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
483 if (aMatches == aList1.Extent()) {
485 builder(++theTag)->Generated(anEdge2);
486 anEdgesToDelete.Add(anEdge2);
487 TCollection_AsciiString aStr(theTag);
488 aName = theName + aStr.ToCString();
489 buildName(theTag, aName);
493 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
494 for(;itDelete.More();itDelete.Next())
495 edgeNaborFaces.UnBind(itDelete.Key());
496 edgeNaborFaces.UnBind(anEdge1);
499 builder(++theTag)->Generated(anEdge1);
500 TCollection_AsciiString aStr(theTag);
501 aName = theName + aStr.ToCString();
502 buildName(theTag, aName);
507 void Model_ResultBody::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
509 if(theShape->isNull()) return;
510 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
511 TopTools_DataMapOfShapeListOfShape vertexNaborFaces;
512 TopTools_ListOfShape empty;
513 TopExp_Explorer explF(aShape, TopAbs_FACE);
514 for (; explF.More(); explF.Next()) {
515 const TopoDS_Shape& aFace = explF.Current();
516 TopExp_Explorer explV(aFace, TopAbs_VERTEX);
517 for (; explV.More(); explV.Next()) {
518 const TopoDS_Shape& aVertex = explV.Current();
519 if (!vertexNaborFaces.IsBound(aVertex)) vertexNaborFaces.Bind(aVertex, empty);
520 Standard_Boolean faceIsNew = Standard_True;
521 TopTools_ListIteratorOfListOfShape itrF(vertexNaborFaces.Find(aVertex));
522 for (; itrF.More(); itrF.Next()) {
523 if (itrF.Value().IsSame(aFace)) {
524 faceIsNew = Standard_False;
529 vertexNaborFaces.ChangeFind(aVertex).Append(aFace);
534 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborFaces);
535 for (; itr.More(); itr.Next()) {
536 const TopTools_ListOfShape& naborFaces = itr.Value();
537 if (naborFaces.Extent() < 3) {
538 builder(++theTag)->Generated(itr.Key());
539 TCollection_AsciiString aStr(theTag);
540 aName = theName + aStr.ToCString();
541 buildName(theTag, aName);