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