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
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 <BRepTest.hxx>
19 #include <GeometryTest.hxx>
21 #include <DrawTrSurf.hxx>
23 #include <Draw_Interpretor.hxx>
24 #include <Draw_Appli.hxx>
26 #include <BRep_Builder.hxx>
27 #include <BRep_Tool.hxx>
28 #include <BRepLib.hxx>
29 #include <BRepTools_Quilt.hxx>
30 #include <BRepAdaptor_Curve.hxx>
31 #include <BRepBuilderAPI_MakeFace.hxx>
32 #include <BRepBuilderAPI_MakeShell.hxx>
33 #include <BRepBuilderAPI.hxx>
34 #include <BRepBuilderAPI_Sewing.hxx>
35 #include <BRepOffsetAPI_FindContigousEdges.hxx>
36 #include <TopExp_Explorer.hxx>
38 #include <TopoDS_Face.hxx>
39 #include <Geom_Surface.hxx>
40 #include <Geom2d_TrimmedCurve.hxx>
41 #include <TopTools_ListOfShape.hxx>
42 #include <TopTools_SequenceOfShape.hxx>
43 #include <Precision.hxx>
44 #include <Draw_ProgressIndicator.hxx>
45 #include <NCollection_Vector.hxx>
46 #include <BRepBuilderAPI_FastSewing.hxx>
48 #include <GeomAPI_ProjectPointOnSurf.hxx>
51 //#define strcasecmp strcmp Already defined
55 //-----------------------------------------------------------------------
56 // suppressarg : suppress a[d],modifie na--
57 //-----------------------------------------------------------------------
58 static void suppressarg(Standard_Integer& na,const char** a,const Standard_Integer d)
60 for(Standard_Integer i=d;i<na;i++) {
68 //=======================================================================
70 //=======================================================================
72 static Standard_Integer mkface(Draw_Interpretor& , Standard_Integer n, const char** a)
76 Handle(Geom_Surface) S = DrawTrSurf::GetSurface(a[2]);
78 cout << a[2] << " is not a surface" << endl;
82 Standard_Boolean mkface = a[0][2] == 'f';
85 Standard_Boolean Segment = Standard_False;
86 if ( !mkface && (n == 4 || n == 8)) {
87 Segment = !strcmp(a[n-1],"1");
93 res = BRepBuilderAPI_MakeFace(S, Precision::Confusion());
95 res = BRepBuilderAPI_MakeShell(S,Segment);
98 if (!mkface) return 1;
99 Standard_Boolean orient = (n == 4);
100 TopoDS_Shape W = DBRep::Get(a[3],TopAbs_WIRE);
101 if (W.IsNull()) return 1;
102 res = BRepBuilderAPI_MakeFace(S,TopoDS::Wire(W),orient);
106 res = BRepBuilderAPI_MakeFace(S,Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]),Draw::Atof(a[6]),Precision::Confusion());
108 res = BRepBuilderAPI_MakeShell(S,Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]),Draw::Atof(a[6]),
112 DBRep::Set(a[1],res);
116 //=======================================================================
118 //=======================================================================
120 static Standard_Integer quilt(Draw_Interpretor& , Standard_Integer n, const char** a)
125 Standard_Integer i = 2;
127 TopoDS_Shape S = DBRep::Get(a[i]);
129 if (S.ShapeType() == TopAbs_EDGE) {
131 TopoDS_Shape E = DBRep::Get(a[i+1]);
133 if (E.ShapeType() == TopAbs_EDGE) {
135 Q.Bind(TopoDS::Edge(S),TopoDS::Edge(E));
140 if (S.ShapeType() == TopAbs_VERTEX) {
142 TopoDS_Shape E = DBRep::Get(a[i+1]);
144 if (E.ShapeType() == TopAbs_VERTEX) {
146 Q.Bind(TopoDS::Vertex(S),TopoDS::Vertex(E));
158 DBRep::Set(a[1],Q.Shells());
163 //=======================================================================
165 //=======================================================================
167 static Standard_Integer mksurface(Draw_Interpretor& , Standard_Integer n, const char** a)
171 TopoDS_Shape S = DBRep::Get(a[2],TopAbs_FACE);
172 if (S.IsNull()) return 1;
174 Handle(Geom_Surface) C = BRep_Tool::Surface(TopoDS::Face(S),L);
177 DrawTrSurf::Set(a[1],C->Transformed(L.Transformation()));
181 //=======================================================================
183 //=======================================================================
185 static Standard_Integer mkplane(Draw_Interpretor& theDI, Standard_Integer n, const char** a)
189 TopoDS_Shape S = DBRep::Get(a[2],TopAbs_WIRE);
190 if (S.IsNull()) return 1;
192 Standard_Boolean OnlyPlane = Standard_False;
194 OnlyPlane = !strcmp(a[3],"1");
197 BRepBuilderAPI_MakeFace aMF(TopoDS::Wire(S), OnlyPlane);
201 case BRepBuilderAPI_FaceDone:
202 DBRep::Set(a[1],aMF.Face());
205 theDI << "Error. mkplane has been finished with \"No Face\" status.\n";
207 case BRepLib_NotPlanar:
208 theDI << "Error. mkplane has been finished with \"Not Planar\" status.\n";
210 case BRepLib_CurveProjectionFailed:
211 theDI << "Error. mkplane has been finished with \"Fail in projection curve\" status.\n";
213 case BRepLib_ParametersOutOfRange:
214 theDI << "Error. mkplane has been finished with \"Parameters are out of range\" status.\n";
217 theDI << "Error. Undefined status. Please check the code.\n";
224 //=======================================================================
226 //=======================================================================
227 Standard_IMPORT Draw_Color DrawTrSurf_CurveColor(const Draw_Color col);
228 Standard_IMPORT void DBRep_WriteColorOrientation ();
229 Standard_IMPORT Draw_Color DBRep_ColorOrientation (const TopAbs_Orientation Or);
231 static Standard_Integer pcurve(Draw_Interpretor& , Standard_Integer n, const char** a)
233 Standard_Boolean mute = Standard_False;
234 for(Standard_Integer ia=1;ia<n;ia++) {
235 if (!strcasecmp(a[ia],"-mute")) {
237 mute = Standard_True;
243 TopoDS_Shape S = DBRep::Get(a[1],TopAbs_FACE);
244 if (S.IsNull()) return 1;
246 if (!mute) DBRep_WriteColorOrientation();
247 Draw_Color col, savecol = DrawTrSurf_CurveColor(Draw_rouge);
249 char* name = new char[100];
251 S.Orientation(TopAbs_FORWARD);
252 TopExp_Explorer ex(S,TopAbs_EDGE);
253 for (Standard_Integer i=1; ex.More(); ex.Next(), i++) {
254 const Handle(Geom2d_Curve) c = BRep_Tool::CurveOnSurface
255 (TopoDS::Edge(ex.Current()),TopoDS::Face(S),f,l);
257 cout << "Error: Edge " << i << " does not have pcurve" << endl;
260 col = DBRep_ColorOrientation(ex.Current().Orientation());
261 DrawTrSurf_CurveColor(col);
263 Sprintf(name,"%s_%d",a[1],i);
264 Standard_Real fr = c->FirstParameter(), lr = c->LastParameter();
265 Standard_Boolean IsPeriodic = c->IsPeriodic();
266 if (c->DynamicType() == STANDARD_TYPE(Geom2d_TrimmedCurve))
268 const Handle(Geom2d_Curve)& aC = Handle(Geom2d_TrimmedCurve)::DownCast (c)->BasisCurve();
269 IsPeriodic = aC->IsPeriodic();
270 fr = aC->FirstParameter();
271 lr = aC->LastParameter();
274 ((fr - f > Precision::PConfusion()) || (l - lr > Precision::PConfusion())))
276 DrawTrSurf::Set(name, c);
280 DrawTrSurf::Set(name,new Geom2d_TrimmedCurve(c,f,l));
283 DrawTrSurf_CurveColor(savecol);
287 TopoDS_Shape SE = DBRep::Get(a[2],TopAbs_EDGE);
288 if (SE.IsNull()) return 1;
289 TopoDS_Shape SF = DBRep::Get(a[3],TopAbs_FACE);
290 if (SF.IsNull()) return 1;
292 Draw_Color col, savecol = DrawTrSurf_CurveColor(Draw_rouge);
294 const Handle(Geom2d_Curve) c = BRep_Tool::CurveOnSurface
295 (TopoDS::Edge(SE),TopoDS::Face(SF),f,l);
296 Standard_Real fr = c->FirstParameter(), lr = c->LastParameter();
297 Standard_Boolean IsPeriodic = c->IsPeriodic();
298 if (c->DynamicType() == STANDARD_TYPE(Geom2d_TrimmedCurve))
300 const Handle(Geom2d_Curve)& aC = Handle(Geom2d_TrimmedCurve)::DownCast (c)->BasisCurve();
301 IsPeriodic = aC->IsPeriodic();
302 fr = aC->FirstParameter();
303 lr = aC->LastParameter();
306 col = DBRep_ColorOrientation(SE.Orientation());
307 DrawTrSurf_CurveColor(col);
309 ((fr - f > Precision::PConfusion()) || (l - lr > Precision::PConfusion())))
311 DrawTrSurf::Set(a[1], c);
315 DrawTrSurf::Set(a[1],new Geom2d_TrimmedCurve(c,f,l));
317 DrawTrSurf_CurveColor(savecol);
326 //=======================================================================
328 //=======================================================================
330 static Standard_Integer sewing (Draw_Interpretor& theDi,
331 Standard_Integer theArgc, const char** theArgv)
333 BRepBuilderAPI_Sewing aSewing;
334 Standard_Integer aPar = 1;
335 TopTools_SequenceOfShape aSeq;
337 Standard_Real aTol = 1.0e-06;
338 Standard_Boolean aSewingMode = Standard_True;
339 Standard_Boolean anAnalysisMode = Standard_True;
340 Standard_Boolean aCuttingMode = Standard_True;
341 Standard_Boolean aNonManifoldMode = Standard_False;
342 Standard_Boolean aSameParameterMode = Standard_True;
343 Standard_Boolean aFloatingEdgesMode = Standard_False;
344 Standard_Boolean aFaceMode = Standard_True;
345 Standard_Boolean aSetMinTol = Standard_False;
346 Standard_Real aMinTol = 0.;
347 Standard_Real aMaxTol = Precision::Infinite();
349 for (Standard_Integer i = 2; i < theArgc; i++)
351 if (theArgv[i][0] == '-' || theArgv[i][0] == '+')
353 Standard_Boolean aVal = (theArgv[i][0] == '+' ? Standard_True : Standard_False);
354 switch (tolower(theArgv[i][1]))
358 if (tolower(theArgv[i][2]) == 'i' && i+1 < theArgc)
360 if (Draw::Atof (theArgv[i+1]))
362 aMinTol = Draw::Atof (theArgv[++i]);
363 aSetMinTol = Standard_True;
367 theDi << "Error! min tolerance can't possess the null value\n";
371 if (tolower(theArgv[i][2]) == 'a' && i+1 < theArgc)
373 if (Draw::Atof (theArgv[i+1]))
374 aMaxTol = Draw::Atof (theArgv[++i]);
377 theDi << "Error! max tolerance can't possess the null value\n";
383 case 's': aSewingMode = aVal; break;
384 case 'a': anAnalysisMode = aVal; break;
385 case 'c': aCuttingMode = aVal; break;
386 case 'n': aNonManifoldMode = aVal; break;
387 case 'p': aSameParameterMode = aVal; break;
388 case 'e': aFloatingEdgesMode = aVal; break;
389 case 'f': aFaceMode = aVal; break;
394 TopoDS_Shape aShape = DBRep::Get (theArgv[i]);
395 if (!aShape.IsNull())
397 aSeq.Append (aShape);
402 if (Draw::Atof (theArgv[i]))
403 aTol = Draw::Atof (theArgv[i]);
410 theDi << "Use: " << theArgv[0] << " result [tolerance] shape1 shape2 ... [min tolerance] [max tolerance] [switches]\n";
411 theDi << "To set user's value of min/max tolerances the following syntax is used: +<parameter> <value>\n";
412 theDi << "- parameters are identified by letters:\n";
413 theDi << " mint - min tolerance\n";
414 theDi << " maxt - max tolerance\n";
415 theDi << "Switches allow to tune other parameters of Sewing\n";
416 theDi << "The following syntax is used: <symbol><parameter>\n";
417 theDi << "- symbol may be - to set parameter off, + to set on\n";
418 theDi << "- parameters are identified by letters:\n";
419 theDi << " s - mode for creating sewed shape\n";
420 theDi << " a - mode for analysis of input shapes\n";
421 theDi << " c - mode for cutting of free edges\n";
422 theDi << " n - mode for non manifold processing\n";
423 theDi << " p - mode for same parameter processing for edges\n";
424 theDi << " e - mode for sewing floating edges\n";
425 theDi << " f - mode for sewing faces\n";
431 if (aTol < Precision::Confusion())
432 aTol = Precision::Confusion();
433 if (aMinTol < Precision::Confusion())
434 aMinTol = Precision::Confusion();
437 theDi << "Error! min tolerance can't exceed working tolerance\n";
442 theDi << "Error! max tolerance can't be less than working tolerance\n";
446 aSewing.Init (aTol, aSewingMode, anAnalysisMode, aCuttingMode, aNonManifoldMode);
447 aSewing.SetSameParameterMode (aSameParameterMode);
448 aSewing.SetFloatingEdgesMode (aFloatingEdgesMode);
449 aSewing.SetFaceMode (aFaceMode);
450 aSewing.SetMinTolerance (aMinTol);
451 aSewing.SetMaxTolerance (aMaxTol);
453 for (Standard_Integer i = 1; i <= aSeq.Length(); i++)
454 aSewing.Add(aSeq.Value(i));
456 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator (theDi, 1);
457 aSewing.Perform (aProgress);
460 const TopoDS_Shape& aRes = aSewing.SewedShape();
462 DBRep::Set(theArgv[1], aRes);
466 //=======================================================================
467 //function : fastsewing
469 //=======================================================================
470 Standard_Integer fastsewing (Draw_Interpretor& theDI,
471 Standard_Integer theNArg,
472 const char** theArgVal)
477 theDI << "Use: fastsewing result [-tol <value>] <list_of_faces>\n";
481 BRepBuilderAPI_FastSewing aFS;
483 Standard_Integer aStartIndex = 2;
485 if(!strcmp(theArgVal[aStartIndex], "-tol"))
487 aFS.SetTolerance(Draw::Atof (theArgVal[aStartIndex+1]));
491 for(Standard_Integer i = aStartIndex; i < theNArg; i++)
493 TopoDS_Shape aS = DBRep::Get(theArgVal[i]);
497 theDI << "Face is not added. See statuses.\n";
501 BRepBuilderAPI_FastSewing::FS_VARStatuses aStatus = aFS.GetStatuses();
505 theDI << "Error: There are some problems while adding (" <<
506 (static_cast<Standard_Integer>(aStatus)) << ")\n";
507 aFS.GetStatuses(&cout);
512 aStatus = aFS.GetStatuses();
516 theDI << "Error: There are some problems while performing (" <<
517 (static_cast<Standard_Integer>(aStatus)) << ")\n";
518 aFS.GetStatuses(&cout);
521 DBRep::Set(theArgVal[1], aFS.GetResult());
526 //=======================================================================
528 //=======================================================================
530 static Standard_Integer continuity (Draw_Interpretor& ,
531 Standard_Integer n, const char** a)
533 if (n < 2) return (1);
535 BRepOffsetAPI_FindContigousEdges aFind;
537 TopoDS_Shape sh = DBRep::Get(a[1]);
538 Standard_Integer i=1;
540 if (n < 3) return (1);
541 Standard_Real tol = Draw::Atof(a[1]);
542 aFind.Init(tol, Standard_False);
547 sh = DBRep::Get(a[i]);
558 //=======================================================================
560 //=======================================================================
561 static Standard_Integer encoderegularity (Draw_Interpretor& ,
562 Standard_Integer n, const char** a)
566 TopoDS_Shape sh = DBRep::Get(a[1]);
567 if (sh.IsNull()) return 1;
569 BRepLib::EncodeRegularity(sh);
571 Standard_Real Tol = Draw::Atof(a[2]);
573 BRepLib::EncodeRegularity(sh, Tol);
578 static Standard_Integer getedgeregul
579 (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
583 cout<<"Invalid number of arguments. Should be: checkedgeregularity edge face1 [face2]"<<endl;
587 TopoDS_Shape anEdge = DBRep::Get(argv[1],TopAbs_EDGE);
588 TopoDS_Shape aFace1 = DBRep::Get(argv[2],TopAbs_FACE);
589 TopoDS_Shape aFace2 = (argc > 3 ? DBRep::Get(argv[3],TopAbs_FACE) : aFace1);
590 if( anEdge.IsNull() || aFace1.IsNull() || aFace2.IsNull())
592 cout<<"Invalid number of arguments. Should be: getedgeregularity edge face1 [face2]"<<endl;
596 GeomAbs_Shape aRegularity = BRep_Tool::Continuity(TopoDS::Edge(anEdge), TopoDS::Face(aFace1), TopoDS::Face(aFace2));
597 TCollection_AsciiString aStrReg("Regularity of edge : ");
601 case GeomAbs_C0 : aStrReg += "C0"; break;
602 case GeomAbs_G1 : aStrReg += "G1"; break;
603 case GeomAbs_C1 : aStrReg += "C1"; break;
604 case GeomAbs_G2 : aStrReg += "G2"; break;
605 case GeomAbs_C2 : aStrReg += "C2"; break;
606 case GeomAbs_C3 : aStrReg += "C3"; break;
607 case GeomAbs_CN : aStrReg += "CN"; break;
610 di<<aStrReg.ToCString()<<"\n";
614 //=======================================================================
615 //function : projponf
617 //=======================================================================
618 static Standard_Integer projponf(Draw_Interpretor& di, Standard_Integer n, const char** a)
620 if (n < 3 || n > 5) {
621 di << "Project point on the face.\n";
622 di << "Usage: projponf face pnt [extrema flag: -min/-max/-minmax] [extrema algo: -g(grad)/-t(tree)]\n";
626 TopoDS_Shape aS = DBRep::Get(a[1]);
628 di << "the face is a null shape\n";
632 if (aS.ShapeType() != TopAbs_FACE) {
633 di << "not a face\n";
637 const TopoDS_Face& aFace = *(TopoDS_Face*)&aS;
641 DrawTrSurf::GetPoint(a[2], aP);
643 // get projection options
645 Extrema_ExtAlgo anExtAlgo = Extrema_ExtAlgo_Grad;
646 Extrema_ExtFlag anExtFlag = Extrema_ExtFlag_MINMAX;
648 for (Standard_Integer i = 3; i < n; ++i) {
649 if (!strcasecmp(a[i], "-min")) {
650 anExtFlag = Extrema_ExtFlag_MIN;
652 else if (!strcasecmp(a[i], "-max")) {
653 anExtFlag = Extrema_ExtFlag_MAX;
655 else if (!strcasecmp(a[i], "-minmax")) {
656 anExtFlag = Extrema_ExtFlag_MINMAX;
658 else if (!strcasecmp(a[i], "-t")) {
659 anExtAlgo = Extrema_ExtAlgo_Tree;
661 else if (!strcasecmp(a[i], "-g")) {
662 anExtAlgo = Extrema_ExtAlgo_Grad;
667 TopLoc_Location aLoc;
668 const Handle(Geom_Surface)& aSurf = BRep_Tool::Surface(aFace, aLoc);
669 // move point to surface location
670 aP.Transform(aLoc.Transformation().Inverted());
672 // get bounds of the surface
673 Standard_Real aUMin, aUMax, aVMin, aVMax;
674 aSurf->Bounds(aUMin, aUMax, aVMin, aVMax);
676 // initialize projector
677 GeomAPI_ProjectPointOnSurf aProjPS;
678 aProjPS.Init(aSurf, aUMin, aUMax, aVMin, aVMax);
680 aProjPS.SetExtremaAlgo(anExtAlgo);
681 aProjPS.SetExtremaFlag(anExtFlag);
682 // perform projection
685 if (aProjPS.NbPoints()) {
687 Standard_Real aDist = aProjPS.LowerDistance();
688 // lower distance parameters
690 aProjPS.LowerDistanceParameters(U, V);
692 gp_Pnt aPProj = aProjPS.NearestPoint();
693 // translate projection point to face location
694 aPProj.Transform(aLoc.Transformation());
696 // print the projection values
697 di << "proj dist = " << aDist << "\n";
698 di << "uvproj = " << U << " " << V << "\n";
699 di << "pproj = " << aPProj.X() << " " << aPProj.Y() << " " << aPProj.Z() << "\n";
702 if (!aProjPS.IsDone()) {
703 di << "projection failed\n";
706 di << "no projection found\n";
712 //=======================================================================
713 //function : SurfaceCommands
715 //=======================================================================
717 void BRepTest::SurfaceCommands(Draw_Interpretor& theCommands)
719 static Standard_Boolean done = Standard_False;
721 done = Standard_True;
723 DBRep::BasicCommands(theCommands);
724 GeometryTest::SurfaceCommands(theCommands);
726 const char* g = "Surface topology commands";
728 theCommands.Add("mkface",
729 "mkface facename surfacename [ufirst ulast vfirst vlast] [wire [norient]]",
732 theCommands.Add("mkshell",
733 "mkshell shellname surfacename [ufirst ulast vfirst vlast] [segment 0/1]",
736 theCommands.Add("quilt",
737 "quilt compoundname shape1 edgeshape2 edgeshape1... shape2 edgeshape3 edgeshape1or2 ... shape3 ...",
740 theCommands.Add("mksurface",
741 "mksurface surfacename facename",
742 __FILE__,mksurface,g);
744 theCommands.Add("mkplane",
745 "mkplane facename wirename [OnlyPlane 0/1]",
748 theCommands.Add("pcurve",
749 "pcurve [name edgename] facename",
752 theCommands.Add("sewing",
753 "sewing result [tolerance] shape1 shape2 ... [min tolerance] [max tolerance] [switches]",
756 theCommands.Add("continuity",
757 "continuity [tolerance] shape1 shape2 ...",
758 __FILE__,continuity, g);
760 theCommands.Add("encoderegularity",
761 "encoderegularity shape [tolerance (in degree)]",
762 __FILE__,encoderegularity, g);
764 theCommands.Add ("fastsewing", "fastsewing result [-tol <value>] <list_of_faces>",
765 __FILE__, fastsewing, g);
766 theCommands.Add ("getedgeregularity", "getedgeregularity edge face1 [face2]", __FILE__,getedgeregul,g);
768 theCommands.Add ("projponf",
769 "projponf face pnt [extrema flag: -min/-max/-minmax] [extrema algo: -g(grad)/-t(tree)]\n"
770 "\t\tProject point on the face.",
771 __FILE__, projponf, g);