]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_OffsetDriver.cxx
Salome HOME
0022666: EDF 7253 GEOM: Add thickness to a shell and integrate BrepOffsetAPI_MakeThic...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_OffsetDriver.cxx
1 // Copyright (C) 2007-2015  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_OffsetDriver.hxx>
26 #include <GEOMImpl_IOffset.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29 #include <GEOMUtils.hxx>
30
31 #include <BRepOffsetAPI_MakeOffsetShape.hxx>
32 #include <BRepOffsetAPI_MakeThickSolid.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopAbs.hxx>
35 #include <TopExp.hxx>
36 #include <BRepClass3d_SolidClassifier.hxx>
37 #include <Precision.hxx>
38 #include <Standard_ConstructionError.hxx>
39 #include <StdFail_NotDone.hxx>
40
41 //=======================================================================
42 //function : GetID
43 //purpose  :
44 //=======================================================================
45 const Standard_GUID& GEOMImpl_OffsetDriver::GetID()
46 {
47   static Standard_GUID aOffsetDriver("FF1BBB51-5D14-4df2-980B-3A668264EA16");
48   return aOffsetDriver;
49 }
50
51 //=======================================================================
52 //function : GEOMImpl_OffsetDriver
53 //purpose  :
54 //=======================================================================
55 GEOMImpl_OffsetDriver::GEOMImpl_OffsetDriver()
56 {
57 }
58
59 //=======================================================================
60 //function : Execute
61 //purpose  :
62 //=======================================================================
63 Standard_Integer GEOMImpl_OffsetDriver::Execute(TFunction_Logbook& log) const
64 {
65   if (Label().IsNull()) return 0;
66   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
67
68   GEOMImpl_IOffset aCI (aFunction);
69   Standard_Integer aType = aFunction->GetType();
70
71   TopoDS_Shape aShape;
72
73   Handle(GEOM_Function) aRefShape = aCI.GetShape();
74   TopoDS_Shape aShapeBase = aRefShape->GetValue();
75   Standard_Real anOffset = aCI.GetValue();
76   Standard_Real aTol = Precision::Confusion();
77
78   if (Abs(anOffset) < aTol) {
79     TCollection_AsciiString aMsg ("Absolute value of offset can not be less than the tolerance value (");
80     aMsg += TCollection_AsciiString(aTol);
81     aMsg += ")";
82     StdFail_NotDone::Raise(aMsg.ToCString());
83   }
84
85   if (aType == OFFSET_SHAPE || aType == OFFSET_SHAPE_COPY) {
86     BRepOffsetAPI_MakeOffsetShape MO (aShapeBase,
87                                       aCI.GetValue(),
88                                       aTol);
89     if (MO.IsDone()) {
90       aShape = MO.Shape();
91       if ( !GEOMUtils::CheckShape(aShape, true) && !GEOMUtils::FixShapeTolerance(aShape) )
92         Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
93     }
94     else {
95       StdFail_NotDone::Raise("Offset construction failed");
96     }
97   }
98   else if (aType == OFFSET_THICKENING || aType == OFFSET_THICKENING_COPY)
99   {
100     const TopAbs_ShapeEnum aType = aShapeBase.ShapeType();
101
102     if (aType == TopAbs_FACE || aType == TopAbs_SHELL) {
103       // Create a thick solid.
104       BRepClass3d_SolidClassifier aClassifier = BRepClass3d_SolidClassifier(aShapeBase);
105       aClassifier.PerformInfinitePoint(Precision::Confusion());
106       if (aClassifier.State()==TopAbs_IN)
107       {
108         // If the generated pipe faces normals are oriented towards the inside, the offset is negative
109         // so that the thickening is still towards outside
110         anOffset=-anOffset;
111       }
112
113       BRepOffset_MakeOffset myOffsetShape(aShapeBase, anOffset, aTol, BRepOffset_Skin,
114                                           Standard_False, Standard_False, GeomAbs_Intersection, Standard_True);
115
116       if (!myOffsetShape.IsDone())
117       {
118         StdFail_NotDone::Raise("Thickening construction failed");
119       }
120       aShape = myOffsetShape.Shape();
121
122       // Control the solid orientation. This is mostly done to fix a bug in case of extrusion
123       // of a circle. The built solid is then badly oriented
124       BRepClass3d_SolidClassifier anotherClassifier = BRepClass3d_SolidClassifier(aShape);
125       anotherClassifier.PerformInfinitePoint(Precision::Confusion());
126       if (anotherClassifier.State()==TopAbs_IN)
127       {
128         aShape.Reverse();
129       }
130     } else if (aType == TopAbs_SOLID) {
131       // Create a hollowed solid.
132       Handle(TColStd_HArray1OfInteger) aFacesIDs = aCI.GetFaceIDs();
133       TopTools_ListOfShape aFacesToRm;
134
135       if (aFacesIDs.IsNull()) {
136         return 0;
137       }
138
139       TopTools_IndexedMapOfShape anIndices;
140
141       TopExp::MapShapes(aShapeBase, anIndices);
142
143       Standard_Integer aNbShapes = anIndices.Extent();
144       Standard_Integer i;
145
146       for (i = aFacesIDs->Lower(); i <= aFacesIDs->Upper(); ++i) {
147         const Standard_Integer anIndex = aFacesIDs->Value(i);
148
149         if (anIndex < 1 || anIndex > aNbShapes) {
150           // Invalid index.
151           return 0;
152         }
153
154         const TopoDS_Shape &aFace = anIndices.FindKey(anIndex);
155
156         if (aFace.ShapeType() != TopAbs_FACE) {
157           // Shape by index is not a face.
158           return 0;
159         }
160
161         aFacesToRm.Append(aFace);
162       }
163
164       // Create a hollowed solid.
165       BRepOffsetAPI_MakeThickSolid aMkSolid
166                   (aShapeBase, aFacesToRm, anOffset, aTol, BRepOffset_Skin,
167                    Standard_False, Standard_False, GeomAbs_Intersection);
168
169       if (aMkSolid.IsDone()) {
170         aShape = aMkSolid.Shape();
171       }
172     }
173   }
174
175   if (aShape.IsNull()) return 0;
176
177   aFunction->SetValue(aShape);
178
179   log.SetTouched(Label());
180
181   return 1;
182 }
183
184 //================================================================================
185 /*!
186  * \brief Returns a name of creation operation and names and values of creation parameters
187  */
188 //================================================================================
189
190 bool GEOMImpl_OffsetDriver::
191 GetCreationInformation(std::string&             theOperationName,
192                        std::vector<GEOM_Param>& theParams)
193 {
194   if (Label().IsNull()) return 0;
195   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
196
197   GEOMImpl_IOffset aCI( function );
198   Standard_Integer aType = function->GetType();
199
200   switch ( aType ) {
201   case OFFSET_SHAPE:
202   case OFFSET_SHAPE_COPY:
203     theOperationName = "OFFSET";
204     AddParam( theParams, "Object", aCI.GetShape() );
205     AddParam( theParams, "Offset", aCI.GetValue() );
206     break;
207   case OFFSET_THICKENING:
208   case OFFSET_THICKENING_COPY:
209     theOperationName = "MakeThickening";
210     AddParam( theParams, "Object", aCI.GetShape() );
211     AddParam( theParams, "Offset", aCI.GetValue() );
212     {
213       Handle(TColStd_HArray1OfInteger) aFacesIDs = aCI.GetFaceIDs();
214
215       if (aFacesIDs.IsNull() == Standard_False) {
216         AddParam(theParams, "Faces IDs", aFacesIDs);
217       }
218     }
219     break;
220   default:
221     return false;
222   }
223   
224   return true;
225 }
226
227 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_OffsetDriver,GEOM_BaseDriver);
228 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_OffsetDriver,GEOM_BaseDriver);