1 // Copyright (C) 2007-2011 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.
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
22 // File: GEOMAlgo_FinderShapeOn.cxx
23 // Created: Tue Jan 11 14:44:31 2005
24 // Author: Peter KURNEV
26 #include <GEOMAlgo_FinderShapeOn.hxx>
28 #include <Basics_OCCTVersion.hxx>
30 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
31 #include <Precision.hxx>
36 #include <TopAbs_ShapeEnum.hxx>
37 #include <TopAbs_Orientation.hxx>
40 #include <TopoDS_Face.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <TopoDS_Shell.hxx>
44 #include <TopoDS_Solid.hxx>
45 #include <TopoDS_Vertex.hxx>
46 #include <TopoDS_Edge.hxx>
47 #include <TopoDS_Iterator.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_IndexedMapOfShape.hxx>
51 #include <TopTools_DataMapOfShapeShape.hxx>
53 #include <Bnd_Box.hxx>
54 #include <BRepBndLib.hxx>
55 #include <BRepMesh_IncrementalMesh.hxx>
57 #include <BRep_Builder.hxx>
58 #include <BRep_Tool.hxx>
61 #include <TopExp_Explorer.hxx>
63 #include <BRepLib_MakeFace.hxx>
64 #include <BRepLib_FaceError.hxx>
66 #include <BOPTools_DSFiller.hxx>
68 #include <GEOMAlgo_WireSolid.hxx>
69 #include <GEOMAlgo_ShellSolid.hxx>
70 #include <GEOMAlgo_VertexSolid.hxx>
71 #include <GEOMAlgo_ShapeSolid.hxx>
72 #include <GEOMAlgo_SolidSolid.hxx>
73 #include <GEOMAlgo_SurfaceTools.hxx>
74 #include <GEOMAlgo_Tools.hxx>
76 //=======================================================================
77 //function : GEOMAlgo_FinderShapeOn
79 //=======================================================================
80 GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
85 myShapeType=TopAbs_VERTEX;
86 myState=GEOMAlgo_ST_UNKNOWN;
87 myIsAnalytic=Standard_True;
89 //=======================================================================
92 //=======================================================================
93 GEOMAlgo_FinderShapeOn::~GEOMAlgo_FinderShapeOn()
96 //=======================================================================
97 //function : SetSurface
99 //=======================================================================
100 void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
104 //=======================================================================
107 //=======================================================================
108 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
112 //=======================================================================
113 //function : SetShapeType
115 //=======================================================================
116 void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
120 //=======================================================================
121 //function : ShapeType
123 //=======================================================================
124 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
128 //=======================================================================
129 //function : SetState
131 //=======================================================================
132 void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
136 //=======================================================================
139 //=======================================================================
140 GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
144 //=======================================================================
147 //=======================================================================
148 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
150 Standard_Boolean bIsConformState;
151 Standard_Integer i, aNb;
153 TopTools_ListOfShape* pL;
155 pL=(TopTools_ListOfShape*) &myLS;
159 for (i=1; i<=aNb; ++i) {
160 const TopoDS_Shape& aS=myMSS.FindKey(i);
161 aSt=myMSS.FindFromIndex(i);
163 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
164 if (bIsConformState) {
170 //=======================================================================
173 //=======================================================================
174 void GEOMAlgo_FinderShapeOn::Perform()
181 if (!myResult.IsNull()){
190 // Initialize the context
191 GEOMAlgo_ShapeAlgo::Perform();
193 myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
200 if (myIsAnalytic && myShapeType==TopAbs_VERTEX) {
211 if(myErrorStatus || myWarningStatus) {
216 //=======================================================================
217 //function : FindVertices
219 //=======================================================================
220 void GEOMAlgo_FinderShapeOn::FindVertices()
222 Standard_Integer i, aNb, iErr;
224 TopAbs_Orientation aOr;
226 TopTools_IndexedMapOfShape aM;
228 TopExp::MapShapes(myArg1, TopAbs_FACE, aM);
229 const TopoDS_Face& aF=TopoDS::Face(aM(1));
230 aOr=aF.Orientation();
233 TopExp::MapShapes(myShape, myShapeType, aM);
236 myWarningStatus=10; // No found sub-shapes of type myShapeType
240 for (i=1; i<=aNb; ++i) {
241 const TopoDS_Shape& aS=aM(i);
242 const TopoDS_Vertex& aV=TopoDS::Vertex(aS);
243 aP=BRep_Tool::Pnt(aV);
244 iErr=GEOMAlgo_SurfaceTools::GetState(aP, mySurface, myTolerance, aSt);
245 if (aOr==TopAbs_REVERSED) {
246 aSt=GEOMAlgo_SurfaceTools::ReverseState(aSt);
251 //=======================================================================
254 //=======================================================================
255 void GEOMAlgo_FinderShapeOn::Find()
257 Standard_Integer i, aNb;
258 Standard_Boolean bICS;
259 TopTools_IndexedMapOfShape aM;
261 TopExp::MapShapes(myArg2, myShapeType, aM);
265 myWarningStatus=10; // No found sub-shapes of type myShapeType
269 bICS=GEOMAlgo_Tools::IsCompositeShape(myArg2);
270 if (!bICS || myIsAnalytic) {
271 TopoDS_Compound aCmp;
274 aBB.MakeCompound(aCmp);
275 for (i=1; i<=aNb; ++i) {
276 const TopoDS_Shape& aSi=aM(i);
285 for (i=1; i<=aNb; ++i) {
286 const TopoDS_Shape& aS=aM(i);
293 //=======================================================================
296 //=======================================================================
297 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
301 Standard_Boolean bIsDone;
302 Standard_Integer i, iErr;
303 TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
304 TopTools_ListIteratorOfListOfShape aIt;
305 BOPTools_DSFiller aDF;
307 // 1. Prepare DSFiller
308 aDF.SetShapes (myArg1, aS);
309 bIsDone=aDF.IsDone();
311 myErrorStatus=30; // wrong args are used for DSFiller
315 bIsDone=aDF.IsDone();
317 myErrorStatus=31; // DSFiller failed
322 GEOMAlgo_ShapeSolid* pSS;
323 GEOMAlgo_VertexSolid aVXS;
324 GEOMAlgo_WireSolid aWRS;
325 GEOMAlgo_ShellSolid aSHS;
326 GEOMAlgo_SolidSolid aSLS;
330 switch (myShapeType) {
341 aSLS.SetShape2(myArg2);
345 myErrorStatus=12; // unallowed sub-shape type
351 iErr=pSS->ErrorStatus();
353 myErrorStatus=32; // builder ShapeSolid failed
357 for (i=0; i<3; ++i) {
358 const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
360 for (; aIt.More(); aIt.Next()) {
361 const TopoDS_Shape& aSImage=aIt.Value();
362 if (myImages.IsBound(aSImage)) {
363 const TopoDS_Shape& aSx=myImages.Find(aSImage);
364 myMSS.Add(aSx, aSts[i]);
367 myErrorStatus=33;// can not find original shape
373 //=======================================================================
374 //function : MakeArgument1
376 //=======================================================================
377 void GEOMAlgo_FinderShapeOn::MakeArgument1()
381 Standard_Integer i, aNb;
382 TopAbs_ShapeEnum aType;
383 BRepLib_FaceError aFErr;
384 BRepLib_MakeFace aMF;
385 TopTools_IndexedMapOfShape aM;
393 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
394 aMF.Init(mySurface, Standard_True, Precision::Confusion());
396 aMF.Init(mySurface, Standard_True);
399 if (aFErr!=BRepLib_FaceDone) {
400 myErrorStatus=20; // can not build the face
404 const TopoDS_Shape& aF=aMF.Shape();
405 aFace=TopoDS::Face(aF);
409 TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
410 TopExp::MapShapes(aF, TopAbs_EDGE, aM);
412 for (i=1; i<=aNb; ++i) {
413 const TopoDS_Shape& aS=aM(i);
414 aType=aS.ShapeType();
416 case TopAbs_VERTEX: {
417 const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
418 aBB.UpdateVertex(aVx, myTolerance);
422 const TopoDS_Edge& aEx=TopoDS::Edge(aS);
423 aBB.UpdateEdge(aEx, myTolerance);
427 const TopoDS_Face& aFx=TopoDS::Face(aS);
428 aBB.UpdateFace(aFx, myTolerance);
437 aBB.MakeFace(aFace, mySurface, myTolerance);
447 //=======================================================================
448 //function : MakeArgument2
450 //=======================================================================
451 void GEOMAlgo_FinderShapeOn::MakeArgument2()
456 TopTools_DataMapOfShapeShape aOriginals;
460 GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
464 //=======================================================================
465 //function : CheckData
467 //=======================================================================
468 void GEOMAlgo_FinderShapeOn::CheckData()
472 if(mySurface.IsNull()) {
473 myErrorStatus=10; // mySurface=NULL
477 if (myShape.IsNull()) {
478 myErrorStatus=11; // myShape=NULL
482 if (!(myShapeType==TopAbs_VERTEX ||
483 myShapeType==TopAbs_EDGE ||
484 myShapeType==TopAbs_FACE ||
485 myShapeType==TopAbs_SOLID)) {
486 myErrorStatus=12; // unallowed sub-shape type
490 if (myState==GEOMAlgo_ST_UNKNOWN ||
491 myState==GEOMAlgo_ST_INOUT) {
492 myErrorStatus=13; // unallowed state type
497 //=======================================================================
498 //function : CopySource
500 //=======================================================================
501 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
502 TopTools_DataMapOfShapeShape& aImages,
503 TopTools_DataMapOfShapeShape& aOriginals,
506 Standard_Boolean bFree;
507 TopAbs_ShapeEnum aType;
512 aType=aE.ShapeType();
514 if (aOriginals.IsBound(aE)) {
515 aEx=aOriginals.ChangeFind(aE);
519 aEx=aE.EmptyCopied();
520 aOriginals.Bind(aE, aEx);
521 aImages.Bind(aEx, aE);
524 aR=(Standard_Integer)aType+1;
525 if (aR>TopAbs_VERTEX) {
530 aEx.Free(Standard_True);
532 aType=(TopAbs_ShapeEnum) aR;
534 aIt.Initialize(aE);//, Standard_False);
535 for (; aIt.More(); aIt.Next()) {
536 const TopoDS_Shape& aV=aIt.Value();
539 CopySource (aV, aImages, aOriginals, aVx);
541 aVx.Orientation(aV.Orientation());
548 //=======================================================================
549 //function : BuildTriangulation
551 //=======================================================================
553 GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
555 // calculate deflection
556 Standard_Real aDeviationCoefficient = 0.001;
559 BRepBndLib::Add(theShape, B);
560 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
561 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
563 Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
564 Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
565 Standard_Real aHLRAngle = 0.349066;
567 // build triangulation
568 BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
570 // check triangulation
571 bool isTriangulation = true;
573 TopExp_Explorer exp (theShape, TopAbs_FACE);
576 TopLoc_Location aTopLoc;
577 Handle(Poly_Triangulation) aTRF;
578 aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
580 isTriangulation = false;
583 else // no faces, try edges
585 TopExp_Explorer expe (theShape, TopAbs_EDGE);
587 isTriangulation = false;
590 TopLoc_Location aLoc;
591 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
593 isTriangulation = false;
598 return isTriangulation;
604 // 10 -mySurface=NULL
606 // 12 -unallowed type of sub-shapes
607 // 13 -unallowed state
608 // 20 -can not build the face
609 // 30 -wrong args are used for DSFiller
610 // 31 -DSFiller failed
611 // 32 -builder ShapeSolid failed
612 // 33 -can not find original shape
616 // 10 - sub-shapes of type myShapeType can not be fond in myShape