Salome HOME
0021684: EDF 2221 : Display the arguments and the name of the operations
[modules/geom.git] / src / GEOMImpl / GEOMImpl_OffsetDriver.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 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_OffsetDriver.hxx>
26 #include <GEOMImpl_IOffset.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <BRepOffsetAPI_MakeOffsetShape.hxx>
31 #include <BRep_Tool.hxx>
32 #include <TopoDS.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Vertex.hxx>
35 #include <TopAbs.hxx>
36 #include <TopExp.hxx>
37
38 #include <BRepClass3d_SolidClassifier.hxx>
39
40 #include <Precision.hxx>
41 #include <gp_Pnt.hxx>
42
43 #include <BRepCheck_Analyzer.hxx>
44 #include <ShapeFix_ShapeTolerance.hxx>
45 #include <ShapeFix_Shape.hxx>
46
47 #include <Standard_ConstructionError.hxx>
48 #include <StdFail_NotDone.hxx>
49
50 #include "utilities.h"
51
52 //=======================================================================
53 //function : GetID
54 //purpose  :
55 //=======================================================================
56 const Standard_GUID& GEOMImpl_OffsetDriver::GetID()
57 {
58   static Standard_GUID aOffsetDriver("FF1BBB51-5D14-4df2-980B-3A668264EA16");
59   return aOffsetDriver;
60 }
61
62
63 //=======================================================================
64 //function : GEOMImpl_OffsetDriver
65 //purpose  :
66 //=======================================================================
67 GEOMImpl_OffsetDriver::GEOMImpl_OffsetDriver()
68 {
69 }
70
71 //=======================================================================
72 //function : Execute
73 //purpose  :
74 //=======================================================================
75 Standard_Integer GEOMImpl_OffsetDriver::Execute(TFunction_Logbook& log) const
76 {
77   if (Label().IsNull()) return 0;
78   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
79
80   GEOMImpl_IOffset aCI (aFunction);
81   Standard_Integer aType = aFunction->GetType();
82
83   TopoDS_Shape aShape;
84   
85   Handle(GEOM_Function) aRefShape = aCI.GetShape();
86   TopoDS_Shape aShapeBase = aRefShape->GetValue();
87   Standard_Real anOffset = aCI.GetValue();
88   Standard_Real aTol = Precision::Confusion();
89   
90   if (Abs(anOffset) < aTol) {
91       TCollection_AsciiString aMsg ("Absolute value of offset can not be less than the tolerance value (");
92       aMsg += TCollection_AsciiString(aTol);
93       aMsg += ")";
94       StdFail_NotDone::Raise(aMsg.ToCString());
95   }
96
97   if (aType == OFFSET_SHAPE || aType == OFFSET_SHAPE_COPY) {  
98     BRepOffsetAPI_MakeOffsetShape MO (aShapeBase,
99                                       aCI.GetValue(),
100                                       aTol);
101     if (MO.IsDone()) {
102       aShape = MO.Shape();
103       // 23.04.2010 skl for bug 21699 from Mantis
104       BRepCheck_Analyzer ana (aShape, Standard_True);
105       ana.Init(aShape);
106       if (!ana.IsValid()) {
107         ShapeFix_ShapeTolerance aSFT;
108         aSFT.LimitTolerance(aShape, Precision::Confusion(),
109                             Precision::Confusion(), TopAbs_SHAPE);
110         Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
111         aSfs->Perform();
112         aShape = aSfs->Shape();
113         ana.Init(aShape);
114         if (!ana.IsValid())
115           Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
116       }
117     }
118     else {
119       StdFail_NotDone::Raise("Offset construction failed");
120     }
121   } 
122   else if (aType == OFFSET_THICKENING || aType == OFFSET_THICKENING_COPY)
123   {
124     BRepClass3d_SolidClassifier aClassifier = BRepClass3d_SolidClassifier(aShapeBase);
125     aClassifier.PerformInfinitePoint(Precision::Confusion());
126     if (aClassifier.State()==TopAbs_IN)
127     {
128       // If the generated pipe faces normals are oriented towards the inside, the offset is negative
129       // so that the thickening is still towards outside
130       anOffset=-anOffset;
131     }
132
133     BRepOffset_MakeOffset myOffsetShape(aShapeBase, anOffset, aTol, BRepOffset_Skin,
134                                         Standard_False, Standard_False, GeomAbs_Intersection, Standard_True);
135   
136     if (!myOffsetShape.IsDone())
137     {
138       StdFail_NotDone::Raise("Thickening construction failed");
139     }
140     aShape = myOffsetShape.Shape();
141     
142     // Control the solid orientation. This is mostly done to fix a bug in case of extrusion
143     // of a circle. The built solid is then badly oriented
144     BRepClass3d_SolidClassifier anotherClassifier = BRepClass3d_SolidClassifier(aShape);
145     anotherClassifier.PerformInfinitePoint(Precision::Confusion());
146     if (anotherClassifier.State()==TopAbs_IN)
147     {
148       aShape.Reverse();
149     }
150   }
151
152   if (aShape.IsNull()) return 0;
153
154   aFunction->SetValue(aShape);
155
156   log.SetTouched(Label());
157
158   return 1;
159 }
160
161
162 //=======================================================================
163 //function :  GEOMImpl_OffsetDriver_Type_
164 //purpose  :
165 //=======================================================================
166 Standard_EXPORT Handle_Standard_Type& GEOMImpl_OffsetDriver_Type_()
167 {
168
169   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
170   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
171   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
172   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
173   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
174   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
175
176
177   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
178   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_OffsetDriver",
179                                                          sizeof(GEOMImpl_OffsetDriver),
180                                                          1,
181                                                          (Standard_Address)_Ancestors,
182                                                          (Standard_Address)NULL);
183
184   return _aType;
185 }
186
187 //=======================================================================
188 //function : DownCast
189 //purpose  :
190 //=======================================================================
191 const Handle(GEOMImpl_OffsetDriver) Handle(GEOMImpl_OffsetDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
192 {
193   Handle(GEOMImpl_OffsetDriver) _anOtherObject;
194
195   if (!AnObject.IsNull()) {
196      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_OffsetDriver))) {
197        _anOtherObject = Handle(GEOMImpl_OffsetDriver)((Handle(GEOMImpl_OffsetDriver)&)AnObject);
198      }
199   }
200
201   return _anOtherObject ;
202 }