0024166: Unable to create file with "Save" menu of voxeldemo Qt sample
[occt.git] / src / BRepTest / BRepTest_SurfaceCommands.cxx
CommitLineData
b311480e 1// Created on: 1993-07-22
2// Created by: Remi LEQUETTE
3// Copyright (c) 1993-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
7fd59977 21
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif
25#include <stdio.h>
26#include <BRepTest.hxx>
27#include <GeometryTest.hxx>
28
29#include <DrawTrSurf.hxx>
30#include <DBRep.hxx>
31#include <Draw_Interpretor.hxx>
32#include <Draw_Appli.hxx>
33
34#include <BRep_Builder.hxx>
35#include <BRep_Tool.hxx>
36#include <BRepLib.hxx>
37#include <BRepTools_Quilt.hxx>
38#include <BRepAdaptor_Curve.hxx>
39#include <BRepBuilderAPI_MakeFace.hxx>
40#include <BRepBuilderAPI_MakeShell.hxx>
41#include <BRepBuilderAPI.hxx>
42#include <BRepBuilderAPI_Sewing.hxx>
43#include <BRepOffsetAPI_FindContigousEdges.hxx>
44#include <TopExp_Explorer.hxx>
45#include <TopoDS.hxx>
46#include <TopoDS_Face.hxx>
47#include <Geom_Surface.hxx>
48#include <Geom2d_TrimmedCurve.hxx>
49#include <TopTools_ListOfShape.hxx>
d63f9881 50#include <TopTools_SequenceOfShape.hxx>
1c72dff6 51#include <Precision.hxx>
92434a36 52#include <Draw_ProgressIndicator.hxx>
7fd59977 53
54#ifdef WNT
55//#define strcasecmp strcmp Already defined
56#include <stdio.h>
57#endif
58#ifdef HAVE_STRINGS_H
59# include <strings.h>
60#endif
61
62//-----------------------------------------------------------------------
63// suppressarg : suppress a[d],modifie na--
64//-----------------------------------------------------------------------
65static void suppressarg(Standard_Integer& na,const char** a,const Standard_Integer d)
66{
67 for(Standard_Integer i=d;i<na;i++) {
68 a[i]=a[i+1];
69 a[i+1]=NULL;
70 }
71 na--;
72}
73
74
75//=======================================================================
76// mkface
77//=======================================================================
78
79static Standard_Integer mkface(Draw_Interpretor& , Standard_Integer n, const char** a)
80{
81 if (n < 3) return 1;
82
83 Handle(Geom_Surface) S = DrawTrSurf::GetSurface(a[2]);
84 if (S.IsNull()) {
85 cout << a[2] << " is not a surface" << endl;
86 return 1;
87 }
88
89 Standard_Boolean mkface = a[0][2] == 'f';
90 TopoDS_Shape res;
91
92 Standard_Boolean Segment = Standard_False;
93 if ( !mkface && (n == 4 || n == 8)) {
94 Segment = !strcmp(a[n-1],"1");
95 n--;
96 }
97
98 if (n == 3) {
99 if (mkface)
1c72dff6 100 res = BRepBuilderAPI_MakeFace(S, Precision::Confusion());
7fd59977 101 else
102 res = BRepBuilderAPI_MakeShell(S,Segment);
103 }
104 else if (n <= 5) {
105 if (!mkface) return 1;
106 Standard_Boolean orient = (n == 4);
107 TopoDS_Shape W = DBRep::Get(a[3],TopAbs_WIRE);
108 if (W.IsNull()) return 1;
109 res = BRepBuilderAPI_MakeFace(S,TopoDS::Wire(W),orient);
110 }
111 else {
112 if (mkface)
91322f44 113 res = BRepBuilderAPI_MakeFace(S,Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]),Draw::Atof(a[6]),Precision::Confusion());
7fd59977 114 else
91322f44 115 res = BRepBuilderAPI_MakeShell(S,Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]),Draw::Atof(a[6]),
7fd59977 116 Segment);
117 }
118
119 DBRep::Set(a[1],res);
120 return 0;
121}
122
123//=======================================================================
124// quilt
125//=======================================================================
126
127static Standard_Integer quilt(Draw_Interpretor& , Standard_Integer n, const char** a)
128{
129 if (n < 4) return 1;
130 BRepTools_Quilt Q;
131
132 Standard_Integer i = 2;
133 while (i < n) {
134 TopoDS_Shape S = DBRep::Get(a[i]);
135 if (!S.IsNull()) {
136 if (S.ShapeType() == TopAbs_EDGE) {
137 if (i+1 < n) {
138 TopoDS_Shape E = DBRep::Get(a[i+1]);
139 if (!E.IsNull()) {
140 if (E.ShapeType() == TopAbs_EDGE) {
141 i++;
142 Q.Bind(TopoDS::Edge(S),TopoDS::Edge(E));
143 }
144 }
145 }
146 }
147 if (S.ShapeType() == TopAbs_VERTEX) {
148 if (i+1 < n) {
149 TopoDS_Shape E = DBRep::Get(a[i+1]);
150 if (!E.IsNull()) {
151 if (E.ShapeType() == TopAbs_VERTEX) {
152 i++;
153 Q.Bind(TopoDS::Vertex(S),TopoDS::Vertex(E));
154 }
155 }
156 }
157 }
158 else {
159 Q.Add(S);
160 }
161 }
162 i++;
163 }
164
165 DBRep::Set(a[1],Q.Shells());
166 return 0;
167}
168
169
170//=======================================================================
171// mksurface
172//=======================================================================
173
174static Standard_Integer mksurface(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_FACE);
179 if (S.IsNull()) return 1;
180 TopLoc_Location L;
181 Handle(Geom_Surface) C = BRep_Tool::Surface(TopoDS::Face(S),L);
182
183
184 DrawTrSurf::Set(a[1],C->Transformed(L.Transformation()));
185 return 0;
186}
187
188//=======================================================================
189// mkplane
190//=======================================================================
191
192static Standard_Integer mkplane(Draw_Interpretor& , Standard_Integer n, const char** a)
193{
194 if (n < 3) return 1;
195
196 TopoDS_Shape S = DBRep::Get(a[2],TopAbs_WIRE);
197 if (S.IsNull()) return 1;
198
199 Standard_Boolean OnlyPlane = Standard_False;
200 if ( n == 4) {
201 OnlyPlane = !strcmp(a[3],"1");
202 }
203
204 TopoDS_Face F = BRepBuilderAPI_MakeFace(TopoDS::Wire(S), OnlyPlane);
205
206 DBRep::Set(a[1],F);
207 return 0;
208}
209
210//=======================================================================
211// pcurve
212//=======================================================================
213Standard_IMPORT Draw_Color DrawTrSurf_CurveColor(const Draw_Color col);
214Standard_IMPORT void DBRep_WriteColorOrientation ();
215Standard_IMPORT Draw_Color DBRep_ColorOrientation (const TopAbs_Orientation Or);
216
217static Standard_Integer pcurve(Draw_Interpretor& , Standard_Integer n, const char** a)
218{
219 Standard_Boolean mute = Standard_False;
220 for(Standard_Integer ia=1;ia<n;ia++) {
221 if (!strcasecmp(a[ia],"-mute")) {
222 suppressarg(n,a,ia);
223 mute = Standard_True;
224 }
225 }
226
227 if (n == 2) {
228 // pcurves of a face
229 TopoDS_Shape S = DBRep::Get(a[1],TopAbs_FACE);
230 if (S.IsNull()) return 1;
231
232 if (!mute) DBRep_WriteColorOrientation();
233 Draw_Color col, savecol = DrawTrSurf_CurveColor(Draw_rouge);
234
235 char* name = new char[100];
236 Standard_Real f,l;
237 S.Orientation(TopAbs_FORWARD);
238 TopExp_Explorer ex(S,TopAbs_EDGE);
239 for (Standard_Integer i=1; ex.More(); ex.Next(), i++) {
240 const Handle(Geom2d_Curve) c = BRep_Tool::CurveOnSurface
241 (TopoDS::Edge(ex.Current()),TopoDS::Face(S),f,l);
242 if ( c.IsNull() ) {
243 cout << "Error: Edge " << i << " does not have pcurve" << endl;
244 continue;
245 }
246 col = DBRep_ColorOrientation(ex.Current().Orientation());
247 DrawTrSurf_CurveColor(col);
248
91322f44 249 Sprintf(name,"%s_%d",a[1],i);
7fd59977 250 DrawTrSurf::Set(name,new Geom2d_TrimmedCurve(c,f,l));
251 }
252 DrawTrSurf_CurveColor(savecol);
253
254 }
255 else if (n >= 4) {
256 TopoDS_Shape SE = DBRep::Get(a[2],TopAbs_EDGE);
257 if (SE.IsNull()) return 1;
258 TopoDS_Shape SF = DBRep::Get(a[3],TopAbs_FACE);
259 if (SF.IsNull()) return 1;
260
261 Draw_Color col, savecol = DrawTrSurf_CurveColor(Draw_rouge);
262 Standard_Real f,l;
263 const Handle(Geom2d_Curve) c = BRep_Tool::CurveOnSurface
264 (TopoDS::Edge(SE),TopoDS::Face(SF),f,l);
265
266 col = DBRep_ColorOrientation(SE.Orientation());
267 DrawTrSurf_CurveColor(col);
268 DrawTrSurf::Set(a[1],new Geom2d_TrimmedCurve(c,f,l));
269 DrawTrSurf_CurveColor(savecol);
270 }
271 else {
272 return 1;
273 }
274
275 return 0;
276}
277
278//=======================================================================
279// sewing
280//=======================================================================
281
92434a36 282static Standard_Integer sewing (Draw_Interpretor& theDi,
d63f9881 283 Standard_Integer theArgc, const char** theArgv)
7fd59977 284{
7fd59977 285 BRepBuilderAPI_Sewing aSewing;
d63f9881 286 Standard_Integer aPar = 1;
287 TopTools_SequenceOfShape aSeq;
288
289 Standard_Real aTol = 1.0e-06;
290 Standard_Boolean aSewingMode = Standard_True;
291 Standard_Boolean anAnalysisMode = Standard_True;
292 Standard_Boolean aCuttingMode = Standard_True;
293 Standard_Boolean aNonManifoldMode = Standard_False;
294 Standard_Boolean aSameParameterMode = Standard_True;
295 Standard_Boolean aFloatingEdgesMode = Standard_False;
296 Standard_Boolean aFaceMode = Standard_True;
dc161d81
A
297 Standard_Boolean aSetMinTol = Standard_False;
298 Standard_Real aMinTol = 0.;
d63f9881 299 Standard_Real aMaxTol = Precision::Infinite();
300
301 for (Standard_Integer i = 2; i < theArgc; i++)
302 {
303 if (theArgv[i][0] == '-' || theArgv[i][0] == '+')
304 {
305 Standard_Boolean aVal = (theArgv[i][0] == '+' ? Standard_True : Standard_False);
306 switch (tolower(theArgv[i][1]))
307 {
308 case 'm':
309 {
310 if (tolower(theArgv[i][2]) == 'i' && i+1 < theArgc)
311 {
91322f44 312 if (Draw::Atof (theArgv[i+1]))
dc161d81 313 {
91322f44 314 aMinTol = Draw::Atof (theArgv[++i]);
dc161d81
A
315 aSetMinTol = Standard_True;
316 }
d63f9881 317 else
318 {
319 theDi << "Error! min tolerance can't possess the null value" << "\n";
320 return (1);
321 }
322 }
323 if (tolower(theArgv[i][2]) == 'a' && i+1 < theArgc)
324 {
91322f44 325 if (Draw::Atof (theArgv[i+1]))
326 aMaxTol = Draw::Atof (theArgv[++i]);
d63f9881 327 else
328 {
329 theDi << "Error! max tolerance can't possess the null value" << "\n";
330 return (1);
331 }
332 }
333 }
334 break;
335 case 's': aSewingMode = aVal; break;
336 case 'a': anAnalysisMode = aVal; break;
337 case 'c': aCuttingMode = aVal; break;
338 case 'n': aNonManifoldMode = aVal; break;
339 case 'p': aSameParameterMode = aVal; break;
340 case 'e': aFloatingEdgesMode = aVal; break;
341 case 'f': aFaceMode = aVal; break;
342 }
343 }
344 else
345 {
dc161d81
A
346 TopoDS_Shape aShape = DBRep::Get (theArgv[i]);
347 if (!aShape.IsNull())
d63f9881 348 {
d63f9881 349 aSeq.Append (aShape);
350 aPar++;
351 }
dc161d81
A
352 else
353 {
91322f44 354 if (Draw::Atof (theArgv[i]))
355 aTol = Draw::Atof (theArgv[i]);
dc161d81 356 }
d63f9881 357 }
7fd59977 358 }
d63f9881 359
360 if (aPar < 2)
361 {
362 theDi << "Use: " << theArgv[0] << " result [tolerance] shape1 shape2 ... [min tolerance] [max tolerance] [switches]" << "\n";
363 theDi << "To set user's value of min/max tolerances the following syntax is used: +<parameter> <value>" << "\n";
364 theDi << "- parameters are identified by letters:" << "\n";
365 theDi << " mint - min tolerance" << "\n";
366 theDi << " maxt - max tolerance" << "\n";
367 theDi << "Switches allow to tune other parameters of Sewing" << "\n";
368 theDi << "The following syntax is used: <symbol><parameter>" << "\n";
369 theDi << "- symbol may be - to set parameter off, + to set on" << "\n";
370 theDi << "- parameters are identified by letters:" << "\n";
371 theDi << " s - mode for creating sewed shape" << "\n";
372 theDi << " a - mode for analysis of input shapes" << "\n";
373 theDi << " c - mode for cutting of free edges" << "\n";
374 theDi << " n - mode for non manifold processing" << "\n";
375 theDi << " p - mode for same parameter processing for edges" << "\n";
376 theDi << " e - mode for sewing floating edges" << "\n";
377 theDi << " f - mode for sewing faces" << "\n";
378 return (1);
7fd59977 379 }
d63f9881 380
dc161d81
A
381 if (!aSetMinTol)
382 aMinTol = aTol*1e-4;
d63f9881 383 if (aTol < Precision::Confusion())
384 aTol = Precision::Confusion();
385 if (aMinTol < Precision::Confusion())
386 aMinTol = Precision::Confusion();
387 if (aMinTol > aTol)
388 {
389 theDi << "Error! min tolerance can't exceed working tolerance" << "\n";
390 return (1);
391 }
392 if (aMaxTol < aTol)
393 {
394 theDi << "Error! max tolerance can't be less than working tolerance" << "\n";
395 return (1);
7fd59977 396 }
d63f9881 397
398 aSewing.Init (aTol, aSewingMode, anAnalysisMode, aCuttingMode, aNonManifoldMode);
399 aSewing.SetSameParameterMode (aSameParameterMode);
400 aSewing.SetFloatingEdgesMode (aFloatingEdgesMode);
401 aSewing.SetFaceMode (aFaceMode);
402 aSewing.SetMinTolerance (aMinTol);
403 aSewing.SetMaxTolerance (aMaxTol);
404
405 for (Standard_Integer i = 1; i <= aSeq.Length(); i++)
406 aSewing.Add(aSeq.Value(i));
407
92434a36
A
408 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator (theDi, 1);
409 aSewing.Perform (aProgress);
7fd59977 410 aSewing.Dump();
d63f9881 411
412 const TopoDS_Shape& aRes = aSewing.SewedShape();
413 if (!aRes.IsNull())
414 DBRep::Set(theArgv[1], aRes);
7fd59977 415 return 0;
416}
417
418//=======================================================================
419// continuity
420//=======================================================================
421
422static Standard_Integer continuity (Draw_Interpretor& ,
423 Standard_Integer n, const char** a)
424{
425 if (n < 2) return (1);
426
427 BRepOffsetAPI_FindContigousEdges aFind;
428
429 TopoDS_Shape sh = DBRep::Get(a[1]);
430 Standard_Integer i=1;
431 if (sh.IsNull()) {
432 if (n < 3) return (1);
91322f44 433 Standard_Real tol = Draw::Atof(a[1]);
7fd59977 434 aFind.Init(tol, Standard_False);
435 i = 2;
436 }
437
438 while (i < n) {
439 sh = DBRep::Get(a[i]);
440 aFind.Add(sh);
441 i++;
442 }
443
444 aFind.Perform();
445 aFind.Dump();
446
7fd59977 447 return 0;
448}
449
450//=======================================================================
451// encoderegularity
452//=======================================================================
453static Standard_Integer encoderegularity (Draw_Interpretor& ,
454 Standard_Integer n, const char** a)
455
456{
457 if (n < 2) return 1;
458 TopoDS_Shape sh = DBRep::Get(a[1]);
459 if (sh.IsNull()) return 1;
460 if (n==2)
461 BRepLib::EncodeRegularity(sh);
462 else {
91322f44 463 Standard_Real Tol = Draw::Atof(a[2]);
c6541a0c 464 Tol *= M_PI/180.;
7fd59977 465 BRepLib::EncodeRegularity(sh, Tol);
466 }
467 return 0;
468}
469
470
471//=======================================================================
472//function : SurfaceCommands
473//purpose :
474//=======================================================================
475
476void BRepTest::SurfaceCommands(Draw_Interpretor& theCommands)
477{
478 static Standard_Boolean done = Standard_False;
479 if (done) return;
480 done = Standard_True;
481
482 DBRep::BasicCommands(theCommands);
483 GeometryTest::SurfaceCommands(theCommands);
484
485 const char* g = "Surface topology commands";
486
487 theCommands.Add("mkface",
488 "mkface facename surfacename [ufirst ulast vfirst vlast] [wire [norient]]",
489 __FILE__,mkface,g);
490
491 theCommands.Add("mkshell",
492 "mkshell shellname surfacename [ufirst ulast vfirst vlast] [segment 0/1]",
493 __FILE__,mkface,g);
494
495 theCommands.Add("quilt",
496 "quilt compoundname shape1 edgeshape2 edgeshape1... shape2 edgeshape3 edgeshape1or2 ... shape3 ...",
497 __FILE__,quilt,g);
498
499 theCommands.Add("mksurface",
500 "mksurface surfacename facename",
501 __FILE__,mksurface,g);
502
503 theCommands.Add("mkplane",
504 "mkplane facename wirename [OnlyPlane 0/1]",
505 __FILE__,mkplane,g);
506
507 theCommands.Add("pcurve",
508 "pcurve [name edgename] facename",
509 __FILE__,pcurve,g);
510
511 theCommands.Add("sewing",
d63f9881 512 "sewing result [tolerance] shape1 shape2 ... [min tolerance] [max tolerance] [switches]",
7fd59977 513 __FILE__,sewing, g);
514
515 theCommands.Add("continuity",
516 "continuity [tolerance] shape1 shape2 ...",
517 __FILE__,continuity, g);
518
519 theCommands.Add("encoderegularity",
520 "encoderegularity shape [tolerance (in degree)]",
521 __FILE__,encoderegularity, g);
522}
523