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.ixx>
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 myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
197 if (myIsAnalytic && myShapeType==TopAbs_VERTEX) {
208 if(myErrorStatus || myWarningStatus) {
213 //=======================================================================
214 //function : FindVertices
216 //=======================================================================
217 void GEOMAlgo_FinderShapeOn::FindVertices()
219 Standard_Integer i, aNb, iErr;
221 TopAbs_Orientation aOr;
223 TopTools_IndexedMapOfShape aM;
225 TopExp::MapShapes(myArg1, TopAbs_FACE, aM);
226 const TopoDS_Face& aF=TopoDS::Face(aM(1));
227 aOr=aF.Orientation();
230 TopExp::MapShapes(myShape, myShapeType, aM);
233 myWarningStatus=10; // No found sub-shapes of type myShapeType
237 for (i=1; i<=aNb; ++i) {
238 const TopoDS_Shape& aS=aM(i);
239 const TopoDS_Vertex& aV=TopoDS::Vertex(aS);
240 aP=BRep_Tool::Pnt(aV);
241 iErr=GEOMAlgo_SurfaceTools::GetState(aP, mySurface, myTolerance, aSt);
242 if (aOr==TopAbs_REVERSED) {
243 aSt=GEOMAlgo_SurfaceTools::ReverseState(aSt);
248 //=======================================================================
251 //=======================================================================
252 void GEOMAlgo_FinderShapeOn::Find()
254 Standard_Integer i, aNb;
255 Standard_Boolean bICS;
256 TopTools_IndexedMapOfShape aM;
258 TopExp::MapShapes(myArg2, myShapeType, aM);
262 myWarningStatus=10; // No found sub-shapes of type myShapeType
266 bICS=GEOMAlgo_Tools::IsCompositeShape(myArg2);
267 if (!bICS || myIsAnalytic) {
268 TopoDS_Compound aCmp;
271 aBB.MakeCompound(aCmp);
272 for (i=1; i<=aNb; ++i) {
273 const TopoDS_Shape& aSi=aM(i);
282 for (i=1; i<=aNb; ++i) {
283 const TopoDS_Shape& aS=aM(i);
290 //=======================================================================
293 //=======================================================================
294 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
298 Standard_Boolean bIsDone;
299 Standard_Integer i, iErr;
300 TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
301 TopTools_ListIteratorOfListOfShape aIt;
302 BOPTools_DSFiller aDF;
304 // 1. Prepare DSFiller
305 aDF.SetShapes (myArg1, aS);
306 bIsDone=aDF.IsDone();
308 myErrorStatus=30; // wrong args are used for DSFiller
312 bIsDone=aDF.IsDone();
314 myErrorStatus=31; // DSFiller 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 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
391 aMF.Init(mySurface, Standard_True, Precision::Confusion());
393 aMF.Init(mySurface, Standard_True);
396 if (aFErr!=BRepLib_FaceDone) {
397 myErrorStatus=20; // can not build the face
401 const TopoDS_Shape& aF=aMF.Shape();
402 aFace=TopoDS::Face(aF);
406 TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
407 TopExp::MapShapes(aF, TopAbs_EDGE, aM);
409 for (i=1; i<=aNb; ++i) {
410 const TopoDS_Shape& aS=aM(i);
411 aType=aS.ShapeType();
413 case TopAbs_VERTEX: {
414 const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
415 aBB.UpdateVertex(aVx, myTolerance);
419 const TopoDS_Edge& aEx=TopoDS::Edge(aS);
420 aBB.UpdateEdge(aEx, myTolerance);
424 const TopoDS_Face& aFx=TopoDS::Face(aS);
425 aBB.UpdateFace(aFx, myTolerance);
434 aBB.MakeFace(aFace, mySurface, myTolerance);
444 //=======================================================================
445 //function : MakeArgument2
447 //=======================================================================
448 void GEOMAlgo_FinderShapeOn::MakeArgument2()
453 TopTools_DataMapOfShapeShape aOriginals;
457 GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
461 //=======================================================================
462 //function : CheckData
464 //=======================================================================
465 void GEOMAlgo_FinderShapeOn::CheckData()
469 if(mySurface.IsNull()) {
470 myErrorStatus=10; // mySurface=NULL
474 if (myShape.IsNull()) {
475 myErrorStatus=11; // myShape=NULL
479 if (!(myShapeType==TopAbs_VERTEX ||
480 myShapeType==TopAbs_EDGE ||
481 myShapeType==TopAbs_FACE ||
482 myShapeType==TopAbs_SOLID)) {
483 myErrorStatus=12; // unallowed sub-shape type
487 if (myState==GEOMAlgo_ST_UNKNOWN ||
488 myState==GEOMAlgo_ST_INOUT) {
489 myErrorStatus=13; // unallowed state type
494 //=======================================================================
495 //function : CopySource
497 //=======================================================================
498 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
499 TopTools_DataMapOfShapeShape& aImages,
500 TopTools_DataMapOfShapeShape& aOriginals,
503 Standard_Boolean bFree;
504 TopAbs_ShapeEnum aType;
509 aType=aE.ShapeType();
511 if (aOriginals.IsBound(aE)) {
512 aEx=aOriginals.ChangeFind(aE);
516 aEx=aE.EmptyCopied();
517 aOriginals.Bind(aE, aEx);
518 aImages.Bind(aEx, aE);
521 aR=(Standard_Integer)aType+1;
522 if (aR>TopAbs_VERTEX) {
527 aEx.Free(Standard_True);
529 aType=(TopAbs_ShapeEnum) aR;
531 aIt.Initialize(aE);//, Standard_False);
532 for (; aIt.More(); aIt.Next()) {
533 const TopoDS_Shape& aV=aIt.Value();
536 CopySource (aV, aImages, aOriginals, aVx);
538 aVx.Orientation(aV.Orientation());
545 //=======================================================================
546 //function : BuildTriangulation
548 //=======================================================================
549 bool GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
551 // calculate deflection
552 Standard_Real aDeviationCoefficient = 0.001;
555 BRepBndLib::Add(theShape, B);
556 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
557 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
559 Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
560 Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
561 Standard_Real aHLRAngle = 0.349066;
563 // build triangulation
564 BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
566 // check triangulation
567 bool isTriangulation = true;
569 TopExp_Explorer exp (theShape, TopAbs_FACE);
572 TopLoc_Location aTopLoc;
573 Handle(Poly_Triangulation) aTRF;
574 aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
576 isTriangulation = false;
579 else // no faces, try edges
581 TopExp_Explorer expe (theShape, TopAbs_EDGE);
583 isTriangulation = false;
586 TopLoc_Location aLoc;
587 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
589 isTriangulation = false;
594 return isTriangulation;
600 // 10 -mySurface=NULL
602 // 12 -unallowed type of sub-shapes
603 // 13 -unallowed state
604 // 20 -can not build the face
605 // 30 -wrong args are used for DSFiller
606 // 31 -DSFiller failed
607 // 32 -builder ShapeSolid failed
608 // 33 -can not find original shape
612 // 10 - sub-shapes of type myShapeType can not be fond in myShape