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