Salome HOME
Change comments style in geompy.py for right processing with HappyDoc
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_FinderShapeOn.cxx
1 // File:        GEOMAlgo_FinderShapeOn.cxx
2 // Created:     Tue Jan 11 14:44:31 2005
3 // Author:      Peter KURNEV
4 //              <pkv@irinox>
5
6
7 #include <GEOMAlgo_FinderShapeOn.ixx>
8
9 #include <TopAbs_ShapeEnum.hxx>
10
11 #include <TopoDS.hxx>
12 #include <TopoDS_Face.hxx>
13 #include <TopoDS_Shape.hxx>
14 #include <TopoDS_Compound.hxx>
15 #include <TopoDS_Shell.hxx>
16 #include <TopoDS_Solid.hxx>
17 #include <TopoDS_Vertex.hxx>
18 #include <TopoDS_Edge.hxx>
19
20 #include <TopoDS_Iterator.hxx>
21
22 #include <TopTools_ListIteratorOfListOfShape.hxx>
23 #include <TopTools_IndexedMapOfShape.hxx>
24 #include <TopTools_DataMapOfShapeShape.hxx>
25
26 #include <BRep_Builder.hxx>
27
28 #include <TopExp.hxx>
29 #include <TopExp_Explorer.hxx>
30
31 #include <BRepLib_MakeFace.hxx>
32 #include <BRepLib_FaceError.hxx>
33
34 #include <BOPTools_DSFiller.hxx>
35
36 #include <GEOMAlgo_WireSolid.hxx>
37 #include <GEOMAlgo_ShellSolid.hxx>
38 #include <GEOMAlgo_VertexSolid.hxx>
39 #include <GEOMAlgo_ShapeSolid.hxx>
40
41
42 //=======================================================================
43 //function : GEOMAlgo_FinderShapeOn
44 //purpose  : 
45 //=======================================================================
46 GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
47 :
48   GEOMAlgo_ShapeAlgo()
49 {
50   myTolerance=0.0001;
51   myShapeType=TopAbs_VERTEX;
52   myState=GEOMAlgo_ST_UNKNOWN; 
53 }
54 //=======================================================================
55 //function : ~
56 //purpose  : 
57 //=======================================================================
58 GEOMAlgo_FinderShapeOn::~GEOMAlgo_FinderShapeOn()
59 {
60 }
61 //=======================================================================
62 //function : SetSurface
63 //purpose  : 
64 //=======================================================================
65 void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
66 {
67   mySurface=aS;
68 }
69 //=======================================================================
70 //function : Surface
71 //purpose  : 
72 //=======================================================================
73 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
74 {
75   return mySurface;
76 }
77 //=======================================================================
78 //function : SetShapeType
79 //purpose  : 
80 //=======================================================================
81 void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
82 {
83   myShapeType=aType;
84 }
85 //=======================================================================
86 //function : ShapeType
87 //purpose  : 
88 //=======================================================================
89 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
90 {
91   return myShapeType;
92 }
93 //=======================================================================
94 //function : SetState
95 //purpose  : 
96 //=======================================================================
97 void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
98 {
99   myState=aState;
100 }
101 //=======================================================================
102 //function : State
103 //purpose  : 
104 //=======================================================================
105 GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
106 {
107   return myState;
108 }
109 //=======================================================================
110 // function: Shapes
111 // purpose: 
112 //=======================================================================
113 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
114 {
115   return myLS;
116 }
117 //=======================================================================
118 //function : Perform
119 //purpose  : 
120 //=======================================================================
121 void GEOMAlgo_FinderShapeOn::Perform()
122 {
123   myErrorStatus=0;
124   myWarningStatus=0;
125   myLS.Clear();
126   //
127   if (!myResult.IsNull()){
128     myResult.Nullify();
129   }
130   //
131   CheckData();
132   if(myErrorStatus) {
133     return;
134   }
135   //
136   MakeArguments();
137   if(myErrorStatus || myWarningStatus) {
138     return;
139   }
140   //
141   Find();
142   if(myErrorStatus) {
143     return;
144   }
145   //
146 }
147 //=======================================================================
148 //function : Find
149 //purpose  : 
150 //=======================================================================
151 void GEOMAlgo_FinderShapeOn::Find()
152 {
153   myErrorStatus=0;
154   //
155   Standard_Boolean bIsDone;
156   Standard_Integer iErr;
157   TopTools_ListIteratorOfListOfShape aIt;
158   BRep_Builder aBB;
159   BOPTools_DSFiller aDF;
160   GEOMAlgo_ShapeSolid* pSS;
161   //
162   // 1. Prepare DSFiller
163   aDF.SetShapes (myArg1, myArg2);
164   bIsDone=aDF.IsDone();
165   if (!bIsDone) {
166     myErrorStatus=30; // wrong args are used for DSFiller
167     return;
168   }
169   aDF.Perform();
170   bIsDone=aDF.IsDone();
171   if (!bIsDone) {
172     myErrorStatus=31; // DSFiller failed
173     return;
174   }
175   // 
176   // 2. Find shapes
177   myLS.Clear();
178   //
179   if (myShapeType==TopAbs_VERTEX) {
180     pSS=new GEOMAlgo_VertexSolid;
181   }
182   else if (myShapeType==TopAbs_EDGE) {
183     pSS=new GEOMAlgo_WireSolid;
184   }
185   else if (myShapeType==TopAbs_FACE) {
186     pSS=new GEOMAlgo_ShellSolid;
187   }
188   //
189   pSS->SetFiller(aDF);
190   pSS->Perform();
191   iErr=pSS->ErrorStatus();
192   if (iErr) {
193     myErrorStatus=32; // builder ShapeSolid failed
194     delete pSS;
195     return;
196   }
197   //
198   const TopTools_ListOfShape& aLS=pSS->Shapes(myState);
199   //
200   aIt.Initialize(aLS);
201   for (; aIt.More(); aIt.Next()) {
202     const TopoDS_Shape& aSImage=aIt.Value(); 
203     if (myImages.IsBound(aSImage)) { 
204       const TopoDS_Shape& aS=myImages.Find(aSImage); 
205       myLS.Append(aS);
206     }
207     else {
208       myErrorStatus=33;// can not find original shape
209       return; 
210     }
211   }
212   //
213   delete pSS;
214 }
215 //=======================================================================
216 //function : MakeArguments
217 //purpose  : 
218 //=======================================================================
219 void GEOMAlgo_FinderShapeOn::MakeArguments()
220 {
221   myErrorStatus=0;
222   //
223   Standard_Integer i, aNb;
224   BRepLib_FaceError aFErr;
225   BRepLib_MakeFace aMF;
226   TopTools_IndexedMapOfShape aM;
227   BRep_Builder aBB;
228   TopoDS_Compound aCmp;
229   TopoDS_Shell aSh;
230   TopoDS_Solid aSd;
231   TopoDS_Shape aSC;
232   TopTools_DataMapOfShapeShape aOriginals;
233   TopExp_Explorer aExp;
234   //
235   // Argument 1
236   aMF.Init(mySurface, Standard_True);
237   aFErr=aMF.Error();
238   if (aFErr!=BRepLib_FaceDone) {
239     myErrorStatus=20; // can not build the face
240     return;
241   }
242   //
243   const TopoDS_Shape& aF=aMF.Shape();
244   //
245   // update tolerance
246   aExp.Init(aF, TopAbs_VERTEX);
247   for (; aExp.More(); aExp.Next()) {
248     const TopoDS_Vertex& aV=TopoDS::Vertex(aExp.Current());
249     aBB.UpdateVertex(aV, myTolerance);
250   }
251   aExp.Init(aF, TopAbs_EDGE);
252   for (; aExp.More(); aExp.Next()) {
253     const TopoDS_Edge& aE=TopoDS::Edge(aExp.Current());
254     aBB.UpdateEdge(aE, myTolerance);
255   }
256   const TopoDS_Face& aFace=TopoDS::Face(aF);
257   aBB.UpdateFace(aFace, myTolerance);
258   //
259   // make solid
260   aBB.MakeShell(aSh);
261   aBB.Add(aSh, aFace);
262   aBB.MakeSolid(aSd);
263   aBB.Add(aSd, aSh);
264   myArg1=aSd;
265   //
266   // Argument 2
267   //
268   myImages.Clear();
269   //
270   GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
271   //
272   TopExp::MapShapes(aSC, myShapeType, aM);
273   aNb=aM.Extent();
274   if (!aNb) {
275     myWarningStatus=10; // No found subshapes of type myShapeType
276     return;
277   }
278   //
279   aBB.MakeCompound(aCmp);
280   for (i=1; i<=aNb; ++i) {
281     const TopoDS_Shape& aS=aM(i);
282     aBB.Add(aCmp, aS);
283   }
284   myArg2=aCmp;
285 }
286 //=======================================================================
287 //function : CheckData
288 //purpose  : 
289 //=======================================================================
290 void GEOMAlgo_FinderShapeOn::CheckData()
291 {
292   myErrorStatus=0;
293   //
294   if(mySurface.IsNull()) {
295     myErrorStatus=10; // mySurface=NULL
296     return;
297   }
298   //
299   if (myShape.IsNull()) {
300     myErrorStatus=11; // myShape=NULL
301     return;
302   }
303   //
304   if (!(myShapeType==TopAbs_VERTEX ||
305         myShapeType==TopAbs_EDGE ||
306         myShapeType==TopAbs_FACE)) {
307     myErrorStatus=12; // unallowed subshape type
308     return;
309   }
310   //
311   if (myState==GEOMAlgo_ST_UNKNOWN || 
312       myState==GEOMAlgo_ST_INOUT) {
313     myErrorStatus=13; // unallowed state type
314     return;
315   }
316 }
317 //
318 //=======================================================================
319 //function : CopySource
320 //purpose  : 
321 //=======================================================================
322 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
323                                         TopTools_DataMapOfShapeShape& aImages,
324                                         TopTools_DataMapOfShapeShape& aOriginals,
325                                         TopoDS_Shape& aEx)
326 {
327   Standard_Boolean bFree;
328   TopAbs_ShapeEnum aType;
329   Standard_Integer aR;
330   BRep_Builder BB;
331   TopoDS_Iterator aIt;
332   //
333   aType=aE.ShapeType();
334   //
335   if (aOriginals.IsBound(aE)) {
336     aEx=aOriginals.ChangeFind(aE);
337     if (aType==TopAbs_EDGE) {
338       return;
339     }
340   }
341   else {
342     aEx=aE.EmptyCopied();
343     aOriginals.Bind(aE, aEx);
344     aImages.Bind(aEx, aE);
345   }
346   //
347   aR=(Standard_Integer)aType+1;
348   if (aR>TopAbs_VERTEX) {
349     return;
350   }
351   //
352   bFree=aEx.Free();
353   aEx.Free(Standard_True);
354   //
355   aType=(TopAbs_ShapeEnum) aR;
356   //
357   aIt.Initialize(aE);//, Standard_False);
358   for (; aIt.More();  aIt.Next()) {
359     const TopoDS_Shape& aV=aIt.Value();
360     TopoDS_Shape aVx;
361     //
362     CopySource (aV, aImages, aOriginals, aVx);  
363     //
364     aVx.Orientation(aV.Orientation());
365     BB.Add(aEx, aVx);
366   }
367   //
368   aEx.Free(bFree);
369 }
370
371 //
372 // myErrorStatus :
373 //
374 // 10 -mySurface=NULL
375 // 11 -myShape=NULL
376 // 12 -unallowed type of subshapes 
377 // 13 -unallowed state  
378 // 20 -can not build the face
379 // 30 -wrong args are used for DSFiller
380 // 31 -DSFiller failed
381 // 32 -builder ShapeSolid failed
382 // 33 -can not find original shape
383 //
384 // myWarningStatus
385 //
386 // 10 -subshapes of type myShapeType can not be fond in myShape
387