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