0024002: Overall code and build procedure refactoring -- automatic
[occt.git] / src / BOPTest / BOPTest_LowCommands.cxx
1 // Created on: 2001-03-28
2 // Created by: Peter KURNEV
3 // Copyright (c) 2001-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <BOPTest.hxx>
18 #include <BOPTools_AlgoTools2D.hxx>
19 #include <BRep_CurveRepresentation.hxx>
20 #include <BRep_GCurve.hxx>
21 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
22 #include <BRep_TEdge.hxx>
23 #include <BRep_Tool.hxx>
24 #include <BRepClass3d_SolidClassifier.hxx>
25 #include <BRepClass_FaceClassifier.hxx>
26 #include <DBRep.hxx>
27 #include <Draw.hxx>
28 #include <DrawTrSurf.hxx>
29 #include <gp_Pnt.hxx>
30 #include <gp_Pnt2d.hxx>
31 #include <IntTools_FClass2d.hxx>
32 #include <TCollection_AsciiString.hxx>
33 #include <TopAbs_State.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Shape.hxx>
36
37 #include <stdio.h>
38 static
39   void PrintState (Draw_Interpretor& aDI,
40                    const TopAbs_State& aState);
41 static
42   Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E, 
43                                       const TopoDS_Face& F,
44                                       Standard_Real& First,
45                                       Standard_Real& Last);
46 static
47   Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E, 
48                                       const Handle(Geom_Surface)& S,
49                                       const TopLoc_Location& L,
50                                       Standard_Real& First,
51                                       Standard_Real& Last);
52
53 static  Standard_Integer bclassify   (Draw_Interpretor& , Standard_Integer , const char** );
54 static  Standard_Integer b2dclassify (Draw_Interpretor& , Standard_Integer , const char** );
55 static  Standard_Integer b2dclassifx (Draw_Interpretor& , Standard_Integer , const char** );
56 static  Standard_Integer bhaspc      (Draw_Interpretor& , Standard_Integer , const char** );
57
58 //=======================================================================
59 //function : LowCommands
60 //purpose  : 
61 //=======================================================================
62   void  BOPTest::LowCommands(Draw_Interpretor& theCommands)
63 {
64   static Standard_Boolean done = Standard_False;
65   if (done) return;
66   done = Standard_True;
67   // Chapter's name
68   const char* g = "BOPTest commands";
69   theCommands.Add("bclassify"    , "use bclassify Solid Point [Tolerance=1.e-7]",
70                   __FILE__, bclassify   , g);
71   theCommands.Add("b2dclassify"  , "use b2dclassify Face Point2d [Tol] ",
72                   __FILE__, b2dclassify , g);
73   theCommands.Add("b2dclassifx"  , "use b2dclassifx Face Point2d [Tol] ",
74                   __FILE__, b2dclassifx , g);
75   theCommands.Add("bhaspc"       , "use bhaspc Edge Face [do]",
76                   __FILE__, bhaspc      , g);
77 }
78
79
80 //
81 //=======================================================================
82 //function : b2dclassifx
83 //purpose  : 
84 //=======================================================================
85 Standard_Integer b2dclassifx (Draw_Interpretor& theDI,
86                               Standard_Integer  theArgNb,
87                               const char**      theArgVec)
88 {
89   if (theArgNb < 3)  {
90     theDI << " use b2dclassifx Face Point2d [Tol]\n";
91     return 1;
92   }
93
94   TopoDS_Shape aS = DBRep::Get (theArgVec[1]);
95   if (aS.IsNull())  {
96     theDI << " Null Shape is not allowed here\n";
97     return 1;
98   }
99   else if (aS.ShapeType() != TopAbs_FACE)  {
100     theDI << " Shape type must be FACE\n";
101     return 1;
102   }
103   TopAbs_State aState;
104   gp_Pnt2d aP (8., 9.);
105   //
106   DrawTrSurf::GetPoint2d (theArgVec[2], aP);
107   const TopoDS_Face&  aF   = TopoDS::Face(aS);
108   const Standard_Real aTol = (theArgNb == 4) ? 
109     Draw::Atof (theArgVec[3]) : BRep_Tool::Tolerance (aF);
110   //
111   IntTools_FClass2d aClassifier(aF, aTol);
112   aState=aClassifier.Perform(aP);
113   PrintState (theDI, aState);
114   //
115   return 0;
116 }
117 //
118 //=======================================================================
119 //function : b2dclassify
120 //purpose  : 
121 //=======================================================================
122 Standard_Integer b2dclassify (Draw_Interpretor& theDI,
123                               Standard_Integer  theArgNb,
124                               const char**      theArgVec)
125 {
126   if (theArgNb < 3)  {
127     theDI << " use b2dclassify Face Point2d [Tol]\n";
128     return 1;
129   }
130
131   TopoDS_Shape aS = DBRep::Get (theArgVec[1]);
132   if (aS.IsNull())  {
133     theDI << " Null Shape is not allowed here\n";
134     return 1;
135   }
136   else if (aS.ShapeType() != TopAbs_FACE)  {
137     theDI << " Shape type must be FACE\n";
138     return 1;
139   }
140   //
141   gp_Pnt2d aP (8., 9.);
142   //
143   DrawTrSurf::GetPoint2d (theArgVec[2], aP);
144   const TopoDS_Face&  aF   = TopoDS::Face(aS);
145   const Standard_Real aTol = (theArgNb == 4) ? 
146     Draw::Atof (theArgVec[3]) : BRep_Tool::Tolerance (aF);
147   
148   BRepClass_FaceClassifier aClassifier;
149   aClassifier.Perform(aF, aP, aTol);
150   PrintState (theDI, aClassifier.State());
151   //
152   return 0;
153 }
154
155 //=======================================================================
156 //function : bclassify
157 //purpose  : 
158 //=======================================================================
159 Standard_Integer bclassify (Draw_Interpretor& theDI,
160                             Standard_Integer  theArgNb,
161                             const char**      theArgVec)
162 {
163   if (theArgNb < 3)  {
164     theDI << " use bclassify Solid Point [Tolerance=1.e-7]\n";
165     return 1;
166   }
167
168   TopoDS_Shape aS = DBRep::Get (theArgVec[1]);
169   if (aS.IsNull())  {
170     theDI << " Null Shape is not allowed\n";
171     return 1;
172   }
173   else if (aS.ShapeType() != TopAbs_SOLID)  {
174     theDI << " Shape type must be SOLID\n";
175     return 1;
176   }
177
178   gp_Pnt aP (8., 9., 10.);
179   DrawTrSurf::GetPoint (theArgVec[2], aP);
180   const Standard_Real aTol = (theArgNb == 4) ? 
181     Draw::Atof (theArgVec[3]) : 1.e-7;
182
183   BRepClass3d_SolidClassifier aSC (aS);
184   aSC.Perform (aP,aTol);
185
186   PrintState (theDI, aSC.State());
187   return 0;
188 }
189
190 //=======================================================================
191 //function : bhaspc
192 //purpose  : 
193 //=======================================================================
194 Standard_Integer bhaspc (Draw_Interpretor& di, 
195                          Standard_Integer n, 
196                          const char** a)
197 {
198   if (n<3) {
199     di << " use bhaspc Edge Face [do]\n";
200     return 1;
201   }
202
203   TopoDS_Shape S1 = DBRep::Get(a[1]);
204   TopoDS_Shape S2 = DBRep::Get(a[2]);
205   
206   if (S1.IsNull() || S2.IsNull()) {
207     di << " Null shapes are not allowed \n";
208     return 1;
209   }
210   if (S1.ShapeType()!=TopAbs_EDGE || S2.ShapeType()!=TopAbs_FACE) {
211     di << " Type mismatch\n";
212     return 1;
213   }
214   //
215   const TopoDS_Edge& aE=TopoDS::Edge(S1);
216   const TopoDS_Face& aF=TopoDS::Face(S2);
217   Standard_Real f2D, l2D; 
218
219   Handle(Geom2d_Curve) C2D=CurveOnSurface(aE, aF, f2D, l2D);
220   
221   if (C2D.IsNull()) {
222     di << " No 2D Curves detected\n";
223   }
224   else {
225     di << " Ok Edge has P-Curve on this Face\n";
226   }
227   
228   if (n==4) {
229     if (!strcmp(a[3], "do")) {
230       BOPTools_AlgoTools2D::BuildPCurveForEdgeOnFace(aE, aF);  
231     }
232   }
233
234   return 0;
235 }
236 //=======================================================================
237 //function : PrintState
238 //purpose  :
239 //=======================================================================
240 void PrintState (Draw_Interpretor&   theDI,
241                  const TopAbs_State& theState)
242 {
243   switch (theState) {
244    case TopAbs_IN:       
245     theDI << "The point is IN shape\n";      
246     break;
247    case TopAbs_OUT:  
248     theDI << "The point is OUT of shape\n";  
249     break;
250    case TopAbs_ON: 
251     theDI << "The point is ON shape\n";      
252     break;
253    case TopAbs_UNKNOWN:
254    default:              
255     theDI << "The point is UNKNOWN shape\n"; 
256     break;
257   }
258 }
259 //=======================================================================
260 //function : CurveOnSurface
261 //purpose  : 
262 //=======================================================================
263 Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E, 
264                                     const TopoDS_Face& F,
265                                     Standard_Real& First,
266                                     Standard_Real& Last)
267 {
268   TopLoc_Location l;
269   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,l);
270   TopoDS_Edge aLocalEdge = E;
271   if (F.Orientation() == TopAbs_REVERSED) {
272     aLocalEdge.Reverse();
273   }
274   return CurveOnSurface(aLocalEdge,S,l,First,Last);
275 }
276
277 static Handle(Geom2d_Curve) nullPCurve;
278 //=======================================================================
279 //function : CurveOnSurface
280 //purpose  : 
281 //=======================================================================
282 Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E, 
283                                     const Handle(Geom_Surface)& S,
284                                     const TopLoc_Location& L,
285                                     Standard_Real& First,
286                                     Standard_Real& Last)
287 {
288   TopLoc_Location l = L.Predivided(E.Location());
289   Standard_Boolean Eisreversed = (E.Orientation() == TopAbs_REVERSED);
290
291   // find the representation
292   BRep_ListIteratorOfListOfCurveRepresentation itcr
293     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
294
295   while (itcr.More()) {
296     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
297     if (cr->IsCurveOnSurface(S,l)) {
298       Handle(BRep_GCurve) GC (Handle(BRep_GCurve)::DownCast (cr));
299       GC->Range(First,Last);
300       if (GC->IsCurveOnClosedSurface() && Eisreversed)
301         return GC->PCurve2();
302       else
303         return GC->PCurve();
304     }
305     itcr.Next();
306   }
307   return nullPCurve;
308 }