Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_ClsfBox.cxx
1 // Copyright (C) 2007-2012  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
23 // File:        GEOMAlgo_ClsfBox.cxx
24 // Created:     Wed Nov 22 10:41:47 2006
25 // Author:      Peter KURNEV
26 //              <pkv@irinox>
27 //
28 #include <GEOMAlgo_ClsfBox.hxx>
29
30 #include <GeomAbs_SurfaceType.hxx>
31 #include <GEOMAlgo_SurfaceTools.hxx>
32 #include <TopAbs_ShapeEnum.hxx>
33 #include <TopTools_IndexedMapOfShape.hxx>
34 #include <TopExp.hxx>
35 #include <TopoDS_Face.hxx>
36 #include <TopoDS.hxx>
37 #include <Geom_Surface.hxx>
38 #include <BRep_Tool.hxx>
39 #include <Geom_Plane.hxx>
40 #include <gp_Pnt.hxx>
41 #include <gp_Dir.hxx>
42 #include <gp_Pln.hxx>
43 #include <gp_Ax1.hxx>
44 #include <Geom_Plane.hxx>
45
46 IMPLEMENT_STANDARD_HANDLE(GEOMAlgo_ClsfBox, GEOMAlgo_Clsf)
47 IMPLEMENT_STANDARD_RTTIEXT(GEOMAlgo_ClsfBox, GEOMAlgo_Clsf)
48
49 //=======================================================================
50 //function :
51 //purpose  :
52 //=======================================================================
53   GEOMAlgo_ClsfBox::GEOMAlgo_ClsfBox()
54 :
55   GEOMAlgo_Clsf()
56 {
57 }
58 //=======================================================================
59 //function : ~
60 //purpose  :
61 //=======================================================================
62   GEOMAlgo_ClsfBox::~GEOMAlgo_ClsfBox()
63 {
64 }
65 //=======================================================================
66 //function : SetBox
67 //purpose  :
68 //=======================================================================
69   void GEOMAlgo_ClsfBox::SetBox(const TopoDS_Shape& aBox)
70 {
71   myBox=aBox;
72 }
73 //=======================================================================
74 //function : Box
75 //purpose  :
76 //=======================================================================
77   const TopoDS_Shape& GEOMAlgo_ClsfBox::Box() const
78 {
79   return myBox;
80 }
81 //=======================================================================
82 //function : CheckData
83 //purpose  :
84 //=======================================================================
85   void GEOMAlgo_ClsfBox::CheckData()
86 {
87   Standard_Integer i, aNbF;
88   TopAbs_ShapeEnum aTypeShape;
89   TopAbs_Orientation aOr;
90   GeomAbs_SurfaceType aType;
91   Handle(Geom_Surface) aS;
92   TopTools_IndexedMapOfShape aMF;
93   //
94   myErrorStatus=0;
95   //
96   if(myBox.IsNull()) {
97     myErrorStatus=10; // myBox=NULL
98     return;
99   }
100   //
101   aTypeShape=myBox.ShapeType();
102   if (aTypeShape!=TopAbs_SOLID) {
103     myErrorStatus=11; // unallowed shape type
104     return;
105   }
106   //
107   TopExp::MapShapes(myBox, TopAbs_FACE, aMF);
108   aNbF=aMF.Extent();
109   if (aNbF!=6) {
110     myErrorStatus=12; // wrong number of faces
111     return;
112   }
113   //
114   for (i=1; i<=aNbF; ++i) {
115     const TopoDS_Face& aF=TopoDS::Face(aMF(i));
116     aOr=aF.Orientation();
117     if (!(aOr==TopAbs_FORWARD || aOr==TopAbs_REVERSED)) {
118       myErrorStatus=12; // unallowed orientation of face
119       return;
120     }
121     //
122     aS=BRep_Tool::Surface(aF);
123     myGAS[i-1].Load(aS);
124     aType=myGAS[i-1].GetType();
125     if (!aType==GeomAbs_Plane) {
126       myErrorStatus=13; // unallowed surface type
127       return;
128     }
129     //
130     if(aOr==TopAbs_REVERSED) {
131       gp_Ax1 aAx1;
132       gp_Pln aPln;
133       gp_Pnt aP;
134       gp_Dir aD;
135       Handle(Geom_Plane) aSR;
136       //
137       aPln=myGAS[i-1].Plane();
138       aAx1=aPln.Axis();
139       aP=aAx1.Location();
140       aD=aAx1.Direction();
141       aD.Reverse();
142       aSR=new Geom_Plane(aP, aD);
143       myGAS[i-1].Load(aSR);
144     }
145   }
146 }
147 //=======================================================================
148 //function : Perform
149 //purpose  :
150 //=======================================================================
151   void GEOMAlgo_ClsfBox::Perform()
152 {
153   myErrorStatus=0;
154   //
155   const Standard_Integer aNbS=6;
156   Standard_Integer i, aNbON, aNbIN, iNext;
157   TopAbs_State aSt;
158   /*
159   CheckData();
160   if(myErrorStatus) {
161     return;
162   }
163   */
164   iNext=1;
165   aNbON=0;
166   aNbIN=0;
167   for(i=0; i<aNbS && iNext; i++) {
168     GEOMAlgo_SurfaceTools::GetState(myPnt, myGAS[i], myTolerance, aSt);
169     //
170     switch (aSt) {
171       case TopAbs_OUT:
172         myState=aSt;
173         iNext=0;
174         break;
175       case TopAbs_ON:
176         ++aNbON;
177         break;
178       case TopAbs_IN:
179         ++aNbIN;
180         break;
181       default:
182         myState=TopAbs_UNKNOWN;
183         iNext=0;
184         break;
185     }
186   }
187   //
188   if (iNext) {
189     myState=TopAbs_UNKNOWN;
190     //
191     if (aNbON && aNbIN) {
192       myState=TopAbs_ON;
193     }
194     else if (aNbIN==aNbS){
195       myState=TopAbs_IN;
196     }
197   }
198 }
199 //=======================================================================
200 //function : CanBeON
201 //purpose  :
202 //=======================================================================
203   Standard_Boolean GEOMAlgo_ClsfBox::CanBeON(const Handle(Geom_Curve)& aC) const
204 {
205   return GEOMAlgo_Clsf::CanBeON(aC);
206 }
207 //=======================================================================
208 //function : CanBeON
209 //purpose  :
210 //=======================================================================
211   Standard_Boolean GEOMAlgo_ClsfBox::CanBeON(const Handle(Geom_Surface)& aS1) const
212 {
213   Standard_Boolean bRet;
214   GeomAbs_SurfaceType  aST1;
215   GeomAdaptor_Surface aGAS1;
216   //
217   aGAS1.Load(aS1);
218   aST1=aGAS1.GetType();
219   bRet=(aST1==GeomAbs_Plane);
220   //
221   return bRet;
222 }