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 <OSD_Chronometer.hxx>
60 #include <BRepTools.hxx>
61 #include <BOPTColStd_CArray1OfInteger.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 //modified by NIZNHY-PKV Thu Nov 10 12:11:15 2011f
80 void DumpArray(const BOPTColStd_CArray1OfInteger& aC,
81 Draw_Interpretor& aDI);
82 //modified by NIZNHY-PKV Thu Nov 10 12:11:18 2011t
84 static Standard_Integer bhaspc (Draw_Interpretor& , Standard_Integer , const char** );
85 static Standard_Integer baddve (Draw_Interpretor& , Standard_Integer , const char** );
86 static Standard_Integer bisclosed (Draw_Interpretor& , Standard_Integer , const char** );
87 static Standard_Integer bexplode (Draw_Interpretor& , Standard_Integer , const char** );
88 static Standard_Integer bupdateedge (Draw_Interpretor& , Standard_Integer , const char**);
89 static Standard_Integer bremovesim (Draw_Interpretor& , Standard_Integer , const char** );
90 static Standard_Integer bopsetdisp (Draw_Interpretor& , Standard_Integer , const char** );
91 static Standard_Integer bfaceprepare(Draw_Interpretor& , Standard_Integer , const char** );
92 static Standard_Integer brefine (Draw_Interpretor& , Standard_Integer , const char** );
93 static Standard_Integer bclassify (Draw_Interpretor& , Standard_Integer , const char** );
94 static Standard_Integer b2dclassify (Draw_Interpretor& , Standard_Integer , const char** );
96 static Standard_Integer bhole (Draw_Interpretor& , Standard_Integer , const char** );
97 static Standard_Integer bxhole (Draw_Interpretor& , Standard_Integer , const char** );
98 //=======================================================================
99 //function : LowCommands
101 //=======================================================================
102 void BOPTest::LowCommands(Draw_Interpretor& theCommands)
104 static Standard_Boolean done = Standard_False;
106 done = Standard_True;
108 const char* g = "CCR commands";
110 // Whether Edge has PCurve on Face
111 theCommands.Add("bhaspc" , "Use >bhaspc Edge Face" , __FILE__, bhaspc , g);
112 theCommands.Add("baddve" , "Use >addve E V1 V2 p1 p2", __FILE__, baddve , g);
113 theCommands.Add("bisclosed" , "Use >bisclosed Edge Face" , __FILE__, bisclosed , g);
114 theCommands.Add("bexplode" , "" , __FILE__, bexplode , g);
115 theCommands.Add("bupdateedge" , "Use bupdateedge Edge Face", __FILE__, bupdateedge , g);
116 theCommands.Add("bremovesim" , "Use bremovesim Face" , __FILE__, bremovesim , g);
117 theCommands.Add("bopsetdisp" , "Use bopsetdisp" , __FILE__, bopsetdisp , g);
120 theCommands.Add("bfaceprepare" , "Use bfaceprepare F1 " , __FILE__, bfaceprepare, g);
122 theCommands.Add("brefine" , "Use brefine Shape " , __FILE__, brefine , g);
123 theCommands.Add("bclassify" , "Use >bclassify Solid Point [Tolerance=1.e-7]",
124 __FILE__, bclassify , g);
125 theCommands.Add("b2dclassify" , "Use >bclassify Face Point2d [Tol2D=Tol(Face)] ",
126 __FILE__, b2dclassify , g);
127 theCommands.Add("bhole" , "Use bhole" , __FILE__, bhole , g);
128 theCommands.Add("bxhole" , "Use bxhole" , __FILE__, bxhole , g);
131 //=======================================================================
134 //=======================================================================
135 Standard_Integer brefine (Draw_Interpretor& di,
140 di << " Use >brefine Shape1\n";
144 TopoDS_Shape aS = DBRep::Get(a[1]);
148 di << " Null shape is not allowed \n";
152 Standard_Boolean bIsDone;
153 BOP_Refiner aRefiner;
154 aRefiner.SetShape(aS);
158 bIsDone=aRefiner.IsDone();
161 Standard_Integer aNbRemovedVertices, aNbRemovedEdges, iFlag=0;
162 aNbRemovedVertices=aRefiner.NbRemovedVertices();
163 aNbRemovedEdges=aRefiner.NbRemovedEdges();
165 if (aNbRemovedVertices) {
166 di << " Vertices removed=" << aNbRemovedVertices << "%d\n";
170 if (aNbRemovedEdges) {
171 di << " Edges removed=" << aNbRemovedEdges << "\n";
177 TCollection_AsciiString aNm(a[1]);
178 Standard_CString pstr;
180 pstr=aNm.ToCString();
181 DBRep::Set(pstr, aS);
186 di << "Not Done, err=" << aRefiner.ErrorStatus() << "\n";
190 //=======================================================================
191 //function : bfaceprepare
193 //=======================================================================
194 Standard_Integer bfaceprepare(Draw_Interpretor& di, Standard_Integer n, const char** a)
197 di << " Use bfaceprepare> F1\n";
201 TopoDS_Shape S1 = DBRep::Get(a[1]);
205 di << " Null shape is not allowed \n";
208 if (S1.ShapeType()!=TopAbs_FACE) {
209 di << " Type mismatch\n";
213 const TopoDS_Face& aF1=TopoDS::Face(S1);
215 TopAbs_Orientation anOr;
216 anOr=aF1.Orientation();
217 if (anOr==TopAbs_FORWARD) {
225 Handle(Geom_Surface) aS1=BRep_Tool::Surface(aF1);
226 Handle(Geom_Surface) aS=Handle(Geom_Surface)::DownCast(aS1->Copy());
229 aTol=BRep_Tool::Tolerance(aF1);
231 aBB.MakeFace (newFace, aS, aTol);
233 TopExp_Explorer anExp(aF1, TopAbs_WIRE);
234 for (; anExp.More(); anExp.Next()) {
235 const TopoDS_Shape& newWire=anExp.Current();
236 aBB.Add(newFace, newWire);
239 TCollection_AsciiString aNm(a[1]), aNmNew("_r");
241 Standard_CString an=aNm.ToCString();
243 DBRep::Set(an, newFace);
246 //=======================================================================
247 //function : bopsetdisp
249 //=======================================================================
250 Standard_Integer bopsetdisp (Draw_Interpretor& di, Standard_Integer , const char** )
252 Standard_Integer aFlag;
254 char* xr=getenv("MDISP");
256 aFlag=putenv((char*)"MDISP=yes");
257 di << " MDISP=yes. Return code=" << aFlag << "\n";
259 else if (!strcmp (xr, "yes")) {
260 aFlag=putenv((char*)"MDISP=no");
261 di << " MDISP=no. Return code=" << aFlag << "\n";
264 aFlag=putenv((char*)"MDISP=yes");
265 di << " MDISP=yes. Return code=" << aFlag << "\n";
271 //=======================================================================
272 //function : bremovesim
274 //=======================================================================
275 Standard_Integer bremovesim (Draw_Interpretor& di, Standard_Integer n, const char** a)
278 di << " Use bremovesim> Face\n";
282 TopoDS_Shape S1 = DBRep::Get(a[1]);
286 di << " Null shapes are not allowed \n";
289 if (S1.ShapeType()!=TopAbs_FACE) {
290 di << " Type mismatch\n";
295 const TopoDS_Face& aF=TopoDS::Face(S1);
297 Handle(IntTools_Context) aCtx=new IntTools_Context;
298 BOPTools_Tools3D::RemoveSims (aF, aCtx);
303 //=======================================================================
304 //function : bupdateedge
306 //=======================================================================
307 Standard_Integer bupdateedge (Draw_Interpretor& di, Standard_Integer n, const char** a)
310 di << " Use bupdateedge> Edge Face\n";
314 TopoDS_Shape S1 = DBRep::Get(a[1]);
315 TopoDS_Shape S2 = DBRep::Get(a[2]);
317 if (S1.IsNull() || S2.IsNull()) {
318 di << " Null shapes are not allowed \n";
321 if (S1.ShapeType()!=TopAbs_EDGE || S2.ShapeType()!=TopAbs_FACE) {
322 di << " Type mismatch\n";
326 const TopoDS_Edge& aE=TopoDS::Edge(S1);
327 const TopoDS_Face& aF=TopoDS::Face(S2);
328 Handle(Geom2d_Curve) aC2D;
331 aTol=BRep_Tool::Tolerance(aE);
333 BB.UpdateEdge(aE, aC2D, aF, aTol);
338 //=======================================================================
339 //function : bisclosed
341 //=======================================================================
342 Standard_Integer bisclosed (Draw_Interpretor& di, Standard_Integer n, const char** a)
345 di << " Use bisclosed> Edge Face\n";
349 TopoDS_Shape S1 = DBRep::Get(a[1]);
350 TopoDS_Shape S2 = DBRep::Get(a[2]);
352 if (S1.IsNull() || S2.IsNull()) {
353 di << " Null shapes are not allowed \n";
356 if (S1.ShapeType()!=TopAbs_EDGE || S2.ShapeType()!=TopAbs_FACE) {
357 di << " Type mismatch\n";
361 const TopoDS_Edge& aE=TopoDS::Edge(S1);
362 const TopoDS_Face& aF=TopoDS::Face(S2);
364 Standard_Boolean anIsClosed;
365 anIsClosed=BRep_Tool::IsClosed(aE, aF);
368 di << " Edge is not closed on this face\n";
371 di << " Edge is closed on this face\n";
376 //=======================================================================
379 //=======================================================================
380 Standard_Integer bhaspc (Draw_Interpretor& di, Standard_Integer n, const char** a)
383 di << " Use bhaspc> Edge Face [do]\n";
387 TopoDS_Shape S1 = DBRep::Get(a[1]);
388 TopoDS_Shape S2 = DBRep::Get(a[2]);
390 if (S1.IsNull() || S2.IsNull()) {
391 di << " Null shapes are not allowed \n";
394 if (S1.ShapeType()!=TopAbs_EDGE || S2.ShapeType()!=TopAbs_FACE) {
395 di << " Type mismatch\n";
399 const TopoDS_Edge& aE=TopoDS::Edge(S1);
400 const TopoDS_Face& aF=TopoDS::Face(S2);
401 Standard_Real f2D, l2D;
403 Handle(Geom2d_Curve) C2D=CurveOnSurface(aE, aF, f2D, l2D);
406 di << " No 2D Curves detected\n";
409 di << " Ok Edge has P-Curve on this Face\n";
413 if (!strcmp(a[3], "do")) {
414 BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aE, aF);
421 //=======================================================================
424 //=======================================================================
425 Standard_Integer baddve (Draw_Interpretor& di, Standard_Integer n, const char** a)
428 di << " Use baddve> E V1 V2 p1 p2\n";
432 TopoDS_Shape S1 = DBRep::Get(a[1]);
433 TopoDS_Shape S2 = DBRep::Get(a[2]);
434 TopoDS_Shape S3 = DBRep::Get(a[3]);
435 if (S1.IsNull() || S2.IsNull() || S3.IsNull()) {
436 di << " Null shapes are not allowed \n";
440 if (S1.ShapeType()!=TopAbs_EDGE ||
441 S2.ShapeType()!=TopAbs_VERTEX ||
442 S3.ShapeType()!=TopAbs_VERTEX ) {
443 di << " Type mismatch\n";
446 const TopoDS_Edge& aE=TopoDS::Edge(S1);
447 const TopoDS_Vertex& aV1=TopoDS::Vertex(S2);
448 const TopoDS_Vertex& aV2=TopoDS::Vertex(S3);
451 Standard_Real p1, p2;
467 static Handle(Geom2d_Curve) nullPCurve;
468 //=======================================================================
469 //function : CurveOnSurface
471 //=======================================================================
472 Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E,
473 const TopoDS_Face& F,
474 Standard_Real& First,
478 const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,l);
479 TopoDS_Edge aLocalEdge = E;
480 if (F.Orientation() == TopAbs_REVERSED) {
481 aLocalEdge.Reverse();
483 return CurveOnSurface(aLocalEdge,S,l,First,Last);
486 //=======================================================================
487 //function : CurveOnSurface
489 //=======================================================================
490 Handle(Geom2d_Curve) CurveOnSurface(const TopoDS_Edge& E,
491 const Handle(Geom_Surface)& S,
492 const TopLoc_Location& L,
493 Standard_Real& First,
496 TopLoc_Location l = L.Predivided(E.Location());
497 Standard_Boolean Eisreversed = (E.Orientation() == TopAbs_REVERSED);
499 // find the representation
500 BRep_ListIteratorOfListOfCurveRepresentation itcr
501 ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
503 while (itcr.More()) {
504 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
505 if (cr->IsCurveOnSurface(S,l)) {
506 const Handle(BRep_GCurve)& GC = *((Handle(BRep_GCurve)*)&cr);
507 GC->Range(First,Last);
508 if (GC->IsCurveOnClosedSurface() && Eisreversed)
509 return GC->PCurve2();
518 //=======================================================================
519 //function : bexplode
521 //=======================================================================
522 static Standard_Integer bexplode(Draw_Interpretor& di,
526 if (n <= 1) return 1;
527 TopoDS_Shape S = DBRep::Get(a[1]);
528 if (S.IsNull()) return 0;
530 strcpy(newname,a[1]);
532 while (*p != '\0') p++;
535 Standard_Integer i = 0;
537 TopoDS_Iterator itr(S);
541 DBRep::Set(newname,itr.Value());
542 di.AppendElement(newname);
548 TopAbs_ShapeEnum typ;
553 if ((a[2][1] == 'd')||(a[2][1] == 'D'))
554 typ = TopAbs_COMPOUND;
556 typ = TopAbs_COMPSOLID;
561 if ((a[2][1] == 'O')||(a[2][1] == 'o'))
563 else if ((a[2][1] == 'H')||(a[2][1] == 'h'))
594 TopExp_Explorer ex(S,typ);
595 for (; ex.More(); ex.Next()) {
596 const TopoDS_Shape& aS = ex.Current();
599 DBRep::Set(newname,aS);
600 di.AppendElement(newname);
606 //=======================================================================
607 //function : bclassify
609 //=======================================================================
610 Standard_Integer bclassify (Draw_Interpretor& aDI,
617 sprintf(sbf, " Use >bclassify Solid Point [Tolerance=1.e-7]\n");
622 TopoDS_Shape aS = DBRep::Get(a[1]);
624 sprintf(sbf, " Null Shape is not allowed here\n");
629 if (aS.ShapeType()!=TopAbs_SOLID) {
630 sprintf(sbf, " Shape type must be SOLID\n");
635 Standard_Real aTol=1.e-7;
636 TopAbs_State aState = TopAbs_UNKNOWN;
637 gp_Pnt aP(8., 9., 10.);
639 DrawTrSurf::GetPoint(a[2], aP);
646 BRepClass3d_SolidClassifier aSC(aS);
647 aSC.Perform(aP,aTol);
649 aState = aSC.State();
651 PrintState (aDI, aState);
656 //=======================================================================
657 //function : b2dclassify
659 //=======================================================================
660 Standard_Integer b2dclassify (Draw_Interpretor& aDI,
667 sprintf(sbf, " Use >bclassify Face Point2d [Tol2D=Tol(Face)]\n");
672 TopoDS_Shape aS = DBRep::Get(a[1]);
674 sprintf(sbf, " Null Shape is not allowed here\n");
679 if (aS.ShapeType()!=TopAbs_FACE) {
680 sprintf(sbf, " Shape type must be FACE\n");
686 TopAbs_State aState = TopAbs_UNKNOWN;
689 DrawTrSurf::GetPoint2d(a[2], aP);
691 const TopoDS_Face& aF=TopoDS::Face(aS);
692 aTol=BRep_Tool::Tolerance(aF);
697 BRepClass_FaceClassifier aClassifier;
698 aClassifier.Perform(aF, aP, aTol);
700 aState = aClassifier.State();
702 PrintState (aDI, aState);
706 //=======================================================================
707 //function : PrintState
709 //=======================================================================
710 void PrintState (Draw_Interpretor& aDI,
711 const TopAbs_State& aState)
714 TCollection_AsciiString sIN("IN"), sOUT("OUT of"), sON("ON"), sUNKNOWN("UNKNOWN");
716 sprintf(sbf, "The point is "); aDI<<sbf;
720 sprintf(sbf, sIN.ToCString());
723 sprintf(sbf, sOUT.ToCString());
726 sprintf(sbf, sON.ToCString());
729 sprintf(sbf, sUNKNOWN.ToCString());
732 sprintf(sbf, sUNKNOWN.ToCString());
737 sprintf(sbf, " shape\n");
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);