0024955: Misuse of DownCast
[occt.git] / src / ShapeUpgrade / ShapeUpgrade_FaceDivideArea.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <ShapeUpgrade_FaceDivideArea.ixx>
15 #include <GProp_GProps.hxx>
16 #include <BRepGProp.hxx>
17 #include <Precision.hxx> 
18 #include <ShapeUpgrade_SplitSurfaceArea.hxx> 
19 #include <TopoDS_Shape.hxx>
20 #include <TopExp_Explorer.hxx>  
21 #include <TopoDS.hxx>
22 #include <ShapeExtend.hxx> 
23 #include <ShapeBuild_ReShape.hxx>
24 #include <BRep_Builder.hxx>
25
26 //=======================================================================
27 //function : ShapeUpgrade_FaceDivideArea
28 //purpose  : 
29 //=======================================================================
30
31 ShapeUpgrade_FaceDivideArea::ShapeUpgrade_FaceDivideArea()
32 {
33   myMaxArea = Precision::Infinite();
34   SetPrecision(1.e-5);
35   SetSplitSurfaceTool (new ShapeUpgrade_SplitSurfaceArea);
36 }
37
38 //=======================================================================
39 //function : ShapeUpgrade_FaceDivideArea
40 //purpose  : 
41 //=======================================================================
42
43 ShapeUpgrade_FaceDivideArea::ShapeUpgrade_FaceDivideArea(const TopoDS_Face& F)
44 {
45   myMaxArea = Precision::Infinite();
46   SetPrecision(1.e-5);
47   SetSplitSurfaceTool (new ShapeUpgrade_SplitSurfaceArea);
48   Init(F);
49 }
50
51 //=======================================================================
52 //function : Perform
53 //purpose  : 
54 //=======================================================================
55
56  Standard_Boolean ShapeUpgrade_FaceDivideArea::Perform() 
57 {
58   myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
59   GProp_GProps aGprop;
60   
61   BRepGProp::SurfaceProperties(myFace,aGprop,Precision());
62   Standard_Real anArea = aGprop.Mass();
63   if((anArea - myMaxArea) < Precision::Confusion())
64     return Standard_False;
65  
66   Standard_Integer anbParts = RealToInt(ceil(anArea/myMaxArea));
67   Handle(ShapeUpgrade_SplitSurfaceArea) aSurfTool= Handle(ShapeUpgrade_SplitSurfaceArea)::
68     DownCast(GetSplitSurfaceTool ());
69   if(aSurfTool.IsNull())
70     return Standard_False;
71   aSurfTool->NbParts() = anbParts;
72   if(!ShapeUpgrade_FaceDivide::Perform())
73     return Standard_False;
74   
75   TopoDS_Shape aResult = Result();
76   if(aResult.ShapeType() == TopAbs_FACE)
77     return Standard_False;
78   Standard_Integer aStatus = myStatus;
79   TopExp_Explorer aExpF(aResult,TopAbs_FACE);
80   TopoDS_Shape aCopyRes = aResult.EmptyCopied();
81   
82   Standard_Boolean isModified = Standard_False;
83   for( ; aExpF.More() ; aExpF.Next()) {
84     TopoDS_Shape aSh = Context()->Apply(aExpF.Current());
85     TopoDS_Face aFace = TopoDS::Face(aSh);
86     Init(aFace);
87     BRep_Builder aB;
88     if(Perform()) {
89       isModified = Standard_True;
90       TopoDS_Shape aRes = Result();
91       TopExp_Explorer aExpR(aRes,TopAbs_FACE);
92       for( ; aExpR.More(); aExpR.Next())
93         aB.Add(aCopyRes,aExpR.Current());
94     }
95     else
96       aB.Add(aCopyRes,aFace);
97   }
98   if(isModified)
99     Context()->Replace(aResult,aCopyRes);
100   myStatus |= aStatus;  
101   myResult = Context()->Apply ( aResult );
102   return Status ( ShapeExtend_DONE ); 
103 }
104