1 // Copyright (C) 2007-2010 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
23 // File: GEOMAlgo_FinderShapeOn.cxx
24 // Created: Tue Jan 11 14:44:31 2005
25 // Author: Peter KURNEV
28 #include <GEOMAlgo_FinderShapeOn.ixx>
32 #include <TopAbs_ShapeEnum.hxx>
33 #include <TopAbs_Orientation.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Compound.hxx>
39 #include <TopoDS_Shell.hxx>
40 #include <TopoDS_Solid.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Iterator.hxx>
45 #include <TopTools_ListIteratorOfListOfShape.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <TopTools_DataMapOfShapeShape.hxx>
49 #include <Bnd_Box.hxx>
50 #include <BRepBndLib.hxx>
51 #include <BRepMesh_IncrementalMesh.hxx>
53 #include <BRep_Builder.hxx>
54 #include <BRep_Tool.hxx>
57 #include <TopExp_Explorer.hxx>
59 #include <BRepLib_MakeFace.hxx>
60 #include <BRepLib_FaceError.hxx>
62 #include <BOPTools_DSFiller.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_Tools.hxx>
72 //=======================================================================
73 //function : GEOMAlgo_FinderShapeOn
75 //=======================================================================
76 GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
81 myShapeType=TopAbs_VERTEX;
82 myState=GEOMAlgo_ST_UNKNOWN;
83 myIsAnalytic=Standard_True;
85 //=======================================================================
88 //=======================================================================
89 GEOMAlgo_FinderShapeOn::~GEOMAlgo_FinderShapeOn()
92 //=======================================================================
93 //function : SetSurface
95 //=======================================================================
96 void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
100 //=======================================================================
103 //=======================================================================
104 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
108 //=======================================================================
109 //function : SetShapeType
111 //=======================================================================
112 void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
116 //=======================================================================
117 //function : ShapeType
119 //=======================================================================
120 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
124 //=======================================================================
125 //function : SetState
127 //=======================================================================
128 void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
132 //=======================================================================
135 //=======================================================================
136 GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
140 //=======================================================================
143 //=======================================================================
144 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
146 Standard_Boolean bIsConformState;
147 Standard_Integer i, aNb;
149 TopTools_ListOfShape* pL;
151 pL=(TopTools_ListOfShape*) &myLS;
155 for (i=1; i<=aNb; ++i) {
156 const TopoDS_Shape& aS=myMSS.FindKey(i);
157 aSt=myMSS.FindFromIndex(i);
159 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
160 if (bIsConformState) {
166 //=======================================================================
169 //=======================================================================
170 void GEOMAlgo_FinderShapeOn::Perform()
177 if (!myResult.IsNull()){
186 myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
193 if (myIsAnalytic && myShapeType==TopAbs_VERTEX) {
204 if(myErrorStatus || myWarningStatus) {
209 //=======================================================================
210 //function : FindVertices
212 //=======================================================================
213 void GEOMAlgo_FinderShapeOn::FindVertices()
215 Standard_Integer i, aNb, iErr;
217 TopAbs_Orientation aOr;
219 TopTools_IndexedMapOfShape aM;
221 TopExp::MapShapes(myArg1, TopAbs_FACE, aM);
222 const TopoDS_Face& aF=TopoDS::Face(aM(1));
223 aOr=aF.Orientation();
226 TopExp::MapShapes(myShape, myShapeType, aM);
229 myWarningStatus=10; // No found subshapes of type myShapeType
233 for (i=1; i<=aNb; ++i) {
234 const TopoDS_Shape& aS=aM(i);
235 const TopoDS_Vertex& aV=TopoDS::Vertex(aS);
236 aP=BRep_Tool::Pnt(aV);
237 iErr=GEOMAlgo_SurfaceTools::GetState(aP, mySurface, myTolerance, aSt);
238 if (aOr==TopAbs_REVERSED) {
239 aSt=GEOMAlgo_SurfaceTools::ReverseState(aSt);
244 //=======================================================================
247 //=======================================================================
248 void GEOMAlgo_FinderShapeOn::Find()
250 Standard_Integer i, aNb;
251 Standard_Boolean bICS;
252 TopTools_IndexedMapOfShape aM;
254 TopExp::MapShapes(myArg2, myShapeType, aM);
258 myWarningStatus=10; // No found subshapes of type myShapeType
262 bICS=GEOMAlgo_Tools::IsCompositeShape(myArg2);
263 if (!bICS || myIsAnalytic) {
264 TopoDS_Compound aCmp;
267 aBB.MakeCompound(aCmp);
268 for (i=1; i<=aNb; ++i) {
269 const TopoDS_Shape& aSi=aM(i);
278 for (i=1; i<=aNb; ++i) {
279 const TopoDS_Shape& aS=aM(i);
286 //=======================================================================
289 //=======================================================================
290 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
294 Standard_Boolean bIsDone;
295 Standard_Integer i, iErr;
296 TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
297 TopTools_ListIteratorOfListOfShape aIt;
298 BOPTools_DSFiller aDF;
300 // 1. Prepare DSFiller
301 aDF.SetShapes (myArg1, aS);
302 bIsDone=aDF.IsDone();
304 myErrorStatus=30; // wrong args are used for DSFiller
308 bIsDone=aDF.IsDone();
310 myErrorStatus=31; // DSFiller failed
315 GEOMAlgo_ShapeSolid* pSS;
316 GEOMAlgo_VertexSolid aVXS;
317 GEOMAlgo_WireSolid aWRS;
318 GEOMAlgo_ShellSolid aSHS;
319 GEOMAlgo_SolidSolid aSLS;
323 switch (myShapeType) {
334 aSLS.SetShape2(myArg2);
338 myErrorStatus=12; // unallowed subshape type
344 iErr=pSS->ErrorStatus();
346 myErrorStatus=32; // builder ShapeSolid failed
350 for (i=0; i<3; ++i) {
351 const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
353 for (; aIt.More(); aIt.Next()) {
354 const TopoDS_Shape& aSImage=aIt.Value();
355 if (myImages.IsBound(aSImage)) {
356 const TopoDS_Shape& aSx=myImages.Find(aSImage);
357 myMSS.Add(aSx, aSts[i]);
360 myErrorStatus=33;// can not find original shape
366 //=======================================================================
367 //function : MakeArgument1
369 //=======================================================================
370 void GEOMAlgo_FinderShapeOn::MakeArgument1()
374 Standard_Integer i, aNb;
375 TopAbs_ShapeEnum aType;
376 BRepLib_FaceError aFErr;
377 BRepLib_MakeFace aMF;
378 TopTools_IndexedMapOfShape aM;
386 aMF.Init(mySurface, Standard_True);
388 if (aFErr!=BRepLib_FaceDone) {
389 myErrorStatus=20; // can not build the face
393 const TopoDS_Shape& aF=aMF.Shape();
394 aFace=TopoDS::Face(aF);
398 TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
399 TopExp::MapShapes(aF, TopAbs_EDGE, aM);
401 for (i=1; i<=aNb; ++i) {
402 const TopoDS_Shape& aS=aM(i);
403 aType=aS.ShapeType();
405 case TopAbs_VERTEX: {
406 const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
407 aBB.UpdateVertex(aVx, myTolerance);
411 const TopoDS_Edge& aEx=TopoDS::Edge(aS);
412 aBB.UpdateEdge(aEx, myTolerance);
416 const TopoDS_Face& aFx=TopoDS::Face(aS);
417 aBB.UpdateFace(aFx, myTolerance);
426 aBB.MakeFace(aFace, mySurface, myTolerance);
436 //=======================================================================
437 //function : MakeArgument2
439 //=======================================================================
440 void GEOMAlgo_FinderShapeOn::MakeArgument2()
445 TopTools_DataMapOfShapeShape aOriginals;
449 GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
453 //=======================================================================
454 //function : CheckData
456 //=======================================================================
457 void GEOMAlgo_FinderShapeOn::CheckData()
461 if(mySurface.IsNull()) {
462 myErrorStatus=10; // mySurface=NULL
466 if (myShape.IsNull()) {
467 myErrorStatus=11; // myShape=NULL
471 if (!(myShapeType==TopAbs_VERTEX ||
472 myShapeType==TopAbs_EDGE ||
473 myShapeType==TopAbs_FACE ||
474 myShapeType==TopAbs_SOLID)) {
475 myErrorStatus=12; // unallowed subshape type
479 if (myState==GEOMAlgo_ST_UNKNOWN ||
480 myState==GEOMAlgo_ST_INOUT) {
481 myErrorStatus=13; // unallowed state type
486 //=======================================================================
487 //function : CopySource
489 //=======================================================================
490 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
491 TopTools_DataMapOfShapeShape& aImages,
492 TopTools_DataMapOfShapeShape& aOriginals,
495 Standard_Boolean bFree;
496 TopAbs_ShapeEnum aType;
501 aType=aE.ShapeType();
503 if (aOriginals.IsBound(aE)) {
504 aEx=aOriginals.ChangeFind(aE);
508 aEx=aE.EmptyCopied();
509 aOriginals.Bind(aE, aEx);
510 aImages.Bind(aEx, aE);
513 aR=(Standard_Integer)aType+1;
514 if (aR>TopAbs_VERTEX) {
519 aEx.Free(Standard_True);
521 aType=(TopAbs_ShapeEnum) aR;
523 aIt.Initialize(aE);//, Standard_False);
524 for (; aIt.More(); aIt.Next()) {
525 const TopoDS_Shape& aV=aIt.Value();
528 CopySource (aV, aImages, aOriginals, aVx);
530 aVx.Orientation(aV.Orientation());
537 //=======================================================================
538 //function : BuildTriangulation
540 //=======================================================================
541 bool GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
543 // calculate deflection
544 Standard_Real aDeviationCoefficient = 0.001;
547 BRepBndLib::Add(theShape, B);
548 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
549 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
551 Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
552 Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
553 Standard_Real aHLRAngle = 0.349066;
555 // build triangulation
556 BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
558 // check triangulation
559 bool isTriangulation = true;
561 TopExp_Explorer exp (theShape, TopAbs_FACE);
564 TopLoc_Location aTopLoc;
565 Handle(Poly_Triangulation) aTRF;
566 aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
568 isTriangulation = false;
571 else // no faces, try edges
573 TopExp_Explorer expe (theShape, TopAbs_EDGE);
575 isTriangulation = false;
578 TopLoc_Location aLoc;
579 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
581 isTriangulation = false;
586 return isTriangulation;
592 // 10 -mySurface=NULL
594 // 12 -unallowed type of subshapes
595 // 13 -unallowed state
596 // 20 -can not build the face
597 // 30 -wrong args are used for DSFiller
598 // 31 -DSFiller failed
599 // 32 -builder ShapeSolid failed
600 // 33 -can not find original shape
604 // 10 -subshapes of type myShapeType can not be fond in myShape