Salome HOME
PAL7508: Development of new block functionalities. Implement GetShapesOn<xxx>() funct...
[modules/geom.git] / src / GEOMAlgo / BlockFix_PeriodicSurfaceModifier.cxx
1 // File:      BlockFix_PeriodicSurfaceModifier.cxx
2 // Created:   15.12.04 10:08:50
3 // Author:    Sergey KUUL
4 // Copyright: Open CASCADE SA 2004
5
6 #include <BlockFix_PeriodicSurfaceModifier.ixx>
7
8 #include <BRep_Builder.hxx>
9 #include <BRep_Tool.hxx>
10 #include <BRepTools.hxx>
11 #include <Geom_CylindricalSurface.hxx>
12 #include <Geom_SphericalSurface.hxx>
13 #include <ShapeFix_Edge.hxx>
14 #include <TopExp.hxx>
15
16
17 //=======================================================================
18 //function : BlockFix_PeriodicSurfaceModifier()
19 //purpose  : Constructor
20 //=======================================================================
21
22 BlockFix_PeriodicSurfaceModifier::BlockFix_PeriodicSurfaceModifier (  )
23 {
24   myMapOfFaces.Clear();
25   myMapOfSurfaces.Clear();
26 }
27
28
29 //=======================================================================
30 //function : SetTolerance
31 //purpose  : 
32 //=======================================================================
33
34 void BlockFix_PeriodicSurfaceModifier::SetTolerance(const Standard_Real Tol)
35 {
36   myTolerance = Tol;
37 }
38
39
40 //=======================================================================
41 //function : ModifySurface
42 //purpose  : auxilary
43 //=======================================================================
44
45 static Standard_Boolean ModifySurface(const TopoDS_Face& aFace,
46                                       const Handle(Geom_Surface)& aSurface,
47                                       Handle(Geom_Surface)& aNewSurface)
48 {
49   Handle(Geom_Surface) S = aSurface;
50
51   if(S->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
52     Handle(Geom_CylindricalSurface) aCyl = 
53       Handle(Geom_CylindricalSurface)::DownCast(S);
54     Standard_Real Umin, Umax, Vmin, Vmax;
55     BRepTools::UVBounds(aFace, Umin, Umax, Vmin, Vmax);
56     if( Umin<-Precision::PConfusion() || Umax>2*PI+Precision::PConfusion() ) {
57       gp_Ax3 ax3 = aCyl->Position();
58       gp_Ax1 NDir = ax3.Axis();
59       gp_Ax3 newax3 = ax3.Rotated(NDir,Umin-Precision::PConfusion());
60       Handle(Geom_CylindricalSurface) aNewCyl =
61         new Geom_CylindricalSurface(newax3,aCyl->Radius());
62       aNewSurface = aNewCyl;
63       return Standard_True;
64     }
65   }
66   
67   if(S->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
68     Handle(Geom_SphericalSurface) aSphere = Handle(Geom_SphericalSurface)::DownCast(S);
69     Standard_Real Umin, Umax, Vmin, Vmax;
70     BRepTools::UVBounds(aFace, Umin, Umax, Vmin, Vmax);
71     if( Umin<-Precision::PConfusion() || Umax>2*PI+Precision::PConfusion() ) {
72       gp_Ax3 ax3 = aSphere->Position();
73       gp_Ax1 NDir = ax3.Axis();
74       gp_Ax3 newax3 = ax3.Rotated(NDir,Umin-Precision::PConfusion());
75       Handle(Geom_SphericalSurface) aNewSphere = new Geom_SphericalSurface(newax3,aSphere->Radius());
76       aNewSurface = aNewSphere;
77       return Standard_True;
78     }
79   }
80
81   return Standard_False;
82 }
83                                       
84
85 //=======================================================================
86 //function : NewSurface
87 //purpose  : 
88 //=======================================================================
89
90 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewSurface(const TopoDS_Face& F,
91                                                               Handle(Geom_Surface)& S,
92                                                               TopLoc_Location& L,Standard_Real& Tol,
93                                                               Standard_Boolean& RevWires,
94                                                               Standard_Boolean& RevFace) 
95 {
96   TopLoc_Location LS;
97   Handle(Geom_Surface) SIni = BRep_Tool::Surface(F, LS);
98   
99   if(ModifySurface(F, SIni, S)) {
100   
101     RevWires = Standard_False;
102     RevFace = Standard_False;
103     
104     L = LS;
105     Tol = BRep_Tool::Tolerance(F);
106      
107     Standard_Integer anIndex = myMapOfSurfaces.Add(S);
108     myMapOfFaces.Bind(F,anIndex);
109     return Standard_True;
110   }
111
112   return Standard_False;
113 }
114
115
116 //=======================================================================
117 //function : NewCurve
118 //purpose  : 
119 //=======================================================================
120
121 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewCurve(const TopoDS_Edge& /*E*/,
122                                                             Handle(Geom_Curve)& /*C*/,
123                                                             TopLoc_Location& /*L*/,
124                                                             Standard_Real& /*Tol*/) 
125 {
126   return Standard_False;
127 }
128
129
130 //=======================================================================
131 //function : NewPoint
132 //purpose  : 
133 //=======================================================================
134
135 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewPoint(const TopoDS_Vertex& /*V*/,
136                                                             gp_Pnt& /*P*/,
137                                                             Standard_Real& /*Tol*/) 
138 {
139   return Standard_False;
140 }
141
142
143 //=======================================================================
144 //function : NewCurve2d
145 //purpose  : 
146 //=======================================================================
147
148 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewCurve2d(const TopoDS_Edge& E,
149                                                               const TopoDS_Face& F,
150                                                               const TopoDS_Edge& /*NewE*/,
151                                                               const TopoDS_Face& /*NewF*/,
152                                                               Handle(Geom2d_Curve)& C,
153                                                               Standard_Real& Tol) 
154 {
155   //check if undelying surface of the face was modified
156   if(myMapOfFaces.IsBound(F)) {
157     Standard_Integer anIndex = myMapOfFaces.Find(F);
158     
159     Handle(Geom_Surface) aNewSurf = Handle(Geom_Surface)::DownCast(myMapOfSurfaces.FindKey(anIndex));
160     
161     Standard_Real f,l;
162     TopLoc_Location LC, LS;
163     Handle(Geom_Curve) C3d = BRep_Tool::Curve ( E, LC, f, l );
164     Handle(Geom_Surface) S = BRep_Tool::Surface(F, LS);
165   
166     //taking into accound the orientation of the seam
167     C = BRep_Tool::CurveOnSurface(E,F,f,l);
168     Tol = BRep_Tool::Tolerance(E);
169      
170     BRep_Builder B;
171     TopoDS_Edge TempE;
172     B.MakeEdge(TempE);
173     B.Add(TempE, TopExp::FirstVertex(E));
174     B.Add(TempE, TopExp::LastVertex(E));
175
176     if(!C3d.IsNull()) 
177       B.UpdateEdge(TempE, Handle(Geom_Curve)::DownCast(C3d->Transformed(LC.Transformation())), Precision::Confusion());
178     B.Range(TempE, f, l);
179     
180     Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
181     Handle(Geom_Surface) STemp = Handle(Geom_Surface)::DownCast(aNewSurf->Transformed(LS.Transformation()));
182     TopLoc_Location LTemp;
183     LTemp.Identity();
184     
185     Standard_Boolean isClosed = BRep_Tool::IsClosed (E, F);
186     Standard_Real aWorkTol = 2*myTolerance+Tol;
187     sfe->FixAddPCurve(TempE, STemp, LTemp, isClosed, Max(Precision::Confusion(), aWorkTol));
188     sfe->FixSameParameter(TempE);
189       
190     //keep the orientation of original edge
191     TempE.Orientation(E.Orientation());
192     C = BRep_Tool::CurveOnSurface(TempE, STemp, LTemp, f, l);
193     
194     //surface was modified
195     return Standard_True;
196   }
197
198   return Standard_False;
199 }
200
201
202 //=======================================================================
203 //function : NewParameter
204 //purpose  : 
205 //=======================================================================
206
207 Standard_Boolean BlockFix_PeriodicSurfaceModifier::NewParameter(const TopoDS_Vertex& /*V*/,
208                                                                 const TopoDS_Edge& /*E*/,
209                                                                 Standard_Real& /*P*/,
210                                                                 Standard_Real& /*Tol*/) 
211 {
212   return Standard_False;
213 }
214
215
216 //=======================================================================
217 //function : Continuity
218 //purpose  : 
219 //=======================================================================
220
221 GeomAbs_Shape BlockFix_PeriodicSurfaceModifier::Continuity(const TopoDS_Edge& E,
222                                                            const TopoDS_Face& F1,
223                                                            const TopoDS_Face& F2,
224                                                            const TopoDS_Edge& /*NewE*/,
225                                                            const TopoDS_Face& /*NewF1*/,
226                                                            const TopoDS_Face& /*NewF2*/) 
227 {
228   return BRep_Tool::Continuity(E,F1,F2);
229 }
230