Salome HOME
22752: [EDF] Provide explicit feedback on what has been done by Shape Processing...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ThruSectionsDriver.cxx
1 // Copyright (C) 2007-2014  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 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_ThruSectionsDriver.hxx>
26 #include <GEOMImpl_IThruSections.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <TColStd_HSequenceOfTransient.hxx>
31 #include <Precision.hxx>
32 #include <BRepCheck_Analyzer.hxx>
33 #include <BRepOffsetAPI_ThruSections.hxx>
34 #include <BRepBuilderAPI_MakeWire.hxx>
35 #include <TopExp_Explorer.hxx>
36 #include <TopoDS.hxx>
37
38 #include <TopAbs.hxx>
39 #include <TopoDS.hxx>
40 #include <TopoDS_Wire.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS_Shape.hxx>
43
44 #include <Standard_NullObject.hxx>
45 #include <Standard_TypeMismatch.hxx>
46 #include <Standard_ConstructionError.hxx>
47 #include <ShapeFix_Shape.hxx>
48 #include <ShapeFix_ShapeTolerance.hxx>
49 #include <Precision.hxx>
50 //=======================================================================
51 //function : GetID
52 //purpose  :
53 //=======================================================================
54 const Standard_GUID& GEOMImpl_ThruSectionsDriver::GetID()
55 {
56   static Standard_GUID aThruSectionsDriver("FF1BB971-E99C-4f89-B989-5B48E061049B");
57   return aThruSectionsDriver;
58 }
59
60
61 //=======================================================================
62 //function : GEOMImpl_ThruSectionsDriver
63 //purpose  :
64 //=======================================================================
65 GEOMImpl_ThruSectionsDriver::GEOMImpl_ThruSectionsDriver()
66 {
67 }
68
69 //=======================================================================
70 //function : Execute
71 //purpose  :
72 //=======================================================================
73 Standard_Integer GEOMImpl_ThruSectionsDriver::Execute(TFunction_Logbook& log) const
74 {
75   if (Label().IsNull()) return 0;
76   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
77
78   GEOMImpl_IThruSections aCI (aFunction);
79   Standard_Integer aType = aFunction->GetType();
80
81   Standard_Boolean isSolid = (aCI.GetSolidMode() == 1);
82   Handle(TColStd_HSequenceOfTransient) aSeqSections = aCI.GetSections();
83
84   if( aSeqSections.IsNull())
85     return 0;
86
87   Standard_Integer aNbSections = aSeqSections->Length();
88   Standard_Real aPreci = Max(aCI.GetPrecision(),Precision::Confusion());
89   if(!aNbSections )
90     return 0;
91
92   BRepOffsetAPI_ThruSections aBuilder(isSolid,aType ==THRUSECTIONS_RULED,aPreci);
93   
94  
95   aBuilder.CheckCompatibility(Standard_False);
96   //added sections for building surface
97   Standard_Integer i =1;
98   Standard_Integer nbAdded =0;
99   for( ; i <= aNbSections; i++,nbAdded++)
100   {
101     Handle(Standard_Transient) anItem = aSeqSections->Value(i);
102     if(anItem.IsNull())
103       continue;
104
105     Handle(GEOM_Function) aSection = Handle(GEOM_Function)::DownCast(anItem);
106     if(aSection.IsNull())
107       continue;
108
109     TopoDS_Shape aShapeSection = aSection->GetValue();
110     TopAbs_ShapeEnum aTypeSect = aShapeSection.ShapeType();
111     if(aTypeSect == TopAbs_WIRE)
112       aBuilder.AddWire(TopoDS::Wire(aShapeSection));
113
114     else if(aTypeSect == TopAbs_EDGE) {
115       TopoDS_Edge anEdge = TopoDS::Edge(aShapeSection);
116       TopoDS_Wire aWire = BRepBuilderAPI_MakeWire(anEdge);
117       aBuilder.AddWire(aWire);
118     }
119     else if(aTypeSect == TopAbs_VERTEX) {
120       TopoDS_Vertex aVert = TopoDS::Vertex(aShapeSection);
121       aBuilder.AddVertex(aVert);
122     }
123     else
124        nbAdded--; 
125   }  
126   if(!nbAdded)
127      Standard_TypeMismatch::Raise("ThruSections aborted : invalid types of sections");
128   //make surface by sections
129   aBuilder.Build();
130   TopoDS_Shape aShape = aBuilder.Shape();
131   if (aShape.IsNull()) {
132     return 0;
133   }
134
135   BRepCheck_Analyzer ana (aShape, Standard_False);
136   if (!ana.IsValid()) {
137     //algoritm thru section creats on the arcs invalid shapes gka
138     ShapeFix_ShapeTolerance aSFT;
139     aSFT.LimitTolerance(aShape,Precision::Confusion(),Precision::Confusion());
140     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
141     aSfs->SetPrecision(Precision::Confusion());
142     aSfs->Perform();
143     aShape = aSfs->Shape();
144     //ana.Init(aShape, Standard_False);
145     //if (!ana.IsValid()) 
146     //  Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
147   }
148
149
150   aFunction->SetValue(aShape);
151
152   log.SetTouched(Label());
153
154   return 1;
155 }
156
157 //================================================================================
158 /*!
159  * \brief Returns a name of creation operation and names and values of creation parameters
160  */
161 //================================================================================
162
163 bool GEOMImpl_ThruSectionsDriver::
164 GetCreationInformation(std::string&             theOperationName,
165                        std::vector<GEOM_Param>& theParams)
166 {
167   if (Label().IsNull()) return 0;
168   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
169
170   GEOMImpl_IThruSections aCI( function );
171   Standard_Integer aType = function->GetType();
172
173   theOperationName = "MakeThruSections";
174
175   switch ( aType ) {
176   case THRUSECTIONS_RULED:
177   case THRUSECTIONS_SMOOTHED:
178     AddParam( theParams, "Sections", aCI.GetSections() );
179     AddParam( theParams, "Is solid", aCI.GetSolidMode() );
180     AddParam( theParams, "Precision", aCI.GetPrecision() );
181     AddParam( theParams, "Is ruled", aType == THRUSECTIONS_RULED );
182     break;
183   default:
184     return false;
185   }
186   
187   return true;
188 }
189
190 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_ThruSectionsDriver,GEOM_BaseDriver);
191 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_ThruSectionsDriver,GEOM_BaseDriver);