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);
330 else if (aShape.ShapeType() == TopAbs_SHELL || aShape.ShapeType() == TopAbs_FACE) {
331 // load faces and all the free edges
332 TopTools_IndexedMapOfShape Faces;
333 TopExp::MapShapes(aShape, TopAbs_FACE, Faces);
334 if (Faces.Extent() > 1 || (aShape.ShapeType() == TopAbs_SHELL && Faces.Extent() == 1)) {
335 TopExp_Explorer expl(aShape, TopAbs_FACE);
336 for (; expl.More(); expl.Next()) {
337 builder(++theTag)->Generated(expl.Current());
338 TCollection_AsciiString aStr(theTag);
339 aName = theName + aStr.ToCString();
340 buildName(theTag, aName);
343 TopTools_IndexedDataMapOfShapeListOfShape anEdgeAndNeighbourFaces;
344 TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeAndNeighbourFaces);
345 for (Standard_Integer i = 1; i <= anEdgeAndNeighbourFaces.Extent(); i++)
347 const TopTools_ListOfShape& aLL = anEdgeAndNeighbourFaces.FindFromIndex(i);
348 if (aLL.Extent() < 2) {
349 builder(++theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
350 TCollection_AsciiString aStr(theTag);
351 aName = theName + aStr.ToCString();
352 buildName(theTag, aName);
354 TopTools_ListIteratorOfListOfShape anIter(aLL);
355 const TopoDS_Face& aFace = TopoDS::Face(anIter.Value());
357 if(aFace.IsEqual(anIter.Value())) {
358 builder(++theTag)->Generated(anEdgeAndNeighbourFaces.FindKey(i));
359 TCollection_AsciiString aStr(theTag);
360 aName = theName + aStr.ToCString();
361 buildName(theTag, aName);
365 } else if (aShape.ShapeType() == TopAbs_WIRE) {
366 TopTools_IndexedMapOfShape Edges;
367 BRepTools::Map3DEdges(aShape, Edges);
368 if (Edges.Extent() == 1) {
369 builder(++theTag)->Generated(Edges.FindKey(1));
370 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
371 for (; expl.More(); expl.Next()) {
372 builder(++theTag)->Generated(expl.Current());
373 TCollection_AsciiString aStr(theTag);
374 aName = theName + aStr.ToCString();
375 buildName(theTag, aName);
378 TopExp_Explorer expl(aShape, TopAbs_EDGE);
379 for (; expl.More(); expl.Next()) {
380 builder(++theTag)->Generated(expl.Current());
381 TCollection_AsciiString aStr(theTag);
382 aName = theName + aStr.ToCString();
383 buildName(theTag, aName);
385 // and load generated vertices.
386 TopTools_DataMapOfShapeShape generated;
387 if (getDangleShapes(aShape, TopAbs_EDGE, generated))
389 TNaming_Builder* pBuilder = builder(++theTag);
390 loadGeneratedDangleShapes(aShape, TopAbs_EDGE, pBuilder);
393 } else if (aShape.ShapeType() == TopAbs_EDGE) {
394 TopExp_Explorer expl(aShape, TopAbs_VERTEX);
395 for (; expl.More(); expl.Next()) {
396 builder(++theTag)->Generated(expl.Current());
397 TCollection_AsciiString aStr(theTag);
398 aName = theName + aStr.ToCString();
399 buildName(theTag, aName);
403 //=======================================================================
404 void Model_ResultBody::loadFirstLevel(
405 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
407 if(theShape->isNull()) return;
408 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
410 if (aShape.ShapeType() == TopAbs_COMPOUND || aShape.ShapeType() == TopAbs_COMPSOLID) {
411 TopoDS_Iterator itr(aShape);
412 for (; itr.More(); itr.Next()) {
413 builder(++theTag)->Generated(itr.Value());
414 TCollection_AsciiString aStr(theTag);
415 aName = theName + aStr.ToCString();
416 buildName(theTag, aName);
417 if(!theName.empty()) buildName(theTag, aName);
418 if (itr.Value().ShapeType() == TopAbs_COMPOUND ||
419 itr.Value().ShapeType() == TopAbs_COMPSOLID)
421 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
422 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
423 loadFirstLevel(itrShape, theName, theTag);
425 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
426 itrShape->setImpl(new TopoDS_Shape(itr.Value()));
427 loadNextLevels(itrShape, theName, theTag);
431 std::shared_ptr<GeomAPI_Shape> itrShape(new GeomAPI_Shape());
432 itrShape->setImpl(new TopoDS_Shape(aShape));
433 loadNextLevels(itrShape, theName, theTag);
437 //=======================================================================
438 void Model_ResultBody::loadDisconnectedEdges(
439 std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
441 if(theShape->isNull()) return;
442 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
443 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
444 TopTools_ListOfShape empty;
445 TopExp_Explorer explF(aShape, TopAbs_FACE);
446 for (; explF.More(); explF.Next()) {
447 const TopoDS_Shape& aFace = explF.Current();
448 TopExp_Explorer explV(aFace, TopAbs_EDGE);
449 for (; explV.More(); explV.Next()) {
450 const TopoDS_Shape& anEdge = explV.Current();
451 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
452 Standard_Boolean faceIsNew = Standard_True;
453 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
454 for (; itrF.More(); itrF.Next()) {
455 if (itrF.Value().IsSame(aFace)) {
456 faceIsNew = Standard_False;
461 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
465 TopTools_MapOfShape anEdgesToDelete;
466 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
468 for(;anEx.More();anEx.Next()) {
469 Standard_Boolean aC0 = Standard_False;
470 TopoDS_Shape anEdge1 = anEx.Current();
471 if (edgeNaborFaces.IsBound(anEdge1)) {
472 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
473 if (aList1.Extent()<2) continue;
474 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
475 for (; itr.More(); itr.Next()) {
476 TopoDS_Shape anEdge2 = itr.Key();
477 if(anEdgesToDelete.Contains(anEdge2)) continue;
478 if (anEdge1.IsSame(anEdge2)) continue;
479 const TopTools_ListOfShape& aList2 = itr.Value();
480 // compare lists of the neighbour faces of edge1 and edge2
481 if (aList1.Extent() == aList2.Extent()) {
482 Standard_Integer aMatches = 0;
483 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
484 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
485 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
486 if (aMatches == aList1.Extent()) {
488 builder(++theTag)->Generated(anEdge2);
489 anEdgesToDelete.Add(anEdge2);
490 TCollection_AsciiString aStr(theTag);
491 aName = theName + aStr.ToCString();
492 buildName(theTag, aName);
496 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
497 for(;itDelete.More();itDelete.Next())
498 edgeNaborFaces.UnBind(itDelete.Key());
499 edgeNaborFaces.UnBind(anEdge1);
502 builder(++theTag)->Generated(anEdge1);
503 TCollection_AsciiString aStr(theTag);
504 aName = theName + aStr.ToCString();
505 buildName(theTag, aName);
510 void Model_ResultBody::loadDisconnectedVertexes(std::shared_ptr<GeomAPI_Shape> theShape, const std::string& theName, int& theTag)
512 if(theShape->isNull()) return;
513 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
514 TopTools_DataMapOfShapeListOfShape vertexNaborFaces;
515 TopTools_ListOfShape empty;
516 TopExp_Explorer explF(aShape, TopAbs_FACE);
517 for (; explF.More(); explF.Next()) {
518 const TopoDS_Shape& aFace = explF.Current();
519 TopExp_Explorer explV(aFace, TopAbs_VERTEX);
520 for (; explV.More(); explV.Next()) {
521 const TopoDS_Shape& aVertex = explV.Current();
522 if (!vertexNaborFaces.IsBound(aVertex)) vertexNaborFaces.Bind(aVertex, empty);
523 Standard_Boolean faceIsNew = Standard_True;
524 TopTools_ListIteratorOfListOfShape itrF(vertexNaborFaces.Find(aVertex));
525 for (; itrF.More(); itrF.Next()) {
526 if (itrF.Value().IsSame(aFace)) {
527 faceIsNew = Standard_False;
532 vertexNaborFaces.ChangeFind(aVertex).Append(aFace);
537 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborFaces);
538 for (; itr.More(); itr.Next()) {
539 const TopTools_ListOfShape& naborFaces = itr.Value();
540 if (naborFaces.Extent() < 3) {
541 builder(++theTag)->Generated(itr.Key());
542 TCollection_AsciiString aStr(theTag);
543 aName = theName + aStr.ToCString();
544 buildName(theTag, aName);