0022492: Scaled sphere (Solid with BSplineSurface) is wrongly exported in STEP.
[occt.git] / src / BRepTest / BRepTest_SurfaceCommands.cxx
1 // File:        DBRep_4.cxx
2 // Created:     Thu Jul 22 16:39:43 1993
3 // Author:      Remi LEQUETTE
4 //              <rle@nonox>
5
6 #ifdef HAVE_CONFIG_H
7 # include <config.h>
8 #endif
9 #include <stdio.h>
10 #include <BRepTest.hxx>
11 #include <GeometryTest.hxx>
12
13 #include <DrawTrSurf.hxx>
14 #include <DBRep.hxx>
15 #include <Draw_Interpretor.hxx>
16 #include <Draw_Appli.hxx>
17
18 #include <BRep_Builder.hxx>
19 #include <BRep_Tool.hxx>
20 #include <BRepLib.hxx>
21 #include <BRepTools_Quilt.hxx>
22 #include <BRepAdaptor_Curve.hxx>
23 #include <BRepBuilderAPI_MakeFace.hxx>
24 #include <BRepBuilderAPI_MakeShell.hxx>
25 #include <BRepBuilderAPI.hxx>
26 #include <BRepBuilderAPI_Sewing.hxx>
27 #include <BRepOffsetAPI_FindContigousEdges.hxx>
28 #include <TopExp_Explorer.hxx>
29 #include <TopoDS.hxx>
30 #include <TopoDS_Face.hxx>
31 #include <Geom_Surface.hxx>
32 #include <Geom2d_TrimmedCurve.hxx>
33 #include <TopTools_ListOfShape.hxx>
34 #include <Precision.hxx>
35
36 #ifdef WNT
37 //#define strcasecmp strcmp Already defined
38 #include <stdio.h>
39 #endif
40 #ifdef HAVE_STRINGS_H
41 # include <strings.h>
42 #endif
43
44 //-----------------------------------------------------------------------
45 // suppressarg : suppress a[d],modifie na--
46 //-----------------------------------------------------------------------
47 static void suppressarg(Standard_Integer& na,const char** a,const Standard_Integer d) 
48 {
49   for(Standard_Integer i=d;i<na;i++) {
50     a[i]=a[i+1];
51     a[i+1]=NULL;
52   }
53   na--;
54 }
55
56
57 //=======================================================================
58 // mkface
59 //=======================================================================
60
61 static Standard_Integer mkface(Draw_Interpretor& , Standard_Integer n, const char** a)
62 {
63   if (n < 3) return 1;
64   
65   Handle(Geom_Surface) S = DrawTrSurf::GetSurface(a[2]);
66   if (S.IsNull()) {
67     cout << a[2] << " is not a surface" << endl;
68     return 1;
69   }
70   
71   Standard_Boolean mkface = a[0][2] == 'f';
72   TopoDS_Shape res;
73
74   Standard_Boolean Segment = Standard_False;
75   if ( !mkface && (n == 4 || n == 8)) {
76     Segment = !strcmp(a[n-1],"1");
77     n--;
78   }
79
80   if (n == 3) {
81     if (mkface)
82       res = BRepBuilderAPI_MakeFace(S, Precision::Confusion());
83     else
84       res = BRepBuilderAPI_MakeShell(S,Segment);
85   }
86   else if (n <= 5) {
87     if (!mkface) return 1;
88     Standard_Boolean orient = (n  == 4);
89     TopoDS_Shape W = DBRep::Get(a[3],TopAbs_WIRE);
90     if (W.IsNull()) return 1;
91     res = BRepBuilderAPI_MakeFace(S,TopoDS::Wire(W),orient);
92   }
93   else {
94     if (mkface)
95       res = BRepBuilderAPI_MakeFace(S,atof(a[3]),atof(a[4]),atof(a[5]),atof(a[6]),Precision::Confusion());
96     else
97       res = BRepBuilderAPI_MakeShell(S,atof(a[3]),atof(a[4]),atof(a[5]),atof(a[6]),
98                               Segment);
99   }
100   
101   DBRep::Set(a[1],res);
102   return 0;
103 }
104
105 //=======================================================================
106 // quilt
107 //=======================================================================
108
109 static Standard_Integer quilt(Draw_Interpretor& , Standard_Integer n, const char** a)
110 {
111   if (n < 4) return 1;
112   BRepTools_Quilt Q;
113
114   Standard_Integer i = 2;
115   while (i < n) {
116     TopoDS_Shape S = DBRep::Get(a[i]);
117     if (!S.IsNull()) {
118       if (S.ShapeType() == TopAbs_EDGE) {
119         if (i+1 < n) {
120           TopoDS_Shape E = DBRep::Get(a[i+1]);
121           if (!E.IsNull()) {
122             if (E.ShapeType() == TopAbs_EDGE) {
123               i++;
124               Q.Bind(TopoDS::Edge(S),TopoDS::Edge(E));
125             }
126           }
127         }
128       }
129       if (S.ShapeType() == TopAbs_VERTEX) {
130         if (i+1 < n) {
131           TopoDS_Shape E = DBRep::Get(a[i+1]);
132           if (!E.IsNull()) {
133             if (E.ShapeType() == TopAbs_VERTEX) {
134               i++;
135               Q.Bind(TopoDS::Vertex(S),TopoDS::Vertex(E));
136             }
137           }
138         }
139       }
140       else {
141         Q.Add(S);
142       }
143     }
144     i++;
145   }
146
147   DBRep::Set(a[1],Q.Shells());
148   return 0;
149 }
150
151
152 //=======================================================================
153 // mksurface
154 //=======================================================================
155
156 static Standard_Integer mksurface(Draw_Interpretor& , Standard_Integer n, const char** a)
157 {
158   if (n < 3) return 1;
159
160   TopoDS_Shape S = DBRep::Get(a[2],TopAbs_FACE);
161   if (S.IsNull()) return 1;
162   TopLoc_Location L;
163   Handle(Geom_Surface) C = BRep_Tool::Surface(TopoDS::Face(S),L);
164
165
166   DrawTrSurf::Set(a[1],C->Transformed(L.Transformation()));
167   return 0;
168 }
169
170 //=======================================================================
171 // mkplane
172 //=======================================================================
173
174 static Standard_Integer mkplane(Draw_Interpretor& , Standard_Integer n, const char** a)
175 {
176   if (n < 3) return 1;
177
178   TopoDS_Shape S = DBRep::Get(a[2],TopAbs_WIRE);
179   if (S.IsNull()) return 1;
180
181   Standard_Boolean OnlyPlane = Standard_False;
182   if ( n == 4) {
183     OnlyPlane =  !strcmp(a[3],"1");
184   }
185
186   TopoDS_Face F = BRepBuilderAPI_MakeFace(TopoDS::Wire(S), OnlyPlane);
187
188   DBRep::Set(a[1],F);
189   return 0;
190 }
191
192 //=======================================================================
193 // pcurve
194 //=======================================================================
195 Standard_IMPORT Draw_Color DrawTrSurf_CurveColor(const Draw_Color col);
196 Standard_IMPORT void DBRep_WriteColorOrientation ();
197 Standard_IMPORT Draw_Color DBRep_ColorOrientation (const TopAbs_Orientation Or);
198
199 static Standard_Integer pcurve(Draw_Interpretor& , Standard_Integer n, const char** a)
200 {
201   Standard_Boolean mute = Standard_False;
202   for(Standard_Integer ia=1;ia<n;ia++) {
203     if (!strcasecmp(a[ia],"-mute")) {
204       suppressarg(n,a,ia);
205       mute = Standard_True;
206     }
207   }
208
209   if (n == 2) {
210     // pcurves of a face
211     TopoDS_Shape S = DBRep::Get(a[1],TopAbs_FACE);
212     if (S.IsNull()) return 1;
213
214     if (!mute) DBRep_WriteColorOrientation();
215     Draw_Color col, savecol = DrawTrSurf_CurveColor(Draw_rouge);
216
217     char* name = new char[100];
218     Standard_Real f,l;
219     S.Orientation(TopAbs_FORWARD);
220     TopExp_Explorer ex(S,TopAbs_EDGE);
221     for (Standard_Integer i=1; ex.More(); ex.Next(), i++) {
222       const Handle(Geom2d_Curve) c = BRep_Tool::CurveOnSurface
223         (TopoDS::Edge(ex.Current()),TopoDS::Face(S),f,l);
224       if ( c.IsNull() ) {
225         cout << "Error: Edge " << i << " does not have pcurve" << endl;
226         continue;
227       }
228       col = DBRep_ColorOrientation(ex.Current().Orientation());
229       DrawTrSurf_CurveColor(col);
230
231       sprintf(name,"%s_%d",a[1],i);
232       DrawTrSurf::Set(name,new Geom2d_TrimmedCurve(c,f,l));
233     }
234     DrawTrSurf_CurveColor(savecol);
235
236   }
237   else if (n >= 4) {
238     TopoDS_Shape SE = DBRep::Get(a[2],TopAbs_EDGE);
239     if (SE.IsNull()) return 1;
240     TopoDS_Shape SF = DBRep::Get(a[3],TopAbs_FACE);
241     if (SF.IsNull()) return 1;
242
243     Draw_Color col, savecol = DrawTrSurf_CurveColor(Draw_rouge);
244     Standard_Real f,l;
245     const Handle(Geom2d_Curve) c = BRep_Tool::CurveOnSurface
246       (TopoDS::Edge(SE),TopoDS::Face(SF),f,l);
247
248     col = DBRep_ColorOrientation(SE.Orientation());
249     DrawTrSurf_CurveColor(col);
250     DrawTrSurf::Set(a[1],new Geom2d_TrimmedCurve(c,f,l));
251     DrawTrSurf_CurveColor(savecol);
252   }
253   else { 
254     return 1;
255   }
256     
257   return 0;
258 }
259
260 //=======================================================================
261 // sewing
262 //=======================================================================
263
264 static Standard_Integer sewing (Draw_Interpretor& , 
265                                 Standard_Integer n, const char** a)
266 {
267   if (n < 3) return (1);
268
269   BRepBuilderAPI_Sewing aSewing;
270   Standard_Integer ntmp = n;
271
272   TopoDS_Shape sh = DBRep::Get(a[2]);
273   Standard_Integer i=2;
274   Standard_Real tol = 1.0e-06;
275   if (sh.IsNull()) {
276     if (n < 4) return (1);
277     tol = atof(a[2]);
278     //Standard_Real tol = atof(a[2]);
279     //aSewing.Init(tol, Standard_True,Standard_True,Standard_True,Standard_False);
280     i = 3;
281   }
282
283   Standard_Boolean NonManifoldMode = Standard_False;
284   sh = DBRep::Get(a[n-1]);
285   if (sh.IsNull()) {
286     // read non manifold mode
287     Standard_Integer nmm = atoi(a[n-1]);
288     if(nmm==1)
289       NonManifoldMode = Standard_True;
290     ntmp--;
291   }
292
293   aSewing.Init(tol, Standard_True,Standard_True,Standard_True,NonManifoldMode);
294   
295   while (i < ntmp) {
296     sh = DBRep::Get(a[i]);
297     aSewing.Add(sh);
298     i++;
299   }
300
301   aSewing.Perform();
302   aSewing.Dump();
303
304   const TopoDS_Shape& sh2 = aSewing.SewedShape();
305   if (!sh2.IsNull()) {
306     DBRep::Set(a[1], sh2);
307   }
308   return 0;
309 }
310
311 //=======================================================================
312 // continuity
313 //=======================================================================
314
315 static Standard_Integer continuity (Draw_Interpretor& , 
316                                     Standard_Integer n, const char** a)
317 {
318   if (n < 2) return (1);
319
320   BRepOffsetAPI_FindContigousEdges aFind;
321
322   TopoDS_Shape sh = DBRep::Get(a[1]);
323   Standard_Integer i=1;
324   if (sh.IsNull()) {
325     if (n < 3) return (1);
326     Standard_Real tol = atof(a[1]);
327     aFind.Init(tol, Standard_False);
328     i = 2;
329   }
330   
331   while (i < n) {
332     sh = DBRep::Get(a[i]);
333     aFind.Add(sh);
334     i++;
335   }
336
337   aFind.Perform();
338   aFind.Dump();
339
340   for (i=1; i<=aFind.NbContigousEdges(); i++) {
341 #ifdef DEB
342     const TopoDS_Edge& edge =
343 #endif
344                               aFind.ContigousEdge(i);
345     const TopTools_ListOfShape& list = aFind.ContigousEdgeCouple(i);
346     const TopoDS_Edge& sec1 = TopoDS::Edge(list.First());
347     const TopoDS_Edge& sec2 = TopoDS::Edge(list.Last());
348 #ifdef DEB
349     const TopoDS_Edge& bound1 =
350 #endif
351                                 aFind.SectionToBoundary(sec1);
352 #ifdef DEB
353     const TopoDS_Edge& bound2 =
354 #endif
355                                 aFind.SectionToBoundary(sec2);
356   }
357
358   return 0;
359 }
360
361 //=======================================================================
362 // encoderegularity
363 //=======================================================================
364 static Standard_Integer encoderegularity (Draw_Interpretor& , 
365                                           Standard_Integer n, const char** a)
366
367 {
368   if (n < 2) return 1;
369   TopoDS_Shape sh = DBRep::Get(a[1]);
370   if (sh.IsNull()) return 1;
371   if (n==2) 
372     BRepLib::EncodeRegularity(sh);
373   else {
374     Standard_Real Tol = atof(a[2]);
375     Tol *= PI/180.;
376     BRepLib::EncodeRegularity(sh, Tol);
377   }
378   return 0;
379 }
380
381
382 //=======================================================================
383 //function : SurfaceCommands
384 //purpose  : 
385 //=======================================================================
386
387 void  BRepTest::SurfaceCommands(Draw_Interpretor& theCommands)
388 {
389   static Standard_Boolean done = Standard_False;
390   if (done) return;
391   done = Standard_True;
392
393   DBRep::BasicCommands(theCommands);
394   GeometryTest::SurfaceCommands(theCommands);
395
396   const char* g = "Surface topology commands";
397
398   theCommands.Add("mkface",
399                   "mkface facename surfacename [ufirst ulast vfirst vlast] [wire [norient]]",
400                   __FILE__,mkface,g);
401
402   theCommands.Add("mkshell",
403                   "mkshell shellname surfacename [ufirst ulast vfirst vlast] [segment 0/1]",
404                   __FILE__,mkface,g);
405
406   theCommands.Add("quilt",
407                   "quilt compoundname shape1 edgeshape2  edgeshape1... shape2  edgeshape3 edgeshape1or2 ... shape3 ...",
408                   __FILE__,quilt,g);
409   
410   theCommands.Add("mksurface",
411                   "mksurface surfacename facename",
412                   __FILE__,mksurface,g);
413
414   theCommands.Add("mkplane",
415                   "mkplane facename wirename [OnlyPlane 0/1]",
416                   __FILE__,mkplane,g);
417
418   theCommands.Add("pcurve",
419                   "pcurve [name edgename] facename",
420                   __FILE__,pcurve,g);
421
422   theCommands.Add("sewing",
423                   "sewing result [tolerance] shape1 shape2 ...",
424                   __FILE__,sewing, g);
425
426   theCommands.Add("continuity", 
427                   "continuity [tolerance] shape1 shape2 ...",
428                   __FILE__,continuity, g);
429
430   theCommands.Add("encoderegularity", 
431                   "encoderegularity shape [tolerance (in degree)]",
432                   __FILE__,encoderegularity, g);
433 }
434