Salome HOME
112966a3f276b1f5547febd55111bccb3bf11e86
[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_Boolean isInside = aCI.GetParam();
77   Standard_Real aTol = Precision::Confusion();
78
79   if (isInside)
80     anOffset = -anOffset;
81
82   if (Abs(anOffset) < aTol) {
83     TCollection_AsciiString aMsg ("Absolute value of offset can not be less than the tolerance value (");
84     aMsg += TCollection_AsciiString(aTol);
85     aMsg += ")";
86     StdFail_NotDone::Raise(aMsg.ToCString());
87   }
88
89   if (aType == OFFSET_SHAPE || aType == OFFSET_SHAPE_COPY) {
90     BRepOffsetAPI_MakeOffsetShape MO (aShapeBase,
91                                       aCI.GetValue(),
92                                       aTol);
93     if (MO.IsDone()) {
94       aShape = MO.Shape();
95       if ( !GEOMUtils::CheckShape(aShape, true) && !GEOMUtils::FixShapeTolerance(aShape) )
96         Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
97     }
98     else {
99       StdFail_NotDone::Raise("Offset construction failed");
100     }
101   }
102   else if (aType == OFFSET_THICKENING || aType == OFFSET_THICKENING_COPY)
103   {
104     const TopAbs_ShapeEnum aType = aShapeBase.ShapeType();
105
106     if (aType == TopAbs_FACE || aType == TopAbs_SHELL) {
107       // Create a thick solid.
108       BRepClass3d_SolidClassifier aClassifier = BRepClass3d_SolidClassifier(aShapeBase);
109       aClassifier.PerformInfinitePoint(Precision::Confusion());
110       if (aClassifier.State()==TopAbs_IN)
111       {
112         // If the generated pipe faces normals are oriented towards the inside, the offset is negative
113         // so that the thickening is still towards outside
114         anOffset=-anOffset;
115       }
116
117       BRepOffset_MakeOffset myOffsetShape(aShapeBase, anOffset, aTol, BRepOffset_Skin,
118                                           Standard_False, Standard_False, GeomAbs_Intersection, Standard_True);
119
120       if (!myOffsetShape.IsDone())
121       {
122         StdFail_NotDone::Raise("Thickening construction failed");
123       }
124       aShape = myOffsetShape.Shape();
125
126       // Control the solid orientation. This is mostly done to fix a bug in case of extrusion
127       // of a circle. The built solid is then badly oriented
128       BRepClass3d_SolidClassifier anotherClassifier = BRepClass3d_SolidClassifier(aShape);
129       anotherClassifier.PerformInfinitePoint(Precision::Confusion());
130       if (anotherClassifier.State()==TopAbs_IN)
131       {
132         aShape.Reverse();
133       }
134     } else if (aType == TopAbs_SOLID) {
135       // Create a hollowed solid.
136       Handle(TColStd_HArray1OfInteger) aFacesIDs = aCI.GetFaceIDs();
137       TopTools_ListOfShape aFacesToRm;
138
139       if (aFacesIDs.IsNull()) {
140         return 0;
141       }
142
143       TopTools_IndexedMapOfShape anIndices;
144
145       TopExp::MapShapes(aShapeBase, anIndices);
146
147       Standard_Integer aNbShapes = anIndices.Extent();
148       Standard_Integer i;
149
150       for (i = aFacesIDs->Lower(); i <= aFacesIDs->Upper(); ++i) {
151         const Standard_Integer anIndex = aFacesIDs->Value(i);
152
153         if (anIndex < 1 || anIndex > aNbShapes) {
154           // Invalid index.
155           return 0;
156         }
157
158         const TopoDS_Shape &aFace = anIndices.FindKey(anIndex);
159
160         if (aFace.ShapeType() != TopAbs_FACE) {
161           // Shape by index is not a face.
162           return 0;
163         }
164
165         aFacesToRm.Append(aFace);
166       }
167
168       // Create a hollowed solid.
169       BRepOffsetAPI_MakeThickSolid aMkSolid
170                   (aShapeBase, aFacesToRm, anOffset, aTol, BRepOffset_Skin,
171                    Standard_False, Standard_False, GeomAbs_Intersection);
172
173       if (aMkSolid.IsDone()) {
174         aShape = aMkSolid.Shape();
175       }
176     }
177   }
178
179   if (aShape.IsNull()) return 0;
180
181   aFunction->SetValue(aShape);
182
183   log.SetTouched(Label());
184
185   return 1;
186 }
187
188 //================================================================================
189 /*!
190  * \brief Returns a name of creation operation and names and values of creation parameters
191  */
192 //================================================================================
193
194 bool GEOMImpl_OffsetDriver::
195 GetCreationInformation(std::string&             theOperationName,
196                        std::vector<GEOM_Param>& theParams)
197 {
198   if (Label().IsNull()) return 0;
199   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
200
201   GEOMImpl_IOffset aCI( function );
202   Standard_Integer aType = function->GetType();
203
204   switch ( aType ) {
205   case OFFSET_SHAPE:
206   case OFFSET_SHAPE_COPY:
207     theOperationName = "OFFSET";
208     AddParam( theParams, "Object", aCI.GetShape() );
209     AddParam( theParams, "Offset", aCI.GetValue() );
210     break;
211   case OFFSET_THICKENING:
212   case OFFSET_THICKENING_COPY:
213     theOperationName = "THICKNESS";
214     AddParam( theParams, "Object", aCI.GetShape() );
215     AddParam( theParams, "Offset", aCI.GetParam() ? -aCI.GetValue() : aCI.GetValue() );
216     {
217       Handle(TColStd_HArray1OfInteger) aFacesIDs = aCI.GetFaceIDs();
218
219       if (aFacesIDs.IsNull() == Standard_False) {
220         AddParam(theParams, "Faces IDs", aFacesIDs);
221       }
222     }
223     break;
224   default:
225     return false;
226   }
227   
228   return true;
229 }
230
231 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_OffsetDriver,GEOM_BaseDriver);
232 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_OffsetDriver,GEOM_BaseDriver);