Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/geom.git] / src / GEOMImpl / GEOMImpl_OffsetDriver.cxx
1 // Copyright (C) 2007-2012  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 <Precision.hxx>
39 #include <gp_Pnt.hxx>
40
41 #include <BRepCheck_Analyzer.hxx>
42 #include <ShapeFix_ShapeTolerance.hxx>
43 #include <ShapeFix_Shape.hxx>
44
45 #include <Standard_ConstructionError.hxx>
46 #include <StdFail_NotDone.hxx>
47
48 //=======================================================================
49 //function : GetID
50 //purpose  :
51 //=======================================================================
52 const Standard_GUID& GEOMImpl_OffsetDriver::GetID()
53 {
54   static Standard_GUID aOffsetDriver("FF1BBB51-5D14-4df2-980B-3A668264EA16");
55   return aOffsetDriver;
56 }
57
58
59 //=======================================================================
60 //function : GEOMImpl_OffsetDriver
61 //purpose  :
62 //=======================================================================
63 GEOMImpl_OffsetDriver::GEOMImpl_OffsetDriver()
64 {
65 }
66
67 //=======================================================================
68 //function : Execute
69 //purpose  :
70 //=======================================================================
71 Standard_Integer GEOMImpl_OffsetDriver::Execute(TFunction_Logbook& log) const
72 {
73   if (Label().IsNull()) return 0;
74   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
75
76   GEOMImpl_IOffset aCI (aFunction);
77   Standard_Integer aType = aFunction->GetType();
78
79   TopoDS_Shape aShape;
80
81   if (aType == OFFSET_SHAPE || aType == OFFSET_SHAPE_COPY) {
82     Handle(GEOM_Function) aRefShape = aCI.GetShape();
83     TopoDS_Shape aShapeBase = aRefShape->GetValue();
84     Standard_Real anOffset = aCI.GetValue();
85     Standard_Real aTol = Precision::Confusion();
86
87     if (Abs(anOffset) < aTol) {
88       TCollection_AsciiString aMsg ("Absolute value of offset can not be less than the tolerance value (");
89       aMsg += TCollection_AsciiString(aTol);
90       aMsg += ")";
91       StdFail_NotDone::Raise(aMsg.ToCString());
92     }
93
94     BRepOffsetAPI_MakeOffsetShape MO (aShapeBase,
95                                       aCI.GetValue(),
96                                       aTol);
97     if (MO.IsDone()) {
98       aShape = MO.Shape();
99       // 23.04.2010 skl for bug 21699 from Mantis
100       BRepCheck_Analyzer ana (aShape, Standard_True);
101       ana.Init(aShape);
102       if (!ana.IsValid()) {
103         ShapeFix_ShapeTolerance aSFT;
104         aSFT.LimitTolerance(aShape, Precision::Confusion(),
105                             Precision::Confusion(), TopAbs_SHAPE);
106         Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
107         aSfs->Perform();
108         aShape = aSfs->Shape();
109         ana.Init(aShape);
110         if (!ana.IsValid())
111           Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
112       }
113     }
114     else {
115       StdFail_NotDone::Raise("Offset construction failed");
116     }
117   } else {
118   }
119
120   if (aShape.IsNull()) return 0;
121
122   aFunction->SetValue(aShape);
123
124   log.SetTouched(Label());
125
126   return 1;
127 }
128
129
130 //=======================================================================
131 //function :  GEOMImpl_OffsetDriver_Type_
132 //purpose  :
133 //=======================================================================
134 Standard_EXPORT Handle_Standard_Type& GEOMImpl_OffsetDriver_Type_()
135 {
136
137   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
138   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
139   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
140   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
141   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
142   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
143
144
145   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
146   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_OffsetDriver",
147                                                          sizeof(GEOMImpl_OffsetDriver),
148                                                          1,
149                                                          (Standard_Address)_Ancestors,
150                                                          (Standard_Address)NULL);
151
152   return _aType;
153 }
154
155 //=======================================================================
156 //function : DownCast
157 //purpose  :
158 //=======================================================================
159 const Handle(GEOMImpl_OffsetDriver) Handle(GEOMImpl_OffsetDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
160 {
161   Handle(GEOMImpl_OffsetDriver) _anOtherObject;
162
163   if (!AnObject.IsNull()) {
164      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_OffsetDriver))) {
165        _anOtherObject = Handle(GEOMImpl_OffsetDriver)((Handle(GEOMImpl_OffsetDriver)&)AnObject);
166      }
167   }
168
169   return _anOtherObject ;
170 }