1 // File: BOPTest_LowCommands.cxx
2 // Created: Wed Mar 28 16:46:22 2001
3 // Author: Peter KURNEV
10 #include <gp_Vec2d.hxx>
11 #include <gp_Pnt2d.hxx>
14 #include <Geom2d_Curve.hxx>
15 #include <Geom_Surface.hxx>
17 #include <BRep_Tool.hxx>
18 #include <BRep_Builder.hxx>
19 #include <BRep_GCurve.hxx>
20 #include <BRep_TEdge.hxx>
21 #include <BRep_CurveRepresentation.hxx>
22 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
24 #include <BRepClass3d_SolidClassifier.hxx>
25 #include <BRepClass_FaceClassifier.hxx>
28 #include <TopoDS_Edge.hxx>
29 #include <TopoDS_Face.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Vertex.hxx>
32 #include <TopoDS_Iterator.hxx>
34 #include <TopTools_MapOfShape.hxx>
36 #include <TopLoc_Location.hxx>
38 #include <TopExp_Explorer.hxx>
41 #include <TopAbs_State.hxx>
45 #include <DrawTrSurf.hxx>
47 #include <BOPTools_Tools2D.hxx>
48 #include <BOPTools_Tools3D.hxx>
50 #include <IntTools_Tools.hxx>
52 #include <BOP_Refiner.hxx>
54 #include <TCollection_AsciiString.hxx>
55 #include <IntTools_Context.hxx>
56 #include <IntTools_FClass2d.hxx>
57 #include <gp_Pnt2d.hxx>
58 #include <Standard_Static.hxx>
59 #include <OSD_Chronometer.hxx>
61 #include <BRepTools.hxx>
64 Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E,
69 Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E,
70 const Handle(Geom_Surface)& S,
71 const TopLoc_Location& L,
75 void PrintState (Draw_Interpretor& aDI,
76 const TopAbs_State& aState);
78 static Standard_Integer bhaspc (Draw_Interpretor& , Standard_Integer , const char** );
79 static Standard_Integer baddve (Draw_Interpretor& , Standard_Integer , const char** );
80 static Standard_Integer bisclosed (Draw_Interpretor& , Standard_Integer , const char** );
81 static Standard_Integer bexplode (Draw_Interpretor& , Standard_Integer , const char** );
82 static Standard_Integer bupdateedge (Draw_Interpretor& , Standard_Integer , const char**);
83 static Standard_Integer bremovesim (Draw_Interpretor& , Standard_Integer , const char** );
84 static Standard_Integer bopsetdisp (Draw_Interpretor& , Standard_Integer , const char** );
85 static Standard_Integer bfaceprepare(Draw_Interpretor& , Standard_Integer , const char** );
86 static Standard_Integer brefine (Draw_Interpretor& , Standard_Integer , const char** );
87 static Standard_Integer bclassify (Draw_Interpretor& , Standard_Integer , const char** );
88 static Standard_Integer b2dclassify (Draw_Interpretor& , Standard_Integer , const char** );
90 //modified by NIZNHY-PKV Mon May 29 11:44:24 2006f
91 static Standard_Integer bhole (Draw_Interpretor& , Standard_Integer , const char** );
92 static Standard_Integer bxhole (Draw_Interpretor& , Standard_Integer , const char** );
93 //modified by NIZNHY-PKV Mon May 29 11:44:28 2006t
95 //=======================================================================
96 //function : LowCommands
98 //=======================================================================
99 void BOPTest::LowCommands(Draw_Interpretor& theCommands)
101 static Standard_Boolean done = Standard_False;
103 done = Standard_True;
105 const char* g = "CCR commands";
107 // Whether Edge has PCurve on Face
108 theCommands.Add("bhaspc" , "Use >bhaspc Edge Face" , __FILE__, bhaspc , g);
109 theCommands.Add("baddve" , "Use >addve E V1 V2 p1 p2", __FILE__, baddve , g);
110 theCommands.Add("bisclosed" , "Use >bisclosed Edge Face" , __FILE__, bisclosed , g);
111 theCommands.Add("bexplode" , "" , __FILE__, bexplode , g);
112 theCommands.Add("bupdateedge" , "Use bupdateedge Edge Face", __FILE__, bupdateedge , g);
113 theCommands.Add("bremovesim" , "Use bremovesim Face" , __FILE__, bremovesim , g);
114 theCommands.Add("bopsetdisp" , "Use bopsetdisp" , __FILE__, bopsetdisp , g);
117 theCommands.Add("bfaceprepare" , "Use bfaceprepare F1 " , __FILE__, bfaceprepare, g);
119 theCommands.Add("brefine" , "Use brefine Shape " , __FILE__, brefine , g);
120 theCommands.Add("bclassify" , "Use >bclassify Solid Point [Tolerance=1.e-7]",
121 __FILE__, bclassify , g);
122 theCommands.Add("b2dclassify" , "Use >bclassify Face Point2d [Tol2D=Tol(Face)] ",
123 __FILE__, b2dclassify , g);
124 //modified by NIZNHY-PKV Mon May 29 11:45:33 2006f
125 theCommands.Add("bhole" , "Use bhole" , __FILE__, bhole , g);
126 theCommands.Add("bxhole" , "Use bxhole" , __FILE__, bxhole , g);
127 //modified by NIZNHY-PKV Mon May 29 11:45:37 2006t
130 //=======================================================================
133 //=======================================================================
134 Standard_Integer brefine (Draw_Interpretor& di,
139 di << " Use >brefine Shape1\n";
143 TopoDS_Shape aS = DBRep::Get(a[1]);
147 di << " Null shape is not allowed \n";
151 Standard_Boolean bIsDone;
152 BOP_Refiner aRefiner;
153 aRefiner.SetShape(aS);
157 bIsDone=aRefiner.IsDone();
160 Standard_Integer aNbRemovedVertices, aNbRemovedEdges, iFlag=0;
161 aNbRemovedVertices=aRefiner.NbRemovedVertices();
162 aNbRemovedEdges=aRefiner.NbRemovedEdges();
164 if (aNbRemovedVertices) {
165 di << " Vertices removed=" << aNbRemovedVertices << "%d\n";
169 if (aNbRemovedEdges) {
170 di << " Edges removed=" << aNbRemovedEdges << "\n";
176 TCollection_AsciiString aNm(a[1]);
177 Standard_CString pstr;
179 pstr=aNm.ToCString();
180 DBRep::Set(pstr, aS);
185 di << "Not Done, err=" << aRefiner.ErrorStatus() << "\n";
189 //=======================================================================
190 //function : bfaceprepare
192 //=======================================================================
193 Standard_Integer bfaceprepare(Draw_Interpretor& di, Standard_Integer n, const char** a)
196 di << " Use bfaceprepare> F1\n";
200 TopoDS_Shape S1 = DBRep::Get(a[1]);
204 di << " Null shape is not allowed \n";
207 if (S1.ShapeType()!=TopAbs_FACE) {
208 di << " Type mismatch\n";
212 const TopoDS_Face& aF1=TopoDS::Face(S1);
214 TopAbs_Orientation anOr;
215 anOr=aF1.Orientation();
216 if (anOr==TopAbs_FORWARD) {
224 Handle(Geom_Surface) aS1=BRep_Tool::Surface(aF1);
225 Handle(Geom_Surface) aS=Handle(Geom_Surface)::DownCast(aS1->Copy());
228 aTol=BRep_Tool::Tolerance(aF1);
230 aBB.MakeFace (newFace, aS, aTol);
232 TopExp_Explorer anExp(aF1, TopAbs_WIRE);
233 for (; anExp.More(); anExp.Next()) {
234 const TopoDS_Shape& newWire=anExp.Current();
235 aBB.Add(newFace, newWire);
238 TCollection_AsciiString aNm(a[1]), aNmNew("_r");
240 Standard_CString an=aNm.ToCString();
242 DBRep::Set(an, newFace);
245 //=======================================================================
246 //function : bopsetdisp
248 //=======================================================================
249 Standard_Integer bopsetdisp (Draw_Interpretor& di, Standard_Integer , const char** )
251 Standard_Integer aFlag;
253 char* xr=getenv("MDISP");
255 aFlag=putenv((char*)"MDISP=yes");
256 di << " MDISP=yes. Return code=" << aFlag << "\n";
258 else if (!strcmp (xr, "yes")) {
259 aFlag=putenv((char*)"MDISP=no");
260 di << " MDISP=no. Return code=" << aFlag << "\n";
263 aFlag=putenv((char*)"MDISP=yes");
264 di << " MDISP=yes. Return code=" << aFlag << "\n";
270 //=======================================================================
271 //function : bremovesim
273 //=======================================================================
274 Standard_Integer bremovesim (Draw_Interpretor& di, Standard_Integer n, const char** a)
277 di << " Use bremovesim> Face\n";
281 TopoDS_Shape S1 = DBRep::Get(a[1]);
285 di << " Null shapes are not allowed \n";
288 if (S1.ShapeType()!=TopAbs_FACE) {
289 di << " Type mismatch\n";
294 const TopoDS_Face& aF=TopoDS::Face(S1);
296 IntTools_Context aCtx;
297 BOPTools_Tools3D::RemoveSims (aF, aCtx);
302 //=======================================================================
303 //function : bupdateedge
305 //=======================================================================
306 Standard_Integer bupdateedge (Draw_Interpretor& di, Standard_Integer n, const char** a)
309 di << " Use bupdateedge> Edge Face\n";
313 TopoDS_Shape S1 = DBRep::Get(a[1]);
314 TopoDS_Shape S2 = DBRep::Get(a[2]);
316 if (S1.IsNull() || S2.IsNull()) {
317 di << " Null shapes are not allowed \n";
320 if (S1.ShapeType()!=TopAbs_EDGE || S2.ShapeType()!=TopAbs_FACE) {
321 di << " Type mismatch\n";
325 const TopoDS_Edge& aE=TopoDS::Edge(S1);
326 const TopoDS_Face& aF=TopoDS::Face(S2);
327 Handle(Geom2d_Curve) aC2D;
330 aTol=BRep_Tool::Tolerance(aE);
332 BB.UpdateEdge(aE, aC2D, aF, aTol);
337 //=======================================================================
338 //function : bisclosed
340 //=======================================================================
341 Standard_Integer bisclosed (Draw_Interpretor& di, Standard_Integer n, const char** a)
344 di << " Use bisclosed> Edge Face\n";
348 TopoDS_Shape S1 = DBRep::Get(a[1]);
349 TopoDS_Shape S2 = DBRep::Get(a[2]);
351 if (S1.IsNull() || S2.IsNull()) {
352 di << " Null shapes are not allowed \n";
355 if (S1.ShapeType()!=TopAbs_EDGE || S2.ShapeType()!=TopAbs_FACE) {
356 di << " Type mismatch\n";
360 const TopoDS_Edge& aE=TopoDS::Edge(S1);
361 const TopoDS_Face& aF=TopoDS::Face(S2);
363 Standard_Boolean anIsClosed;
364 anIsClosed=BRep_Tool::IsClosed(aE, aF);
367 di << " Edge is not closed on this face\n";
370 di << " Edge is closed on this face\n";
375 //=======================================================================
378 //=======================================================================
379 Standard_Integer bhaspc (Draw_Interpretor& di, Standard_Integer n, const char** a)
382 di << " Use bhaspc> Edge Face [do]\n";
386 TopoDS_Shape S1 = DBRep::Get(a[1]);
387 TopoDS_Shape S2 = DBRep::Get(a[2]);
389 if (S1.IsNull() || S2.IsNull()) {
390 di << " Null shapes are not allowed \n";
393 if (S1.ShapeType()!=TopAbs_EDGE || S2.ShapeType()!=TopAbs_FACE) {
394 di << " Type mismatch\n";
398 const TopoDS_Edge& aE=TopoDS::Edge(S1);
399 const TopoDS_Face& aF=TopoDS::Face(S2);
400 Standard_Real f2D, l2D;
402 Handle(Geom2d_Curve) C2D=CurveOnSurface(aE, aF, f2D, l2D);
405 di << " No 2D Curves detected\n";
408 di << " Ok Edge has P-Curve on this Face\n";
412 if (!strcmp(a[3], "do")) {
413 BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aE, aF);
420 //=======================================================================
423 //=======================================================================
424 Standard_Integer baddve (Draw_Interpretor& di, Standard_Integer n, const char** a)
427 di << " Use baddve> E V1 V2 p1 p2\n";
431 TopoDS_Shape S1 = DBRep::Get(a[1]);
432 TopoDS_Shape S2 = DBRep::Get(a[2]);
433 TopoDS_Shape S3 = DBRep::Get(a[3]);
434 if (S1.IsNull() || S2.IsNull() || S3.IsNull()) {
435 di << " Null shapes are not allowed \n";
439 if (S1.ShapeType()!=TopAbs_EDGE ||
440 S2.ShapeType()!=TopAbs_VERTEX ||
441 S3.ShapeType()!=TopAbs_VERTEX ) {
442 di << " Type mismatch\n";
445 const TopoDS_Edge& aE=TopoDS::Edge(S1);
446 const TopoDS_Vertex& aV1=TopoDS::Vertex(S2);
447 const TopoDS_Vertex& aV2=TopoDS::Vertex(S3);
450 Standard_Real p1, p2;
466 static Handle(Geom2d_Curve) nullPCurve;
467 //=======================================================================
468 //function : CurveOnSurface
470 //=======================================================================
471 Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E,
472 const TopoDS_Face& F,
473 Standard_Real& First,
477 const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,l);
478 TopoDS_Edge aLocalEdge = E;
479 if (F.Orientation() == TopAbs_REVERSED) {
480 aLocalEdge.Reverse();
482 return CurveOnSurface(aLocalEdge,S,l,First,Last);
485 //=======================================================================
486 //function : CurveOnSurface
488 //=======================================================================
489 Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E,
490 const Handle(Geom_Surface)& S,
491 const TopLoc_Location& L,
492 Standard_Real& First,
495 TopLoc_Location l = L.Predivided(E.Location());
496 Standard_Boolean Eisreversed = (E.Orientation() == TopAbs_REVERSED);
498 // find the representation
499 BRep_ListIteratorOfListOfCurveRepresentation itcr
500 ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
502 while (itcr.More()) {
503 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
504 if (cr->IsCurveOnSurface(S,l)) {
505 const Handle(BRep_GCurve)& GC = *((Handle(BRep_GCurve)*)&cr);
506 GC->Range(First,Last);
507 if (GC->IsCurveOnClosedSurface() && Eisreversed)
508 return GC->PCurve2();
517 //=======================================================================
518 //function : bexplode
520 //=======================================================================
521 static Standard_Integer bexplode(Draw_Interpretor& di,
525 if (n <= 1) return 1;
526 TopoDS_Shape S = DBRep::Get(a[1]);
527 if (S.IsNull()) return 0;
529 strcpy(newname,a[1]);
531 while (*p != '\0') p++;
534 Standard_Integer i = 0;
536 TopoDS_Iterator itr(S);
540 DBRep::Set(newname,itr.Value());
541 di.AppendElement(newname);
547 TopAbs_ShapeEnum typ;
552 if ((a[2][1] == 'd')||(a[2][1] == 'D'))
553 typ = TopAbs_COMPOUND;
555 typ = TopAbs_COMPSOLID;
560 if ((a[2][1] == 'O')||(a[2][1] == 'o'))
562 else if ((a[2][1] == 'H')||(a[2][1] == 'h'))
593 TopExp_Explorer ex(S,typ);
594 for (; ex.More(); ex.Next()) {
595 const TopoDS_Shape& aS = ex.Current();
598 DBRep::Set(newname,aS);
599 di.AppendElement(newname);
605 //=======================================================================
606 //function : bclassify
608 //=======================================================================
609 Standard_Integer bclassify (Draw_Interpretor& aDI,
616 sprintf(sbf, " Use >bclassify Solid Point [Tolerance=1.e-7]\n");
621 TopoDS_Shape aS = DBRep::Get(a[1]);
623 sprintf(sbf, " Null Shape is not allowed here\n");
628 if (aS.ShapeType()!=TopAbs_SOLID) {
629 sprintf(sbf, " Shape type must be SOLID\n");
634 Standard_Real aTol=1.e-7;
635 TopAbs_State aState = TopAbs_UNKNOWN;
636 gp_Pnt aP(8., 9., 10.);
638 DrawTrSurf::GetPoint(a[2], aP);
645 BRepClass3d_SolidClassifier aSC(aS);
646 aSC.Perform(aP,aTol);
648 aState = aSC.State();
650 PrintState (aDI, aState);
655 //=======================================================================
656 //function : b2dclassify
658 //=======================================================================
659 Standard_Integer b2dclassify (Draw_Interpretor& aDI,
666 sprintf(sbf, " Use >bclassify Face Point2d [Tol2D=Tol(Face)]\n");
671 TopoDS_Shape aS = DBRep::Get(a[1]);
673 sprintf(sbf, " Null Shape is not allowed here\n");
678 if (aS.ShapeType()!=TopAbs_FACE) {
679 sprintf(sbf, " Shape type must be FACE\n");
685 TopAbs_State aState = TopAbs_UNKNOWN;
688 DrawTrSurf::GetPoint2d(a[2], aP);
690 const TopoDS_Face& aF=TopoDS::Face(aS);
691 aTol=BRep_Tool::Tolerance(aF);
696 BRepClass_FaceClassifier aClassifier;
697 aClassifier.Perform(aF, aP, aTol);
699 aState = aClassifier.State();
701 PrintState (aDI, aState);
705 //=======================================================================
706 //function : PrintState
708 //=======================================================================
709 void PrintState (Draw_Interpretor& aDI,
710 const TopAbs_State& aState)
713 TCollection_AsciiString sIN("IN"), sOUT("OUT of"), sON("ON"), sUNKNOWN("UNKNOWN");
715 sprintf(sbf, "The point is "); aDI<<sbf;
719 sprintf(sbf, sIN.ToCString());
722 sprintf(sbf, sOUT.ToCString());
725 sprintf(sbf, sON.ToCString());
728 sprintf(sbf, sUNKNOWN.ToCString());
731 sprintf(sbf, sUNKNOWN.ToCString());
736 sprintf(sbf, " shape\n");
741 //modified by NIZNHY-PKV Mon May 29 11:40:29 2006f
742 //=======================================================================
745 //=======================================================================
746 Standard_Integer bhole (Draw_Interpretor& aDI,
753 sprintf(sbf, " Use bhole Face\n");
758 TopoDS_Shape aS = DBRep::Get(a[1]);
760 sprintf(sbf, " Null Shape is not allowed here\n");
765 if (aS.ShapeType()!=TopAbs_FACE) {
766 sprintf(sbf, " Shape type must be FACE\n");
771 Standard_Boolean bIsHole;
772 Standard_Real aTol=1.e-7;
773 IntTools_FClass2d aFClass2d;
775 const TopoDS_Face& aF=TopoDS::Face(aS);
776 aTol=BRep_Tool::Tolerance(aF);
778 aFClass2d.Init(aF, aTol);
780 bIsHole=aFClass2d.IsHole();
781 printf(" bIsHole=%d\n", bIsHole);
789 //=======================================================================
792 //=======================================================================
804 myChronometer.Reset();
805 myChronometer.Start();
810 myChronometer.Stop();
811 myChronometer.Show(aTime);
815 void Show(const char *pComment) {
816 printf(" Tps: %7.2lf\tCnt: %d\tfor %s\n", myTime, myCnt, pComment);
820 Standard_Real myTime;
821 Standard_Integer myCnt;
822 OSD_Chronometer myChronometer;
827 //=======================================================================
830 //=======================================================================
831 Standard_Integer bxhole (Draw_Interpretor& aDI,
836 Standard_Integer aNbIter;
839 sprintf(sbf, " Use bxhole Face NbIter\n");
844 TopoDS_Shape aS = DBRep::Get(a[1]);
846 sprintf(sbf, " Null Shape is not allowed here\n");
850 if (aS.ShapeType()!=TopAbs_FACE) {
851 sprintf(sbf, " Shape type must be FACE\n");
862 Standard_Boolean bIsHole;
863 Standard_Real aTol, aUMin, aUMax, aVMin, aVMax;
869 const TopoDS_Face& aF=TopoDS::Face(aS);
870 aTol=BRep_Tool::Tolerance(aF);
872 BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
873 aP2DInf.SetCoord(aUMin, aVMin);
878 for (i=1; i<=aNbIter; ++i) {
879 IntTools_FClass2d aFClass2d;
881 aFClass2d.Init(aF, aTol);
883 bIsHole=aFClass2d.IsHole();
887 printf(" new bIsHole=%d\n", bIsHole);
888 //``````````````````````````````````````````
891 for (i=1; i<=aNbIter; ++i) {
892 IntTools_FClass2d aFClass2d;
894 aFClass2d.Init(aF, aTol);
896 aState=aFClass2d.Perform(aP2DInf);
897 bIsHole=(aState==TopAbs_IN);
901 printf(" was bIsHole=%d\n", bIsHole);
905 //modified by NIZNHY-PKV Mon May 29 11:40:31 2006t