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