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