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