]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_ResultBody.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / Model / Model_ResultBody.cpp
1 // File:        Model_ResultBody.cpp
2 // Created:     08 Jul 2014
3 // Author:      Mikhail PONIKAROV
4
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 <GeomAPI_Shape.h>
14 #include <GeomAlgoAPI_MakeShape.h>
15 // DEB
16 //#include <TCollection_AsciiString.hxx>
17 //#include <TDF_Tool.hxx>
18 Model_ResultBody::Model_ResultBody()
19 {
20   setIsConcealed(false);
21 }
22
23 void Model_ResultBody::store(const boost::shared_ptr<GeomAPI_Shape>& theShape)
24 {
25   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
26   if (aData) {
27     TDF_Label& aShapeLab = aData->shapeLab();
28     // clean builders
29     clean();   
30     // store the new shape as primitive
31     TNaming_Builder aBuilder(aShapeLab);
32     if (!theShape)
33       return;  // bad shape
34     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
35     if (aShape.IsNull())
36       return;  // null shape inside
37
38     aBuilder.Generated(aShape);
39   }
40 }
41
42 void Model_ResultBody::storeGenerated(const boost::shared_ptr<GeomAPI_Shape>& theFromShape,
43                                           const boost::shared_ptr<GeomAPI_Shape>& theToShape)
44 {
45   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
46   if (aData) {
47     TDF_Label& aShapeLab = aData->shapeLab();
48     // clean builders
49     clean();   
50     // store the new shape as primitive
51     TNaming_Builder aBuilder(aShapeLab);
52     if (!theFromShape || !theToShape)
53       return;  // bad shape
54     TopoDS_Shape aShapeBasis = theFromShape->impl<TopoDS_Shape>();
55     if (aShapeBasis.IsNull())
56       return;  // null shape inside
57         TopoDS_Shape aShapeNew = theToShape->impl<TopoDS_Shape>();
58     if (aShapeNew.IsNull())
59       return;  // null shape inside
60     aBuilder.Generated(aShapeBasis, aShapeNew);
61   }
62 }
63
64 void Model_ResultBody::storeModified(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
65                                           const boost::shared_ptr<GeomAPI_Shape>& theNewShape)
66 {
67   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
68   if (aData) {
69     TDF_Label& aShapeLab = aData->shapeLab();
70     // clean builders
71     clean();   
72     // store the new shape as primitive
73     TNaming_Builder aBuilder(aShapeLab);
74     if (!theOldShape || !theNewShape)
75       return;  // bad shape
76     TopoDS_Shape aShapeOld = theOldShape->impl<TopoDS_Shape>();
77     if (aShapeOld.IsNull())
78       return;  // null shape inside
79         TopoDS_Shape aShapeNew = theNewShape->impl<TopoDS_Shape>();
80     if (aShapeNew.IsNull())
81       return;  // null shape inside
82     aBuilder.Generated(aShapeOld, aShapeNew);
83   }
84 }
85
86 boost::shared_ptr<GeomAPI_Shape> Model_ResultBody::shape()
87 {
88   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(data());
89   if (aData) {
90     TDF_Label& aShapeLab = aData->shapeLab();
91     Handle(TNaming_NamedShape) aName;
92     if (aShapeLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
93       TopoDS_Shape aShape = aName->Get();
94       if (!aShape.IsNull()) {
95         boost::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
96         aRes->setImpl(new TopoDS_Shape(aShape));
97         return aRes;
98       }
99     }
100   }
101   return boost::shared_ptr<GeomAPI_Shape>();
102 }
103
104 boost::shared_ptr<ModelAPI_Feature> Model_ResultBody::owner()
105 {
106   return myOwner;
107 }
108
109 void Model_ResultBody::clean()
110 {
111   std::vector<TNaming_Builder*>::iterator aBuilder = myBuilders.begin();
112   for(; aBuilder != myBuilders.end(); aBuilder++)
113     delete *aBuilder;
114   myBuilders.clear();
115 }
116
117 Model_ResultBody::~Model_ResultBody()
118 {
119   clean();
120 }
121
122 TNaming_Builder* Model_ResultBody::builder(const int theTag)
123 {
124   if (myBuilders.size() <= (unsigned int)theTag) {
125     myBuilders.insert(myBuilders.end(), theTag - myBuilders.size() + 1, NULL);
126   }
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;
133   }
134   return myBuilders[theTag];
135 }
136
137 void Model_ResultBody::generated(
138   const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
139 {
140   TopoDS_Shape aShape = theNewShape->impl<TopoDS_Shape>();
141   builder(theTag)->Generated(aShape);
142 }
143
144 void Model_ResultBody::generated(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
145     const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
146 {
147   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
148   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
149   builder(theTag)->Generated(anOldShape, aNewShape);
150 }
151
152
153 void Model_ResultBody::modified(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
154     const boost::shared_ptr<GeomAPI_Shape>& theNewShape, const int theTag)
155 {
156   TopoDS_Shape anOldShape = theOldShape->impl<TopoDS_Shape>();
157   TopoDS_Shape aNewShape = theNewShape->impl<TopoDS_Shape>();
158   builder(theTag)->Modify(anOldShape, aNewShape);
159 }
160
161 void Model_ResultBody::deleted(const boost::shared_ptr<GeomAPI_Shape>& theOldShape,
162     const int theTag)
163 {
164   TopoDS_Shape aShape = theOldShape->impl<TopoDS_Shape>();
165   builder(theTag)->Delete(aShape);
166 }
167
168 void Model_ResultBody::loadDeletedShapes (GeomAlgoAPI_MakeShape* theMS,
169                                                boost::shared_ptr<GeomAPI_Shape>  theShapeIn,
170                                                const int  theKindOfShape,
171                                                const int  theTag)
172 {
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);
183     }
184   }
185 }
186
187 void Model_ResultBody::loadAndOrientModifiedShapes (
188                                                    GeomAlgoAPI_MakeShape* theMS,
189                                                boost::shared_ptr<GeomAPI_Shape>  theShapeIn,
190                                                const int  theKindOfShape,
191                                                const int  theTag,
192                                                GeomAPI_DataMapOfShapeShape& theSubShapes)
193 {
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;
200         ListOfShape aList;
201         boost::shared_ptr<GeomAPI_Shape> aRShape(new GeomAPI_Shape());
202         aRShape->setImpl((new TopoDS_Shape(aRoot)));
203         theMS->generated(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());
210       }
211       if (!aRoot.IsSame (aNewShape)) 
212                   builder(theTag)->Modify(aRoot,aNewShape);
213     }
214   }
215 }
216
217 void Model_ResultBody::loadAndOrientGeneratedShapes (
218                                                    GeomAlgoAPI_MakeShape* theMS,
219                                                boost::shared_ptr<GeomAPI_Shape>  theShapeIn,
220                                                const int  theKindOfShape,
221                                                const int  theTag,
222                                                GeomAPI_DataMapOfShapeShape& theSubShapes)
223 {
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;
230         ListOfShape aList;
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());
240       }
241       if (!aRoot.IsSame (aNewShape)) 
242                   builder(theTag)->Generated(aRoot,aNewShape);
243     }
244   }
245 }