Salome HOME
Merge branch occ/shape_reparation_2
[modules/geom.git] / src / GEOMImpl / GEOMImpl_OffsetDriver.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_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 <TopoDS_Shape.hxx>
33 #include <TopAbs.hxx>
34 #include <BRepClass3d_SolidClassifier.hxx>
35 #include <Precision.hxx>
36 #include <Standard_ConstructionError.hxx>
37 #include <StdFail_NotDone.hxx>
38
39 //=======================================================================
40 //function : GetID
41 //purpose  :
42 //=======================================================================
43 const Standard_GUID& GEOMImpl_OffsetDriver::GetID()
44 {
45   static Standard_GUID aOffsetDriver("FF1BBB51-5D14-4df2-980B-3A668264EA16");
46   return aOffsetDriver;
47 }
48
49 //=======================================================================
50 //function : GEOMImpl_OffsetDriver
51 //purpose  :
52 //=======================================================================
53 GEOMImpl_OffsetDriver::GEOMImpl_OffsetDriver()
54 {
55 }
56
57 //=======================================================================
58 //function : Execute
59 //purpose  :
60 //=======================================================================
61 Standard_Integer GEOMImpl_OffsetDriver::Execute(TFunction_Logbook& log) const
62 {
63   if (Label().IsNull()) return 0;
64   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
65
66   GEOMImpl_IOffset aCI (aFunction);
67   Standard_Integer aType = aFunction->GetType();
68
69   TopoDS_Shape aShape;
70
71   Handle(GEOM_Function) aRefShape = aCI.GetShape();
72   TopoDS_Shape aShapeBase = aRefShape->GetValue();
73   Standard_Real anOffset = aCI.GetValue();
74   Standard_Real aTol = Precision::Confusion();
75
76   if (Abs(anOffset) < aTol) {
77     TCollection_AsciiString aMsg ("Absolute value of offset can not be less than the tolerance value (");
78     aMsg += TCollection_AsciiString(aTol);
79     aMsg += ")";
80     StdFail_NotDone::Raise(aMsg.ToCString());
81   }
82
83   if (aType == OFFSET_SHAPE || aType == OFFSET_SHAPE_COPY) {
84     BRepOffsetAPI_MakeOffsetShape MO (aShapeBase,
85                                       aCI.GetValue(),
86                                       aTol);
87     if (MO.IsDone()) {
88       aShape = MO.Shape();
89       if ( !GEOMUtils::CheckShape(aShape, true) && !GEOMUtils::FixShapeTolerance(aShape) )
90         Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
91     }
92     else {
93       StdFail_NotDone::Raise("Offset construction failed");
94     }
95   }
96   else if (aType == OFFSET_THICKENING || aType == OFFSET_THICKENING_COPY)
97   {
98     BRepClass3d_SolidClassifier aClassifier = BRepClass3d_SolidClassifier(aShapeBase);
99     aClassifier.PerformInfinitePoint(Precision::Confusion());
100     if (aClassifier.State()==TopAbs_IN)
101     {
102       // If the generated pipe faces normals are oriented towards the inside, the offset is negative
103       // so that the thickening is still towards outside
104       anOffset=-anOffset;
105     }
106
107     BRepOffset_MakeOffset myOffsetShape(aShapeBase, anOffset, aTol, BRepOffset_Skin,
108                                         Standard_False, Standard_False, GeomAbs_Intersection, Standard_True);
109
110     if (!myOffsetShape.IsDone())
111     {
112       StdFail_NotDone::Raise("Thickening construction failed");
113     }
114     aShape = myOffsetShape.Shape();
115
116     // Control the solid orientation. This is mostly done to fix a bug in case of extrusion
117     // of a circle. The built solid is then badly oriented
118     BRepClass3d_SolidClassifier anotherClassifier = BRepClass3d_SolidClassifier(aShape);
119     anotherClassifier.PerformInfinitePoint(Precision::Confusion());
120     if (anotherClassifier.State()==TopAbs_IN)
121     {
122       aShape.Reverse();
123     }
124   }
125
126   if (aShape.IsNull()) return 0;
127
128   aFunction->SetValue(aShape);
129
130   log.SetTouched(Label());
131
132   return 1;
133 }
134
135 //================================================================================
136 /*!
137  * \brief Returns a name of creation operation and names and values of creation parameters
138  */
139 //================================================================================
140
141 bool GEOMImpl_OffsetDriver::
142 GetCreationInformation(std::string&             theOperationName,
143                        std::vector<GEOM_Param>& theParams)
144 {
145   if (Label().IsNull()) return 0;
146   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
147
148   GEOMImpl_IOffset aCI( function );
149   Standard_Integer aType = function->GetType();
150
151   switch ( aType ) {
152   case OFFSET_SHAPE:
153   case OFFSET_SHAPE_COPY:
154     theOperationName = "OFFSET";
155     AddParam( theParams, "Object", aCI.GetShape() );
156     AddParam( theParams, "Offset", aCI.GetValue() );
157     break;
158   case OFFSET_THICKENING:
159   case OFFSET_THICKENING_COPY:
160     theOperationName = "MakeThickening";
161     AddParam( theParams, "Object", aCI.GetShape() );
162     AddParam( theParams, "Offset", aCI.GetValue() );
163     break;
164   default:
165     return false;
166   }
167   
168   return true;
169 }
170
171 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_OffsetDriver,GEOM_BaseDriver);
172 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_OffsetDriver,GEOM_BaseDriver);