0024624: Lost word in license statement in source files
[occt.git] / src / BRepTest / BRepTest_OtherCommands.cxx
1 // Created on: 1995-04-13
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <BRepTest.hxx>
18
19 #include <Draw.hxx>
20 #include <Draw_Interpretor.hxx>
21 #include <DBRep.hxx>
22 #include <DrawTrSurf.hxx>
23
24 #include <string.h>
25 #include <stdio.h>
26
27 #include <Standard_ErrorHandler.hxx>
28 #include <Precision.hxx>
29 #include <TCollection_AsciiString.hxx>
30 #include <gp_Lin.hxx>
31 #include <gp_Pnt.hxx>
32 #include <gp_Dir.hxx>
33 #include <gp_Ax1.hxx>
34
35 #include <ElCLib.hxx>
36
37 #include <TColgp_SequenceOfPnt.hxx>
38
39 #include <GeomAbs_JoinType.hxx>
40 #include <Geom_Line.hxx>
41
42 #include <IntCurvesFace_Intersector.hxx>
43
44 #include <TopAbs.hxx>
45 #include <TopAbs_Orientation.hxx>
46
47 #include <TopoDS.hxx>
48 #include <TopoDS_Shape.hxx>
49 #include <TopoDS_Compound.hxx>
50 #include <TopoDS_CompSolid.hxx>
51 #include <TopoDS_Solid.hxx>
52 #include <TopoDS_Shell.hxx>
53 #include <TopoDS_Face.hxx>
54 #include <TopoDS_Wire.hxx>
55 #include <TopoDS_Edge.hxx>
56 #include <TopoDS_Vertex.hxx>
57 #include <TopoDS_Iterator.hxx>
58
59 #include <BRep_Builder.hxx>
60 #include <BRep_Tool.hxx>
61
62 #include <TopExp_Explorer.hxx>
63
64 #include <TopTools_ListOfShape.hxx>
65 #include <TopTools_ListIteratorOfListOfShape.hxx>
66 #include <TopTools_MapOfShape.hxx>
67
68 #include <LocOpe_CSIntersector.hxx>
69 #include <LocOpe_SequenceOfLin.hxx>
70 #include <LocOpe_PntFace.hxx>
71 #include <BRepFeat_MakeDPrism.hxx>
72
73 #include <BRepTools.hxx>
74 #include <BRepIntCurveSurface_Inter.hxx>
75 #include <BRepOffset.hxx>
76 #include <BRepOffset_MakeOffset.hxx>
77 #include <BRepClass3d_SolidClassifier.hxx>
78
79 static 
80   void SampleEdges (const TopoDS_Shape&   theShape, 
81                     TColgp_SequenceOfPnt& theSeq);
82 static 
83   TopoDS_Face NextFaceForPrism (const TopoDS_Shape& shape, 
84                                 const TopoDS_Shape& basis,
85                                 const gp_Ax1&       ax1);
86 static
87   void PrintState (Draw_Interpretor& aDI, 
88                    const TopAbs_State& aState);
89 //
90 static Standard_Integer emptyshape(Draw_Interpretor&, Standard_Integer, const char** );
91 static Standard_Integer subshape  (Draw_Interpretor&, Standard_Integer, const char** );
92 static Standard_Integer brepintcs (Draw_Interpretor&, Standard_Integer, const char** );
93 static Standard_Integer MakeBoss  (Draw_Interpretor&, Standard_Integer, const char** );
94 static Standard_Integer MakeShell (Draw_Interpretor&, Standard_Integer, const char** );
95 static Standard_Integer xbounds   (Draw_Interpretor&, Standard_Integer, const char** );
96 static Standard_Integer xclassify (Draw_Interpretor&, Standard_Integer, const char** );
97
98 //=======================================================================
99 //function : OtherCommands
100 //purpose  : 
101 //=======================================================================
102 void  BRepTest::OtherCommands(Draw_Interpretor& theCommands)
103 {
104   static Standard_Boolean done = Standard_False;
105   if (done) return;
106   done = Standard_True;
107
108   const char* g = "TOPOLOGY other commands";
109
110   theCommands.Add("shape",
111                   "shape name V/E/W/F/Sh/So/CS/C; make a empty shape",__FILE__,emptyshape,g);
112
113   theCommands.Add("subshape",
114                   "subshape name V/E/W/F/Sh/So/CS/C index; get subsshape <index> of given type"
115                   ,__FILE__,subshape,g);
116
117   theCommands.Add("BRepIntCS",
118                   "Calcul d'intersection entre face et curve : BRepIntCS curve shape"
119                   ,__FILE__,brepintcs,g);
120
121   theCommands.Add("makeboss",  "create a boss on the shape myS", __FILE__, MakeBoss, g);
122   theCommands.Add("mksh", "create a shell on Shape", __FILE__, MakeShell, g);
123   theCommands.Add("xbounds",  "xbounds face", __FILE__, xbounds, g);
124   theCommands.Add("xclassify",  "use xclassify Solid [Tolerance=1.e-7]", __FILE__, xclassify, g);
125   
126
127 }
128 //=======================================================================
129 //function : emptyshape
130 //purpose  : shape : shape name V/E/W/F/SH/SO/CS/C
131 //=======================================================================
132 Standard_Integer emptyshape(Draw_Interpretor& , Standard_Integer n, const char** a)
133 {
134   if (n <= 1) return 1;
135
136   BRep_Builder B;
137   TopoDS_Shape S;
138
139   if (n == 3) {
140     TCollection_AsciiString as(a[2]); as.LowerCase();
141     const char* a2 = as.ToCString();
142
143     if      ( ! strcmp(a2,"c") ) {
144       TopoDS_Compound SS; B.MakeCompound(SS); S = SS;
145     }
146     else if ( ! strcmp(a2,"cs") ) {
147       TopoDS_CompSolid SS; B.MakeCompSolid(SS); S = SS;
148     }
149     else if ( ! strcmp(a2,"so") ) {
150       TopoDS_Solid SS; B.MakeSolid(SS); S = SS;
151     }
152     else if ( ! strcmp(a2,"sh") ) {
153       TopoDS_Shell SS; B.MakeShell(SS); S = SS;
154     }
155     else if ( ! strcmp(a2,"f") ) {
156       TopoDS_Face SS; B.MakeFace(SS); S = SS;
157     }
158     else if ( ! strcmp(a2,"w") ) {
159       TopoDS_Wire SS; B.MakeWire(SS); S = SS;
160     }
161     else if ( ! strcmp(a2,"e") ) {
162       TopoDS_Edge SS; B.MakeEdge(SS); S = SS;
163     }
164     else if ( ! strcmp(a2,"v") ) {
165       TopoDS_Vertex SS; B.MakeVertex(SS); S = SS;
166     }
167     else {
168       return 1;
169     }
170   }
171
172   const char *shapename = a[1];
173   DBRep::Set(shapename,S);
174   return 0;
175 }
176
177 //=======================================================================
178 //function : subshape
179 //purpose  : 
180 //=======================================================================
181 Standard_Integer subshape(Draw_Interpretor& di, Standard_Integer n, const char** a)
182 {
183   if (n <= 2) return 1;
184
185   
186   TopoDS_Shape S = DBRep::Get(a[1]);
187   if (S.IsNull()) return 0;
188   char newname[1024];
189   strcpy(newname,a[1]);
190   char* p = newname;
191   while (*p != '\0') p++;
192   *p = '_';
193   p++;
194   Standard_Integer i = 0;
195   if (n == 3) {
196     Standard_Integer isub = Draw::Atoi(a[2]);
197     TopoDS_Iterator itr(S);
198     while (itr.More()) {
199       i++;
200       if ( i == isub ) {
201         Sprintf(p,"%d",i);
202         DBRep::Set(newname,itr.Value());
203         di.AppendElement(newname);
204         break;
205       }
206       itr.Next();
207     }
208   }
209   else {
210     // explode a type
211     TopAbs_ShapeEnum typ;
212     switch (a[2][0]) {
213       
214     case 'C' :
215     case 'c' :
216       typ = TopAbs_COMPSOLID;
217       break;
218       
219     case 'S' :
220     case 's' :
221       if ((a[2][1] == 'O')||(a[2][1] == 'o')) 
222         typ = TopAbs_SOLID;
223       else if ((a[2][1] == 'H')||(a[2][1] == 'h')) 
224         typ = TopAbs_SHELL;
225       else
226         return 1;
227       break;
228       
229     case 'F' :
230     case 'f' :
231       typ = TopAbs_FACE;
232       break;
233       
234     case 'W' :
235     case 'w' :
236       typ = TopAbs_WIRE;
237       break;
238       
239     case 'E' :
240     case 'e' :
241       typ = TopAbs_EDGE;
242       break;
243       
244     case 'V' :
245     case 'v' :
246       typ = TopAbs_VERTEX;
247       break;
248       
249       default :
250         return 1;
251     }
252     
253     Standard_Integer isub = Draw::Atoi(a[3]);
254     TopTools_MapOfShape M;
255     M.Add(S);
256     TopExp_Explorer ex(S,typ);
257     while (ex.More()) {
258       if (M.Add(ex.Current())) {
259         i++;
260         if ( i == isub ) {
261           Sprintf(p,"%d",i);
262           DBRep::Set(newname,ex.Current());
263           di.AppendElement(newname);
264           break;
265         }
266       }
267       ex.Next();
268     }
269   }
270   return 0;
271 }
272 //=======================================================================
273 //function : brepintcs
274 //purpose  : 
275 //=======================================================================
276 Standard_Integer brepintcs(Draw_Interpretor& , Standard_Integer n, const char** a)
277 {
278   if (n <= 2) return 1;
279   TopoDS_Shape S = DBRep::Get(a[n-1]);
280   if (S.IsNull()) return 3;
281
282   static BRepIntCurveSurface_Inter theAlg;
283   static double tol=1e-6;
284   static int nbpi=0;
285   static gp_Pnt curp;
286
287   if (n==3) {
288     Handle(Geom_Curve) C= DrawTrSurf::GetCurve(a[1]);
289     if (C.IsNull()) return 2;
290     GeomAdaptor_Curve acur(C);
291     theAlg.Init(S, acur, tol);
292     for (; theAlg.More(); theAlg.Next()) {
293       curp=theAlg.Pnt();
294       nbpi++;
295       char name[64];
296       char* temp = name; // pour portage WNT
297       Sprintf(temp, "%s_%d", "brics", nbpi); 
298       DrawTrSurf::Set(temp, curp);
299     }
300   }
301   else {
302     Handle(Geom_Line) hl;
303     gp_Lin thel;
304     for (Standard_Integer il = 1; il<n ; il++) {
305       hl= Handle(Geom_Line)::DownCast(DrawTrSurf::GetCurve(a[il]));
306       if (!hl.IsNull()) {
307         thel=hl->Lin();
308           theAlg.Init(S, thel, tol);
309         for (; theAlg.More(); theAlg.Next()) {
310           curp=theAlg.Pnt();
311           nbpi++;
312           char name[64];
313           char* temp = name; // pour portage WNT
314           Sprintf(temp, "%s_%d", "brics", nbpi); 
315           DrawTrSurf::Set(temp, curp);
316         }
317       }
318     }
319   }
320   //POP pour NT
321   return 0;
322 }
323 //=======================================================================
324 //function : MakeBoss
325 //purpose  : 
326 //=======================================================================
327 Standard_Integer MakeBoss(Draw_Interpretor& , Standard_Integer , const char** a)
328 {
329   TopoDS_Shape myS = DBRep::Get( a[2] );
330
331   TopoDS_Shape myBasis = DBRep::Get( a[3] ) ;
332
333   Standard_Real ang = -0.05235987901687622;
334
335
336   TopoDS_Face basis = TopoDS::Face(myBasis);
337
338   BRepFeat_MakeDPrism DPRISM(myS, basis, basis, ang, 1, Standard_True);
339
340   TopoDS_Shape myFaceOnShape;
341   gp_Pnt Pnt(0.0, 0.0, 50.0);
342   gp_Dir Dir(-0.0, -0.0, -1.0);
343   gp_Ax1 ax(Pnt, Dir);
344   
345   myFaceOnShape = NextFaceForPrism(myS, myBasis, ax);
346
347   DPRISM.Perform (myFaceOnShape);
348   DPRISM.Build();
349
350   if( DPRISM.IsDone() )  DBRep::Set( a[1], DPRISM.Shape() );
351
352   return 0;
353 }
354 //=======================================================================
355 //function : MakeShell
356 //purpose  : 
357 //=======================================================================
358 Standard_Integer MakeShell(Draw_Interpretor& , Standard_Integer , const char** a)
359 {
360
361   TopoDS_Shape aShape = DBRep::Get( a[1] ); 
362   TopTools_ListOfShape Lst;
363   TopExp_Explorer Exp(aShape, TopAbs_FACE);
364   TopoDS_Shape InputShape(DBRep::Get( a[2] ));
365   TopoDS_Face F = TopoDS::Face(InputShape);
366 //  TopoDS_Face F = TopoDS::Face(DBRep::Get( a[2] ));
367   
368   Standard_Real Off = -Draw::Atof( a[3] );
369
370   BRepOffset_MakeOffset Offset;
371
372   Offset.Initialize( aShape, Off,  1.0e-3, BRepOffset_Skin, 
373                                Standard_True , Standard_False , GeomAbs_Arc );
374   Offset.AddFace( F );
375   Offset.MakeThickSolid();
376
377   if( Offset.IsDone() ) {
378   //    SaveShape::Save(Offset.Shape(), "ss");
379     DBRep::Set( a[1], Offset.Shape() );
380   }
381   return 0;
382 }
383 //=======================================================================
384 //function : xbounds
385 //purpose  : 
386 //=======================================================================
387 Standard_Integer xbounds(Draw_Interpretor& di, Standard_Integer n, const char** a)
388 {
389   if (n<2) {
390     di << "Usage : " << a[0] << " face" << "\n";
391     return 0;
392   }
393   //
394  
395   Standard_Real aUMin, aUMax, aVMin, aVMax;
396   TopoDS_Shape aS;
397   TopoDS_Face aF;
398   //
399   aS=DBRep::Get(a[1]);
400   if (aS.IsNull()) {
401     di << " null shapes is not allowed here\n";
402     return 0;
403   }
404   if (aS.ShapeType()!=TopAbs_FACE) {
405     di << " shape" << a[1] <<" must be a face\n";
406     return 0;
407   }
408   //
409   aF=*((TopoDS_Face*)&aS);
410   //
411   BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
412   //
413   TCollection_AsciiString aStr;
414   TCollection_AsciiString sUMin(aUMin);
415   TCollection_AsciiString sUMax(aUMax);
416   TCollection_AsciiString sVMin(aVMin);
417   TCollection_AsciiString sVMax(aVMax);
418   //
419   aStr=aStr+sUMin + "\n";
420   aStr=aStr+sUMax + "\n";
421   aStr=aStr+sVMin + "\n";
422   aStr=aStr+sVMax + "\n";
423   di <<aStr.ToCString();
424   //
425   return 0;
426 }
427 //=======================================================================
428 //function : xclassify
429 //purpose  : 
430 //=======================================================================
431 Standard_Integer xclassify (Draw_Interpretor& aDI, Standard_Integer n, const char** a)
432 {
433   if (n < 2) {
434     aDI<<" use xclassify Solid [Tolerance=1.e-7]\n";
435     return 1;
436   }
437   
438   TopoDS_Shape aS = DBRep::Get(a[1]);
439   if (aS.IsNull()) {
440     aDI<<" Null Shape is not allowed here\n";
441     return 0;
442   }
443   
444   if (aS.ShapeType()!=TopAbs_SOLID) {
445     aDI<< " Shape type must be SOLID\n";
446     return 0;
447   }
448   //
449   Standard_Real aTol=1.e-7;
450   TopAbs_State aState = TopAbs_UNKNOWN;
451   //
452   aTol=1.e-7; 
453   if (n==3) {
454     aTol=Draw::Atof(a[2]);
455   }
456   //
457   BRepClass3d_SolidClassifier aSC(aS);
458   aSC.PerformInfinitePoint(aTol);
459   
460   aState = aSC.State();
461   PrintState(aDI, aState);
462   //
463   return 0;
464 }
465 //=======================================================================
466 //function : PrintState
467 //purpose  : 
468 //=======================================================================
469 void PrintState (Draw_Interpretor& aDI, 
470                  const TopAbs_State& aState)
471 {
472   aDI<<"state is: ";
473   switch (aState) {
474   case TopAbs_IN:
475     aDI<<"IN\n"; 
476     break;
477   case TopAbs_OUT:
478     aDI<<"OUT\n";               
479     break;
480   case TopAbs_ON:        
481     aDI<<"ON\n";        
482     break;
483   case TopAbs_UNKNOWN:
484   default:
485     aDI<<"UNKNOWN\n";           
486     break;
487   }
488 }
489 //=======================================================================
490 //function : NextFaceForPrism
491 //purpose  : Search a face from <shape> which intersects with a line of
492 //           direction <ax1> and location a point of <basis>.
493 //=======================================================================
494 TopoDS_Face NextFaceForPrism (const TopoDS_Shape& shape, 
495                               const TopoDS_Shape& basis,
496                               const gp_Ax1&       ax1)
497 {
498   TopoDS_Face nextFace;
499
500   TColgp_SequenceOfPnt seqPnts;
501   SampleEdges(basis, seqPnts);
502   
503   for (Standard_Integer i=1; i<=seqPnts.Length(); i++) {
504     const gp_Pnt& pt = seqPnts(i);
505     // find a axis through a face
506     gp_Dir dir = ax1.Direction();
507     gp_Ax1 ax1b(pt, dir);
508     
509     LocOpe_CSIntersector ASI(shape);
510     LocOpe_SequenceOfLin slin;
511     slin.Append(ax1b);
512     ASI.Perform(slin);
513     
514     if (ASI.IsDone()) {
515       Standard_Integer no=1, IndFrom, IndTo;
516       TopAbs_Orientation theOr;
517       Standard_Real min = 1.e-04, Tol = -Precision::Confusion();
518       if (ASI.LocalizeAfter (no, min, Tol, theOr, IndFrom, IndTo))  {
519         nextFace = ASI.Point(no, IndFrom).Face();  
520         break;
521       }
522     }
523   }
524   
525   return nextFace;
526 }
527
528
529 //=======================================================================
530 //function : SampleEdges
531 //purpose  : Sampling of <theShape>.
532 //design   : Collect the vertices and points on the edges
533 //=======================================================================
534 void SampleEdges (const TopoDS_Shape&   theShape, TColgp_SequenceOfPnt& theSeq)
535 {
536
537   theSeq.Clear();
538
539    
540   TopTools_MapOfShape theMap;
541   TopExp_Explorer exp;
542   
543   // Adds all vertices/pnt
544   for (exp.Init(theShape,TopAbs_VERTEX); exp.More(); exp.Next()) {
545     if (theMap.Add(exp.Current())) {
546       theSeq.Append (BRep_Tool::Pnt(TopoDS::Vertex(exp.Current())));
547     }
548   }
549
550   // Computes points on edge, but does not take the extremities into account
551   Standard_Integer NECHANT = 5;
552   Handle(Geom_Curve) C;
553   Standard_Real f,l,prm;
554   for (exp.Init (theShape,TopAbs_EDGE); exp.More(); exp.Next()) {
555     const TopoDS_Edge& edg = TopoDS::Edge(exp.Current());
556     if (theMap.Add(edg)) {
557       if (!BRep_Tool::Degenerated(edg)) {
558         C = BRep_Tool::Curve(edg,f,l);
559         for (Standard_Integer i=1; i < NECHANT; i++) {
560           prm = ((NECHANT-i)*f+i*l)/NECHANT;
561           theSeq.Append (C->Value(prm));
562         }
563       }
564     }
565   }
566 }
567