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