Salome HOME
a28b4aab8581104e2a4a364d456b5992a0bce634
[modules/geom.git] / src / ShHealOper / ShHealOper_ChangeOrientation.cxx
1 // Copyright (C) 2007-2022  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, or (at your option) any later version.
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       myStatistics.AddModif("Face reversed");
77     }
78   }
79   else if (myInitShape.ShapeType() == TopAbs_FACE)
80   {
81     myResultShape = myInitShape.EmptyCopied();
82     TopoDS_Iterator itr (myInitShape);
83     while (itr.More()) {
84       B.Add(myResultShape,itr.Value());
85       itr.Next();
86       myStatistics.AddModif("Wire reversed");
87     }
88     myResultShape.Reverse();
89   }
90   else if ( myInitShape.ShapeType() == TopAbs_WIRE || myInitShape.ShapeType() == TopAbs_EDGE)
91   {
92     TopTools_ListOfShape reversedEdges;
93     for ( TopExp_Explorer edgeIt( myInitShape, TopAbs_EDGE ); edgeIt.More(); edgeIt.Next() )
94     {
95       const TopoDS_Edge& edge = TopoDS::Edge( edgeIt.Current() );
96
97       double f,l;
98       Handle(Geom_Curve) curve = BRep_Tool::Curve( edge, f,l );
99       Handle(Geom_TrimmedCurve) tc = Handle(Geom_TrimmedCurve)::DownCast(curve);
100       if ( !tc.IsNull() ) curve = tc->BasisCurve();
101
102       f = curve->ReversedParameter( f );
103       l = curve->ReversedParameter( l );
104       curve = curve->Reversed();
105       reversedEdges.Prepend( BRepBuilderAPI_MakeEdge( curve, Min( f, l ), Max( f, l )));
106     }
107     if ( myInitShape.ShapeType() == TopAbs_EDGE )
108     {
109       myResultShape = reversedEdges.First();
110       myStatistics.AddModif("Edge reversed");
111     }
112     else
113     {
114       BRepBuilderAPI_MakeWire wire;
115       wire.Add( reversedEdges );
116       myResultShape = wire;
117       myStatistics.AddModif("Wire reversed");
118     }
119     // myResultShape = myInitShape.EmptyCopied();
120     // TopoDS_Iterator itr (myInitShape);
121     // while (itr.More()) {
122     //   B.Add(myResultShape,itr.Value());
123     //   itr.Next();
124     // }
125     // myResultShape.Reverse();
126   }
127   else
128   {
129     BRepBuilderAPI_Copy Copy (myInitShape);
130     if (!Copy.IsDone()) return false;
131
132     myResultShape = Copy.Shape();
133     if (myResultShape.IsNull()) return false;
134
135     if (myResultShape.Orientation() == TopAbs_FORWARD)
136       myResultShape.Orientation(TopAbs_REVERSED);
137     else
138       myResultShape.Orientation(TopAbs_FORWARD);
139
140     switch( myResultShape.ShapeType() ) {
141     case TopAbs_SOLID    : myStatistics.AddModif("Solid reversed"); break;
142     case TopAbs_COMPSOLID: myStatistics.AddModif("Compsolid reversed"); break;
143     case TopAbs_COMPOUND : myStatistics.AddModif("Compound reversed"); break;
144     default:;
145     }
146   }
147
148   return true;
149 }