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.hxx>
76 #include <BRepOffset_MakeOffset.hxx>
77 #include <BRepClass3d_SolidClassifier.hxx>
78 #include <GeomAdaptor_Curve.hxx>
81 void SampleEdges (const TopoDS_Shape& theShape,
82 TColgp_SequenceOfPnt& theSeq);
84 TopoDS_Face NextFaceForPrism (const TopoDS_Shape& shape,
85 const TopoDS_Shape& basis,
88 void PrintState (Draw_Interpretor& aDI,
89 const TopAbs_State& aState);
91 static Standard_Integer emptyshape(Draw_Interpretor&, Standard_Integer, const char** );
92 static Standard_Integer subshape (Draw_Interpretor&, Standard_Integer, const char** );
93 static Standard_Integer brepintcs (Draw_Interpretor&, Standard_Integer, const char** );
94 static Standard_Integer MakeBoss (Draw_Interpretor&, Standard_Integer, const char** );
95 static Standard_Integer MakeShell (Draw_Interpretor&, Standard_Integer, const char** );
96 static Standard_Integer xbounds (Draw_Interpretor&, Standard_Integer, const char** );
97 static Standard_Integer xclassify (Draw_Interpretor&, Standard_Integer, const char** );
99 //=======================================================================
100 //function : OtherCommands
102 //=======================================================================
103 void BRepTest::OtherCommands(Draw_Interpretor& theCommands)
105 static Standard_Boolean done = Standard_False;
107 done = Standard_True;
109 const char* g = "TOPOLOGY other commands";
111 theCommands.Add("shape",
112 "shape name V/E/W/F/Sh/So/CS/C; make a empty shape",__FILE__,emptyshape,g);
114 theCommands.Add("subshape",
115 "subshape name V/E/W/F/Sh/So/CS/C index; get subsshape <index> of given type"
116 ,__FILE__,subshape,g);
118 theCommands.Add("BRepIntCS",
119 "Calcul d'intersection entre face et curve : BRepIntCS curve1 [curve2 ...] shape [res] [tol]"
120 ,__FILE__,brepintcs,g);
122 theCommands.Add("makeboss", "create a boss on the shape myS", __FILE__, MakeBoss, g);
123 theCommands.Add("mksh", "create a shell on Shape", __FILE__, MakeShell, g);
124 theCommands.Add("xbounds", "xbounds face", __FILE__, xbounds, g);
125 theCommands.Add("xclassify", "use xclassify Solid [Tolerance=1.e-7]", __FILE__, xclassify, g);
129 //=======================================================================
130 //function : emptyshape
131 //purpose : shape : shape name V/E/W/F/SH/SO/CS/C
132 //=======================================================================
133 Standard_Integer emptyshape(Draw_Interpretor& , Standard_Integer n, const char** a)
135 if (n <= 1) return 1;
141 TCollection_AsciiString as(a[2]); as.LowerCase();
142 const char* a2 = as.ToCString();
144 if ( ! strcmp(a2,"c") ) {
145 TopoDS_Compound SS; B.MakeCompound(SS); S = SS;
147 else if ( ! strcmp(a2,"cs") ) {
148 TopoDS_CompSolid SS; B.MakeCompSolid(SS); S = SS;
150 else if ( ! strcmp(a2,"so") ) {
151 TopoDS_Solid SS; B.MakeSolid(SS); S = SS;
153 else if ( ! strcmp(a2,"sh") ) {
154 TopoDS_Shell SS; B.MakeShell(SS); S = SS;
156 else if ( ! strcmp(a2,"f") ) {
157 TopoDS_Face SS; B.MakeFace(SS); S = SS;
159 else if ( ! strcmp(a2,"w") ) {
160 TopoDS_Wire SS; B.MakeWire(SS); S = SS;
162 else if ( ! strcmp(a2,"e") ) {
163 TopoDS_Edge SS; B.MakeEdge(SS); S = SS;
165 else if ( ! strcmp(a2,"v") ) {
166 TopoDS_Vertex SS; B.MakeVertex(SS); S = SS;
173 const char *shapename = a[1];
174 DBRep::Set(shapename,S);
178 //=======================================================================
179 //function : subshape
181 //=======================================================================
182 Standard_Integer subshape(Draw_Interpretor& di, Standard_Integer n, const char** a)
184 if (n <= 2) return 1;
187 TopoDS_Shape S = DBRep::Get(a[1]);
188 if (S.IsNull()) return 0;
190 strcpy(newname,a[1]);
192 while (*p != '\0') p++;
195 Standard_Integer i = 0;
197 Standard_Integer isub = Draw::Atoi(a[2]);
198 TopoDS_Iterator itr(S);
203 DBRep::Set(newname,itr.Value());
204 di.AppendElement(newname);
212 TopAbs_ShapeEnum typ;
217 typ = TopAbs_COMPSOLID;
222 if ((a[2][1] == 'O')||(a[2][1] == 'o'))
224 else if ((a[2][1] == 'H')||(a[2][1] == 'h'))
254 Standard_Integer isub = Draw::Atoi(a[3]);
255 TopTools_MapOfShape M;
257 TopExp_Explorer ex(S,typ);
259 if (M.Add(ex.Current())) {
263 DBRep::Set(newname,ex.Current());
264 di.AppendElement(newname);
273 //=======================================================================
274 //function : brepintcs
276 //=======================================================================
277 Standard_Integer brepintcs(Draw_Interpretor& di, Standard_Integer n, const char** a)
281 cout<<"Invalid input arguments. Should be: curve1 [curve2 ...] shape [result] [tol]"<<endl;
284 Standard_Integer indshape = 2;
286 for( ; indshape <= n-1 ; indshape++)
288 S = DBRep::Get(a[indshape]);
294 cout<<"Invalid input shape"<<endl;
298 BRepIntCurveSurface_Inter theAlg;
302 Standard_Real preci = atof(a[n-1]);
303 if(preci >= Precision::Confusion())
308 TopoDS_Compound aComp;
310 aB.MakeCompound(aComp);
312 Handle(Geom_Curve) C= DrawTrSurf::GetCurve(a[1]);
313 if (C.IsNull()) return 2;
314 GeomAdaptor_Curve acur(C);
315 theAlg.Init(S, acur, tol);
317 for (; theAlg.More(); theAlg.Next()) {
321 aB.MakeVertex(aV, curp, 0);
324 di<<"Point "<<nbpi<<" : "<<curp.X()<<" "<<curp.Y()<<" "<<curp.Z()<<"\n";
326 char* temp = name; // pour portage WNT
327 Sprintf(temp, "%s_%d", "brics", nbpi);
328 DrawTrSurf::Set(temp, curp);
333 for (Standard_Integer il = 1; il<indshape ; il++)
335 Handle(Geom_Curve) hl= DrawTrSurf::GetCurve(a[il]);
338 for (; theAlg.More(); theAlg.Next()) {
342 aB.MakeVertex(aV, curp, 0);
344 di<<"Point "<<nbpi<<" : "<<curp.X()<<" "<<curp.Y()<<" "<<curp.Z()<<"\n";
346 char* temp = name; // pour portage WNT
347 Sprintf(temp, "%s_%d", "brics", nbpi);
348 DrawTrSurf::Set(temp, curp);
354 di<<"Points of intersections are not found\n";
356 DBRep::Set(a[n-1], aComp);
360 //=======================================================================
361 //function : MakeBoss
363 //=======================================================================
364 Standard_Integer MakeBoss(Draw_Interpretor& , Standard_Integer , const char** a)
366 TopoDS_Shape myS = DBRep::Get( a[2] );
368 TopoDS_Shape myBasis = DBRep::Get( a[3] ) ;
370 Standard_Real ang = -0.05235987901687622;
373 TopoDS_Face basis = TopoDS::Face(myBasis);
375 BRepFeat_MakeDPrism DPRISM(myS, basis, basis, ang, 1, Standard_True);
377 TopoDS_Shape myFaceOnShape;
378 gp_Pnt Pnt(0.0, 0.0, 50.0);
379 gp_Dir Dir(-0.0, -0.0, -1.0);
382 myFaceOnShape = NextFaceForPrism(myS, myBasis, ax);
384 DPRISM.Perform (myFaceOnShape);
387 if( DPRISM.IsDone() ) DBRep::Set( a[1], DPRISM.Shape() );
391 //=======================================================================
392 //function : MakeShell
394 //=======================================================================
395 Standard_Integer MakeShell(Draw_Interpretor& , Standard_Integer , const char** a)
398 TopoDS_Shape aShape = DBRep::Get( a[1] );
399 TopTools_ListOfShape Lst;
400 TopExp_Explorer Exp(aShape, TopAbs_FACE);
401 TopoDS_Shape InputShape(DBRep::Get( a[2] ));
402 TopoDS_Face F = TopoDS::Face(InputShape);
403 // TopoDS_Face F = TopoDS::Face(DBRep::Get( a[2] ));
405 Standard_Real Off = -Draw::Atof( a[3] );
407 BRepOffset_MakeOffset Offset;
409 Offset.Initialize( aShape, Off, 1.0e-3, BRepOffset_Skin,
410 Standard_True , Standard_False , GeomAbs_Arc );
412 Offset.MakeThickSolid();
414 if( Offset.IsDone() ) {
415 // SaveShape::Save(Offset.Shape(), "ss");
416 DBRep::Set( a[1], Offset.Shape() );
420 //=======================================================================
423 //=======================================================================
424 Standard_Integer xbounds(Draw_Interpretor& di, Standard_Integer n, const char** a)
427 di << "Usage : " << a[0] << " face\n";
432 Standard_Real aUMin, aUMax, aVMin, aVMax;
438 di << " null shapes is not allowed here\n";
441 if (aS.ShapeType()!=TopAbs_FACE) {
442 di << " shape" << a[1] <<" must be a face\n";
446 aF=*((TopoDS_Face*)&aS);
448 BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
450 TCollection_AsciiString aStr;
451 TCollection_AsciiString sUMin(aUMin);
452 TCollection_AsciiString sUMax(aUMax);
453 TCollection_AsciiString sVMin(aVMin);
454 TCollection_AsciiString sVMax(aVMax);
456 aStr=aStr+sUMin + "\n";
457 aStr=aStr+sUMax + "\n";
458 aStr=aStr+sVMin + "\n";
459 aStr=aStr+sVMax + "\n";
460 di <<aStr.ToCString();
464 //=======================================================================
465 //function : xclassify
467 //=======================================================================
468 Standard_Integer xclassify (Draw_Interpretor& aDI, Standard_Integer n, const char** a)
471 aDI<<" use xclassify Solid [Tolerance=1.e-7]\n";
475 TopoDS_Shape aS = DBRep::Get(a[1]);
477 aDI<<" Null Shape is not allowed here\n";
481 if (aS.ShapeType()!=TopAbs_SOLID) {
482 aDI<< " Shape type must be SOLID\n";
486 Standard_Real aTol=1.e-7;
487 TopAbs_State aState = TopAbs_UNKNOWN;
491 aTol=Draw::Atof(a[2]);
494 BRepClass3d_SolidClassifier aSC(aS);
495 aSC.PerformInfinitePoint(aTol);
497 aState = aSC.State();
498 PrintState(aDI, aState);
502 //=======================================================================
503 //function : PrintState
505 //=======================================================================
506 void PrintState (Draw_Interpretor& aDI,
507 const TopAbs_State& aState)
526 //=======================================================================
527 //function : NextFaceForPrism
528 //purpose : Search a face from <shape> which intersects with a line of
529 // direction <ax1> and location a point of <basis>.
530 //=======================================================================
531 TopoDS_Face NextFaceForPrism (const TopoDS_Shape& shape,
532 const TopoDS_Shape& basis,
535 TopoDS_Face nextFace;
537 TColgp_SequenceOfPnt seqPnts;
538 SampleEdges(basis, seqPnts);
540 for (Standard_Integer i=1; i<=seqPnts.Length(); i++) {
541 const gp_Pnt& pt = seqPnts(i);
542 // find a axis through a face
543 gp_Dir dir = ax1.Direction();
544 gp_Ax1 ax1b(pt, dir);
546 LocOpe_CSIntersector ASI(shape);
547 LocOpe_SequenceOfLin slin;
552 Standard_Integer no=1, IndFrom, IndTo;
553 TopAbs_Orientation theOr;
554 Standard_Real min = 1.e-04, Tol = -Precision::Confusion();
555 if (ASI.LocalizeAfter (no, min, Tol, theOr, IndFrom, IndTo)) {
556 nextFace = ASI.Point(no, IndFrom).Face();
566 //=======================================================================
567 //function : SampleEdges
568 //purpose : Sampling of <theShape>.
569 //design : Collect the vertices and points on the edges
570 //=======================================================================
571 void SampleEdges (const TopoDS_Shape& theShape, TColgp_SequenceOfPnt& theSeq)
577 TopTools_MapOfShape theMap;
580 // Adds all vertices/pnt
581 for (exp.Init(theShape,TopAbs_VERTEX); exp.More(); exp.Next()) {
582 if (theMap.Add(exp.Current())) {
583 theSeq.Append (BRep_Tool::Pnt(TopoDS::Vertex(exp.Current())));
587 // Computes points on edge, but does not take the extremities into account
588 Standard_Integer NECHANT = 5;
589 Handle(Geom_Curve) C;
590 Standard_Real f,l,prm;
591 for (exp.Init (theShape,TopAbs_EDGE); exp.More(); exp.Next()) {
592 const TopoDS_Edge& edg = TopoDS::Edge(exp.Current());
593 if (theMap.Add(edg)) {
594 if (!BRep_Tool::Degenerated(edg)) {
595 C = BRep_Tool::Curve(edg,f,l);
596 for (Standard_Integer i=1; i < NECHANT; i++) {
597 prm = ((NECHANT-i)*f+i*l)/NECHANT;
598 theSeq.Append (C->Value(prm));