]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMAlgo/GEOMAlgo_FinderShapeOn.cxx
Salome HOME
Win32 compliation.
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_FinderShapeOn.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 // File:        GEOMAlgo_FinderShapeOn.cxx
23 // Created:     Tue Jan 11 14:44:31 2005
24 // Author:      Peter KURNEV
25
26 #include <GEOMAlgo_FinderShapeOn.ixx>
27
28 #include <Basics_OCCTVersion.hxx>
29
30 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
31 #include <Precision.hxx>
32 #endif
33
34 #include <gp_Pnt.hxx>
35
36 #include <TopAbs_ShapeEnum.hxx>
37 #include <TopAbs_Orientation.hxx>
38
39 #include <TopoDS.hxx>
40 #include <TopoDS_Face.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <TopoDS_Shell.hxx>
44 #include <TopoDS_Solid.hxx>
45 #include <TopoDS_Vertex.hxx>
46 #include <TopoDS_Edge.hxx>
47 #include <TopoDS_Iterator.hxx>
48
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_IndexedMapOfShape.hxx>
51 #include <TopTools_DataMapOfShapeShape.hxx>
52
53 #include <Bnd_Box.hxx>
54 #include <BRepBndLib.hxx>
55 #include <BRepMesh_IncrementalMesh.hxx>
56
57 #include <BRep_Builder.hxx>
58 #include <BRep_Tool.hxx>
59
60 #include <TopExp.hxx>
61 #include <TopExp_Explorer.hxx>
62
63 #include <BRepLib_MakeFace.hxx>
64 #include <BRepLib_FaceError.hxx>
65
66 #include <BOPTools_DSFiller.hxx>
67
68 #include <GEOMAlgo_WireSolid.hxx>
69 #include <GEOMAlgo_ShellSolid.hxx>
70 #include <GEOMAlgo_VertexSolid.hxx>
71 #include <GEOMAlgo_ShapeSolid.hxx>
72 #include <GEOMAlgo_SolidSolid.hxx>
73 #include <GEOMAlgo_SurfaceTools.hxx>
74 #include <GEOMAlgo_Tools.hxx>
75
76 //=======================================================================
77 //function : GEOMAlgo_FinderShapeOn
78 //purpose  : 
79 //=======================================================================
80 GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
81 :
82   GEOMAlgo_ShapeAlgo()
83 {
84   myTolerance=0.0001;
85   myShapeType=TopAbs_VERTEX;
86   myState=GEOMAlgo_ST_UNKNOWN;
87   myIsAnalytic=Standard_True;
88 }
89 //=======================================================================
90 //function : ~
91 //purpose  : 
92 //=======================================================================
93 GEOMAlgo_FinderShapeOn::~GEOMAlgo_FinderShapeOn()
94 {
95 }
96 //=======================================================================
97 //function : SetSurface
98 //purpose  : 
99 //=======================================================================
100 void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
101 {
102   mySurface=aS;
103 }
104 //=======================================================================
105 //function : Surface
106 //purpose  : 
107 //=======================================================================
108 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
109 {
110   return mySurface;
111 }
112 //=======================================================================
113 //function : SetShapeType
114 //purpose  : 
115 //=======================================================================
116 void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
117 {
118   myShapeType=aType;
119 }
120 //=======================================================================
121 //function : ShapeType
122 //purpose  : 
123 //=======================================================================
124 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
125 {
126   return myShapeType;
127 }
128 //=======================================================================
129 //function : SetState
130 //purpose  : 
131 //=======================================================================
132 void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
133 {
134   myState=aState;
135 }
136 //=======================================================================
137 //function : State
138 //purpose  : 
139 //=======================================================================
140 GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
141 {
142   return myState;
143 }
144 //=======================================================================
145 // function: Shapes
146 // purpose: 
147 //=======================================================================
148 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
149 {
150   Standard_Boolean bIsConformState;
151   Standard_Integer i, aNb;
152   TopAbs_State aSt;
153   TopTools_ListOfShape* pL;
154   //
155   pL=(TopTools_ListOfShape*) &myLS;
156   pL->Clear();
157   //
158   aNb=myMSS.Extent();
159   for (i=1; i<=aNb; ++i) {
160     const TopoDS_Shape& aS=myMSS.FindKey(i);
161     aSt=myMSS.FindFromIndex(i);
162     //
163     bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
164     if (bIsConformState) {
165       pL->Append(aS);
166     }
167   }
168   return myLS;
169 }
170 //=======================================================================
171 //function : Perform
172 //purpose  : 
173 //=======================================================================
174 void GEOMAlgo_FinderShapeOn::Perform()
175 {
176   myErrorStatus=0;
177   myWarningStatus=0;
178   myLS.Clear();
179   myMSS.Clear();
180   //
181   if (!myResult.IsNull()){
182     myResult.Nullify();
183   }
184   //
185   CheckData();
186   if(myErrorStatus) {
187     return;
188   }
189   //
190 #if OCC_VERSION_LARGE > 0x06050200
191   // Initialize the context
192   GEOMAlgo_ShapeAlgo::Perform();
193 #endif
194   //
195   myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
196   //
197   MakeArgument1();
198   if(myErrorStatus) {
199     return;
200   }
201   //
202   if (myIsAnalytic && myShapeType==TopAbs_VERTEX) {
203     FindVertices();
204     return;
205   }
206   //
207   MakeArgument2();
208   if(myErrorStatus) {
209     return;
210   }
211   //
212   Find();
213   if(myErrorStatus || myWarningStatus) {
214     return;
215   }
216   //
217 }
218 //=======================================================================
219 //function : FindVertices
220 //purpose  : 
221 //=======================================================================
222 void GEOMAlgo_FinderShapeOn::FindVertices()
223
224   Standard_Integer i, aNb, iErr;
225   TopAbs_State aSt;
226   TopAbs_Orientation aOr;
227   gp_Pnt aP;
228   TopTools_IndexedMapOfShape aM;
229   //
230   TopExp::MapShapes(myArg1, TopAbs_FACE, aM);
231   const TopoDS_Face& aF=TopoDS::Face(aM(1));
232   aOr=aF.Orientation();
233   //
234   aM.Clear();
235   TopExp::MapShapes(myShape, myShapeType, aM);
236   aNb=aM.Extent();
237   if (!aNb) {
238     myWarningStatus=10; // No found sub-shapes of type myShapeType
239     return;
240   }
241   //
242   for (i=1; i<=aNb; ++i) {
243     const TopoDS_Shape& aS=aM(i);
244     const TopoDS_Vertex& aV=TopoDS::Vertex(aS);
245     aP=BRep_Tool::Pnt(aV);
246     iErr=GEOMAlgo_SurfaceTools::GetState(aP, mySurface, myTolerance, aSt);
247     if (aOr==TopAbs_REVERSED) {
248       aSt=GEOMAlgo_SurfaceTools::ReverseState(aSt);
249     }
250     myMSS.Add(aS, aSt);
251   }
252 }
253 //=======================================================================
254 //function : Find
255 //purpose  : 
256 //=======================================================================
257 void GEOMAlgo_FinderShapeOn::Find()
258 {
259   Standard_Integer i, aNb;
260   Standard_Boolean bICS;
261   TopTools_IndexedMapOfShape aM;
262   //
263   TopExp::MapShapes(myArg2, myShapeType, aM);
264   //
265   aNb=aM.Extent();
266   if (!aNb) {
267     myWarningStatus=10; // No found sub-shapes of type myShapeType
268     return;
269   }
270   //
271   bICS=GEOMAlgo_Tools::IsCompositeShape(myArg2);
272   if (!bICS || myIsAnalytic) {
273     TopoDS_Compound aCmp;
274     BRep_Builder aBB;
275     //
276     aBB.MakeCompound(aCmp);
277     for (i=1; i<=aNb; ++i) {
278       const TopoDS_Shape& aSi=aM(i);
279       aBB.Add(aCmp, aSi);
280     }
281     //
282     aM.Clear();
283     aM.Add(aCmp);
284     aNb=1;
285   }
286   //
287   for (i=1; i<=aNb; ++i) {
288     const TopoDS_Shape& aS=aM(i);
289     Find(aS);
290     if (myErrorStatus) {
291       return;
292     }
293   }
294 }
295 //=======================================================================
296 //function : Find
297 //purpose  : 
298 //=======================================================================
299 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
300 {
301   myErrorStatus=0;
302   //
303   Standard_Boolean bIsDone;
304   Standard_Integer i, iErr;
305   TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
306   TopTools_ListIteratorOfListOfShape aIt;
307   BOPTools_DSFiller aDF;
308   //
309   // 1. Prepare DSFiller
310   aDF.SetShapes (myArg1, aS);
311   bIsDone=aDF.IsDone();
312   if (!bIsDone) {
313     myErrorStatus=30; // wrong args are used for DSFiller
314     return;
315   }
316   aDF.Perform();
317   bIsDone=aDF.IsDone();
318   if (!bIsDone) {
319     myErrorStatus=31; // DSFiller failed
320     return;
321   }
322   // 
323   // 2. Find shapes
324   GEOMAlgo_ShapeSolid* pSS;
325   GEOMAlgo_VertexSolid aVXS;
326   GEOMAlgo_WireSolid aWRS;
327   GEOMAlgo_ShellSolid aSHS;
328   GEOMAlgo_SolidSolid aSLS;
329   //
330   pSS=NULL;
331   //
332   switch (myShapeType) {
333     case TopAbs_VERTEX:
334       pSS=&aVXS;
335       break;
336     case TopAbs_EDGE:
337       pSS=&aWRS;
338       break;
339     case TopAbs_FACE:
340       pSS=&aSHS;
341       break;
342     case TopAbs_SOLID:
343       aSLS.SetShape2(myArg2);
344       pSS=&aSLS;
345       break;
346     default:
347       myErrorStatus=12; // unallowed sub-shape type
348       return;
349   }
350   //
351   pSS->SetFiller(aDF);
352   pSS->Perform();
353   iErr=pSS->ErrorStatus();
354   if (iErr) {
355     myErrorStatus=32; // builder ShapeSolid failed
356     return;
357   }
358   //
359   for (i=0; i<3; ++i) {
360     const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
361     aIt.Initialize(aLS);
362     for (; aIt.More(); aIt.Next()) {
363       const TopoDS_Shape& aSImage=aIt.Value(); 
364       if (myImages.IsBound(aSImage)) { 
365         const TopoDS_Shape& aSx=myImages.Find(aSImage); 
366         myMSS.Add(aSx, aSts[i]);
367       }
368       else {
369         myErrorStatus=33;// can not find original shape
370         return; 
371       }
372     }
373   }
374 }
375 //=======================================================================
376 //function : MakeArgument1
377 //purpose  : 
378 //=======================================================================
379 void GEOMAlgo_FinderShapeOn::MakeArgument1()
380 {
381   myErrorStatus=0;
382   //
383   Standard_Integer i, aNb;
384   TopAbs_ShapeEnum aType;
385   BRepLib_FaceError aFErr;
386   BRepLib_MakeFace aMF;
387   TopTools_IndexedMapOfShape aM;
388   BRep_Builder aBB;
389   TopoDS_Face aFace;
390   TopoDS_Shell aSh;
391   TopoDS_Solid aSd;
392   //
393   // Argument 1
394   if (!myIsAnalytic) {
395 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
396     aMF.Init(mySurface, Standard_True, Precision::Confusion());
397 #else
398     aMF.Init(mySurface, Standard_True);
399 #endif
400     aFErr=aMF.Error();
401     if (aFErr!=BRepLib_FaceDone) {
402       myErrorStatus=20; // can not build the face
403       return;
404     }
405     //
406     const TopoDS_Shape& aF=aMF.Shape();
407     aFace=TopoDS::Face(aF);
408     //
409     // update tolerances
410     aM.Add(aF);
411     TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
412     TopExp::MapShapes(aF, TopAbs_EDGE, aM);
413     aNb=aM.Extent();
414     for (i=1; i<=aNb; ++i) {
415       const TopoDS_Shape& aS=aM(i);
416       aType=aS.ShapeType();
417       switch (aType) {
418       case TopAbs_VERTEX: {
419         const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
420         aBB.UpdateVertex(aVx, myTolerance);
421       }
422         break;
423       case TopAbs_EDGE: {
424         const TopoDS_Edge& aEx=TopoDS::Edge(aS);
425         aBB.UpdateEdge(aEx, myTolerance);
426       }
427         break;
428       case TopAbs_FACE: {
429         const TopoDS_Face& aFx=TopoDS::Face(aS);
430         aBB.UpdateFace(aFx, myTolerance);
431       }
432         break;
433       default:
434         break;
435       }
436     }
437   } //  
438   else {
439     aBB.MakeFace(aFace, mySurface, myTolerance);
440   }
441   //
442   // make solid
443   aBB.MakeShell(aSh);
444   aBB.Add(aSh, aFace);
445   aBB.MakeSolid(aSd);
446   aBB.Add(aSd, aSh);
447   myArg1=aSd;
448 }
449 //=======================================================================
450 //function : MakeArgument2
451 //purpose  : 
452 //=======================================================================
453 void GEOMAlgo_FinderShapeOn::MakeArgument2()
454 {
455   myErrorStatus=0;
456   //
457   TopoDS_Shape aSC;
458   TopTools_DataMapOfShapeShape aOriginals;
459   //
460   myImages.Clear();
461   //
462   GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
463   //
464   myArg2=aSC;
465 }
466 //=======================================================================
467 //function : CheckData
468 //purpose  : 
469 //=======================================================================
470 void GEOMAlgo_FinderShapeOn::CheckData()
471 {
472   myErrorStatus=0;
473   //
474   if(mySurface.IsNull()) {
475     myErrorStatus=10; // mySurface=NULL
476     return;
477   }
478   //
479   if (myShape.IsNull()) {
480     myErrorStatus=11; // myShape=NULL
481     return;
482   }
483   //
484   if (!(myShapeType==TopAbs_VERTEX ||
485         myShapeType==TopAbs_EDGE ||
486         myShapeType==TopAbs_FACE ||
487         myShapeType==TopAbs_SOLID)) {
488     myErrorStatus=12; // unallowed sub-shape type
489     return;
490   }
491   //
492   if (myState==GEOMAlgo_ST_UNKNOWN || 
493       myState==GEOMAlgo_ST_INOUT) {
494     myErrorStatus=13; // unallowed state type
495     return;
496   }
497 }
498 //
499 //=======================================================================
500 //function : CopySource
501 //purpose  : 
502 //=======================================================================
503 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
504                                         TopTools_DataMapOfShapeShape& aImages,
505                                         TopTools_DataMapOfShapeShape& aOriginals,
506                                         TopoDS_Shape& aEx)
507 {
508   Standard_Boolean bFree;
509   TopAbs_ShapeEnum aType;
510   Standard_Integer aR;
511   BRep_Builder BB;
512   TopoDS_Iterator aIt;
513   //
514   aType=aE.ShapeType();
515   //
516   if (aOriginals.IsBound(aE)) {
517     aEx=aOriginals.ChangeFind(aE);
518     return;
519   }
520   else {
521     aEx=aE.EmptyCopied();
522     aOriginals.Bind(aE, aEx);
523     aImages.Bind(aEx, aE);
524   }
525   //
526   aR=(Standard_Integer)aType+1;
527   if (aR>TopAbs_VERTEX) {
528     return;
529   }
530   //
531   bFree=aEx.Free();
532   aEx.Free(Standard_True);
533   //
534   aType=(TopAbs_ShapeEnum) aR;
535   //
536   aIt.Initialize(aE);//, Standard_False);
537   for (; aIt.More();  aIt.Next()) {
538     const TopoDS_Shape& aV=aIt.Value();
539     TopoDS_Shape aVx;
540     //
541     CopySource (aV, aImages, aOriginals, aVx);  
542     //
543     aVx.Orientation(aV.Orientation());
544     BB.Add(aEx, aVx);
545   }
546   //
547   aEx.Free(bFree);
548 }
549 //
550 //=======================================================================
551 //function : BuildTriangulation
552 //purpose  :
553 //=======================================================================
554 Standard_Boolean GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
555 {
556   // calculate deflection
557   Standard_Real aDeviationCoefficient = 0.001;
558
559   Bnd_Box B;
560   BRepBndLib::Add(theShape, B);
561   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
562   B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
563
564   Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
565   Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
566   Standard_Real aHLRAngle = 0.349066;
567
568   // build triangulation
569   BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
570
571   // check triangulation
572   bool isTriangulation = true;
573
574   TopExp_Explorer exp (theShape, TopAbs_FACE);
575   if (exp.More())
576   {
577     TopLoc_Location aTopLoc;
578     Handle(Poly_Triangulation) aTRF;
579     aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
580     if (aTRF.IsNull()) {
581       isTriangulation = false;
582     }
583   }
584   else // no faces, try edges
585   {
586     TopExp_Explorer expe (theShape, TopAbs_EDGE);
587     if (!expe.More()) {
588       isTriangulation = false;
589     }
590     else {
591       TopLoc_Location aLoc;
592       Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
593       if (aPE.IsNull()) {
594         isTriangulation = false;
595       }
596     }
597   }
598
599   return isTriangulation;
600 }
601
602 //
603 // myErrorStatus :
604 //
605 // 10 -mySurface=NULL
606 // 11 -myShape=NULL
607 // 12 -unallowed type of sub-shapes 
608 // 13 -unallowed state  
609 // 20 -can not build the face
610 // 30 -wrong args are used for DSFiller
611 // 31 -DSFiller failed
612 // 32 -builder ShapeSolid failed
613 // 33 -can not find original shape
614 //
615 // myWarningStatus
616 //
617 // 10 - sub-shapes of type myShapeType can not be fond in myShape