1 // Copyright (C) 2007-2014 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 <Basics_OCCTVersion.hxx>
31 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
32 #include <Precision.hxx>
37 #include <TopAbs_ShapeEnum.hxx>
38 #include <TopAbs_Orientation.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS_Compound.hxx>
44 #include <TopoDS_Shell.hxx>
45 #include <TopoDS_Solid.hxx>
46 #include <TopoDS_Vertex.hxx>
47 #include <TopoDS_Edge.hxx>
48 #include <TopoDS_Iterator.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <TopTools_DataMapOfShapeShape.hxx>
54 #include <Bnd_Box.hxx>
55 #include <BRepBndLib.hxx>
56 #include <BRepMesh_IncrementalMesh.hxx>
58 #include <BRep_Builder.hxx>
59 #include <BRep_Tool.hxx>
62 #include <TopExp_Explorer.hxx>
64 #include <BRepLib_MakeFace.hxx>
65 #include <BRepLib_FaceError.hxx>
67 //#include <BOPTools_DSFiller.hxx>
69 #include <GEOMAlgo_WireSolid.hxx>
70 #include <GEOMAlgo_ShellSolid.hxx>
71 #include <GEOMAlgo_VertexSolid.hxx>
72 #include <GEOMAlgo_ShapeSolid.hxx>
73 #include <GEOMAlgo_SolidSolid.hxx>
74 #include <GEOMAlgo_SurfaceTools.hxx>
75 #include <GEOMAlgo_AlgoTools.hxx>
77 #include <BOPAlgo_PaveFiller.hxx>
78 #include <BOPCol_ListOfShape.hxx>
80 //=======================================================================
81 //function : GEOMAlgo_FinderShapeOn
83 //=======================================================================
84 GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
89 myShapeType=TopAbs_VERTEX;
90 myState=GEOMAlgo_ST_UNKNOWN;
91 myIsAnalytic=Standard_True;
93 //=======================================================================
96 //=======================================================================
97 GEOMAlgo_FinderShapeOn::~GEOMAlgo_FinderShapeOn()
100 //=======================================================================
101 //function : SetSurface
103 //=======================================================================
104 void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
108 //=======================================================================
111 //=======================================================================
112 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
116 //=======================================================================
117 //function : SetShapeType
119 //=======================================================================
120 void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
124 //=======================================================================
125 //function : ShapeType
127 //=======================================================================
128 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
132 //=======================================================================
133 //function : SetState
135 //=======================================================================
136 void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
140 //=======================================================================
143 //=======================================================================
144 GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
148 //=======================================================================
151 //=======================================================================
152 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
154 Standard_Boolean bIsConformState;
155 Standard_Integer i, aNb;
157 TopTools_ListOfShape* pL;
159 pL=(TopTools_ListOfShape*) &myLS;
163 for (i=1; i<=aNb; ++i) {
164 const TopoDS_Shape& aS=myMSS.FindKey(i);
165 aSt=myMSS.FindFromIndex(i);
167 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
168 if (bIsConformState) {
174 //=======================================================================
177 //=======================================================================
178 void GEOMAlgo_FinderShapeOn::Perform()
185 if (!myResult.IsNull()){
194 // Initialize the context
195 GEOMAlgo_ShapeAlgo::Perform();
197 myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
204 if (myIsAnalytic && myShapeType==TopAbs_VERTEX) {
215 if(myErrorStatus || myWarningStatus) {
220 //=======================================================================
221 //function : FindVertices
223 //=======================================================================
224 void GEOMAlgo_FinderShapeOn::FindVertices()
226 Standard_Integer i, aNb, iErr;
228 TopAbs_Orientation aOr;
230 TopTools_IndexedMapOfShape aM;
232 TopExp::MapShapes(myArg1, TopAbs_FACE, aM);
233 const TopoDS_Face& aF=TopoDS::Face(aM(1));
234 aOr=aF.Orientation();
237 TopExp::MapShapes(myShape, myShapeType, aM);
240 myWarningStatus=10; // No found sub-shapes of type myShapeType
244 for (i=1; i<=aNb; ++i) {
245 const TopoDS_Shape& aS=aM(i);
246 const TopoDS_Vertex& aV=TopoDS::Vertex(aS);
247 aP=BRep_Tool::Pnt(aV);
248 iErr=GEOMAlgo_SurfaceTools::GetState(aP, mySurface, myTolerance, aSt);
249 if (aOr==TopAbs_REVERSED) {
250 aSt=GEOMAlgo_SurfaceTools::ReverseState(aSt);
255 //=======================================================================
258 //=======================================================================
259 void GEOMAlgo_FinderShapeOn::Find()
261 Standard_Integer i, aNb;
262 Standard_Boolean bICS;
263 TopTools_IndexedMapOfShape aM;
265 TopExp::MapShapes(myArg2, myShapeType, aM);
269 myWarningStatus=10; // No found sub-shapes of type myShapeType
273 bICS=GEOMAlgo_AlgoTools::IsCompositeShape(myArg2);
274 if (!bICS || myIsAnalytic) {
275 TopoDS_Compound aCmp;
278 aBB.MakeCompound(aCmp);
279 for (i=1; i<=aNb; ++i) {
280 const TopoDS_Shape& aSi=aM(i);
289 for (i=1; i<=aNb; ++i) {
290 const TopoDS_Shape& aS=aM(i);
297 //=======================================================================
300 //=======================================================================
301 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
305 Standard_Boolean bIsDone;
306 Standard_Integer i, iErr;
307 TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
308 TopTools_ListIteratorOfListOfShape aIt;
309 BOPCol_ListOfShape aLS;
310 BOPAlgo_PaveFiller aPF;
312 // 1. Prepare DSFiller
315 aPF.SetArguments(aLS);
318 iErr=aPF.ErrorStatus();
320 myErrorStatus=31; // PaveFiller is failed
325 GEOMAlgo_ShapeSolid* pSS;
326 GEOMAlgo_VertexSolid aVXS;
327 GEOMAlgo_WireSolid aWRS;
328 GEOMAlgo_ShellSolid aSHS;
329 GEOMAlgo_SolidSolid aSLS;
333 switch (myShapeType) {
344 aSLS.SetShape2(myArg2);
348 myErrorStatus=12; // unallowed sub-shape type
354 iErr=pSS->ErrorStatus();
356 myErrorStatus=32; // builder ShapeSolid failed
360 for (i=0; i<3; ++i) {
361 const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
363 for (; aIt.More(); aIt.Next()) {
364 const TopoDS_Shape& aSImage=aIt.Value();
365 if (myImages.IsBound(aSImage)) {
366 const TopoDS_Shape& aSx=myImages.Find(aSImage);
367 myMSS.Add(aSx, aSts[i]);
370 myErrorStatus=33;// can not find original shape
376 //=======================================================================
377 //function : MakeArgument1
379 //=======================================================================
380 void GEOMAlgo_FinderShapeOn::MakeArgument1()
384 Standard_Integer i, aNb;
385 TopAbs_ShapeEnum aType;
386 BRepLib_FaceError aFErr;
387 BRepLib_MakeFace aMF;
388 TopTools_IndexedMapOfShape aM;
396 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
397 aMF.Init(mySurface, Standard_True, Precision::Confusion());
399 aMF.Init(mySurface, Standard_True);
403 if (aFErr!=BRepLib_FaceDone) {
404 myErrorStatus=20; // can not build the face
408 const TopoDS_Shape& aF=aMF.Shape();
409 aFace=TopoDS::Face(aF);
413 TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
414 TopExp::MapShapes(aF, TopAbs_EDGE, aM);
417 for (i=1; i<=aNb; ++i) {
418 const TopoDS_Shape& aS=aM(i);
419 aType=aS.ShapeType();
421 case TopAbs_VERTEX: {
422 const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
423 aBB.UpdateVertex(aVx, myTolerance);
427 const TopoDS_Edge& aEx=TopoDS::Edge(aS);
428 aBB.UpdateEdge(aEx, myTolerance);
432 const TopoDS_Face& aFx=TopoDS::Face(aS);
433 aBB.UpdateFace(aFx, myTolerance);
442 aBB.MakeFace(aFace, mySurface, myTolerance);
452 //=======================================================================
453 //function : MakeArgument2
455 //=======================================================================
456 void GEOMAlgo_FinderShapeOn::MakeArgument2()
461 TopTools_DataMapOfShapeShape aOriginals;
465 GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
469 //=======================================================================
470 //function : CheckData
472 //=======================================================================
473 void GEOMAlgo_FinderShapeOn::CheckData()
477 if(mySurface.IsNull()) {
478 myErrorStatus=10; // mySurface=NULL
482 if (myShape.IsNull()) {
483 myErrorStatus=11; // myShape=NULL
487 if (!(myShapeType==TopAbs_VERTEX ||
488 myShapeType==TopAbs_EDGE ||
489 myShapeType==TopAbs_FACE ||
490 myShapeType==TopAbs_SOLID)) {
491 myErrorStatus=12; // unallowed sub-shape type
495 if (myState==GEOMAlgo_ST_UNKNOWN ||
496 myState==GEOMAlgo_ST_INOUT) {
497 myErrorStatus=13; // unallowed state type
502 //=======================================================================
503 //function : CopySource
505 //=======================================================================
506 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
507 TopTools_DataMapOfShapeShape& aImages,
508 TopTools_DataMapOfShapeShape& aOriginals,
511 Standard_Boolean bFree;
512 TopAbs_ShapeEnum aType;
517 aType=aE.ShapeType();
519 if (aOriginals.IsBound(aE)) {
520 aEx=aOriginals.ChangeFind(aE);
524 aEx=aE.EmptyCopied();
525 aOriginals.Bind(aE, aEx);
526 aImages.Bind(aEx, aE);
529 aR=(Standard_Integer)aType+1;
530 if (aR>TopAbs_VERTEX) {
535 aEx.Free(Standard_True);
537 aType=(TopAbs_ShapeEnum) aR;
539 aIt.Initialize(aE);//, Standard_False);
540 for (; aIt.More(); aIt.Next()) {
541 const TopoDS_Shape& aV=aIt.Value();
544 CopySource (aV, aImages, aOriginals, aVx);
546 aVx.Orientation(aV.Orientation());
553 //=======================================================================
554 //function : BuildTriangulation
556 //=======================================================================
558 GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
560 // calculate deflection
561 Standard_Real aDeviationCoefficient = 0.001;
564 BRepBndLib::Add(theShape, B);
565 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
566 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
568 Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
569 Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
570 Standard_Real aHLRAngle = 0.349066;
572 // build triangulation
573 BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
575 // check triangulation
576 bool isTriangulation = true;
578 TopExp_Explorer exp (theShape, TopAbs_FACE);
581 TopLoc_Location aTopLoc;
582 Handle(Poly_Triangulation) aTRF;
583 aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
585 isTriangulation = false;
588 else // no faces, try edges
590 TopExp_Explorer expe (theShape, TopAbs_EDGE);
592 isTriangulation = false;
595 TopLoc_Location aLoc;
596 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
598 isTriangulation = false;
603 return isTriangulation;
609 // 10 -mySurface=NULL
611 // 12 -unallowed type of sub-shapes
612 // 13 -unallowed state
613 // 20 -can not build the face
614 // 30 -wrong args are used for DSFiller
615 // 31 -DSFiller failed
616 // 32 -builder ShapeSolid failed
617 // 33 -can not find original shape
621 // 10 - sub-shapes of type myShapeType can not be fond in myShape