Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/geom.git] / src / GEOMImpl / GEOMImpl_TranslateDriver.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_TranslateDriver.hxx>
26 #include <GEOMImpl_ITranslate.hxx>
27 #include <GEOMImpl_ITransformOperations.hxx>
28 #include <GEOMImpl_Types.hxx>
29 #include <GEOM_Function.hxx>
30
31 #include <ShapeFix_Shape.hxx>
32 #include <ShapeFix_ShapeTolerance.hxx>
33
34 #include <BRep_Tool.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRepCheck_Analyzer.hxx>
37 #include <BRepBuilderAPI_Transform.hxx>
38
39 #include <TopoDS.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <TopAbs.hxx>
44 #include <TopExp.hxx>
45 #include <TopoDS_Vertex.hxx>
46 #include <TopoDS_Edge.hxx>
47
48 #include <gp_Trsf.hxx>
49 #include <gp_Pnt.hxx>
50 #include <gp_Vec.hxx>
51
52 //=======================================================================
53 //function : GetID
54 //purpose  :
55 //=======================================================================
56 const Standard_GUID& GEOMImpl_TranslateDriver::GetID()
57 {
58   static Standard_GUID aTranslateDriver("FF1BBB03-5D14-4df2-980B-3A668264EA16");
59   return aTranslateDriver;
60 }
61
62
63 //=======================================================================
64 //function : GEOMImpl_TranslateDriver
65 //purpose  :
66 //=======================================================================
67
68 GEOMImpl_TranslateDriver::GEOMImpl_TranslateDriver()
69 {
70 }
71
72 //=======================================================================
73 //function : Execute
74 //purpose  :
75 //=======================================================================
76 Standard_Integer GEOMImpl_TranslateDriver::Execute(TFunction_Logbook& log) const
77 {
78   if (Label().IsNull()) return 0;
79   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
80
81   if (aFunction.IsNull()) return 0;
82
83   GEOMImpl_ITranslate TI (aFunction);
84   gp_Trsf aTrsf;
85   gp_Pnt aP1, aP2;
86   Standard_Integer aType = aFunction->GetType();
87
88   Handle(GEOM_Function) anOriginalFunction = TI.GetOriginal();
89   if (anOriginalFunction.IsNull()) return 0;
90   TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
91   if (anOriginal.IsNull()) return 0;
92
93   if (aType == TRANSLATE_TWO_POINTS || aType == TRANSLATE_TWO_POINTS_COPY) {
94     Handle(GEOM_Function) aPoint1 = TI.GetPoint1();
95     Handle(GEOM_Function) aPoint2 = TI.GetPoint2();
96     if(aPoint1.IsNull() || aPoint2.IsNull()) return 0;
97     TopoDS_Shape aV1 = aPoint1->GetValue();
98     TopoDS_Shape aV2 = aPoint2->GetValue();
99     if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
100     if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
101
102     aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
103     aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
104
105     aTrsf.SetTranslation(aP1, aP2);
106     //NPAL18620: performance problem: multiple locations are accumulated
107     //           in shape and need a great time to process
108     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
109     //aShape = aTransformation.Shape();
110     TopLoc_Location aLocOrig = anOriginal.Location();
111     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
112     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
113     aShape = anOriginal.Located(aLocRes);
114   }
115   else if (aType == TRANSLATE_VECTOR || aType == TRANSLATE_VECTOR_COPY) {
116     Handle(GEOM_Function) aVector = TI.GetVector();
117     if(aVector.IsNull()) return 0;
118     TopoDS_Shape aV = aVector->GetValue();
119     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
120     TopoDS_Edge anEdge = TopoDS::Edge(aV);
121
122     aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
123     aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
124
125     aTrsf.SetTranslation(aP1, aP2);
126     //NPAL18620: performance problem: multiple locations are accumulated
127     //           in shape and need a great time to process
128     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
129     //aShape = aTransformation.Shape();
130     TopLoc_Location aLocOrig = anOriginal.Location();
131     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
132     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
133     aShape = anOriginal.Located(aLocRes);
134   }
135   else if (aType == TRANSLATE_VECTOR_DISTANCE) {
136     Handle(GEOM_Function) aVector = TI.GetVector();
137     double aDistance = TI.GetDistance();
138     if(aVector.IsNull()) return 0;
139     TopoDS_Shape aV = aVector->GetValue();
140     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
141     TopoDS_Edge anEdge = TopoDS::Edge(aV);
142
143     aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
144     aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
145
146     gp_Vec aVec (aP1, aP2);
147     aVec.Normalize();
148     aTrsf.SetTranslation(aVec * aDistance);
149
150     TopLoc_Location aLocOrig = anOriginal.Location();
151     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
152     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
153     aShape = anOriginal.Located(aLocRes);
154   }
155   else if (aType == TRANSLATE_XYZ || aType == TRANSLATE_XYZ_COPY) {
156     gp_Vec aVec (TI.GetDX(), TI.GetDY(), TI.GetDZ());
157     aTrsf.SetTranslation(aVec);
158     //NPAL18620: performance problem: multiple locations are accumulated
159     //           in shape and need a great time to process
160     //BRepBuilderAPI_Transform aTransformation (anOriginal, aTrsf, Standard_False);
161     //aShape = aTransformation.Shape();
162     TopLoc_Location aLocOrig = anOriginal.Location();
163     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
164     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
165     aShape = anOriginal.Located(aLocRes);
166   }
167   else if (aType == TRANSLATE_1D) {
168     Standard_Real DX, DY, DZ, step = TI.GetStep1();
169     Standard_Integer nbtimes = TI.GetNbIter1();
170     gp_Vec aVec;
171     TopoDS_Compound aCompound;
172     BRep_Builder B;
173     B.MakeCompound( aCompound );
174
175     Handle(GEOM_Function) aVector = TI.GetVector();
176     if(aVector.IsNull()) return 0;
177     TopoDS_Shape aV = aVector->GetValue();
178     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
179     TopoDS_Edge anEdge = TopoDS::Edge(aV);
180
181     gp_Vec Vec(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
182     Vec.Normalize();
183
184     TopLoc_Location aLocOrig = anOriginal.Location();
185     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
186
187     for (int i = 0; i < nbtimes; i++) {
188       DX = i * step * Vec.X();
189       DY = i * step * Vec.Y();
190       DZ = i * step * Vec.Z();
191       aVec.SetCoord( DX, DY, DZ );
192       aTrsf.SetTranslation(aVec);
193       //NPAL18620: performance problem: multiple locations are accumulated
194       //           in shape and need a great time to process
195       //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
196       //B.Add(aCompound, aTransformation.Shape());
197       TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
198       B.Add(aCompound, anOriginal.Located(aLocRes));
199     }
200     aShape = aCompound;
201     //aShape = GEOMImpl_ITransformOperations::TranslateShape1D(anOriginal, &TI);
202   }
203   else if (aType == TRANSLATE_2D) {
204     Standard_Integer nbtimes1 = TI.GetNbIter1(), nbtimes2 = TI.GetNbIter2();
205     Standard_Real DX, DY, DZ,  step1 = TI.GetStep1(),  step2 = TI.GetStep2();
206     gp_Vec aVec;
207     Handle(GEOM_Function) aVector = TI.GetVector();
208     if(aVector.IsNull()) return 0;
209     TopoDS_Shape aV = aVector->GetValue();
210     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
211     TopoDS_Edge anEdge = TopoDS::Edge(aV);
212
213     gp_Vec Vec1(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
214     Vec1.Normalize();
215
216     Handle(GEOM_Function) aVector2 = TI.GetVector2();
217     if(aVector2.IsNull()) return 0;
218     aV = aVector2->GetValue();
219     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
220     anEdge = TopoDS::Edge(aV);
221
222     gp_Vec Vec2(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
223     Vec2.Normalize();
224
225     TopoDS_Compound aCompound;
226     BRep_Builder B;
227     B.MakeCompound( aCompound );
228
229     TopLoc_Location aLocOrig = anOriginal.Location();
230     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
231
232     for (int i = 0; i < nbtimes1; i++) {
233       for (int j = 0; j < nbtimes2; j++) {
234         DX = i * step1 * Vec1.X() + j * step2 * Vec2.X();
235         DY = i * step1 * Vec1.Y() + j * step2 * Vec2.Y();
236         DZ = i * step1 * Vec1.Z() + j * step2 * Vec2.Z();
237         aVec.SetCoord( DX, DY, DZ );
238         aTrsf.SetTranslation(aVec);
239         //NPAL18620: performance problem: multiple locations are accumulated
240         //           in shape and need a great time to process
241         //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
242         //B.Add(aCompound, aBRepTransformation.Shape());
243         TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
244         B.Add(aCompound, anOriginal.Located(aLocRes));
245       }
246     }
247     aShape = aCompound;
248     //aShape = GEOMImpl_ITransformOperations::TranslateShape2D(anOriginal, &TI);
249   }
250   else return 0;
251
252   if (aShape.IsNull()) return 0;
253
254   BRepCheck_Analyzer ana (aShape, Standard_True);
255   if (!ana.IsValid()) {
256     ShapeFix_ShapeTolerance aSFT;
257     aSFT.LimitTolerance(aShape,Precision::Confusion(),Precision::Confusion());
258     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
259     aSfs->SetPrecision(Precision::Confusion());
260     aSfs->Perform();
261     aShape = aSfs->Shape();
262
263     ana.Init(aShape, Standard_False);
264     if (!ana.IsValid())
265       Standard_ConstructionError::Raise("Scaling aborted : algorithm has produced an invalid shape result");
266   }
267
268   aFunction->SetValue(aShape);
269
270   log.SetTouched(Label());
271
272   return 1;
273 }
274
275
276 //=======================================================================
277 //function :  GEOMImpl_TranslateDriver_Type_
278 //purpose  :
279 //=======================================================================
280 Standard_EXPORT Handle_Standard_Type& GEOMImpl_TranslateDriver_Type_()
281 {
282
283   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
284   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
285   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
286   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
287   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
288   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
289
290   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
291   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_TranslateDriver",
292                                                          sizeof(GEOMImpl_TranslateDriver),
293                                                          1,
294                                                          (Standard_Address)_Ancestors,
295                                                          (Standard_Address)NULL);
296
297   return _aType;
298 }
299
300 //=======================================================================
301 //function : DownCast
302 //purpose  :
303 //=======================================================================
304 const Handle(GEOMImpl_TranslateDriver) Handle(GEOMImpl_TranslateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
305 {
306   Handle(GEOMImpl_TranslateDriver) _anOtherObject;
307
308   if (!AnObject.IsNull()) {
309      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_TranslateDriver))) {
310        _anOtherObject = Handle(GEOMImpl_TranslateDriver)((Handle(GEOMImpl_TranslateDriver)&)AnObject);
311      }
312   }
313
314   return _anOtherObject;
315 }