Salome HOME
04b59af77bdf690f246b8c56afd9c4b0959fa0fb
[modules/geom.git] / src / ShHealOper / ShHealOper_ChangeOrientation.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:      ShHealOper_ChangeOrientation.cxx
24 // Created:   11.07.06 11:46:45
25 // Author:    Sergey KUUL
26
27 #include <ShHealOper_ChangeOrientation.hxx>
28
29 #include <BRepBuilderAPI_Copy.hxx>
30 #include <BRepBuilderAPI_MakeEdge.hxx>
31 #include <BRepBuilderAPI_MakeWire.hxx>
32 #include <BRep_Builder.hxx>
33 #include <Geom_Curve.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TopTools_ListOfShape.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Iterator.hxx>
39 #include <Geom_TrimmedCurve.hxx>
40
41 //=======================================================================
42 //function : ShHealOper_ChangeOrientation()
43 //purpose  : Constructor
44 //=======================================================================
45
46 ShHealOper_ChangeOrientation::ShHealOper_ChangeOrientation ( const TopoDS_Shape& theShape )
47 {
48   Init(theShape);
49 }
50
51 //=======================================================================
52 //function : Init
53 //purpose  : 
54 //=======================================================================
55
56 void ShHealOper_ChangeOrientation::Init(const TopoDS_Shape& theShape)
57 {
58   ShHealOper_Tool::Init(theShape);
59 }
60
61 //=======================================================================
62 //function : Perform
63 //purpose  : 
64 //=======================================================================
65
66 Standard_Boolean ShHealOper_ChangeOrientation::Perform()
67 {
68   BRep_Builder B;
69   if (myInitShape.ShapeType() == TopAbs_SHELL)
70   {
71     myResultShape = myInitShape.EmptyCopied();
72     TopoDS_Iterator itr (myInitShape);
73     while (itr.More()) {
74       B.Add(myResultShape,itr.Value().Reversed());
75       itr.Next();
76     }
77   }
78   else if (myInitShape.ShapeType() == TopAbs_FACE)
79   {
80     myResultShape = myInitShape.EmptyCopied();
81     TopoDS_Iterator itr (myInitShape);
82     while (itr.More()) {
83       B.Add(myResultShape,itr.Value());
84       itr.Next();
85     }
86     myResultShape.Reverse();
87   }
88   else if ( myInitShape.ShapeType() == TopAbs_WIRE || myInitShape.ShapeType() == TopAbs_EDGE)
89   {
90     TopTools_ListOfShape reversedEdges;
91     for ( TopExp_Explorer edgeIt( myInitShape, TopAbs_EDGE ); edgeIt.More(); edgeIt.Next() )
92     {
93       const TopoDS_Edge& edge = TopoDS::Edge( edgeIt.Current() );
94
95       double f,l;
96       Handle(Geom_Curve) curve = BRep_Tool::Curve( edge, f,l );
97       Handle(Geom_TrimmedCurve) tc = Handle(Geom_TrimmedCurve)::DownCast(curve);
98       if ( !tc.IsNull() ) curve = tc->BasisCurve();
99
100       f = curve->ReversedParameter( f );
101       l = curve->ReversedParameter( l );
102       curve = curve->Reversed();
103       reversedEdges.Prepend( BRepBuilderAPI_MakeEdge( curve, Min( f, l ), Max( f, l )));
104     }
105     if ( myInitShape.ShapeType() == TopAbs_EDGE )
106     {
107       myResultShape = reversedEdges.First();
108     }
109     else
110     {
111       BRepBuilderAPI_MakeWire wire;
112       wire.Add( reversedEdges );
113       myResultShape = wire;
114     }
115     // myResultShape = myInitShape.EmptyCopied();
116     // TopoDS_Iterator itr (myInitShape);
117     // while (itr.More()) {
118     //   B.Add(myResultShape,itr.Value());
119     //   itr.Next();
120     // }
121     // myResultShape.Reverse();
122   }
123   else
124   {
125     BRepBuilderAPI_Copy Copy (myInitShape);
126     if (!Copy.IsDone()) return false;
127
128     myResultShape = Copy.Shape();
129     if (myResultShape.IsNull()) return false;
130
131     if (myResultShape.Orientation() == TopAbs_FORWARD)
132       myResultShape.Orientation(TopAbs_REVERSED);
133     else
134       myResultShape.Orientation(TopAbs_FORWARD);
135   }
136
137   return true;
138 }