1 // Created on: 1999-03-09
2 // Created by: Roman LYGIN
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
18 #include <BRep_Tool.hxx>
21 #include <DrawTrSurf.hxx>
22 #include <Geom2d_BezierCurve.hxx>
23 #include <Geom2d_BSplineCurve.hxx>
24 #include <Geom2d_Curve.hxx>
25 #include <Geom2d_OffsetCurve.hxx>
26 #include <Geom2d_TrimmedCurve.hxx>
27 #include <Geom_BezierCurve.hxx>
28 #include <Geom_BezierSurface.hxx>
29 #include <Geom_BSplineCurve.hxx>
30 #include <Geom_BSplineSurface.hxx>
31 #include <Geom_Curve.hxx>
32 #include <Geom_OffsetCurve.hxx>
33 #include <Geom_OffsetSurface.hxx>
34 #include <Geom_Plane.hxx>
35 #include <Geom_RectangularTrimmedSurface.hxx>
36 #include <Geom_Surface.hxx>
37 #include <Geom_SweptSurface.hxx>
38 #include <Geom_TrimmedCurve.hxx>
39 #include <GeomAbs_Shape.hxx>
40 #include <ShapeCustom.hxx>
41 #include <ShapeCustom_BSplineRestriction.hxx>
42 #include <ShapeCustom_RestrictionParameters.hxx>
43 #include <ShapeFix.hxx>
45 #include <SWDRAW_ShapeCustom.hxx>
46 #include <TColStd_Array1OfReal.hxx>
47 #include <TColStd_Array2OfInteger.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <TopLoc_Location.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Face.hxx>
54 #include <TopoDS_Shape.hxx>
56 static Standard_Integer ContToInteger( const GeomAbs_Shape Cont)
58 Standard_Integer result =0;
61 case GeomAbs_G1: result = 0; break;
63 case GeomAbs_G2: result = 1; break;
64 case GeomAbs_C2: result = 2; break;
65 case GeomAbs_C3: result = 3; break;
66 default : result = 4; break;
71 static Standard_Integer directfaces(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
73 if (argc < 3) { di<<"Donner un nom de SHAPE + un nom de RESULTAT\n"; return 1 /* Error */; }
74 Standard_CString arg1 = argv[1];
75 Standard_CString arg2 = argv[2];
76 TopoDS_Shape Shape = DBRep::Get(arg2);
77 if (Shape.IsNull()) { di<<"Shape unknown : "<<arg2<<"\n"; return 1 /* Error */; }
79 TopoDS_Shape result = ShapeCustom::DirectFaces (Shape);
80 if (result.IsNull()) { di<<"NO RESULT\n"; return 1; }
81 else if (result == Shape) { di<<"No modif\n"; return 0; }
82 di<<"DirectFaces -> Result : "<<arg1<<"\n";
83 DBRep::Set (arg1,result);
87 static Standard_Integer ckeckKnots(const TColStd_Array1OfReal& theKnots,
88 Standard_Real theFirst,
89 Standard_Real theLast)
91 Standard_Integer i =1, nb = theKnots.Length();
92 Standard_Integer aNum =0;
97 Standard_Real aDF = theKnots(i) - theFirst;
98 if(fabs(aDF) <= Precision::PConfusion() ||
99 aDF > Precision::PConfusion())
107 Standard_Real aDL = theKnots(i) - theLast;
108 if(fabs(aDL) <= Precision::PConfusion() ||
109 aDL > Precision::PConfusion())
119 static void expcurv2d(const Handle(Geom2d_Curve)& aCurve, TColStd_Array2OfInteger& Nb, const Standard_Integer Degree,
120 const Standard_Integer MaxSeg,
121 const Standard_Integer theCont,
122 Standard_Real theFirst,
123 Standard_Real theLast)
125 if(aCurve.IsNull()) return;
127 if (aCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
128 Handle(Geom2d_TrimmedCurve) tmp = Handle(Geom2d_TrimmedCurve)::DownCast (aCurve);
129 Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
130 expcurv2d(BasCurve,Nb,Degree,MaxSeg,theCont,theFirst,theLast);
133 if (aCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve))) {
134 Handle(Geom2d_OffsetCurve) tmp = Handle(Geom2d_OffsetCurve)::DownCast (aCurve);
135 Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
136 expcurv2d(BasCurve,Nb,Degree,MaxSeg,theCont,theFirst,theLast);
139 Standard_Integer aCont = ContToInteger(aCurve->Continuity());
140 if(aCurve->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve))) {
141 Handle(Geom2d_BSplineCurve) Bs = Handle(Geom2d_BSplineCurve)::DownCast(aCurve);
142 if(Bs->Degree() > Degree) Nb.ChangeValue(1,1)++;
143 if(Bs->NbKnots() -1 > MaxSeg) Nb.ChangeValue(1,2)++;
144 if(Bs->IsRational()) Nb.ChangeValue(1,3)++;
145 if(aCont < theCont && Bs->NbKnots() > 2)
147 TColStd_Array1OfReal aKnots(1,Bs->NbKnots());
149 Standard_Integer nbInt = ckeckKnots(aKnots,theFirst,theLast);
151 Nb.ChangeValue(1,4)++;
155 if (aCurve->IsKind(STANDARD_TYPE(Geom2d_BezierCurve))) {
156 Handle(Geom2d_BezierCurve) Bs = Handle(Geom2d_BezierCurve)::DownCast(aCurve);
157 if(Bs->Degree() > Degree) Nb.ChangeValue(2,1)++;
158 if(Bs->IsRational()) Nb.ChangeValue(2,3)++;
162 Nb.ChangeValue(2,4)++;
167 Nb.ChangeValue(1,5)++;
171 static void expcurv(const Handle(Geom_Curve)& aCurve, TColStd_Array2OfInteger& Nb, const Standard_Integer Degree,
172 const Standard_Integer MaxSeg,
173 const Standard_Integer theCont,
174 Standard_Real theFirst,
175 Standard_Real theLast)
177 if(aCurve.IsNull()) return;
178 if (aCurve->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
179 Handle(Geom_TrimmedCurve) tmp = Handle(Geom_TrimmedCurve)::DownCast (aCurve);
180 Handle(Geom_Curve) BasCurve = tmp->BasisCurve();
181 expcurv(BasCurve,Nb,Degree,MaxSeg,theCont,theFirst,theLast);
184 if (aCurve->IsKind(STANDARD_TYPE(Geom_OffsetCurve))) {
185 Handle(Geom_OffsetCurve) tmp = Handle(Geom_OffsetCurve)::DownCast (aCurve);
186 Handle(Geom_Curve) BasCurve = tmp->BasisCurve();
187 expcurv(BasCurve,Nb,Degree,MaxSeg,theCont,theFirst,theLast);
191 Standard_Integer aCont = ContToInteger(aCurve->Continuity());
192 if(aCurve->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) {
193 Handle(Geom_BSplineCurve) Bs = Handle(Geom_BSplineCurve)::DownCast(aCurve);
194 if(Bs->Degree() > Degree) Nb.ChangeValue(1,1)++;
195 if(Bs->NbKnots() -1 > MaxSeg) Nb.ChangeValue(1,2)++;
196 if(Bs->IsRational()) Nb.ChangeValue(1,3)++;
197 if(aCont < theCont && Bs->NbKnots() > 2)
199 TColStd_Array1OfReal aKnots(1,Bs->NbKnots());
201 Standard_Integer nbInt = ckeckKnots(aKnots,theFirst,theLast);
203 Nb.ChangeValue(1,4)++;
207 if (aCurve->IsKind(STANDARD_TYPE(Geom_BezierCurve))) {
208 Handle(Geom_BezierCurve) Bs = Handle(Geom_BezierCurve)::DownCast(aCurve);
209 if(Bs->Degree() > Degree) Nb.ChangeValue(2,1)++;
210 if(Bs->IsRational()) Nb.ChangeValue(2,3)++;
212 Nb.ChangeValue(2,4)++;
216 Nb.ChangeValue(1,5)++;
219 static void expsurf(const Handle(Geom_Surface)& aSurface, TColStd_Array2OfInteger& NbSurf, const Standard_Integer Degree,
220 const Standard_Integer MaxSeg,
221 const Standard_Integer theCont)
223 if (aSurface.IsNull()) return;
224 if (aSurface->IsKind(STANDARD_TYPE(Geom_SweptSurface))) {
225 Handle(Geom_SweptSurface) aSurf = Handle(Geom_SweptSurface)::DownCast(aSurface);
226 Handle(Geom_Curve) BasCurve = aSurf->BasisCurve();
227 expcurv(BasCurve,NbSurf,Degree,MaxSeg,theCont,BasCurve->FirstParameter(),
228 BasCurve->LastParameter());
231 if (aSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
232 Handle(Geom_RectangularTrimmedSurface) aSurf = Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
233 Handle(Geom_Surface) theSurf = aSurf->BasisSurface();
234 expsurf(theSurf,NbSurf,Degree,MaxSeg,theCont);
237 if(aSurface->IsKind(STANDARD_TYPE(Geom_OffsetSurface))) {
238 Handle(Geom_OffsetSurface) aSurf = Handle(Geom_OffsetSurface)::DownCast(aSurface);
239 Handle(Geom_Surface) theSurf = aSurf->BasisSurface();
240 expsurf(theSurf,NbSurf,Degree,MaxSeg,theCont);
244 Standard_Integer aCont = ContToInteger(aSurface->Continuity());
246 if (aSurface->IsKind(STANDARD_TYPE(Geom_BSplineSurface))) {
247 Handle(Geom_BSplineSurface) BS = Handle(Geom_BSplineSurface)::DownCast(aSurface);
248 if(BS->UDegree() > Degree || BS->VDegree() > Degree) NbSurf.ChangeValue(1,1)++;
249 if(((BS->NbUKnots()-1) * (BS->NbVKnots()-1)) >MaxSeg) NbSurf.ChangeValue(1,2)++;
250 if(BS->IsURational() || BS->IsVRational()) NbSurf.ChangeValue(1,3)++;
252 NbSurf.ChangeValue(1,4)++;
255 if (aSurface->IsKind(STANDARD_TYPE(Geom_BezierSurface))) {
256 Handle(Geom_BezierSurface) BS = Handle(Geom_BezierSurface)::DownCast(aSurface);
257 if(BS->UDegree() > Degree || BS->VDegree() > Degree) NbSurf.ChangeValue(2,1)++;
258 if(BS->IsURational() || BS->IsVRational()) NbSurf.ChangeValue(2,3)++;
260 NbSurf.ChangeValue(2,4)++;
263 if(aSurface->IsKind(STANDARD_TYPE(Geom_Plane)))
265 NbSurf.ChangeValue(1,5)++;
268 NbSurf.ChangeValue(2,5)++;
271 static Standard_Integer expshape(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
274 di << "Incorrect number of arguments. Must be 3\n";
275 return 1 /* Error */;
277 Standard_CString arg2 = argv[1];
278 TopoDS_Shape Shape = DBRep::Get(arg2);
279 Standard_Integer Degree = Draw::Atoi(argv[2]);
280 Standard_Integer MaxSeg = Draw::Atoi(argv[3]);
281 GeomAbs_Shape aCont3 = GeomAbs_C0;
282 Standard_Integer k = 4;
285 if(strcmp(argv[k],"C0") == 0) aCont3 = GeomAbs_C0;
286 else if (strcmp(argv[k],"C1") == 0) aCont3 = GeomAbs_C1;
287 else if (strcmp(argv[k],"C2") == 0) aCont3 = GeomAbs_C2;
288 else if (strcmp(argv[k],"C3") == 0) aCont3 = GeomAbs_C3;
289 else if (strcmp(argv[k],"CN") == 0) aCont3 = GeomAbs_CN;
290 else if (strcmp(argv[k],"G1") == 0) aCont3 = GeomAbs_C0;
291 else if (strcmp(argv[k],"G2") == 0) aCont3 = GeomAbs_C1;
293 di<< "Invalid argument Cont3e\n"; return 1; }
298 Standard_Integer aCont = ContToInteger(aCont3);
299 TColStd_Array2OfInteger NbSurf(1,2,1,5);
300 TColStd_Array2OfInteger NbCurv(1,2,1,5);
301 TColStd_Array2OfInteger NbCurv2d(1,2,1,5);
302 Standard_Integer nbSeam =0;
306 if (Shape.IsNull()) { di << "Shape unknown: " << arg2 << "\n"; return 1 /* Error */; }
309 Standard_Integer nbF =1;
310 for (Ex.Init(Shape,TopAbs_FACE); Ex.More(); Ex.Next(),nbF++) {
311 TopoDS_Face F = TopoDS::Face(Ex.Current());
313 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(F,L);
314 expsurf(aSurface,NbSurf,Degree,MaxSeg,aCont);
317 Standard_Integer nbE = 1;
318 for(exp.Init(F,TopAbs_EDGE); exp.More(); exp.Next(),nbE++) {
319 TopoDS_Edge E = TopoDS::Edge(exp.Current());
320 if(BRep_Tool::IsClosed(E,F))
322 Standard_Real First,Last;
323 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(E,L,First, Last);
324 expcurv(aCurve,NbCurv,Degree,MaxSeg,aCont,First, Last);
325 Standard_Real First2d,Last2d;
326 Handle(Geom2d_Curve) aCurve2d = BRep_Tool::CurveOnSurface(E,F,First2d,Last2d);
327 expcurv2d(aCurve2d,NbCurv2d,Degree,MaxSeg,aCont,First2d,Last2d);
330 di<<"Number of seam edges - "<<nbSeam<<"\n";
331 //if(NbSurf.Value(1,1) !=0)
332 di<< "Number of BSpline surfaces with degree more then "<< Degree << " - " <<NbSurf.Value(1,1)<<"\n";
333 //if(NbSurf.Value(1,2) !=0)
334 di<< "Number of BSpline surfaces with number of spans more then "<< MaxSeg << " - " <<NbSurf.Value(1,2)<<"\n";
335 //if(NbSurf.Value(1,3) !=0)
336 di<< "Number of Rational BSpline surfaces " << NbSurf.Value(1,3)<<"\n";
337 //if(NbSurf.Value(1,4) !=0)
338 di<< "Number of BSpline surfaces with continuity less than specified - " << NbSurf.Value(1,4)<<"\n";
339 //if(NbSurf.Value(2,1) !=0)
340 di<< "Number of Bezier surfaces with degree more then "<< Degree << " - " <<NbSurf.Value(2,1)<<"\n";
341 //if(NbSurf.Value(2,3) !=0)
342 di<< "Number of Rational Bezier surfaces - " <<NbSurf.Value(2,3)<<"\n";
343 //if(NbSurf.Value(2,4) !=0)
344 di<< "Number of Bezier surfaces with continuity less than specified - " << NbSurf.Value(2,4)<<"\n";
346 //if(NbSurf.Value(1,5) !=0)
347 di<< "Number of Planes - " << NbSurf.Value(1,5)<<"\n";
348 //if(NbSurf.Value(2,5) !=0)
349 di<< "Number of other surfaces - " << NbSurf.Value(2,5)<<"\n";
351 //if(NbCurv.Value(1,1) !=0)
352 di<< "Number of BSpline curves with degree more then "<< Degree << " - " <<NbCurv.Value(1,1)<<"\n";
353 //if(NbCurv.Value(1,2) !=0)
354 di<< "Number of BSpline curves with number of spans more then - "<< MaxSeg << " - " <<NbCurv.Value(1,2)<<"\n";
355 //if(NbCurv.Value(1,3) !=0)
356 di<< "Number of Rational BSpline curves " << NbCurv.Value(1,3)<<"\n";
357 //if(NbCurv.Value(1,4) !=0)
358 di<< "Number of BSpline curves with less continuity - " << NbCurv.Value(1,4)<<"\n";
359 //if(NbCurv.Value(2,1) !=0)
360 di<< "Number of Bezier curves with degree more then - "<< Degree << " - " <<NbCurv.Value(2,1)<<"\n";
361 //if(NbCurv.Value(2,3) !=0)
362 di<< "Number of Rational Bezier curves - " <<NbCurv.Value(2,3)<<"\n";
363 //if(NbCurv.Value(2,4) !=0)
364 di<< "Number of Bezier curves with less continuity - " << NbCurv.Value(2,4)<<"\n";
366 //if(NbCurv.Value(1,5) !=0)
367 di<< "Number of other curves - " << NbCurv.Value(1,5)<<"\n";
369 //if(NbCurv2d.Value(1,1) !=0)
370 di<< "Number of BSpline pcurves with degree more then - "<< Degree << " - " <<NbCurv2d.Value(1,1)<<"\n";
371 //if(NbCurv2d.Value(1,2) !=0)
372 di<< "Number of BSpline pcurves with number of spans more then "<< MaxSeg << " - " <<NbCurv2d.Value(1,2)<<"\n";
373 //if(NbCurv2d.Value(1,3) !=0)
374 di<< "Number of Rational BSpline pcurves - " << NbCurv2d.Value(1,3)<<"\n";
375 // if(NbCurv2d.Value(1,4) !=0)
376 di<< "Number of BSpline pcurves with less continuity - " << NbCurv2d.Value(1,4)<<"\n";
377 //if(NbCurv2d.Value(2,1) !=0)
378 di<< "Number of Bezier pcurves with degree more then "<< Degree <<" - - " <<NbCurv2d.Value(2,1)<<"\n";
379 //if(NbCurv2d.Value(2,3) !=0)
380 di<< "Number of Rational Bezier pcurves - " <<NbCurv2d.Value(2,3)<<"\n";
381 //if(NbCurv2d.Value(2,4) !=0)
382 di<< "Number of Bezier pcurves with less continuity - " << NbCurv2d.Value(2,4)<<"\n";
384 //if(NbCurv2d.Value(1,5) !=0)
385 di<< "Number of other pcurves - " << NbCurv2d.Value(1,5)<<"\n";
389 static Standard_Integer scaleshape(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
392 di << "Incorrect number of arguments. Must be 4\n";
393 return 1 /* Error */;
395 Standard_CString arg2 = argv[2];
396 TopoDS_Shape Shape = DBRep::Get(arg2);
397 if (Shape.IsNull()) { di << "Shape unknown: " << arg2 << "\n"; return 1 /* Error */; }
399 TopoDS_Shape result = ShapeCustom::ScaleShape (Shape, Draw::Atof(argv[3]));
400 if (result.IsNull()) { di << "NO RESULT\n"; return 1; }
401 else if (result == Shape) { di << "NO MODIFICATIONS\n"; return 0; }
402 DBRep::Set (argv[1],result);
405 static Standard_Integer BSplRes(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
408 di << "Incorrect number of arguments. Must be 10\n";
409 return 1 /* Error */;
411 Standard_CString arg2 = argv[2];
412 TopoDS_Shape Shape = DBRep::Get(arg2);
413 if (Shape.IsNull()) { di << "Shape unknown: " << arg2 << "\n"; return 1 /* Error */; }
414 GeomAbs_Shape aCont3;
415 if(strcmp(argv[7],"C0") == 0) aCont3 = GeomAbs_C0;
416 else if (strcmp(argv[7],"C1") == 0) aCont3 = GeomAbs_C1;
417 else if (strcmp(argv[7],"C2") == 0) aCont3 = GeomAbs_C2;
418 else if (strcmp(argv[7],"C3") == 0) aCont3 = GeomAbs_C3;
419 else if (strcmp(argv[7],"CN") == 0) aCont3 = GeomAbs_CN;
420 else if (strcmp(argv[7],"G1") == 0) aCont3 = GeomAbs_C0;
421 else if (strcmp(argv[7],"G2") == 0) aCont3 = GeomAbs_C1;
423 di<< "Invalid argument Cont3e\n"; return 1; }
424 GeomAbs_Shape aCont2;
425 if(strcmp(argv[8],"C0") == 0) aCont2 = GeomAbs_C0;
426 else if (strcmp(argv[8],"C1") == 0) aCont2 = GeomAbs_C1;
427 else if (strcmp(argv[8],"C2") == 0) aCont2 = GeomAbs_C2;
428 else if (strcmp(argv[8],"C3") == 0) aCont2 = GeomAbs_C3;
429 else if (strcmp(argv[8],"CN") == 0) aCont2 = GeomAbs_CN;
430 else if (strcmp(argv[8],"G1") == 0) aCont2 = GeomAbs_C0;
431 else if (strcmp(argv[8],"G2") == 0) aCont2 = GeomAbs_C1;
433 di<< "Invalid argument Cont3e\n"; return 1; }
435 Handle(ShapeCustom_RestrictionParameters) aParameters = new ShapeCustom_RestrictionParameters;
436 TopoDS_Shape result = ShapeCustom::BSplineRestriction (Shape, Draw::Atof(argv[3]), Draw::Atof(argv[4]), Draw::Atoi(argv[5]),Draw::Atoi(argv[6]),aCont3,aCont2, Draw::Atoi(argv[9]) != 0, Draw::Atoi(argv[10]) != 0,aParameters);
437 if (result.IsNull()) { di << "NO RESULT\n"; return 1; }
438 else if (result == Shape) {
439 di << "NO MODIFICATIONS\n";
440 DBRep::Set (argv[1],result); return 0;
442 ShapeFix::SameParameter(result,Standard_False);
444 DBRep::Set (argv[1],result);
448 static Standard_Integer convtorevol(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
450 if (argc < 3) { di<<"Convert surfaces to revolution: convtorevol result shape\n"; return 1; }
452 Standard_CString arg1 = argv[1];
453 Standard_CString arg2 = argv[2];
454 TopoDS_Shape Shape = DBRep::Get(arg2);
455 if (Shape.IsNull()) { di<<"Shape unknown : "<<arg2<<"\n"; return 1; }
457 TopoDS_Shape result = ShapeCustom::ConvertToRevolution (Shape);
458 if (result.IsNull()) { di<<"NO RESULT\n"; return 1; }
459 else if (result == Shape) { di<<"No modif\n"; return 0; }
460 di<<"ConvertToRevolution -> Result : "<<arg1<<"\n";
461 DBRep::Set (arg1,result);
465 //======================================================================
466 //function : InitCommands
468 //=======================================================================
470 void SWDRAW_ShapeCustom::InitCommands(Draw_Interpretor& theCommands)
472 static Standard_Integer initactor = 0;
479 Standard_CString g = SWDRAW::GroupName();
481 theCommands.Add ("directfaces", "directfaces result shape", __FILE__, directfaces, g);
482 theCommands.Add ("expshape", "expshape shape maxdegree maxseg [min_continuity]", __FILE__, expshape, g);
483 theCommands.Add ("scaleshape", "scaleshape result shape scale", __FILE__, scaleshape, g);
484 theCommands.Add ("bsplres", "BSplineRestriction result shape tol3d tol2d reqdegree reqnbsegments continuity3d continuity2d PriorDeg RationalConvert", __FILE__,BSplRes, g);
485 theCommands.Add ("convtorevol", "convtorevol result shape", __FILE__, convtorevol, g);