69c8f357cf437da922661c44ddb183cb100b9d6e
[occt.git] / src / BOPTest / BOPTest_LowCommands.cxx
1 // Created on: 2001-03-28
2 // Created by: Peter KURNEV
3 // Copyright (c) 2001-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 #include <BOPTest.ixx>
22
23 #include <stdio.h>
24
25 #include <gp_Pnt2d.hxx>
26 #include <gp_Pnt.hxx>
27
28 #include <TopoDS.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopAbs_State.hxx>
31
32 #include <TCollection_AsciiString.hxx>
33
34 #include <BRepClass3d_SolidClassifier.hxx>
35 #include <BRepClass_FaceClassifier.hxx>
36
37 #include <BRep_Tool.hxx>
38 #include <BRep_CurveRepresentation.hxx>
39 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
40 #include <BRep_TEdge.hxx>
41 #include <BRep_GCurve.hxx>
42
43 #include <Draw.hxx>
44 #include <DBRep.hxx>
45 #include <DrawTrSurf.hxx>
46
47 #include <BOPTools_AlgoTools2D.hxx>
48
49 static
50   void PrintState (Draw_Interpretor& aDI,
51                    const TopAbs_State& aState);
52 static
53   Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E, 
54                                       const TopoDS_Face& F,
55                                       Standard_Real& First,
56                                       Standard_Real& Last);
57 static
58   Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E, 
59                                       const Handle(Geom_Surface)& S,
60                                       const TopLoc_Location& L,
61                                       Standard_Real& First,
62                                       Standard_Real& Last);
63
64 static  Standard_Integer bclassify   (Draw_Interpretor& , Standard_Integer , const char** );
65 static  Standard_Integer b2dclassify (Draw_Interpretor& , Standard_Integer , const char** );
66 static  Standard_Integer bhaspc      (Draw_Interpretor& , Standard_Integer , const char** );
67
68 //=======================================================================
69 //function : LowCommands
70 //purpose  : 
71 //=======================================================================
72   void  BOPTest::LowCommands(Draw_Interpretor& theCommands)
73 {
74   static Standard_Boolean done = Standard_False;
75   if (done) return;
76   done = Standard_True;
77   // Chapter's name
78   const char* g = "CCR commands";
79   theCommands.Add("bclassify"    , "Use >bclassify Solid Point [Tolerance=1.e-7]",
80                   __FILE__, bclassify   , g);
81   theCommands.Add("b2dclassify"  , "Use >bclassify Face Point2d [Tol2D=Tol(Face)] ",
82                   __FILE__, b2dclassify , g);
83   theCommands.Add("bhaspc"       , "Use >bhaspc Edge Face [do]",
84                   __FILE__, bhaspc      , g);
85 }
86
87 //=======================================================================
88 //function : bclassify
89 //purpose  : 
90 //=======================================================================
91 Standard_Integer bclassify (Draw_Interpretor& aDI,
92                             Standard_Integer n, 
93                             const char** a)
94 {
95   char sbf[512];        
96   
97   if (n < 3) {
98     Sprintf(sbf, " Use >bclassify Solid Point [Tolerance=1.e-7]\n");
99     aDI<<sbf;
100     return 1;
101   }
102   
103   TopoDS_Shape aS = DBRep::Get(a[1]);
104   if (aS.IsNull()) {
105     Sprintf(sbf, " Null Shape is not allowed here\n");
106     aDI<<sbf;
107     return 1;
108   }
109   
110   if (aS.ShapeType()!=TopAbs_SOLID) {
111     Sprintf(sbf, " Shape type must be SOLID\n");
112     aDI<<sbf;
113     return 1;
114   }
115   //
116   Standard_Real aTol=1.e-7;
117   TopAbs_State aState = TopAbs_UNKNOWN;
118   gp_Pnt aP(8., 9., 10.);
119   
120   DrawTrSurf::GetPoint(a[2], aP);
121   
122   aTol=1.e-7; 
123   if (n==4) {
124     aTol=Draw::Atof(a[3]);
125   }
126   //
127   BRepClass3d_SolidClassifier aSC(aS);
128   aSC.Perform(aP,aTol);
129   //
130   aState = aSC.State();
131   //
132   PrintState (aDI, aState);
133   //
134   return 0;
135 }
136 //
137 //=======================================================================
138 //function : b2dclassify
139 //purpose  : 
140 //=======================================================================
141 Standard_Integer b2dclassify (Draw_Interpretor& aDI,
142                               Standard_Integer n, 
143                               const char** a)
144 {
145   char sbf[512];        
146   
147   if (n < 3) {
148     Sprintf(sbf, " Use >bclassify Face Point2d [Tol2D=Tol(Face)]\n");
149     aDI<<sbf;
150     return 1;
151   }
152   
153   TopoDS_Shape aS = DBRep::Get(a[1]);
154   if (aS.IsNull()) {
155     Sprintf(sbf, " Null Shape is not allowed here\n");
156     aDI<<sbf;
157     return 1;
158   }
159   
160   if (aS.ShapeType()!=TopAbs_FACE) {
161     Sprintf(sbf, " Shape type must be FACE\n");
162     aDI<<sbf;
163     return 1;
164   }
165   //
166   Standard_Real aTol;
167   TopAbs_State aState = TopAbs_UNKNOWN;
168   gp_Pnt2d aP(8., 9.);
169   
170   DrawTrSurf::GetPoint2d(a[2], aP);
171   
172   const TopoDS_Face& aF=TopoDS::Face(aS);
173   aTol=BRep_Tool::Tolerance(aF); 
174   if (n==4) {
175     aTol=Draw::Atof(a[3]);
176   }
177   //
178   BRepClass_FaceClassifier aClassifier;
179   aClassifier.Perform(aF, aP, aTol);
180   //
181   aState = aClassifier.State();
182   //
183   PrintState (aDI, aState);
184   //
185   return 0;
186 }
187
188 //=======================================================================
189 //function : bhaspc
190 //purpose  : 
191 //=======================================================================
192 Standard_Integer bhaspc (Draw_Interpretor& di, Standard_Integer n, const char** a)
193 {
194   if (n<3) {
195     di << " Use bhaspc> Edge Face [do]\n";
196     return 1;
197   }
198
199   TopoDS_Shape S1 = DBRep::Get(a[1]);
200   TopoDS_Shape S2 = DBRep::Get(a[2]);
201   
202   if (S1.IsNull() || S2.IsNull()) {
203     di << " Null shapes are not allowed \n";
204     return 1;
205   }
206   if (S1.ShapeType()!=TopAbs_EDGE || S2.ShapeType()!=TopAbs_FACE) {
207     di << " Type mismatch\n";
208     return 1;
209   }
210   //
211   const TopoDS_Edge& aE=TopoDS::Edge(S1);
212   const TopoDS_Face& aF=TopoDS::Face(S2);
213   Standard_Real f2D, l2D; 
214
215   Handle(Geom2d_Curve) C2D=CurveOnSurface(aE, aF, f2D, l2D);
216   
217   if (C2D.IsNull()) {
218     di << " No 2D Curves detected\n";
219   }
220   else {
221     di << " Ok Edge has P-Curve on this Face\n";
222   }
223   
224   if (n==4) {
225     if (!strcmp(a[3], "do")) {
226       BOPTools_AlgoTools2D::BuildPCurveForEdgeOnFace(aE, aF);  
227     }
228   }
229
230   return 0;
231 }
232
233 //=======================================================================
234 //function : PrintState
235 //purpose  : 
236 //=======================================================================
237 void PrintState (Draw_Interpretor& aDI,
238                  const TopAbs_State& aState)
239 {
240   char sbf[512];        
241   TCollection_AsciiString sIN("IN"), sOUT("OUT of"), sON("ON"), sUNKNOWN("UNKNOWN"); 
242   //
243   Sprintf(sbf, "The point is "); aDI<<sbf;
244   //
245   switch (aState) {
246   case TopAbs_IN:
247     Sprintf(sbf, sIN.ToCString());
248     break;
249   case TopAbs_OUT:
250     Sprintf(sbf, sOUT.ToCString());
251     break;
252   case TopAbs_ON:
253     Sprintf(sbf, sON.ToCString());
254     break;
255   case TopAbs_UNKNOWN:
256     Sprintf(sbf, sUNKNOWN.ToCString());
257     break;
258   default:
259     Sprintf(sbf, sUNKNOWN.ToCString()); 
260     break;
261   }
262   aDI<<sbf; 
263   //
264   Sprintf(sbf, " shape\n");
265   aDI<<sbf;
266   
267 }
268
269 //=======================================================================
270 //function : CurveOnSurface
271 //purpose  : 
272 //=======================================================================
273 Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E, 
274                                     const TopoDS_Face& F,
275                                     Standard_Real& First,
276                                     Standard_Real& Last)
277 {
278   TopLoc_Location l;
279   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,l);
280   TopoDS_Edge aLocalEdge = E;
281   if (F.Orientation() == TopAbs_REVERSED) {
282     aLocalEdge.Reverse();
283   }
284   return CurveOnSurface(aLocalEdge,S,l,First,Last);
285 }
286
287 static Handle(Geom2d_Curve) nullPCurve;
288 //=======================================================================
289 //function : CurveOnSurface
290 //purpose  : 
291 //=======================================================================
292 Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E, 
293                                     const Handle(Geom_Surface)& S,
294                                     const TopLoc_Location& L,
295                                     Standard_Real& First,
296                                     Standard_Real& Last)
297 {
298   TopLoc_Location l = L.Predivided(E.Location());
299   Standard_Boolean Eisreversed = (E.Orientation() == TopAbs_REVERSED);
300
301   // find the representation
302   BRep_ListIteratorOfListOfCurveRepresentation itcr
303     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
304
305   while (itcr.More()) {
306     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
307     if (cr->IsCurveOnSurface(S,l)) {
308       const Handle(BRep_GCurve)& GC = *((Handle(BRep_GCurve)*)&cr);
309       GC->Range(First,Last);
310       if (GC->IsCurveOnClosedSurface() && Eisreversed)
311         return GC->PCurve2();
312       else
313         return GC->PCurve();
314     }
315     itcr.Next();
316   }
317   return nullPCurve;
318 }
319