1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: GEOMAlgo_FinderShapeOn.cxx
24 // Created: Tue Jan 11 14:44:31 2005
25 // Author: Peter KURNEV
27 #include <GEOMAlgo_FinderShapeOn.hxx>
29 #include <Precision.hxx>
33 #include <TopAbs_ShapeEnum.hxx>
34 #include <TopAbs_Orientation.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Compound.hxx>
40 #include <TopoDS_Shell.hxx>
41 #include <TopoDS_Solid.hxx>
42 #include <TopoDS_Vertex.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Iterator.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48 #include <TopTools_DataMapOfShapeShape.hxx>
50 #include <Bnd_Box.hxx>
51 #include <BRepBndLib.hxx>
52 #include <BRepMesh_IncrementalMesh.hxx>
54 #include <BRep_Builder.hxx>
55 #include <BRep_Tool.hxx>
58 #include <TopExp_Explorer.hxx>
60 #include <BRepLib_MakeFace.hxx>
61 #include <BRepLib_FaceError.hxx>
64 #include <GEOMAlgo_WireSolid.hxx>
65 #include <GEOMAlgo_ShellSolid.hxx>
66 #include <GEOMAlgo_VertexSolid.hxx>
67 #include <GEOMAlgo_ShapeSolid.hxx>
68 #include <GEOMAlgo_SolidSolid.hxx>
69 #include <GEOMAlgo_SurfaceTools.hxx>
70 #include <GEOMAlgo_AlgoTools.hxx>
72 #include <BOPAlgo_PaveFiller.hxx>
73 #include <BOPCol_ListOfShape.hxx>
75 //=======================================================================
76 //function : GEOMAlgo_FinderShapeOn
78 //=======================================================================
79 GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
84 myShapeType=TopAbs_VERTEX;
85 myState=GEOMAlgo_ST_UNKNOWN;
86 myIsAnalytic=Standard_True;
88 //=======================================================================
91 //=======================================================================
92 GEOMAlgo_FinderShapeOn::~GEOMAlgo_FinderShapeOn()
95 //=======================================================================
96 //function : SetSurface
98 //=======================================================================
99 void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
103 //=======================================================================
106 //=======================================================================
107 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
111 //=======================================================================
112 //function : SetShapeType
114 //=======================================================================
115 void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
119 //=======================================================================
120 //function : ShapeType
122 //=======================================================================
123 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
127 //=======================================================================
128 //function : SetState
130 //=======================================================================
131 void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
135 //=======================================================================
138 //=======================================================================
139 GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
143 //=======================================================================
146 //=======================================================================
147 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
149 Standard_Boolean bIsConformState;
150 Standard_Integer i, aNb;
152 TopTools_ListOfShape* pL;
154 pL=(TopTools_ListOfShape*) &myLS;
158 for (i=1; i<=aNb; ++i) {
159 const TopoDS_Shape& aS=myMSS.FindKey(i);
160 aSt=myMSS.FindFromIndex(i);
162 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
163 if (bIsConformState) {
169 //=======================================================================
172 //=======================================================================
173 void GEOMAlgo_FinderShapeOn::Perform()
180 if (!myResult.IsNull()){
189 // Initialize the context
190 GEOMAlgo_ShapeAlgo::Perform();
192 myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
199 if (myIsAnalytic && myShapeType==TopAbs_VERTEX) {
210 if(myErrorStatus || myWarningStatus) {
215 //=======================================================================
216 //function : FindVertices
218 //=======================================================================
219 void GEOMAlgo_FinderShapeOn::FindVertices()
221 Standard_Integer i, aNb, iErr;
223 TopAbs_Orientation aOr;
225 TopTools_IndexedMapOfShape aM;
227 TopExp::MapShapes(myArg1, TopAbs_FACE, aM);
228 const TopoDS_Face& aF=TopoDS::Face(aM(1));
229 aOr=aF.Orientation();
232 TopExp::MapShapes(myShape, myShapeType, aM);
235 myWarningStatus=10; // No found sub-shapes of type myShapeType
239 for (i=1; i<=aNb; ++i) {
240 const TopoDS_Shape& aS=aM(i);
241 const TopoDS_Vertex& aV=TopoDS::Vertex(aS);
242 aP=BRep_Tool::Pnt(aV);
243 iErr=GEOMAlgo_SurfaceTools::GetState(aP, mySurface, myTolerance, aSt);
244 if (aOr==TopAbs_REVERSED) {
245 aSt=GEOMAlgo_SurfaceTools::ReverseState(aSt);
250 //=======================================================================
253 //=======================================================================
254 void GEOMAlgo_FinderShapeOn::Find()
256 Standard_Integer i, aNb;
257 Standard_Boolean bICS;
258 TopTools_IndexedMapOfShape aM;
260 TopExp::MapShapes(myArg2, myShapeType, aM);
264 myWarningStatus=10; // No found sub-shapes of type myShapeType
268 bICS=GEOMAlgo_AlgoTools::IsCompositeShape(myArg2);
269 if (!bICS || myIsAnalytic) {
270 TopoDS_Compound aCmp;
273 aBB.MakeCompound(aCmp);
274 for (i=1; i<=aNb; ++i) {
275 const TopoDS_Shape& aSi=aM(i);
284 for (i=1; i<=aNb; ++i) {
285 const TopoDS_Shape& aS=aM(i);
292 //=======================================================================
295 //=======================================================================
296 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
300 Standard_Integer i, iErr;
301 TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
302 TopTools_ListIteratorOfListOfShape aIt;
303 BOPCol_ListOfShape aLS;
304 BOPAlgo_PaveFiller aPF;
306 // 1. Prepare DSFiller
309 aPF.SetArguments(aLS);
312 iErr=aPF.ErrorStatus();
314 myErrorStatus=31; // PaveFiller is failed
319 GEOMAlgo_ShapeSolid* pSS;
320 GEOMAlgo_VertexSolid aVXS;
321 GEOMAlgo_WireSolid aWRS;
322 GEOMAlgo_ShellSolid aSHS;
323 GEOMAlgo_SolidSolid aSLS;
327 switch (myShapeType) {
338 aSLS.SetShape2(myArg2);
342 myErrorStatus=12; // unallowed sub-shape type
348 iErr=pSS->ErrorStatus();
350 myErrorStatus=32; // builder ShapeSolid failed
354 for (i=0; i<3; ++i) {
355 const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
357 for (; aIt.More(); aIt.Next()) {
358 const TopoDS_Shape& aSImage=aIt.Value();
359 if (myImages.IsBound(aSImage)) {
360 const TopoDS_Shape& aSx=myImages.Find(aSImage);
361 myMSS.Add(aSx, aSts[i]);
364 myErrorStatus=33;// can not find original shape
370 //=======================================================================
371 //function : MakeArgument1
373 //=======================================================================
374 void GEOMAlgo_FinderShapeOn::MakeArgument1()
378 Standard_Integer i, aNb;
379 TopAbs_ShapeEnum aType;
380 BRepLib_FaceError aFErr;
381 BRepLib_MakeFace aMF;
382 TopTools_IndexedMapOfShape aM;
390 aMF.Init(mySurface, Standard_True, Precision::Confusion());
393 if (aFErr!=BRepLib_FaceDone) {
394 myErrorStatus=20; // can not build the face
398 const TopoDS_Shape& aF=aMF.Shape();
399 aFace=TopoDS::Face(aF);
403 TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
404 TopExp::MapShapes(aF, TopAbs_EDGE, aM);
407 for (i=1; i<=aNb; ++i) {
408 const TopoDS_Shape& aS=aM(i);
409 aType=aS.ShapeType();
411 case TopAbs_VERTEX: {
412 const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
413 aBB.UpdateVertex(aVx, myTolerance);
417 const TopoDS_Edge& aEx=TopoDS::Edge(aS);
418 aBB.UpdateEdge(aEx, myTolerance);
422 const TopoDS_Face& aFx=TopoDS::Face(aS);
423 aBB.UpdateFace(aFx, myTolerance);
432 aBB.MakeFace(aFace, mySurface, myTolerance);
442 //=======================================================================
443 //function : MakeArgument2
445 //=======================================================================
446 void GEOMAlgo_FinderShapeOn::MakeArgument2()
451 TopTools_DataMapOfShapeShape aOriginals;
455 GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
459 //=======================================================================
460 //function : CheckData
462 //=======================================================================
463 void GEOMAlgo_FinderShapeOn::CheckData()
467 if(mySurface.IsNull()) {
468 myErrorStatus=10; // mySurface=NULL
472 if (myShape.IsNull()) {
473 myErrorStatus=11; // myShape=NULL
477 if (!(myShapeType==TopAbs_VERTEX ||
478 myShapeType==TopAbs_EDGE ||
479 myShapeType==TopAbs_FACE ||
480 myShapeType==TopAbs_SOLID)) {
481 myErrorStatus=12; // unallowed sub-shape type
485 if (myState==GEOMAlgo_ST_UNKNOWN ||
486 myState==GEOMAlgo_ST_INOUT) {
487 myErrorStatus=13; // unallowed state type
492 //=======================================================================
493 //function : CopySource
495 //=======================================================================
496 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
497 TopTools_DataMapOfShapeShape& aImages,
498 TopTools_DataMapOfShapeShape& aOriginals,
501 Standard_Boolean bFree;
502 TopAbs_ShapeEnum aType;
507 aType=aE.ShapeType();
509 if (aOriginals.IsBound(aE)) {
510 aEx=aOriginals.ChangeFind(aE);
514 aEx=aE.EmptyCopied();
515 aOriginals.Bind(aE, aEx);
516 aImages.Bind(aEx, aE);
519 aR=(Standard_Integer)aType+1;
520 if (aR>TopAbs_VERTEX) {
525 aEx.Free(Standard_True);
527 aType=(TopAbs_ShapeEnum) aR;
529 aIt.Initialize(aE);//, Standard_False);
530 for (; aIt.More(); aIt.Next()) {
531 const TopoDS_Shape& aV=aIt.Value();
534 CopySource (aV, aImages, aOriginals, aVx);
536 aVx.Orientation(aV.Orientation());
543 //=======================================================================
544 //function : BuildTriangulation
546 //=======================================================================
548 GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
550 // calculate deflection
551 Standard_Real aDeviationCoefficient = 0.001;
554 BRepBndLib::Add(theShape, B);
555 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
556 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
558 Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
559 Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
560 Standard_Real aHLRAngle = 0.349066;
562 // build triangulation
563 BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
565 // check triangulation
566 bool isTriangulation = true;
568 TopExp_Explorer exp (theShape, TopAbs_FACE);
571 TopLoc_Location aTopLoc;
572 Handle(Poly_Triangulation) aTRF;
573 aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
575 isTriangulation = false;
578 else // no faces, try edges
580 TopExp_Explorer expe (theShape, TopAbs_EDGE);
582 isTriangulation = false;
585 TopLoc_Location aLoc;
586 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
588 isTriangulation = false;
593 return isTriangulation;
599 // 10 -mySurface=NULL
601 // 12 -unallowed type of sub-shapes
602 // 13 -unallowed state
603 // 20 -can not build the face
604 // 30 -wrong args are used for DSFiller
605 // 31 -DSFiller failed
606 // 32 -builder ShapeSolid failed
607 // 33 -can not find original shape
611 // 10 - sub-shapes of type myShapeType can not be fond in myShape