1 // Created on: 1995-04-13
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1995-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.
17 #include <BRepTest.hxx>
20 #include <Draw_Interpretor.hxx>
22 #include <DrawTrSurf.hxx>
27 #include <Standard_ErrorHandler.hxx>
28 #include <Precision.hxx>
29 #include <TCollection_AsciiString.hxx>
37 #include <TColgp_SequenceOfPnt.hxx>
39 #include <GeomAbs_JoinType.hxx>
40 #include <Geom_Line.hxx>
42 #include <IntCurvesFace_Intersector.hxx>
45 #include <TopAbs_Orientation.hxx>
48 #include <TopoDS_Shape.hxx>
49 #include <TopoDS_Compound.hxx>
50 #include <TopoDS_CompSolid.hxx>
51 #include <TopoDS_Solid.hxx>
52 #include <TopoDS_Shell.hxx>
53 #include <TopoDS_Face.hxx>
54 #include <TopoDS_Wire.hxx>
55 #include <TopoDS_Edge.hxx>
56 #include <TopoDS_Vertex.hxx>
57 #include <TopoDS_Iterator.hxx>
59 #include <BRep_Builder.hxx>
60 #include <BRep_Tool.hxx>
62 #include <TopExp_Explorer.hxx>
64 #include <TopTools_ListOfShape.hxx>
65 #include <TopTools_ListIteratorOfListOfShape.hxx>
66 #include <TopTools_MapOfShape.hxx>
68 #include <LocOpe_CSIntersector.hxx>
69 #include <LocOpe_SequenceOfLin.hxx>
70 #include <LocOpe_PntFace.hxx>
71 #include <BRepFeat_MakeDPrism.hxx>
73 #include <BRepTools.hxx>
74 #include <BRepIntCurveSurface_Inter.hxx>
75 #include <BRepOffset_MakeOffset.hxx>
76 #include <BRepClass3d_SolidClassifier.hxx>
77 #include <GeomAdaptor_Curve.hxx>
80 void SampleEdges (const TopoDS_Shape& theShape,
81 TColgp_SequenceOfPnt& theSeq);
83 TopoDS_Face NextFaceForPrism (const TopoDS_Shape& shape,
84 const TopoDS_Shape& basis,
87 void PrintState (Draw_Interpretor& aDI,
88 const TopAbs_State& aState);
90 static Standard_Integer emptyshape(Draw_Interpretor&, Standard_Integer, const char** );
91 static Standard_Integer subshape (Draw_Interpretor&, Standard_Integer, const char** );
92 static Standard_Integer brepintcs (Draw_Interpretor&, Standard_Integer, const char** );
93 static Standard_Integer MakeBoss (Draw_Interpretor&, Standard_Integer, const char** );
94 static Standard_Integer MakeShell (Draw_Interpretor&, Standard_Integer, const char** );
95 static Standard_Integer xbounds (Draw_Interpretor&, Standard_Integer, const char** );
96 static Standard_Integer xclassify (Draw_Interpretor&, Standard_Integer, const char** );
98 //=======================================================================
99 //function : OtherCommands
101 //=======================================================================
102 void BRepTest::OtherCommands(Draw_Interpretor& theCommands)
104 static Standard_Boolean done = Standard_False;
106 done = Standard_True;
108 const char* g = "TOPOLOGY other commands";
110 theCommands.Add("shape",
111 "shape name V/E/W/F/Sh/So/CS/C; make a empty shape",__FILE__,emptyshape,g);
113 theCommands.Add("subshape",
114 "subshape name V/E/W/F/Sh/So/CS/C index; get subsshape <index> of given type"
115 ,__FILE__,subshape,g);
117 theCommands.Add("BRepIntCS",
118 "Calcul d'intersection entre face et curve : BRepIntCS curve1 [curve2 ...] shape [res] [tol]"
119 ,__FILE__,brepintcs,g);
121 theCommands.Add("makeboss", "create a boss on the shape myS", __FILE__, MakeBoss, g);
122 theCommands.Add("mksh", "create a shell on Shape", __FILE__, MakeShell, g);
123 theCommands.Add("xbounds", "xbounds face", __FILE__, xbounds, g);
124 theCommands.Add("xclassify", "use xclassify Solid [Tolerance=1.e-7]", __FILE__, xclassify, g);
128 //=======================================================================
129 //function : emptyshape
130 //purpose : shape : shape name V/E/W/F/SH/SO/CS/C
131 //=======================================================================
132 Standard_Integer emptyshape(Draw_Interpretor& , Standard_Integer n, const char** a)
134 if (n <= 1) return 1;
140 TCollection_AsciiString as(a[2]); as.LowerCase();
141 const char* a2 = as.ToCString();
143 if ( ! strcmp(a2,"c") ) {
144 TopoDS_Compound SS; B.MakeCompound(SS); S = SS;
146 else if ( ! strcmp(a2,"cs") ) {
147 TopoDS_CompSolid SS; B.MakeCompSolid(SS); S = SS;
149 else if ( ! strcmp(a2,"so") ) {
150 TopoDS_Solid SS; B.MakeSolid(SS); S = SS;
152 else if ( ! strcmp(a2,"sh") ) {
153 TopoDS_Shell SS; B.MakeShell(SS); S = SS;
155 else if ( ! strcmp(a2,"f") ) {
156 TopoDS_Face SS; B.MakeFace(SS); S = SS;
158 else if ( ! strcmp(a2,"w") ) {
159 TopoDS_Wire SS; B.MakeWire(SS); S = SS;
161 else if ( ! strcmp(a2,"e") ) {
162 TopoDS_Edge SS; B.MakeEdge(SS); S = SS;
164 else if ( ! strcmp(a2,"v") ) {
165 TopoDS_Vertex SS; B.MakeVertex(SS); S = SS;
172 const char *shapename = a[1];
173 DBRep::Set(shapename,S);
177 //=======================================================================
178 //function : subshape
180 //=======================================================================
181 Standard_Integer subshape(Draw_Interpretor& di, Standard_Integer n, const char** a)
183 if (n <= 2) return 1;
186 TopoDS_Shape S = DBRep::Get(a[1]);
187 if (S.IsNull()) return 0;
189 strcpy(newname,a[1]);
191 while (*p != '\0') p++;
194 Standard_Integer i = 0;
196 Standard_Integer isub = Draw::Atoi(a[2]);
197 TopoDS_Iterator itr(S);
202 DBRep::Set(newname,itr.Value());
203 di.AppendElement(newname);
211 TopAbs_ShapeEnum typ;
216 typ = TopAbs_COMPSOLID;
221 if ((a[2][1] == 'O')||(a[2][1] == 'o'))
223 else if ((a[2][1] == 'H')||(a[2][1] == 'h'))
253 Standard_Integer isub = Draw::Atoi(a[3]);
254 TopTools_MapOfShape M;
256 TopExp_Explorer ex(S,typ);
258 if (M.Add(ex.Current())) {
262 DBRep::Set(newname,ex.Current());
263 di.AppendElement(newname);
272 //=======================================================================
273 //function : brepintcs
275 //=======================================================================
276 Standard_Integer brepintcs(Draw_Interpretor& di, Standard_Integer n, const char** a)
280 std::cout<<"Invalid input arguments. Should be: curve1 [curve2 ...] shape [result] [tol]"<<std::endl;
283 Standard_Integer indshape = 2;
285 for( ; indshape <= n-1 ; indshape++)
287 S = DBRep::Get(a[indshape]);
293 std::cout<<"Invalid input shape"<<std::endl;
297 BRepIntCurveSurface_Inter theAlg;
301 Standard_Real preci = atof(a[n-1]);
302 if(preci >= Precision::Confusion())
307 TopoDS_Compound aComp;
309 aB.MakeCompound(aComp);
311 Handle(Geom_Curve) C= DrawTrSurf::GetCurve(a[1]);
312 if (C.IsNull()) return 2;
313 GeomAdaptor_Curve acur(C);
314 theAlg.Init(S, acur, tol);
316 for (; theAlg.More(); theAlg.Next()) {
320 aB.MakeVertex(aV, curp, 0);
323 di<<"Point "<<nbpi<<" : "<<curp.X()<<" "<<curp.Y()<<" "<<curp.Z()<<"\n";
325 char* temp = name; // pour portage WNT
326 Sprintf(temp, "%s_%d", "brics", nbpi);
327 DrawTrSurf::Set(temp, curp);
332 for (Standard_Integer il = 1; il<indshape ; il++)
334 Handle(Geom_Curve) hl= DrawTrSurf::GetCurve(a[il]);
337 for (; theAlg.More(); theAlg.Next()) {
341 aB.MakeVertex(aV, curp, 0);
343 di<<"Point "<<nbpi<<" : "<<curp.X()<<" "<<curp.Y()<<" "<<curp.Z()<<"\n";
345 char* temp = name; // pour portage WNT
346 Sprintf(temp, "%s_%d", "brics", nbpi);
347 DrawTrSurf::Set(temp, curp);
353 di<<"Points of intersections are not found\n";
355 DBRep::Set(a[n-1], aComp);
359 //=======================================================================
360 //function : MakeBoss
362 //=======================================================================
363 Standard_Integer MakeBoss(Draw_Interpretor& , Standard_Integer , const char** a)
365 TopoDS_Shape myS = DBRep::Get( a[2] );
367 TopoDS_Shape myBasis = DBRep::Get( a[3] ) ;
369 Standard_Real ang = -0.05235987901687622;
372 TopoDS_Face basis = TopoDS::Face(myBasis);
374 BRepFeat_MakeDPrism DPRISM(myS, basis, basis, ang, 1, Standard_True);
376 TopoDS_Shape myFaceOnShape;
377 gp_Pnt Pnt(0.0, 0.0, 50.0);
378 gp_Dir Dir(-0.0, -0.0, -1.0);
381 myFaceOnShape = NextFaceForPrism(myS, myBasis, ax);
383 DPRISM.Perform (myFaceOnShape);
386 if( DPRISM.IsDone() ) DBRep::Set( a[1], DPRISM.Shape() );
390 //=======================================================================
391 //function : MakeShell
393 //=======================================================================
394 Standard_Integer MakeShell(Draw_Interpretor& , Standard_Integer , const char** a)
397 TopoDS_Shape aShape = DBRep::Get( a[1] );
398 TopTools_ListOfShape Lst;
399 TopExp_Explorer Exp(aShape, TopAbs_FACE);
400 TopoDS_Shape InputShape(DBRep::Get( a[2] ));
401 TopoDS_Face F = TopoDS::Face(InputShape);
402 // TopoDS_Face F = TopoDS::Face(DBRep::Get( a[2] ));
404 Standard_Real Off = -Draw::Atof( a[3] );
406 BRepOffset_MakeOffset Offset;
408 Offset.Initialize( aShape, Off, 1.0e-3, BRepOffset_Skin,
409 Standard_True , Standard_False , GeomAbs_Arc );
411 Offset.MakeThickSolid();
413 if( Offset.IsDone() ) {
414 // SaveShape::Save(Offset.Shape(), "ss");
415 DBRep::Set( a[1], Offset.Shape() );
419 //=======================================================================
422 //=======================================================================
423 Standard_Integer xbounds(Draw_Interpretor& di, Standard_Integer n, const char** a)
426 di << "Usage : " << a[0] << " face\n";
431 Standard_Real aUMin, aUMax, aVMin, aVMax;
437 di << " null shapes is not allowed here\n";
440 if (aS.ShapeType()!=TopAbs_FACE) {
441 di << " shape" << a[1] <<" must be a face\n";
445 aF=*((TopoDS_Face*)&aS);
447 BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
449 TCollection_AsciiString aStr;
450 TCollection_AsciiString sUMin(aUMin);
451 TCollection_AsciiString sUMax(aUMax);
452 TCollection_AsciiString sVMin(aVMin);
453 TCollection_AsciiString sVMax(aVMax);
455 aStr=aStr+sUMin + "\n";
456 aStr=aStr+sUMax + "\n";
457 aStr=aStr+sVMin + "\n";
458 aStr=aStr+sVMax + "\n";
459 di <<aStr.ToCString();
463 //=======================================================================
464 //function : xclassify
466 //=======================================================================
467 Standard_Integer xclassify (Draw_Interpretor& aDI, Standard_Integer n, const char** a)
470 aDI<<" use xclassify Solid [Tolerance=1.e-7]\n";
474 TopoDS_Shape aS = DBRep::Get(a[1]);
476 aDI<<" Null Shape is not allowed here\n";
480 if (aS.ShapeType()!=TopAbs_SOLID) {
481 aDI<< " Shape type must be SOLID\n";
485 Standard_Real aTol=1.e-7;
486 TopAbs_State aState = TopAbs_UNKNOWN;
490 aTol=Draw::Atof(a[2]);
493 BRepClass3d_SolidClassifier aSC(aS);
494 aSC.PerformInfinitePoint(aTol);
496 aState = aSC.State();
497 PrintState(aDI, aState);
501 //=======================================================================
502 //function : PrintState
504 //=======================================================================
505 void PrintState (Draw_Interpretor& aDI,
506 const TopAbs_State& aState)
525 //=======================================================================
526 //function : NextFaceForPrism
527 //purpose : Search a face from <shape> which intersects with a line of
528 // direction <ax1> and location a point of <basis>.
529 //=======================================================================
530 TopoDS_Face NextFaceForPrism (const TopoDS_Shape& shape,
531 const TopoDS_Shape& basis,
534 TopoDS_Face nextFace;
536 TColgp_SequenceOfPnt seqPnts;
537 SampleEdges(basis, seqPnts);
539 for (Standard_Integer i=1; i<=seqPnts.Length(); i++) {
540 const gp_Pnt& pt = seqPnts(i);
541 // find a axis through a face
542 gp_Dir dir = ax1.Direction();
543 gp_Ax1 ax1b(pt, dir);
545 LocOpe_CSIntersector ASI(shape);
546 LocOpe_SequenceOfLin slin;
551 Standard_Integer no=1, IndFrom, IndTo;
552 TopAbs_Orientation theOr;
553 Standard_Real min = 1.e-04, Tol = -Precision::Confusion();
554 if (ASI.LocalizeAfter (no, min, Tol, theOr, IndFrom, IndTo)) {
555 nextFace = ASI.Point(no, IndFrom).Face();
565 //=======================================================================
566 //function : SampleEdges
567 //purpose : Sampling of <theShape>.
568 //design : Collect the vertices and points on the edges
569 //=======================================================================
570 void SampleEdges (const TopoDS_Shape& theShape, TColgp_SequenceOfPnt& theSeq)
576 TopTools_MapOfShape theMap;
579 // Adds all vertices/pnt
580 for (exp.Init(theShape,TopAbs_VERTEX); exp.More(); exp.Next()) {
581 if (theMap.Add(exp.Current())) {
582 theSeq.Append (BRep_Tool::Pnt(TopoDS::Vertex(exp.Current())));
586 // Computes points on edge, but does not take the extremities into account
587 Standard_Integer NECHANT = 5;
588 Handle(Geom_Curve) C;
589 Standard_Real f,l,prm;
590 for (exp.Init (theShape,TopAbs_EDGE); exp.More(); exp.Next()) {
591 const TopoDS_Edge& edg = TopoDS::Edge(exp.Current());
592 if (theMap.Add(edg)) {
593 if (!BRep_Tool::Degenerated(edg)) {
594 C = BRep_Tool::Curve(edg,f,l);
595 for (Standard_Integer i=1; i < NECHANT; i++) {
596 prm = ((NECHANT-i)*f+i*l)/NECHANT;
597 theSeq.Append (C->Value(prm));