1 // File: Model_ResultBody.cpp
2 // Created: 08 Jul 2014
3 // Author: Mikhail PONIKAROV
5 #include <Model_ResultBody.h>
6 #include <Model_Data.h>
7 #include <TNaming_Builder.hxx>
8 #include <TNaming_NamedShape.hxx>
9 #include <TopoDS_Shape.hxx>
10 #include <TDF_ChildIterator.hxx>
11 #include <TopTools_MapOfShape.hxx>
12 #include <TopExp_Explorer.hxx>
13 #include <TopTools_ListOfShape.hxx>
14 #include <TopTools_ListIteratorOfListOfShape.hxx>
15 #include <TopTools_DataMapOfShapeListOfShape.hxx>
16 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
17 #include <TopTools_MapIteratorOfMapOfShape.hxx>
18 #include <GeomAPI_Shape.h>
19 #include <GeomAlgoAPI_MakeShape.h>
21 //#include <TCollection_AsciiString.hxx>
22 //#include <TDF_Tool.hxx>
23 Model_ResultBody::Model_ResultBody()
25 setIsConcealed(false);
28 void Model_ResultBody::store(const boost::shared_ptr<GeomAPI_Shape>& theShape)
30 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
32 TDF_Label& aShapeLab = aData->shapeLab();
35 // store the new shape as primitive
36 TNaming_Builder aBuilder(aShapeLab);
39 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
41 return; // null shape inside
43 aBuilder.Generated(aShape);
47 void Model_ResultBody::storeGenerated(const boost::shared_ptr<GeomAPI_Shape>& theFromShape,
48 const boost::shared_ptr<GeomAPI_Shape>& theToShape)
50 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
52 TDF_Label& aShapeLab = aData->shapeLab();
55 // store the new shape as primitive
56 TNaming_Builder aBuilder(aShapeLab);
57 if (!theFromShape || !theToShape)
59 TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
60 if (aShapeBasis.IsNull())
61 return; // null shape inside
62 TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
63 if (aShapeNew.IsNull())
64 return; // null shape inside
65 aBuilder.Generated(aShapeBasis, aShapeNew);
69 void Model_ResultBody::storeModified(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
70 const boost::shared_ptr<GeomAPI_Shape>& theNewShape)
72 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
74 TDF_Label& aShapeLab = aData->shapeLab();
77 // store the new shape as primitive
78 TNaming_Builder aBuilder(aShapeLab);
79 if (!theOldShape || !theNewShape)
81 TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
82 if (aShapeOld.IsNull())
83 return; // null shape inside
84 TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
85 if (aShapeNew.IsNull())
86 return; // null shape inside
87 aBuilder.Generated(aShapeOld, aShapeNew);
91 boost::shared_ptr<GeomAPI_Shape> Model_ResultBody::shape()
93 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
95 TDF_Label& aShapeLab = aData->shapeLab();
96 Handle(TNaming_NamedShape) aName;
97 if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
98 TopoDS_Shape aShape = aName->Get();
99 if (!aShape.IsNull()) {
100 boost::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
101 aRes->setImpl(new TopoDS_Shape(aShape));
106 return boost::shared_ptr<GeomAPI_Shape>();
109 void Model_ResultBody::clean()
111 std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
112 for(; aBuilder != myBuilders.end(); aBuilder++)
117 Model_ResultBody::~Model_ResultBody()
122 TNaming_Builder* Model_ResultBody::builder(const int theTag)
124 if (myBuilders.size() <= (unsigned int)theTag) {
125 myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
127 if (!myBuilders[theTag]) {
128 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
129 myBuilders[theTag] = new TNaming_Builder(aData->shapeLab().FindChild(theTag));
130 //TCollection_AsciiString entry;//
131 //TDF_Tool::Entry(aData->shapeLab().FindChild(theTag), entry);
132 //cout << "Label = " <<entry.ToCString() <<endl;
134 return myBuilders[theTag];
137 void Model_ResultBody::generated(
138 const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
140 TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
141 builder(theTag)->Generated(aShape);
144 void Model_ResultBody::generated(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
145 const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
147 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
148 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
149 builder(theTag)->Generated(anOldShape, aNewShape);
153 void Model_ResultBody::modified(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
154 const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
156 TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
157 TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
158 builder(theTag)->Modify(anOldShape, aNewShape);
161 void Model_ResultBody::deleted(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
164 TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
165 builder(theTag)->Delete(aShape);
168 void Model_ResultBody::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
169 boost::shared_ptr<GeomAPI_Shape> theShapeIn,
170 const int theKindOfShape,
173 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
174 TopTools_MapOfShape aView;
175 TopExp_Explorer ShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
176 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
177 const TopoDS_Shape& aRoot = ShapeExplorer.Current ();
178 if (!aView.Add(aRoot)) continue;
179 boost::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
180 aRShape->setImpl((new TopoDS_Shape(aRoot)));
181 if (theMS->isDeleted (aRShape)) {
182 builder(theTag)->Delete(aRoot);
187 void Model_ResultBody::loadAndOrientModifiedShapes (
188 GeomAlgoAPI_MakeShape* theMS,
189 boost::shared_ptr<GeomAPI_Shape> theShapeIn,
190 const int theKindOfShape,
192 GeomAPI_DataMapOfShapeShape& theSubShapes)
194 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
195 TopTools_MapOfShape aView;
196 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
197 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
198 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
199 if (!aView.Add(aRoot)) continue;
201 boost::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
202 aRShape->setImpl((new TopoDS_Shape(aRoot)));
203 theMS->modified(aRShape, aList);
204 std::list<boost::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
205 for (; anIt != aLast; anIt++) {
206 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
207 if (theSubShapes.isBound(*anIt)) {
208 boost::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
209 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
211 if (!aRoot.IsSame (aNewShape))
212 builder(theTag)->Modify(aRoot,aNewShape);
217 void Model_ResultBody::loadAndOrientGeneratedShapes (
218 GeomAlgoAPI_MakeShape* theMS,
219 boost::shared_ptr<GeomAPI_Shape> theShapeIn,
220 const int theKindOfShape,
222 GeomAPI_DataMapOfShapeShape& theSubShapes)
224 TopoDS_Shape aShapeIn = theShapeIn->impl<TopoDS_Shape>();
225 TopTools_MapOfShape aView;
226 TopExp_Explorer aShapeExplorer (aShapeIn, (TopAbs_ShapeEnum)theKindOfShape);
227 for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
228 const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
229 if (!aView.Add(aRoot)) continue;
231 boost::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
232 aRShape->setImpl((new TopoDS_Shape(aRoot)));
233 theMS->generated(aRShape, aList);
234 std::list<boost::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aList.begin(), aLast = aList.end();
235 for (; anIt != aLast; anIt++) {
236 TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
237 if (theSubShapes.isBound(*anIt)) {
238 boost::shared_ptr<GeomAPI_Shape> aMapShape(theSubShapes.find(*anIt));
239 aNewShape.Orientation(aMapShape->impl<TopoDS_Shape>().Orientation());
241 if (!aRoot.IsSame (aNewShape))
242 builder(theTag)->Generated(aRoot,aNewShape);
247 void Model_ResultBody::loadFirstLevel(boost::shared_ptr<GeomAPI_Shape> theShape, int& theTag)
252 void Model_ResultBody::loadDisconnectedEdges(boost::shared_ptr<GeomAPI_Shape> theShape, int& theTag)
254 if(theShape->isNull()) return;
255 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
256 TopTools_DataMapOfShapeListOfShape edgeNaborFaces;
257 TopTools_ListOfShape empty;
258 TopExp_Explorer explF(aShape, TopAbs_FACE);
259 for (; explF.More(); explF.Next()) {
260 const TopoDS_Shape& aFace = explF.Current();
261 TopExp_Explorer explV(aFace, TopAbs_EDGE);
262 for (; explV.More(); explV.Next()) {
263 const TopoDS_Shape& anEdge = explV.Current();
264 if (!edgeNaborFaces.IsBound(anEdge)) edgeNaborFaces.Bind(anEdge, empty);
265 Standard_Boolean faceIsNew = Standard_True;
266 TopTools_ListIteratorOfListOfShape itrF(edgeNaborFaces.Find(anEdge));
267 for (; itrF.More(); itrF.Next()) {
268 if (itrF.Value().IsSame(aFace)) {
269 faceIsNew = Standard_False;
274 edgeNaborFaces.ChangeFind(anEdge).Append(aFace);
278 TopTools_MapOfShape anEdgesToDelete;
279 TopExp_Explorer anEx(aShape,TopAbs_EDGE);
280 for(;anEx.More();anEx.Next()) {
281 Standard_Boolean aC0 = Standard_False;
282 TopoDS_Shape anEdge1 = anEx.Current();
283 if (edgeNaborFaces.IsBound(anEdge1)) {
284 const TopTools_ListOfShape& aList1 = edgeNaborFaces.Find(anEdge1);
285 if (aList1.Extent()<2) continue;
286 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(edgeNaborFaces);
287 for (; itr.More(); itr.Next()) {
288 TopoDS_Shape anEdge2 = itr.Key();
289 if(anEdgesToDelete.Contains(anEdge2)) continue;
290 if (anEdge1.IsSame(anEdge2)) continue;
291 const TopTools_ListOfShape& aList2 = itr.Value();
292 // compare lists of the neighbour faces of edge1 and edge2
293 if (aList1.Extent() == aList2.Extent()) {
294 Standard_Integer aMatches = 0;
295 for(TopTools_ListIteratorOfListOfShape aLIter1(aList1);aLIter1.More();aLIter1.Next())
296 for(TopTools_ListIteratorOfListOfShape aLIter2(aList2);aLIter2.More();aLIter2.Next())
297 if (aLIter1.Value().IsSame(aLIter2.Value())) aMatches++;
298 if (aMatches == aList1.Extent()) {
300 builder(++theTag)->Generated(anEdge2);
301 anEdgesToDelete.Add(anEdge2);
305 TopTools_MapIteratorOfMapOfShape itDelete(anEdgesToDelete);
306 for(;itDelete.More();itDelete.Next())
307 edgeNaborFaces.UnBind(itDelete.Key());
308 edgeNaborFaces.UnBind(anEdge1);
311 builder(++theTag)->Generated(anEdge1);
315 void Model_ResultBody::loadDisconnectedVertexes(boost::shared_ptr<GeomAPI_Shape> theShape, int& theTag)
317 if(theShape->isNull()) return;
318 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
319 TopTools_DataMapOfShapeListOfShape vertexNaborFaces;
320 TopTools_ListOfShape empty;
321 TopExp_Explorer explF(aShape, TopAbs_FACE);
322 for (; explF.More(); explF.Next()) {
323 const TopoDS_Shape& aFace = explF.Current();
324 TopExp_Explorer explV(aFace, TopAbs_VERTEX);
325 for (; explV.More(); explV.Next()) {
326 const TopoDS_Shape& aVertex = explV.Current();
327 if (!vertexNaborFaces.IsBound(aVertex)) vertexNaborFaces.Bind(aVertex, empty);
328 Standard_Boolean faceIsNew = Standard_True;
329 TopTools_ListIteratorOfListOfShape itrF(vertexNaborFaces.Find(aVertex));
330 for (; itrF.More(); itrF.Next()) {
331 if (itrF.Value().IsSame(aFace)) {
332 faceIsNew = Standard_False;
337 vertexNaborFaces.ChangeFind(aVertex).Append(aFace);
342 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itr(vertexNaborFaces);
343 for (; itr.More(); itr.Next()) {
344 const TopTools_ListOfShape& naborFaces = itr.Value();
345 if (naborFaces.Extent() < 3)
346 builder(++theTag)->Generated(itr.Key());