1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: GEOMAlgo_FinderShapeOn.cxx
21 // Created: Tue Jan 11 14:44:31 2005
22 // Author: Peter KURNEV
26 #include <GEOMAlgo_FinderShapeOn.ixx>
30 #include <TopAbs_ShapeEnum.hxx>
31 #include <TopAbs_Orientation.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Compound.hxx>
37 #include <TopoDS_Shell.hxx>
38 #include <TopoDS_Solid.hxx>
39 #include <TopoDS_Vertex.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Iterator.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45 #include <TopTools_DataMapOfShapeShape.hxx>
47 #include <Bnd_Box.hxx>
48 #include <BRepBndLib.hxx>
49 #include <BRepMesh_IncrementalMesh.hxx>
51 #include <BRep_Builder.hxx>
52 #include <BRep_Tool.hxx>
55 #include <TopExp_Explorer.hxx>
57 #include <BRepLib_MakeFace.hxx>
58 #include <BRepLib_FaceError.hxx>
60 #include <BOPTools_DSFiller.hxx>
62 #include <GEOMAlgo_WireSolid.hxx>
63 #include <GEOMAlgo_ShellSolid.hxx>
64 #include <GEOMAlgo_VertexSolid.hxx>
65 #include <GEOMAlgo_ShapeSolid.hxx>
66 #include <GEOMAlgo_SolidSolid.hxx>
67 #include <GEOMAlgo_SurfaceTools.hxx>
68 #include <GEOMAlgo_Tools.hxx>
70 //=======================================================================
71 //function : GEOMAlgo_FinderShapeOn
73 //=======================================================================
74 GEOMAlgo_FinderShapeOn::GEOMAlgo_FinderShapeOn()
79 myShapeType=TopAbs_VERTEX;
80 myState=GEOMAlgo_ST_UNKNOWN;
81 myIsAnalytic=Standard_True;
83 //=======================================================================
86 //=======================================================================
87 GEOMAlgo_FinderShapeOn::~GEOMAlgo_FinderShapeOn()
90 //=======================================================================
91 //function : SetSurface
93 //=======================================================================
94 void GEOMAlgo_FinderShapeOn::SetSurface(const Handle(Geom_Surface)& aS)
98 //=======================================================================
101 //=======================================================================
102 const Handle(Geom_Surface)& GEOMAlgo_FinderShapeOn::Surface() const
106 //=======================================================================
107 //function : SetShapeType
109 //=======================================================================
110 void GEOMAlgo_FinderShapeOn::SetShapeType(const TopAbs_ShapeEnum aType)
114 //=======================================================================
115 //function : ShapeType
117 //=======================================================================
118 TopAbs_ShapeEnum GEOMAlgo_FinderShapeOn::ShapeType()const
122 //=======================================================================
123 //function : SetState
125 //=======================================================================
126 void GEOMAlgo_FinderShapeOn::SetState(const GEOMAlgo_State aState)
130 //=======================================================================
133 //=======================================================================
134 GEOMAlgo_State GEOMAlgo_FinderShapeOn::State() const
138 //=======================================================================
141 //=======================================================================
142 const TopTools_ListOfShape& GEOMAlgo_FinderShapeOn::Shapes() const
144 Standard_Boolean bIsConformState;
145 Standard_Integer i, aNb;
147 TopTools_ListOfShape* pL;
149 pL=(TopTools_ListOfShape*) &myLS;
153 for (i=1; i<=aNb; ++i) {
154 const TopoDS_Shape& aS=myMSS.FindKey(i);
155 aSt=myMSS.FindFromIndex(i);
157 bIsConformState=GEOMAlgo_SurfaceTools::IsConformState(aSt, myState);
158 if (bIsConformState) {
164 //=======================================================================
167 //=======================================================================
168 void GEOMAlgo_FinderShapeOn::Perform()
175 if (!myResult.IsNull()){
184 myIsAnalytic=GEOMAlgo_SurfaceTools::IsAnalytic(mySurface);
191 if (myIsAnalytic && myShapeType==TopAbs_VERTEX) {
202 if(myErrorStatus || myWarningStatus) {
207 //=======================================================================
208 //function : FindVertices
210 //=======================================================================
211 void GEOMAlgo_FinderShapeOn::FindVertices()
213 Standard_Integer i, aNb, iErr;
215 TopAbs_Orientation aOr;
217 TopTools_IndexedMapOfShape aM;
219 TopExp::MapShapes(myArg1, TopAbs_FACE, aM);
220 const TopoDS_Face& aF=TopoDS::Face(aM(1));
221 aOr=aF.Orientation();
224 TopExp::MapShapes(myShape, myShapeType, aM);
227 myWarningStatus=10; // No found subshapes of type myShapeType
231 for (i=1; i<=aNb; ++i) {
232 const TopoDS_Shape& aS=aM(i);
233 const TopoDS_Vertex& aV=TopoDS::Vertex(aS);
234 aP=BRep_Tool::Pnt(aV);
235 iErr=GEOMAlgo_SurfaceTools::GetState(aP, mySurface, myTolerance, aSt);
236 if (aOr==TopAbs_REVERSED) {
237 aSt=GEOMAlgo_SurfaceTools::ReverseState(aSt);
242 //=======================================================================
245 //=======================================================================
246 void GEOMAlgo_FinderShapeOn::Find()
248 Standard_Integer i, aNb;
249 Standard_Boolean bICS;
250 TopTools_IndexedMapOfShape aM;
252 TopExp::MapShapes(myArg2, myShapeType, aM);
256 myWarningStatus=10; // No found subshapes of type myShapeType
260 bICS=GEOMAlgo_Tools::IsCompositeShape(myArg2);
261 if (!bICS || myIsAnalytic) {
262 TopoDS_Compound aCmp;
265 aBB.MakeCompound(aCmp);
266 for (i=1; i<=aNb; ++i) {
267 const TopoDS_Shape& aSi=aM(i);
276 for (i=1; i<=aNb; ++i) {
277 const TopoDS_Shape& aS=aM(i);
284 //=======================================================================
287 //=======================================================================
288 void GEOMAlgo_FinderShapeOn::Find(const TopoDS_Shape& aS)
292 Standard_Boolean bIsDone;
293 Standard_Integer i, iErr;
294 TopAbs_State aSts[]={TopAbs_IN, TopAbs_OUT, TopAbs_ON};
295 TopTools_ListIteratorOfListOfShape aIt;
296 BOPTools_DSFiller aDF;
298 // 1. Prepare DSFiller
299 aDF.SetShapes (myArg1, aS);
300 bIsDone=aDF.IsDone();
302 myErrorStatus=30; // wrong args are used for DSFiller
306 bIsDone=aDF.IsDone();
308 myErrorStatus=31; // DSFiller failed
313 GEOMAlgo_ShapeSolid* pSS;
314 GEOMAlgo_VertexSolid aVXS;
315 GEOMAlgo_WireSolid aWRS;
316 GEOMAlgo_ShellSolid aSHS;
317 GEOMAlgo_SolidSolid aSLS;
321 switch (myShapeType) {
332 aSLS.SetShape2(myArg2);
336 myErrorStatus=12; // unallowed subshape type
342 iErr=pSS->ErrorStatus();
344 myErrorStatus=32; // builder ShapeSolid failed
348 for (i=0; i<3; ++i) {
349 const TopTools_ListOfShape& aLS=pSS->Shapes(aSts[i]);
351 for (; aIt.More(); aIt.Next()) {
352 const TopoDS_Shape& aSImage=aIt.Value();
353 if (myImages.IsBound(aSImage)) {
354 const TopoDS_Shape& aSx=myImages.Find(aSImage);
355 myMSS.Add(aSx, aSts[i]);
358 myErrorStatus=33;// can not find original shape
364 //=======================================================================
365 //function : MakeArgument1
367 //=======================================================================
368 void GEOMAlgo_FinderShapeOn::MakeArgument1()
372 Standard_Integer i, aNb;
373 TopAbs_ShapeEnum aType;
374 BRepLib_FaceError aFErr;
375 BRepLib_MakeFace aMF;
376 TopTools_IndexedMapOfShape aM;
384 aMF.Init(mySurface, Standard_True);
386 if (aFErr!=BRepLib_FaceDone) {
387 myErrorStatus=20; // can not build the face
391 const TopoDS_Shape& aF=aMF.Shape();
392 aFace=TopoDS::Face(aF);
396 TopExp::MapShapes(aF, TopAbs_VERTEX, aM);
397 TopExp::MapShapes(aF, TopAbs_EDGE, aM);
399 for (i=1; i<=aNb; ++i) {
400 const TopoDS_Shape& aS=aM(i);
401 aType=aS.ShapeType();
403 case TopAbs_VERTEX: {
404 const TopoDS_Vertex& aVx=TopoDS::Vertex(aS);
405 aBB.UpdateVertex(aVx, myTolerance);
409 const TopoDS_Edge& aEx=TopoDS::Edge(aS);
410 aBB.UpdateEdge(aEx, myTolerance);
414 const TopoDS_Face& aFx=TopoDS::Face(aS);
415 aBB.UpdateFace(aFx, myTolerance);
424 aBB.MakeFace(aFace, mySurface, myTolerance);
434 //=======================================================================
435 //function : MakeArgument2
437 //=======================================================================
438 void GEOMAlgo_FinderShapeOn::MakeArgument2()
443 TopTools_DataMapOfShapeShape aOriginals;
447 GEOMAlgo_FinderShapeOn::CopySource(myShape, myImages, aOriginals, aSC);
451 //=======================================================================
452 //function : CheckData
454 //=======================================================================
455 void GEOMAlgo_FinderShapeOn::CheckData()
459 if(mySurface.IsNull()) {
460 myErrorStatus=10; // mySurface=NULL
464 if (myShape.IsNull()) {
465 myErrorStatus=11; // myShape=NULL
469 if (!(myShapeType==TopAbs_VERTEX ||
470 myShapeType==TopAbs_EDGE ||
471 myShapeType==TopAbs_FACE ||
472 myShapeType==TopAbs_SOLID)) {
473 myErrorStatus=12; // unallowed subshape type
477 if (myState==GEOMAlgo_ST_UNKNOWN ||
478 myState==GEOMAlgo_ST_INOUT) {
479 myErrorStatus=13; // unallowed state type
484 //=======================================================================
485 //function : CopySource
487 //=======================================================================
488 void GEOMAlgo_FinderShapeOn::CopySource(const TopoDS_Shape& aE,
489 TopTools_DataMapOfShapeShape& aImages,
490 TopTools_DataMapOfShapeShape& aOriginals,
493 Standard_Boolean bFree;
494 TopAbs_ShapeEnum aType;
499 aType=aE.ShapeType();
501 if (aOriginals.IsBound(aE)) {
502 aEx=aOriginals.ChangeFind(aE);
506 aEx=aE.EmptyCopied();
507 aOriginals.Bind(aE, aEx);
508 aImages.Bind(aEx, aE);
511 aR=(Standard_Integer)aType+1;
512 if (aR>TopAbs_VERTEX) {
517 aEx.Free(Standard_True);
519 aType=(TopAbs_ShapeEnum) aR;
521 aIt.Initialize(aE);//, Standard_False);
522 for (; aIt.More(); aIt.Next()) {
523 const TopoDS_Shape& aV=aIt.Value();
526 CopySource (aV, aImages, aOriginals, aVx);
528 aVx.Orientation(aV.Orientation());
535 //=======================================================================
536 //function : BuildTriangulation
538 //=======================================================================
539 bool GEOMAlgo_FinderShapeOn::BuildTriangulation (const TopoDS_Shape& theShape)
541 // calculate deflection
542 Standard_Real aDeviationCoefficient = 0.001;
545 BRepBndLib::Add(theShape, B);
546 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
547 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
549 Standard_Real dx = aXmax - aXmin, dy = aYmax - aYmin, dz = aZmax - aZmin;
550 Standard_Real aDeflection = Max(Max(dx, dy), dz) * aDeviationCoefficient * 4;
551 Standard_Real aHLRAngle = 0.349066;
553 // build triangulation
554 BRepMesh_IncrementalMesh Inc (theShape, aDeflection, Standard_False, aHLRAngle);
556 // check triangulation
557 bool isTriangulation = true;
559 TopExp_Explorer exp (theShape, TopAbs_FACE);
562 TopLoc_Location aTopLoc;
563 Handle(Poly_Triangulation) aTRF;
564 aTRF = BRep_Tool::Triangulation(TopoDS::Face(exp.Current()), aTopLoc);
566 isTriangulation = false;
569 else // no faces, try edges
571 TopExp_Explorer expe (theShape, TopAbs_EDGE);
573 isTriangulation = false;
576 TopLoc_Location aLoc;
577 Handle(Poly_Polygon3D) aPE = BRep_Tool::Polygon3D(TopoDS::Edge(expe.Current()), aLoc);
579 isTriangulation = false;
584 return isTriangulation;
590 // 10 -mySurface=NULL
592 // 12 -unallowed type of subshapes
593 // 13 -unallowed state
594 // 20 -can not build the face
595 // 30 -wrong args are used for DSFiller
596 // 31 -DSFiller failed
597 // 32 -builder ShapeSolid failed
598 // 33 -can not find original shape
602 // 10 -subshapes of type myShapeType can not be fond in myShape