1 // Copyright (C) 2007-2015 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 #include <Precision.hxx>
35 #include <TopAbs_ShapeEnum.hxx>
36 #include <TopAbs_Orientation.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Compound.hxx>
42 #include <TopoDS_Shell.hxx>
43 #include <TopoDS_Solid.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <TopoDS_Iterator.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <TopTools_DataMapOfShapeShape.hxx>
52 #include <Bnd_Box.hxx>
53 #include <BRepBndLib.hxx>
54 #include <BRepMesh_IncrementalMesh.hxx>
56 #include <BRep_Builder.hxx>
57 #include <BRep_Tool.hxx>
60 #include <TopExp_Explorer.hxx>
62 #include <BRepLib_MakeFace.hxx>
63 #include <BRepLib_FaceError.hxx>
65 //#include <BOPTools_DSFiller.hxx>
67 #include <GEOMAlgo_WireSolid.hxx>
68 #include <GEOMAlgo_ShellSolid.hxx>
69 #include <GEOMAlgo_VertexSolid.hxx>
70 #include <GEOMAlgo_ShapeSolid.hxx>
71 #include <GEOMAlgo_SolidSolid.hxx>
72 #include <GEOMAlgo_SurfaceTools.hxx>
73 #include <GEOMAlgo_AlgoTools.hxx>
75 #include <BOPAlgo_PaveFiller.hxx>
76 #include <BOPCol_ListOfShape.hxx>
78 //=======================================================================
79 //function : GEOMAlgo_FinderShapeOn
81 //=======================================================================
82 GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
87 myShapeType=TopAbs_VERTEX;
88 myState=GEOMAlgo_ST_UNKNOWN;
89 myIsAnalytic=Standard_True;
91 //=======================================================================
94 //=======================================================================
95 GEOMAlgo_FinderShapeOn::~GEOMAlgo_FinderShapeOn()
98 //=======================================================================
99 //function : SetSurface
101 //=======================================================================
102 void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
106 //=======================================================================
109 //=======================================================================
110 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
114 //=======================================================================
115 //function : SetShapeType
117 //=======================================================================
118 void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
122 //=======================================================================
123 //function : ShapeType
125 //=======================================================================
126 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
130 //=======================================================================
131 //function : SetState
133 //=======================================================================
134 void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
138 //=======================================================================
141 //=======================================================================
142 GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
146 //=======================================================================
149 //=======================================================================
150 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
152 Standard_Boolean bIsConformState;
153 Standard_Integer i, aNb;
155 TopTools_ListOfShape* pL;
157 pL=(TopTools_ListOfShape*) &myLS;
161 for (i=1; i<=aNb; ++i) {
162 const TopoDS_Shape& aS=myMSS.FindKey(i);
163 aSt=myMSS.FindFromIndex(i);
165 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
166 if (bIsConformState) {
172 //=======================================================================
175 //=======================================================================
176 void GEOMAlgo_FinderShapeOn::Perform()
183 if (!myResult.IsNull()){
192 // Initialize the context
193 GEOMAlgo_ShapeAlgo::Perform();
195 myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
202 if (myIsAnalytic && myShapeType==TopAbs_VERTEX) {
213 if(myErrorStatus || myWarningStatus) {
218 //=======================================================================
219 //function : FindVertices
221 //=======================================================================
222 void GEOMAlgo_FinderShapeOn::FindVertices()
224 Standard_Integer i, aNb, iErr;
226 TopAbs_Orientation aOr;
228 TopTools_IndexedMapOfShape aM;
230 TopExp::MapShapes(myArg1, TopAbs_FACE, aM);
231 const TopoDS_Face& aF=TopoDS::Face(aM(1));
232 aOr=aF.Orientation();
235 TopExp::MapShapes(myShape, myShapeType, aM);
238 myWarningStatus=10; // No found sub-shapes of type myShapeType
242 for (i=1; i<=aNb; ++i) {
243 const TopoDS_Shape& aS=aM(i);
244 const TopoDS_Vertex& aV=TopoDS::Vertex(aS);
245 aP=BRep_Tool::Pnt(aV);
246 iErr=GEOMAlgo_SurfaceTools::GetState(aP, mySurface, myTolerance, aSt);
247 if (aOr==TopAbs_REVERSED) {
248 aSt=GEOMAlgo_SurfaceTools::ReverseState(aSt);
253 //=======================================================================
256 //=======================================================================
257 void GEOMAlgo_FinderShapeOn::Find()
259 Standard_Integer i, aNb;
260 Standard_Boolean bICS;
261 TopTools_IndexedMapOfShape aM;
263 TopExp::MapShapes(myArg2, myShapeType, aM);
267 myWarningStatus=10; // No found sub-shapes of type myShapeType
271 bICS=GEOMAlgo_AlgoTools::IsCompositeShape(myArg2);
272 if (!bICS || myIsAnalytic) {
273 TopoDS_Compound aCmp;
276 aBB.MakeCompound(aCmp);
277 for (i=1; i<=aNb; ++i) {
278 const TopoDS_Shape& aSi=aM(i);
287 for (i=1; i<=aNb; ++i) {
288 const TopoDS_Shape& aS=aM(i);
295 //=======================================================================
298 //=======================================================================
299 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
303 Standard_Boolean bIsDone;
304 Standard_Integer i, iErr;
305 TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
306 TopTools_ListIteratorOfListOfShape aIt;
307 BOPCol_ListOfShape aLS;
308 BOPAlgo_PaveFiller aPF;
310 // 1. Prepare DSFiller
313 aPF.SetArguments(aLS);
316 iErr=aPF.ErrorStatus();
318 myErrorStatus=31; // PaveFiller is failed
323 GEOMAlgo_ShapeSolid* pSS;
324 GEOMAlgo_VertexSolid aVXS;
325 GEOMAlgo_WireSolid aWRS;
326 GEOMAlgo_ShellSolid aSHS;
327 GEOMAlgo_SolidSolid aSLS;
331 switch (myShapeType) {
342 aSLS.SetShape2(myArg2);
346 myErrorStatus=12; // unallowed sub-shape type
352 iErr=pSS->ErrorStatus();
354 myErrorStatus=32; // builder ShapeSolid failed
358 for (i=0; i<3; ++i) {
359 const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
361 for (; aIt.More(); aIt.Next()) {
362 const TopoDS_Shape& aSImage=aIt.Value();
363 if (myImages.IsBound(aSImage)) {
364 const TopoDS_Shape& aSx=myImages.Find(aSImage);
365 myMSS.Add(aSx, aSts[i]);
368 myErrorStatus=33;// can not find original shape
374 //=======================================================================
375 //function : MakeArgument1
377 //=======================================================================
378 void GEOMAlgo_FinderShapeOn::MakeArgument1()
382 Standard_Integer i, aNb;
383 TopAbs_ShapeEnum aType;
384 BRepLib_FaceError aFErr;
385 BRepLib_MakeFace aMF;
386 TopTools_IndexedMapOfShape aM;
394 aMF.Init(mySurface, Standard_True, Precision::Confusion());
397 if (aFErr!=BRepLib_FaceDone) {
398 myErrorStatus=20; // can not build the face
402 const TopoDS_Shape& aF=aMF.Shape();
403 aFace=TopoDS::Face(aF);
407 TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
408 TopExp::MapShapes(aF, TopAbs_EDGE, aM);
411 for (i=1; i<=aNb; ++i) {
412 const TopoDS_Shape& aS=aM(i);
413 aType=aS.ShapeType();
415 case TopAbs_VERTEX: {
416 const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
417 aBB.UpdateVertex(aVx, myTolerance);
421 const TopoDS_Edge& aEx=TopoDS::Edge(aS);
422 aBB.UpdateEdge(aEx, myTolerance);
426 const TopoDS_Face& aFx=TopoDS::Face(aS);
427 aBB.UpdateFace(aFx, myTolerance);
436 aBB.MakeFace(aFace, mySurface, myTolerance);
446 //=======================================================================
447 //function : MakeArgument2
449 //=======================================================================
450 void GEOMAlgo_FinderShapeOn::MakeArgument2()
455 TopTools_DataMapOfShapeShape aOriginals;
459 GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
463 //=======================================================================
464 //function : CheckData
466 //=======================================================================
467 void GEOMAlgo_FinderShapeOn::CheckData()
471 if(mySurface.IsNull()) {
472 myErrorStatus=10; // mySurface=NULL
476 if (myShape.IsNull()) {
477 myErrorStatus=11; // myShape=NULL
481 if (!(myShapeType==TopAbs_VERTEX ||
482 myShapeType==TopAbs_EDGE ||
483 myShapeType==TopAbs_FACE ||
484 myShapeType==TopAbs_SOLID)) {
485 myErrorStatus=12; // unallowed sub-shape type
489 if (myState==GEOMAlgo_ST_UNKNOWN ||
490 myState==GEOMAlgo_ST_INOUT) {
491 myErrorStatus=13; // unallowed state type
496 //=======================================================================
497 //function : CopySource
499 //=======================================================================
500 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
501 TopTools_DataMapOfShapeShape& aImages,
502 TopTools_DataMapOfShapeShape& aOriginals,
505 Standard_Boolean bFree;
506 TopAbs_ShapeEnum aType;
511 aType=aE.ShapeType();
513 if (aOriginals.IsBound(aE)) {
514 aEx=aOriginals.ChangeFind(aE);
518 aEx=aE.EmptyCopied();
519 aOriginals.Bind(aE, aEx);
520 aImages.Bind(aEx, aE);
523 aR=(Standard_Integer)aType+1;
524 if (aR>TopAbs_VERTEX) {
529 aEx.Free(Standard_True);
531 aType=(TopAbs_ShapeEnum) aR;
533 aIt.Initialize(aE);//, Standard_False);
534 for (; aIt.More(); aIt.Next()) {
535 const TopoDS_Shape& aV=aIt.Value();
538 CopySource (aV, aImages, aOriginals, aVx);
540 aVx.Orientation(aV.Orientation());
547 //=======================================================================
548 //function : BuildTriangulation
550 //=======================================================================
552 GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
554 // calculate deflection
555 Standard_Real aDeviationCoefficient = 0.001;
558 BRepBndLib::Add(theShape, B);
559 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
560 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
562 Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
563 Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
564 Standard_Real aHLRAngle = 0.349066;
566 // build triangulation
567 BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
569 // check triangulation
570 bool isTriangulation = true;
572 TopExp_Explorer exp (theShape, TopAbs_FACE);
575 TopLoc_Location aTopLoc;
576 Handle(Poly_Triangulation) aTRF;
577 aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
579 isTriangulation = false;
582 else // no faces, try edges
584 TopExp_Explorer expe (theShape, TopAbs_EDGE);
586 isTriangulation = false;
589 TopLoc_Location aLoc;
590 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
592 isTriangulation = false;
597 return isTriangulation;
603 // 10 -mySurface=NULL
605 // 12 -unallowed type of sub-shapes
606 // 13 -unallowed state
607 // 20 -can not build the face
608 // 30 -wrong args are used for DSFiller
609 // 31 -DSFiller failed
610 // 32 -builder ShapeSolid failed
611 // 33 -can not find original shape
615 // 10 - sub-shapes of type myShapeType can not be fond in myShape