1 // Copyright (C) 2007-2008 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
27 #include <GEOMAlgo_FinderShapeOn.ixx>
31 #include <TopAbs_ShapeEnum.hxx>
32 #include <TopAbs_Orientation.hxx>
35 #include <TopoDS_Face.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Compound.hxx>
38 #include <TopoDS_Shell.hxx>
39 #include <TopoDS_Solid.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS_Iterator.hxx>
44 #include <TopTools_ListIteratorOfListOfShape.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
46 #include <TopTools_DataMapOfShapeShape.hxx>
48 #include <Bnd_Box.hxx>
49 #include <BRepBndLib.hxx>
50 #include <BRepMesh_IncrementalMesh.hxx>
52 #include <BRep_Builder.hxx>
53 #include <BRep_Tool.hxx>
56 #include <TopExp_Explorer.hxx>
58 #include <BRepLib_MakeFace.hxx>
59 #include <BRepLib_FaceError.hxx>
61 #include <BOPTools_DSFiller.hxx>
63 #include <GEOMAlgo_WireSolid.hxx>
64 #include <GEOMAlgo_ShellSolid.hxx>
65 #include <GEOMAlgo_VertexSolid.hxx>
66 #include <GEOMAlgo_ShapeSolid.hxx>
67 #include <GEOMAlgo_SolidSolid.hxx>
68 #include <GEOMAlgo_SurfaceTools.hxx>
69 #include <GEOMAlgo_Tools.hxx>
71 //=======================================================================
72 //function : GEOMAlgo_FinderShapeOn
74 //=======================================================================
75 GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
80 myShapeType=TopAbs_VERTEX;
81 myState=GEOMAlgo_ST_UNKNOWN;
82 myIsAnalytic=Standard_True;
84 //=======================================================================
87 //=======================================================================
88 GEOMAlgo_FinderShapeOn::~GEOMAlgo_FinderShapeOn()
91 //=======================================================================
92 //function : SetSurface
94 //=======================================================================
95 void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
99 //=======================================================================
102 //=======================================================================
103 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
107 //=======================================================================
108 //function : SetShapeType
110 //=======================================================================
111 void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
115 //=======================================================================
116 //function : ShapeType
118 //=======================================================================
119 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
123 //=======================================================================
124 //function : SetState
126 //=======================================================================
127 void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
131 //=======================================================================
134 //=======================================================================
135 GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
139 //=======================================================================
142 //=======================================================================
143 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
145 Standard_Boolean bIsConformState;
146 Standard_Integer i, aNb;
148 TopTools_ListOfShape* pL;
150 pL=(TopTools_ListOfShape*) &myLS;
154 for (i=1; i<=aNb; ++i) {
155 const TopoDS_Shape& aS=myMSS.FindKey(i);
156 aSt=myMSS.FindFromIndex(i);
158 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
159 if (bIsConformState) {
165 //=======================================================================
168 //=======================================================================
169 void GEOMAlgo_FinderShapeOn::Perform()
176 if (!myResult.IsNull()){
185 myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
192 if (myIsAnalytic && myShapeType==TopAbs_VERTEX) {
203 if(myErrorStatus || myWarningStatus) {
208 //=======================================================================
209 //function : FindVertices
211 //=======================================================================
212 void GEOMAlgo_FinderShapeOn::FindVertices()
214 Standard_Integer i, aNb, iErr;
216 TopAbs_Orientation aOr;
218 TopTools_IndexedMapOfShape aM;
220 TopExp::MapShapes(myArg1, TopAbs_FACE, aM);
221 const TopoDS_Face& aF=TopoDS::Face(aM(1));
222 aOr=aF.Orientation();
225 TopExp::MapShapes(myShape, myShapeType, aM);
228 myWarningStatus=10; // No found subshapes of type myShapeType
232 for (i=1; i<=aNb; ++i) {
233 const TopoDS_Shape& aS=aM(i);
234 const TopoDS_Vertex& aV=TopoDS::Vertex(aS);
235 aP=BRep_Tool::Pnt(aV);
236 iErr=GEOMAlgo_SurfaceTools::GetState(aP, mySurface, myTolerance, aSt);
237 if (aOr==TopAbs_REVERSED) {
238 aSt=GEOMAlgo_SurfaceTools::ReverseState(aSt);
243 //=======================================================================
246 //=======================================================================
247 void GEOMAlgo_FinderShapeOn::Find()
249 Standard_Integer i, aNb;
250 Standard_Boolean bICS;
251 TopTools_IndexedMapOfShape aM;
253 TopExp::MapShapes(myArg2, myShapeType, aM);
257 myWarningStatus=10; // No found subshapes of type myShapeType
261 bICS=GEOMAlgo_Tools::IsCompositeShape(myArg2);
262 if (!bICS || myIsAnalytic) {
263 TopoDS_Compound aCmp;
266 aBB.MakeCompound(aCmp);
267 for (i=1; i<=aNb; ++i) {
268 const TopoDS_Shape& aSi=aM(i);
277 for (i=1; i<=aNb; ++i) {
278 const TopoDS_Shape& aS=aM(i);
285 //=======================================================================
288 //=======================================================================
289 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
293 Standard_Boolean bIsDone;
294 Standard_Integer i, iErr;
295 TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
296 TopTools_ListIteratorOfListOfShape aIt;
297 BOPTools_DSFiller aDF;
299 // 1. Prepare DSFiller
300 aDF.SetShapes (myArg1, aS);
301 bIsDone=aDF.IsDone();
303 myErrorStatus=30; // wrong args are used for DSFiller
307 bIsDone=aDF.IsDone();
309 myErrorStatus=31; // DSFiller failed
314 GEOMAlgo_ShapeSolid* pSS;
315 GEOMAlgo_VertexSolid aVXS;
316 GEOMAlgo_WireSolid aWRS;
317 GEOMAlgo_ShellSolid aSHS;
318 GEOMAlgo_SolidSolid aSLS;
322 switch (myShapeType) {
333 aSLS.SetShape2(myArg2);
337 myErrorStatus=12; // unallowed subshape type
343 iErr=pSS->ErrorStatus();
345 myErrorStatus=32; // builder ShapeSolid failed
349 for (i=0; i<3; ++i) {
350 const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
352 for (; aIt.More(); aIt.Next()) {
353 const TopoDS_Shape& aSImage=aIt.Value();
354 if (myImages.IsBound(aSImage)) {
355 const TopoDS_Shape& aSx=myImages.Find(aSImage);
356 myMSS.Add(aSx, aSts[i]);
359 myErrorStatus=33;// can not find original shape
365 //=======================================================================
366 //function : MakeArgument1
368 //=======================================================================
369 void GEOMAlgo_FinderShapeOn::MakeArgument1()
373 Standard_Integer i, aNb;
374 TopAbs_ShapeEnum aType;
375 BRepLib_FaceError aFErr;
376 BRepLib_MakeFace aMF;
377 TopTools_IndexedMapOfShape aM;
385 aMF.Init(mySurface, Standard_True);
387 if (aFErr!=BRepLib_FaceDone) {
388 myErrorStatus=20; // can not build the face
392 const TopoDS_Shape& aF=aMF.Shape();
393 aFace=TopoDS::Face(aF);
397 TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
398 TopExp::MapShapes(aF, TopAbs_EDGE, aM);
400 for (i=1; i<=aNb; ++i) {
401 const TopoDS_Shape& aS=aM(i);
402 aType=aS.ShapeType();
404 case TopAbs_VERTEX: {
405 const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
406 aBB.UpdateVertex(aVx, myTolerance);
410 const TopoDS_Edge& aEx=TopoDS::Edge(aS);
411 aBB.UpdateEdge(aEx, myTolerance);
415 const TopoDS_Face& aFx=TopoDS::Face(aS);
416 aBB.UpdateFace(aFx, myTolerance);
425 aBB.MakeFace(aFace, mySurface, myTolerance);
435 //=======================================================================
436 //function : MakeArgument2
438 //=======================================================================
439 void GEOMAlgo_FinderShapeOn::MakeArgument2()
444 TopTools_DataMapOfShapeShape aOriginals;
448 GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
452 //=======================================================================
453 //function : CheckData
455 //=======================================================================
456 void GEOMAlgo_FinderShapeOn::CheckData()
460 if(mySurface.IsNull()) {
461 myErrorStatus=10; // mySurface=NULL
465 if (myShape.IsNull()) {
466 myErrorStatus=11; // myShape=NULL
470 if (!(myShapeType==TopAbs_VERTEX ||
471 myShapeType==TopAbs_EDGE ||
472 myShapeType==TopAbs_FACE ||
473 myShapeType==TopAbs_SOLID)) {
474 myErrorStatus=12; // unallowed subshape type
478 if (myState==GEOMAlgo_ST_UNKNOWN ||
479 myState==GEOMAlgo_ST_INOUT) {
480 myErrorStatus=13; // unallowed state type
485 //=======================================================================
486 //function : CopySource
488 //=======================================================================
489 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
490 TopTools_DataMapOfShapeShape& aImages,
491 TopTools_DataMapOfShapeShape& aOriginals,
494 Standard_Boolean bFree;
495 TopAbs_ShapeEnum aType;
500 aType=aE.ShapeType();
502 if (aOriginals.IsBound(aE)) {
503 aEx=aOriginals.ChangeFind(aE);
507 aEx=aE.EmptyCopied();
508 aOriginals.Bind(aE, aEx);
509 aImages.Bind(aEx, aE);
512 aR=(Standard_Integer)aType+1;
513 if (aR>TopAbs_VERTEX) {
518 aEx.Free(Standard_True);
520 aType=(TopAbs_ShapeEnum) aR;
522 aIt.Initialize(aE);//, Standard_False);
523 for (; aIt.More(); aIt.Next()) {
524 const TopoDS_Shape& aV=aIt.Value();
527 CopySource (aV, aImages, aOriginals, aVx);
529 aVx.Orientation(aV.Orientation());
536 //=======================================================================
537 //function : BuildTriangulation
539 //=======================================================================
540 bool GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
542 // calculate deflection
543 Standard_Real aDeviationCoefficient = 0.001;
546 BRepBndLib::Add(theShape, B);
547 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
548 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
550 Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
551 Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
552 Standard_Real aHLRAngle = 0.349066;
554 // build triangulation
555 BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
557 // check triangulation
558 bool isTriangulation = true;
560 TopExp_Explorer exp (theShape, TopAbs_FACE);
563 TopLoc_Location aTopLoc;
564 Handle(Poly_Triangulation) aTRF;
565 aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
567 isTriangulation = false;
570 else // no faces, try edges
572 TopExp_Explorer expe (theShape, TopAbs_EDGE);
574 isTriangulation = false;
577 TopLoc_Location aLoc;
578 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
580 isTriangulation = false;
585 return isTriangulation;
591 // 10 -mySurface=NULL
593 // 12 -unallowed type of subshapes
594 // 13 -unallowed state
595 // 20 -can not build the face
596 // 30 -wrong args are used for DSFiller
597 // 31 -DSFiller failed
598 // 32 -builder ShapeSolid failed
599 // 33 -can not find original shape
603 // 10 -subshapes of type myShapeType can not be fond in myShape