2cd7a1c06c9ccf3df07a0db9172b947639fea67e
[occt.git] / src / BRepTest / BRepTest_SweepCommands.cxx
1 // Created on: 1993-07-22
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-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 #include <DBRep.hxx>
19 #include <Draw_Interpretor.hxx>
20 #include <Draw_Appli.hxx>
21
22 #include <BRepFill.hxx>
23 #include <BRepBuilderAPI_PipeError.hxx>
24 #include <BRepFill_Generator.hxx>
25 #include <BRepPrimAPI_MakePrism.hxx>
26 #include <BRepPrimAPI_MakeRevol.hxx>
27 #include <BRepOffsetAPI_MakePipe.hxx>
28 #include <BRepOffsetAPI_MakeEvolved.hxx>
29 #include <BRepOffsetAPI_ThruSections.hxx>
30 #include <BRepOffsetAPI_MakePipeShell.hxx>
31 #include <BRepOffsetAPI_MiddlePath.hxx>
32
33 #include <BRepLib_MakeWire.hxx>
34 #include <TopoDS.hxx>
35 #include <TopTools_ListIteratorOfListOfShape.hxx>
36 #include <TopExp_Explorer.hxx>
37
38 #include <Precision.hxx>
39 #include <Law_Interpol.hxx>
40 #include <gp_Ax1.hxx>
41 #include <gp_Ax2.hxx>
42 #include <gp_Pnt2d.hxx>
43 #include <TColgp_Array1OfPnt2d.hxx>
44
45 static BRepOffsetAPI_MakePipeShell* Sweep= 0;
46 static BRepOffsetAPI_ThruSections* Generator = 0;
47
48 #include <stdio.h>
49 #include <Geom_Curve.hxx>
50 #include <GeomAdaptor_HCurve.hxx>
51 #include <GeomFill_Pipe.hxx>
52 #include <Geom_Surface.hxx>
53 #include <BRepBuilderAPI_MakeFace.hxx>
54 #include <BRep_Tool.hxx>
55 #include <gp_Pnt.hxx>
56 #include <gp_Vec.hxx>
57 #include <Geom_Circle.hxx>
58 #include <gp_Ax2.hxx>
59
60
61 //=======================================================================
62 // prism
63 //=======================================================================
64
65 static Standard_Integer prism(Draw_Interpretor& , Standard_Integer n, const char** a)
66 {
67   if (n < 6) return 1;
68
69   TopoDS_Shape base = DBRep::Get(a[2]);
70   if (base.IsNull()) return 1;
71
72   gp_Vec V(Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]));
73   
74   Standard_Boolean copy = Standard_False;
75   Standard_Boolean inf  = Standard_False;
76   Standard_Boolean sinf = Standard_False;
77
78   if (n > 6) {
79     copy = (*a[6] == 'c') || (*a[6] == 'C');
80     inf  = (*a[6] == 'i') || (*a[6] == 'I');
81     sinf = (*a[6] == 's') || (*a[6] == 'S');
82   }
83
84   TopoDS_Shape res;
85
86   if (inf || sinf) 
87     res = BRepPrimAPI_MakePrism(base,gp_Dir(V),inf);
88   else
89     res = BRepPrimAPI_MakePrism(base,V,copy);
90
91  DBRep::Set(a[1],res);
92
93   return 0;
94 }
95
96
97 //=======================================================================
98 // revol
99 //=======================================================================
100
101 static Standard_Integer revol(Draw_Interpretor& ,
102                               Standard_Integer n, const char** a)
103 {
104   if (n < 10) return 1; 
105
106   TopoDS_Shape base = DBRep::Get(a[2]);
107   if (base.IsNull()) return 1;
108
109   gp_Pnt P(Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]));
110   gp_Dir D(Draw::Atof(a[6]),Draw::Atof(a[7]),Draw::Atof(a[8]));
111   gp_Ax1 A(P,D);
112
113   Standard_Real angle = Draw::Atof(a[9]) * (M_PI / 180.0);
114   
115   Standard_Boolean copy = n > 10;
116
117   TopoDS_Shape res = BRepPrimAPI_MakeRevol(base,A,angle,copy);
118
119   DBRep::Set(a[1],res);
120
121   return 0;
122 }
123
124
125 //=======================================================================
126 // pipe
127 //=======================================================================
128
129 static Standard_Integer pipe(Draw_Interpretor& di,
130                              Standard_Integer n, const char** a)
131 {
132   if (n == 1)
133   {
134     di << "pipe result Wire_spine Profile [Mode [Approx]]\n";
135     di << "Mode = 0 - CorrectedFrenet,\n";
136     di << "     = 1 - Frenet,\n";
137     di << "     = 2 - DiscreteTrihedron\n";
138     di << "Approx - force C1-approximation if result is C0\n";
139     return 0;
140   }
141   
142   if (n > 1 && n < 4) return 1;
143
144   TopoDS_Shape Spine = DBRep::Get(a[2],TopAbs_WIRE);
145   if ( Spine.IsNull()) return 1;
146
147   TopoDS_Shape Profile = DBRep::Get(a[3]);
148   if ( Profile.IsNull()) return 1;
149
150   GeomFill_Trihedron Mode = GeomFill_IsCorrectedFrenet;
151   if (n >= 5)
152   {
153     Standard_Integer iMode = atoi(a[4]);
154     if (iMode == 1)
155       Mode = GeomFill_IsFrenet;
156     else if (iMode == 2)
157       Mode = GeomFill_IsDiscreteTrihedron;
158   }
159
160   Standard_Boolean ForceApproxC1 = Standard_False;
161   if (n >= 6)
162     ForceApproxC1 = Standard_True;
163   
164   TopoDS_Shape S = BRepOffsetAPI_MakePipe(TopoDS::Wire(Spine),
165                                           Profile,
166                                           Mode,
167                                           ForceApproxC1);
168
169   DBRep::Set(a[1],S);
170   
171   return 0;
172 }
173
174 //=======================================================================
175
176 static Standard_Integer geompipe(Draw_Interpretor& ,
177                              Standard_Integer n, const char** a)
178 {
179   TopoDS_Shape Spine = DBRep::Get(a[2],TopAbs_EDGE);
180   if ( Spine.IsNull()) return 1;
181   if ( n < 5) return 1;
182   TopoDS_Shape Profile = DBRep::Get(a[3],TopAbs_EDGE);
183   if ( Profile.IsNull()) return 1;
184   Standard_Real aSpFirst,aSpLast,aPrFirst,aPrLast;
185   Handle(Geom_Curve) SpineCurve = BRep_Tool::Curve(TopoDS::Edge(Spine),aSpFirst,aSpLast);
186   Handle(Geom_Curve) ProfileCurve = BRep_Tool::Curve(TopoDS::Edge(Profile),aPrFirst,aPrLast);
187   Handle(GeomAdaptor_HCurve) aAdaptCurve = new GeomAdaptor_HCurve(SpineCurve,aSpFirst,aSpLast);
188   Standard_Boolean ByACR = Standard_False;
189   Standard_Boolean rotate = Standard_False;
190   Standard_Real Radius = Draw::Atof(a[4]);
191   gp_Pnt ctr;
192   gp_Vec norm;
193   ProfileCurve->D1(aSpFirst,ctr,norm);
194   gp_Vec xAxisStart(ctr,SpineCurve->Value(aSpFirst));
195   gp_Ax2 aAx2Start(ctr,norm,xAxisStart);
196   Handle(Geom_Circle) cStart=new Geom_Circle(aAx2Start,Radius);                       
197   Standard_Integer k =5;
198   if(n > k)
199     ByACR = (Draw::Atoi(a[k++]) ==1);
200   if(n > k)
201     rotate = (Draw::Atoi(a[k++])==1);
202   GeomFill_Pipe aPipe(ProfileCurve,aAdaptCurve,cStart,ByACR,rotate);
203   aPipe.Perform(Standard_True);
204   if (!aPipe.IsDone())
205   {
206     cout << "GeomFill_Pipe cannot make a surface" << endl;
207     return 1;
208   }
209   Handle(Geom_Surface) Sur=aPipe.Surface();
210   TopoDS_Face F;
211   if(!Sur.IsNull())
212     F = BRepBuilderAPI_MakeFace(Sur, Precision::Confusion());
213   DBRep::Set(a[1],F);
214   return 0;
215 }
216
217 //=======================================================================
218 //function : evolved
219 //purpose  : 
220 //=======================================================================
221
222 Standard_Integer evolved(Draw_Interpretor& di, Standard_Integer n, const char** a)
223 {
224   if ( n == 1) {
225     //cout << " 1) evolved result base profil : "<< endl;
226     //cout << "        The relative position of the profil on the base" << endl;
227     //cout << "        is given in the referencial axis. " << endl;
228     //cout << " 2) evolved result base profil o : "<< endl;
229     //cout << "        This position is automatically computed." << endl;
230     di << " 1) evolved result base profil : \n";
231     di << "        The relative position of the profil on the base\n";
232     di << "        is given in the referencial axis. \n";
233     di << " 2) evolved result base profil o : \n";
234     di << "        This position is automatically computed.\n";
235     return 0;
236   }
237
238   if ( n < 4 ) return 1;
239   Standard_Boolean IsAFace = Standard_False;
240   Standard_Boolean Solid   = (!strcmp(a[0],"evolvedsolid"));
241
242
243  
244   TopoDS_Shape Base = DBRep::Get(a[2],TopAbs_WIRE,Standard_False);
245   if ( Base.IsNull()) {
246     Base = DBRep::Get(a[2],TopAbs_FACE,Standard_False);
247     IsAFace = Standard_True; 
248   }
249   if ( Base.IsNull()) return 1;
250
251   TopoDS_Shape InpuTShape(DBRep::Get(a[3],TopAbs_WIRE,Standard_False));
252   TopoDS_Wire Prof = TopoDS::Wire(InpuTShape);
253 //  TopoDS_Wire Prof = 
254 //    TopoDS::Wire(DBRep::Get(a[3],TopAbs_WIRE,Standard_False));
255   if ( Prof.IsNull()) return 1;
256
257   if (IsAFace) {
258     TopoDS_Shape Volevo 
259       = BRepOffsetAPI_MakeEvolved(TopoDS::Face(Base),Prof,GeomAbs_Arc,n == 4,Solid);
260     DBRep::Set(a[1],Volevo);
261   }
262   else {
263     TopoDS_Shape Volevo 
264       = BRepOffsetAPI_MakeEvolved(TopoDS::Wire(Base),Prof,GeomAbs_Arc,n == 4,Solid);
265     DBRep::Set(a[1],Volevo);
266   }
267
268   return 0;
269 }
270
271
272 //=======================================================================
273 //function : pruled
274 //purpose  : 
275 //=======================================================================
276
277 static Standard_Integer pruled(Draw_Interpretor& ,
278                                Standard_Integer n, const char** a)
279 {
280   if ( n != 4) return 1;
281
282   Standard_Boolean YaWIRE = Standard_False;
283   TopoDS_Shape S1 = DBRep::Get(a[2],TopAbs_EDGE);
284   if ( S1.IsNull()) {
285     S1 = DBRep::Get(a[2],TopAbs_WIRE);
286     if (S1.IsNull()) return 1;
287     YaWIRE = Standard_True;
288   }
289
290   TopoDS_Shape S2 = DBRep::Get(a[3],TopAbs_EDGE);
291   if ( S2.IsNull()) {
292     S2 = DBRep::Get(a[3],TopAbs_WIRE);
293     if ( S2.IsNull()) return 1;
294     if (!YaWIRE) {
295       S1 = BRepLib_MakeWire(TopoDS::Edge(S1));
296       YaWIRE = Standard_True;
297     }
298   }
299   else if ( YaWIRE) {
300     S2 = BRepLib_MakeWire(TopoDS::Edge(S2));
301   }
302
303   TopoDS_Shape Result;
304   if ( YaWIRE) {
305     Result = BRepFill::Shell(TopoDS::Wire(S1),TopoDS::Wire(S2));
306   }
307   else {
308     Result = BRepFill::Face(TopoDS::Edge(S1),TopoDS::Edge(S2));
309   }
310
311   DBRep::Set(a[1],Result);
312   return 0;
313 }
314
315
316 //=======================================================================
317 //function : gener
318 //purpose  : Create a surface between generating wires
319 //=======================================================================
320
321 Standard_Integer gener(Draw_Interpretor&, Standard_Integer n, const char** a)
322 {
323   if ( n < 4) return 1;
324
325   TopoDS_Shape Shape; 
326
327   BRepFill_Generator aGenerator;
328   
329   for ( Standard_Integer i = 2; i<= n-1 ; i++) {
330     Shape = DBRep::Get(a[i],TopAbs_WIRE);
331     if ( Shape.IsNull()) 
332       return 1;
333
334     aGenerator.AddWire(TopoDS::Wire(Shape));
335   }
336
337   aGenerator.Perform();
338
339   TopoDS_Shell Shell = aGenerator.Shell();
340   
341   DBRep::Set(a[1], Shell);
342
343
344   return 0;
345 }
346
347
348 //=======================================================================
349 //function : thrusections
350 //purpose  : 
351 //=======================================================================
352
353 Standard_Integer thrusections(Draw_Interpretor&, Standard_Integer n, const char** a)
354 {
355   if (n<6) return 1;
356
357   Standard_Boolean check = Standard_True;
358   Standard_Boolean samenumber = Standard_True;
359   Standard_Integer index = 2;
360     // Lecture option
361   if (!strcmp(a[1],"-N")) {
362     if (n<7) return 1;
363     check = Standard_False;
364     index++;
365   }
366
367   TopoDS_Shape Shape; 
368
369   Standard_Boolean issolid = ( Draw::Atoi(a[index]) == 1 );
370   Standard_Boolean isruled = ( Draw::Atoi(a[index+1]) == 1 );
371
372   if (Generator != 0)
373   {
374     delete Generator; 
375     Generator = 0;
376   }
377   Generator = new BRepOffsetAPI_ThruSections(issolid,isruled);
378   
379   Standard_Integer NbEdges = 0;
380   Standard_Boolean IsFirstWire = Standard_False;
381   for ( Standard_Integer i = index+2; i<= n-1 ; i++) {
382     Standard_Boolean IsWire = Standard_True;
383     Shape = DBRep::Get(a[i], TopAbs_WIRE);
384     if (!Shape.IsNull())
385       {
386         Generator->AddWire(TopoDS::Wire(Shape));
387         if (!IsFirstWire)
388           IsFirstWire = Standard_True;
389         else
390           IsFirstWire = Standard_False;
391       }
392     else
393       {
394         Shape = DBRep::Get(a[i], TopAbs_VERTEX);
395         IsWire = Standard_False;
396         if (!Shape.IsNull())
397           Generator->AddVertex(TopoDS::Vertex(Shape));
398         else
399           return 1;
400       }
401
402     Standard_Integer cpt = 0;
403     TopExp_Explorer PE;
404     for (PE.Init(Shape, TopAbs_EDGE); PE.More(); PE.Next()) {
405       cpt++;
406     }
407     if (IsFirstWire) 
408       NbEdges = cpt;
409     else
410       if (IsWire && cpt != NbEdges)
411         samenumber = Standard_False;
412     
413   }
414
415   check = (check || !samenumber);
416   Generator->CheckCompatibility(check);
417
418   Generator->Build();
419
420   if (Generator->IsDone()) {
421     TopoDS_Shape Shell = Generator->Shape();
422     DBRep::Set(a[index-1], Shell);
423   }
424   else {
425     cout << "Algorithm is not done" << endl;
426   }
427   return 0;
428 }
429
430 //============================================================================
431 //function : genthrus
432 //purpose  : returns generated shape for subshape of a section of thrusections
433 //           Thrusections must be done previously
434 //============================================================================
435 static Standard_Integer genthrus(Draw_Interpretor& di,
436                                  Standard_Integer n, const char** a)
437 {
438   if (n != 3)
439   {
440     di << "genthrus: it is called after thrusections command\n";
441     di << "returns:\n";
442     di << "- chain of generated faces for sub-edge of a profile;\n";
443     di << "- chain of generated edges for sub-vertex of a profile;\n";
444     di << "- bunch of chains of generated edges for start or end vertex if it is degenerated section.\n";
445     di << "Usage: genthrus res subshape_of_profile, thrusections must be done\n";
446     return 1;
447   }
448   if (Generator == 0)
449   {
450     di << "You have forgotten the <<thrusections>> command !\n";
451     return 1;
452   }
453   if (!Generator->IsDone())
454   {
455     di << "Thrusections is not done\n";
456     return 1;
457   }
458   TopoDS_Shape aShape = DBRep::Get(a[2]);
459   if (aShape.IsNull())
460   {
461     cout<<"Null subshape"<<endl;
462     return 1;
463   }
464   const TopTools_ListOfShape& Edges = Generator->Generated(aShape);
465   TopoDS_Compound aCompound;
466   BRep_Builder BB;
467   BB.MakeCompound(aCompound);
468   TopTools_ListIteratorOfListOfShape iter(Edges);
469   for (; iter.More(); iter.Next())
470   {
471     const TopoDS_Shape& anEdge = iter.Value();
472     BB.Add(aCompound, anEdge);
473   }
474
475   DBRep::Set(a[1], aCompound);
476   return 0;
477 }
478
479 //=======================================================================
480 //  mksweep
481 //=======================================================================
482 static Standard_Integer mksweep(Draw_Interpretor& ,
483                              Standard_Integer n, const char** a)
484 {
485   if ( n != 2) return 1;
486   TopoDS_Shape Spine = DBRep::Get(a[1],TopAbs_WIRE);
487   if ( Spine.IsNull()) return 1;
488   if (Sweep !=0)  {
489     delete Sweep; 
490     Sweep = 0;
491   }
492   Sweep = new BRepOffsetAPI_MakePipeShell(TopoDS::Wire(Spine));
493   return 0;
494 }
495
496 //=======================================================================
497 //  setsweep
498 //=======================================================================
499 static Standard_Integer setsweep(Draw_Interpretor& di,
500                                  Standard_Integer n, const char** a)
501 {
502   if ( n == 1) {
503     //cout << "setsweep options [arg1 [arg2 [...]]] : options are :" << endl;
504     //cout << "   -FR : Tangent and Normal are given by Frenet trihedron" <<endl;
505     //cout << "   -CF : Tangente is given by Frenet," << endl;
506     //cout << "         the Normal is computed to minimize the torsion " << endl;
507     //cout << "   -DX Surf : Tangent and Normal are given by Darboux trihedron,"
508     //  <<endl;     
509     //cout << "       Surf have to be a shell or a face" <<endl;
510     //cout << "   -CN dx dy dz : BiNormal is given by dx dy dz" << endl;
511     //cout << "   -FX Tx Ty TZ [Nx Ny Nz] : Tangent and Normal are fixed" <<endl;
512     //cout << "   -G guide  0|1(ACR|Plan)  0|1(contact|no contact) : with guide"<<endl;
513     di << "setsweep options [arg1 [arg2 [...]]] : options are :\n";
514     di << "   -FR : Tangent and Normal are given by Frenet trihedron\n";
515     di << "   -CF : Tangente is given by Frenet,\n";
516     di << "         the Normal is computed to minimize the torsion \n";
517     di << "   -DT : discrete trihedron\n";
518     di << "   -DX Surf : Tangent and Normal are given by Darboux trihedron,\n";     
519     di << "       Surf have to be a shell or a face\n";
520     di << "   -CN dx dy dz : BiNormal is given by dx dy dz\n";
521     di << "   -FX Tx Ty TZ [Nx Ny Nz] : Tangent and Normal are fixed\n";
522     di << "   -G guide  0|1(Plan|ACR)  0|1|2(no contact|contact|contact on border) : with guide\n";
523     return 0;
524   }
525
526    if (Sweep ==0) {
527      //cout << "You have forgotten the <<mksweep>> command  !"<< endl;
528      di << "You have forgotten the <<mksweep>> command  !\n";
529      return 1;
530    }
531   if (!strcmp(a[1],"-FR")) {
532     Sweep->SetMode(Standard_True);
533   }
534   else if (!strcmp(a[1],"-CF")) {
535     Sweep->SetMode(Standard_False);
536   }
537   else if (!strcmp(a[1],"-DT")) {
538     Sweep->SetDiscreteMode();
539   }
540   else if (!strcmp(a[1],"-DX")) {
541     if (n!=3) {
542       //cout << "bad arguments !" << endl;
543       di << "bad arguments !\n";
544       return 1;
545     }
546     TopoDS_Shape Surf;
547     Surf = DBRep::Get(a[2],TopAbs_SHAPE);
548     if (Surf.IsNull()) {
549        //cout << a[2] <<"is not a shape !" << endl;
550        di << a[2] <<"is not a shape !\n";
551       return 1;
552     }
553     Sweep->SetMode(Surf);
554   }
555   else if (!strcmp(a[1],"-CN")) {
556     if (n!=5) {
557       //cout << "bad arguments !" << endl;
558       di << "bad arguments !\n";
559       return 1;
560     }
561     gp_Dir D(Draw::Atof(a[2]), Draw::Atof(a[3]), Draw::Atof(a[4]));
562     Sweep->SetMode(D);;
563   }
564   else if (!strcmp(a[1],"-FX")) {
565     if ((n!=5)&&(n!=8)) {
566       //cout << "bad arguments !" << endl;
567       di << "bad arguments !\n";
568       return 1;
569     }
570     gp_Dir D(Draw::Atof(a[2]), Draw::Atof(a[3]), Draw::Atof(a[4]));
571     if (n==8) {
572       gp_Dir DN(Draw::Atof(a[5]), Draw::Atof(a[6]), Draw::Atof(a[7]));
573       gp_Ax2 Axe(gp_Pnt(0., 0., 0.), D, DN);
574       Sweep->SetMode(Axe);
575     }
576     else {
577       gp_Ax2 Axe(gp_Pnt(0., 0., 0.), D);
578       Sweep->SetMode(Axe);
579     }
580   }
581   else if (!strcmp(a[1],"-G"))  // contour guide
582     {
583      if (n != 5)
584        {
585          //cout << "bad arguments !" << endl;
586          di << "bad arguments !\n";
587          return 1; 
588        }
589      else
590         {  
591           TopoDS_Shape Guide = DBRep::Get(a[2],TopAbs_WIRE);
592           Standard_Boolean CurvilinearEquivalence = Draw::Atoi(a[3]) != 0;
593           Standard_Integer KeepContact = Draw::Atoi(a[4]);
594           Sweep->SetMode(TopoDS::Wire(Guide),
595                          CurvilinearEquivalence,
596                          (BRepFill_TypeOfContact)KeepContact);
597         }
598     }
599  
600   else {
601     //cout << "The option "<< a[1] << " is unknown !" << endl;
602     di << "The option "<< a[1] << " is unknown !\n";
603     return 1;
604   }
605   return 0;
606 }
607
608
609 //=======================================================================
610 //  addsweep
611 //=======================================================================
612 static Standard_Integer addsweep(Draw_Interpretor& di,
613                              Standard_Integer n, const char** a)
614 {
615   if ( n == 1) {
616     //cout << "addsweep wire/vertex [Vertex] [-T] [-R] [u0 v0 u1 v1 [...[uN vN]]] : options are :" << endl;
617     //cout << "   -T : the wire/vertex have to be translated to assume contact"<< endl;
618     //cout << "        with the spine" <<endl;
619     //cout << "   -R : the wire have to be rotated to assume orthogonality"<<endl;
620     //cout << "        with the spine's tangent" << endl;
621     di << "addsweep wire/vertex [Vertex] [-T] [-R] [u0 v0 u1 v1 [...[uN vN]]] : options are :\n";
622     di << "   -T : the wire/vertex have to be translated to assume contact\n";
623     di << "        with the spine\n";
624     di << "   -R : the wire have to be rotated to assume orthogonality\n";
625     di << "        with the spine's tangent\n";
626     return 0;
627   }
628
629   if (Sweep ==0) {
630     //cout << "You have forgotten the <<mksweep>> command  !"<< endl;
631     di << "You have forgotten the <<mksweep>> command  !\n";
632     return 1;
633   }
634
635   TopoDS_Shape  Section;
636   TopoDS_Vertex Vertex;
637   Handle(Law_Interpol) thelaw;
638
639   Section = DBRep::Get(a[1], TopAbs_SHAPE);
640   if (Section.IsNull() ||
641       (Section.ShapeType() != TopAbs_WIRE &&
642        Section.ShapeType() != TopAbs_VERTEX))
643     {
644       //cout << a[1] <<"is not a wire and is not a vertex!" << endl;
645       di << a[1] <<" is not a wire and is not a vertex!\n";
646       return 1;
647     }
648
649   Standard_Boolean HasVertex=Standard_False, 
650                    isT=Standard_False, 
651                    isR=Standard_False;
652
653   if (n > 2) { 
654     Standard_Integer cur = 2;
655     // Reading of Vertex
656     TopoDS_Shape InputVertex(DBRep::Get(a[cur],TopAbs_VERTEX));
657     Vertex = TopoDS::Vertex(InputVertex);
658 //    Vertex = TopoDS::Vertex(DBRep::Get(a[cur],TopAbs_VERTEX));
659     if (!Vertex.IsNull()) {
660       cur++;
661       HasVertex = Standard_True;
662     }
663    
664     // Reading of the translation option
665     if ((n>cur) && !strcmp(a[cur],"-T")) {
666       cur++;
667       isT = Standard_True;
668     }
669
670     // Reading of the rotation option
671     if ((n>cur) && !strcmp(a[cur],"-R")) {
672       cur++;
673       isR = Standard_True;
674     }
675
676     // law ?
677     if (n>cur) {
678       Standard_Integer nbreal = n-cur;
679       if ( (nbreal < 4) || (nbreal % 2 != 0) ) {
680         //cout << "bad arguments ! :" <<a[cur] << endl;
681         di << "bad arguments ! :" <<a[cur] << "\n";
682       } else { //law of interpolation
683         Standard_Integer ii, L= nbreal/2;
684         TColgp_Array1OfPnt2d ParAndRad(1, L);
685         for (ii=1; ii<=L; ii++, cur+=2) {
686           ParAndRad(ii).SetX(Draw::Atof(a[cur]));
687           ParAndRad(ii).SetY(Draw::Atof(a[cur+1]));
688         }
689         thelaw = new (Law_Interpol) ();
690         thelaw->Set(ParAndRad, 
691                    Abs(ParAndRad(1).Y() - ParAndRad(L).Y()) < Precision::Confusion());
692       }
693     }
694   }
695
696   if (thelaw.IsNull()) {
697     if (HasVertex) Sweep->Add(Section, Vertex, isT, isR);
698     else           Sweep->Add(Section, isT, isR);
699   }
700   else {
701     if (HasVertex) Sweep->SetLaw(Section, thelaw, Vertex, isT, isR);
702     else           Sweep->SetLaw(Section, thelaw, isT, isR);
703   }
704
705   return 0;
706 }
707
708 //=======================================================================
709 //  deletesweep
710 //=======================================================================
711 static Standard_Integer deletesweep(Draw_Interpretor& di,
712                                     Standard_Integer n, const char** a)
713 {
714   if ( n != 2) {
715     return 1;
716   }
717   TopoDS_Wire Section;
718   TopoDS_Shape InputShape(DBRep::Get(a[1],TopAbs_SHAPE));
719   Section = TopoDS::Wire(InputShape);
720 //  Section = TopoDS::Wire(DBRep::Get(a[1],TopAbs_SHAPE));
721   if (Section.IsNull()) {
722     //cout << a[1] <<"is not a wire !" << endl;
723     di << a[1] <<"is not a wire !\n";
724     return 1;
725   }  
726
727   Sweep->Delete(Section);
728
729   return 0;
730 }
731
732 //=======================================================================
733 //  buildsweep
734 //=======================================================================
735 static Standard_Integer buildsweep(Draw_Interpretor& di,
736                                    Standard_Integer n, const char** a)
737 {
738   if ( n == 1) {
739     //cout << "build sweep result [-M/-C/-R] [-S] [tol] : options are" << endl;
740     //cout << "   -M : Discontinuities are treated by Modfication of"<< endl; 
741     //cout << "        the sweeping mode : it is the default" <<endl;
742     //cout << "   -C : Discontinuities are treated like Right Corner" << endl;
743     //cout << "        Treatement is Extent && Intersect" << endl;
744     //cout << "   -R : Discontinuities are treated like Round Corner" << endl;
745     //cout << "        Treatement is Intersect and Fill" << endl;
746     //cout << "   -S : To build a Solid" << endl;
747     di << "build sweep result [-M/-C/-R] [-S] [tol] : options are\n";
748     di << "   -M : Discontinuities are treated by Modfication of\n"; 
749     di << "        the sweeping mode : it is the default\n";
750     di << "   -C : Discontinuities are treated like Right Corner\n";
751     di << "        Treatement is Extent && Intersect\n";
752     di << "   -R : Discontinuities are treated like Round Corner\n";
753     di << "        Treatement is Intersect and Fill\n";
754     di << "   -S : To build a Solid\n";
755     return 0;
756   }
757
758   Standard_Boolean mksolid = Standard_False;
759   if (Sweep ==0) {
760     //cout << "You have forgotten the <<mksweep>> command  !"<< endl;
761     di << "You have forgotten the <<mksweep>> command  !\n";
762     return 1;
763   }
764
765   if (!Sweep->IsReady()) {
766     //cout << "You have forgotten the <<addsweep>> command  !"<< endl;
767     di << "You have forgotten the <<addsweep>> command  !\n";
768     return 1;
769   }
770
771   TopoDS_Shape result;
772   Standard_Integer cur=2;
773   if (n>cur) {
774     BRepBuilderAPI_TransitionMode Transition = BRepBuilderAPI_Transformed;
775
776     // Reading Transition
777     if (!strcmp(a[cur],"-C")) {
778       Transition = BRepBuilderAPI_RightCorner;
779       cur++;
780     }
781     else if (!strcmp(a[cur],"-R")) {
782       Transition = BRepBuilderAPI_RoundCorner;
783       cur++;
784     }
785     Sweep->SetTransitionMode(Transition);
786   }
787   // Reading solid ?
788   if ((n>cur) && (!strcmp(a[cur],"-S")) ) mksolid = Standard_True;
789
790   // Calcul le resultat
791   Sweep->Build();
792   if (!Sweep->IsDone()) {
793     //cout << "Buildsweep : Not Done" << endl;
794     di << "Buildsweep : Not Done\n";
795     BRepBuilderAPI_PipeError Stat = Sweep->GetStatus(); 
796     if (Stat == BRepBuilderAPI_PlaneNotIntersectGuide) {
797       //cout << "Buildsweep : One Plane not intersect the guide" << endl;
798       di << "Buildsweep : One Plane not intersect the guide\n";
799     }
800     if (Stat == BRepBuilderAPI_ImpossibleContact) {
801       //cout << "BuildSweep : One section can not be in contact with the guide" << endl;
802       di << "BuildSweep : One section can not be in contact with the guide\n";
803     }
804     return 1;
805   }
806   else {
807     if (mksolid) {
808       Standard_Boolean B;
809       B = Sweep->MakeSolid();
810       //if (!B) cout << " BuildSweep : It is impossible to make a solid !" << endl;
811       if (!B) di << " BuildSweep : It is impossible to make a solid !\n";
812     }
813     result = Sweep->Shape();
814     DBRep::Set(a[1],result);
815   }
816
817   return 0;
818 }
819
820 //=======================================================================
821 //function : gensweep
822 //purpose  : returns generated shape for subshape of a section of sweep
823 //           Sweep must be done previously
824 //=======================================================================
825 static Standard_Integer gensweep(Draw_Interpretor&,
826                                  Standard_Integer n, const char** a)
827 {
828   if (n != 3)
829   {
830     cout<<"Usage: gensweep res subshape_of_profile, sweep must be done"<<endl;
831     return 1;
832   }
833   if (!Sweep->IsDone())
834   {
835     cout<<"Sweep is not done"<<endl;
836     return 1;
837   }
838   TopoDS_Shape aShape = DBRep::Get(a[2]);
839   if (aShape.IsNull())
840   {
841     cout<<"Null subshape"<<endl;
842     return 1;
843   }
844   TopTools_ListOfShape Shells = Sweep->Generated(aShape);
845   TopoDS_Compound aCompound;
846   BRep_Builder BB;
847   BB.MakeCompound(aCompound);
848   TopTools_ListIteratorOfListOfShape itsh(Shells);
849   for (; itsh.More(); itsh.Next())
850   {
851     const TopoDS_Shape& aShell = itsh.Value();
852     BB.Add(aCompound, aShell);
853   }
854
855   DBRep::Set(a[1], aCompound);
856   return 0;
857 }
858
859 //=======================================================================
860 //  simulsweep
861 //=======================================================================
862 static Standard_Integer simulsweep(Draw_Interpretor& di,
863                                    Standard_Integer n, const char** a)
864 {
865   if ( (n!=3) && (n!=4) ) return 1;
866   
867   if (Sweep ==0) {
868     //cout << "You have forgotten the <<mksweep>> command  !"<< endl;
869     di << "You have forgotten the <<mksweep>> command  !\n";
870     return 1;
871   }
872   
873   if (!Sweep->IsReady()) {
874     //cout << "You have forgotten the <<addsweep>> command  !"<< endl;
875     di << "You have forgotten the <<addsweep>> command  !\n";
876     return 1;
877   }
878   
879   char name[100];
880   TopTools_ListOfShape List;
881   TopTools_ListIteratorOfListOfShape it;
882   Standard_Integer N, ii;
883   N = Draw::Atoi(a[2]);
884
885   if (n>3) {
886     BRepBuilderAPI_TransitionMode Transition = BRepBuilderAPI_Transformed;
887     // Lecture Transition
888     if (!strcmp(a[3],"-C")) {
889       Transition = BRepBuilderAPI_RightCorner;
890     }
891     else if (!strcmp(a[3],"-R")) {
892       Transition = BRepBuilderAPI_RoundCorner;
893     }
894     Sweep->SetTransitionMode(Transition);
895   }
896
897   // Calculate the result
898   Sweep->Simulate(N, List);
899   for (ii=1, it.Initialize(List); it.More(); it.Next(), ii++) {
900     Sprintf(name,"%s_%d",a[1],ii);
901     DBRep::Set(name, it.Value());
902   }
903
904   return 0;
905 }
906
907 //=======================================================================
908 //  middlepath
909 //=======================================================================
910 static Standard_Integer middlepath(Draw_Interpretor& /*di*/,
911                                    Standard_Integer n, const char** a)
912 {
913   if (n < 5) return 1;
914
915   TopoDS_Shape aShape = DBRep::Get(a[2]);
916   if (aShape.IsNull()) return 1;
917
918   TopoDS_Shape StartShape = DBRep::Get(a[3]);
919   if (StartShape.IsNull()) return 1;
920   
921   TopoDS_Shape EndShape   = DBRep::Get(a[4]);
922   if (EndShape.IsNull()) return 1;
923
924   BRepOffsetAPI_MiddlePath Builder(aShape, StartShape, EndShape);
925   Builder.Build();
926
927   TopoDS_Shape Result = Builder.Shape();
928   DBRep::Set(a[1], Result);
929
930   return 0;
931 }
932
933 //=======================================================================
934 //function : SweepCommands
935 //purpose  : 
936 //=======================================================================
937
938 void  BRepTest::SweepCommands(Draw_Interpretor& theCommands)
939 {
940   static Standard_Boolean done = Standard_False;
941   if (done) return;
942   done = Standard_True;
943
944   DBRep::BasicCommands(theCommands);
945
946   const char* g = "Sweep commands";
947   
948   theCommands.Add("prism",
949                   "prism result base dx dy dz [Copy | Inf | Seminf]",
950                   __FILE__,prism,g);
951   
952   theCommands.Add("revol",
953                   "revol result base px py pz dx dy dz angle [Copy]",
954                   __FILE__,revol,g);
955   
956   theCommands.Add("pipe",
957                   "pipe result Wire_spine Profile [Mode [Approx]], no args to get help",
958                   __FILE__,pipe,g);
959   
960   theCommands.Add("evolved",
961                   "evolved , no args to get help",
962                   __FILE__,evolved,g);  
963
964   theCommands.Add("evolvedsolid",
965                   "evolved , no args to get help",
966                   __FILE__,evolved,g);  
967   
968   theCommands.Add("pruled",
969                   "pruled result Edge1/Wire1 Edge2/Wire2",
970                   __FILE__,pruled,g);
971
972   theCommands.Add("gener", "gener result wire1 wire2 [..wire..]",
973                   __FILE__,gener,g);
974
975   theCommands.Add("thrusections", "thrusections [-N] result issolid isruled shape1 shape2 [..shape..], the option -N means no check on wires, shapes must be wires or vertices (only first or last)",
976                   __FILE__,thrusections,g);
977
978   theCommands.Add("genthrus", "genthrus res subshape_of_profile",
979                   __FILE__,genthrus,g);
980   
981   theCommands.Add("mksweep", "mksweep wire",
982                   __FILE__,mksweep,g);
983
984   theCommands.Add("setsweep", "setsweep  no args to get help",
985                   __FILE__,setsweep,g);
986   
987   theCommands.Add("addsweep", 
988                   "addsweep wire [vertex] [-M ] [-C] [auxiilaryshape]:no args to get help",
989                   __FILE__,addsweep,g);
990
991  theCommands.Add("deletesweep", 
992                   "deletesweep wire, To delete a section",
993                   __FILE__,deletesweep,g);
994
995  theCommands.Add("buildsweep", "builsweep [r] [option] [Tol] , no args to get help",
996                   __FILE__,buildsweep,g);
997
998  theCommands.Add("gensweep", "gensweep res subshape_of_profile",
999                   __FILE__,gensweep,g);
1000
1001   theCommands.Add("simulsweep", "simulsweep r [n] [option]"
1002                   __FILE__,simulsweep,g);
1003   theCommands.Add("geompipe", "geompipe r spineedge profileedge radius [byACR [byrotate]]"
1004                   __FILE__,geompipe,g);
1005   
1006   theCommands.Add("middlepath", "middlepath res shape startshape endshape",
1007                   __FILE__,middlepath,g);
1008 }
1009