Salome HOME
Workaround for bugs 0019899, 0019908 and 0019910 from Mantis.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_TranslateDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_TranslateDriver.hxx>
24 #include <GEOMImpl_ITranslate.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27 #include <gp_Trsf.hxx>
28 #include <gp_Pnt.hxx>
29 #include <gp_Vec.hxx>
30 #include <BRepBuilderAPI_Transform.hxx>
31 #include <BRep_Builder.hxx>
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 #include <TopoDS_Vertex.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <BRep_Tool.hxx>
41
42 //=======================================================================
43 //function : GetID
44 //purpose  :
45 //=======================================================================
46 const Standard_GUID& GEOMImpl_TranslateDriver::GetID()
47 {
48   static Standard_GUID aTranslateDriver("FF1BBB03-5D14-4df2-980B-3A668264EA16");
49   return aTranslateDriver;
50 }
51
52
53 //=======================================================================
54 //function : GEOMImpl_TranslateDriver
55 //purpose  :
56 //=======================================================================
57
58 GEOMImpl_TranslateDriver::GEOMImpl_TranslateDriver()
59 {
60 }
61
62 //=======================================================================
63 //function : Execute
64 //purpose  :
65 //=======================================================================
66 Standard_Integer GEOMImpl_TranslateDriver::Execute(TFunction_Logbook& log) const
67 {
68   if (Label().IsNull()) return 0;
69   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
70
71   if (aFunction.IsNull()) return 0;
72
73   GEOMImpl_ITranslate TI(aFunction);
74   gp_Trsf aTrsf;
75   gp_Pnt aP1, aP2;
76   Standard_Integer aType = aFunction->GetType();
77
78   Handle(GEOM_Function) anOriginalFunction = TI.GetOriginal();
79   if (anOriginalFunction.IsNull()) return 0;
80   TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
81   if (anOriginal.IsNull()) return 0;
82
83   if (aType == TRANSLATE_TWO_POINTS || aType == TRANSLATE_TWO_POINTS_COPY) {
84     Handle(GEOM_Function) aPoint1 = TI.GetPoint1();
85     Handle(GEOM_Function) aPoint2 = TI.GetPoint2();
86     if(aPoint1.IsNull() || aPoint2.IsNull()) return 0;
87     TopoDS_Shape aV1 = aPoint1->GetValue();
88     TopoDS_Shape aV2 = aPoint2->GetValue();
89     if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
90     if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
91
92     aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
93     aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
94
95     aTrsf.SetTranslation(aP1, aP2);
96     //NPAL18620: performance problem: multiple locations are accumulated
97     //           in shape and need a great time to process
98     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
99     //aShape = aTransformation.Shape();
100     TopLoc_Location aLocOrig = anOriginal.Location();
101     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
102     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
103     aShape = anOriginal.Located(aLocRes);
104   }
105   else if (aType == TRANSLATE_VECTOR || aType == TRANSLATE_VECTOR_COPY) {
106     Handle(GEOM_Function) aVector = TI.GetVector();
107     if(aVector.IsNull()) return 0;
108     TopoDS_Shape aV = aVector->GetValue();
109     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
110     TopoDS_Edge anEdge = TopoDS::Edge(aV);
111
112     aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
113     aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
114
115     aTrsf.SetTranslation(aP1, aP2);
116     //NPAL18620: performance problem: multiple locations are accumulated
117     //           in shape and need a great time to process
118     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
119     //aShape = aTransformation.Shape();
120     TopLoc_Location aLocOrig = anOriginal.Location();
121     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
122     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
123     aShape = anOriginal.Located(aLocRes);
124   }
125   else if (aType == TRANSLATE_VECTOR_DISTANCE) {
126     Handle(GEOM_Function) aVector = TI.GetVector();
127     double aDistance = TI.GetDistance();
128     if(aVector.IsNull()) return 0;
129     TopoDS_Shape aV = aVector->GetValue();
130     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
131     TopoDS_Edge anEdge = TopoDS::Edge(aV);
132
133     aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
134     aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
135
136     gp_Vec aVec (aP1, aP2);
137     aVec.Normalize();
138     aTrsf.SetTranslation(aVec * aDistance);
139
140     TopLoc_Location aLocOrig = anOriginal.Location();
141     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
142     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
143     aShape = anOriginal.Located(aLocRes);
144   }
145   else if (aType == TRANSLATE_XYZ || aType == TRANSLATE_XYZ_COPY) {
146     gp_Vec aVec (TI.GetDX(), TI.GetDY(), TI.GetDZ());
147     aTrsf.SetTranslation(aVec);
148     //NPAL18620: performance problem: multiple locations are accumulated
149     //           in shape and need a great time to process
150     //BRepBuilderAPI_Transform aTransformation (anOriginal, aTrsf, Standard_False);
151     //aShape = aTransformation.Shape();
152     TopLoc_Location aLocOrig = anOriginal.Location();
153     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
154     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
155     aShape = anOriginal.Located(aLocRes);
156   }
157   else if (aType == TRANSLATE_1D) {
158     Standard_Real DX, DY, DZ, step = TI.GetStep1();
159     Standard_Integer nbtimes = TI.GetNbIter1();
160     gp_Vec aVec;
161     TopoDS_Compound aCompound;
162     BRep_Builder B;
163     B.MakeCompound( aCompound );
164
165     Handle(GEOM_Function) aVector = TI.GetVector();
166     if(aVector.IsNull()) return 0;
167     TopoDS_Shape aV = aVector->GetValue();
168     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
169     TopoDS_Edge anEdge = TopoDS::Edge(aV);
170
171     gp_Vec Vec(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
172     Vec.Normalize();
173
174     TopLoc_Location aLocOrig = anOriginal.Location();
175     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
176
177     for (int i = 0; i < nbtimes; i++) {
178       DX = i * step * Vec.X();
179       DY = i * step * Vec.Y();
180       DZ = i * step * Vec.Z();
181       aVec.SetCoord( DX, DY, DZ );
182       aTrsf.SetTranslation(aVec);
183       //NPAL18620: performance problem: multiple locations are accumulated
184       //           in shape and need a great time to process
185       //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
186       //B.Add(aCompound, aTransformation.Shape());
187       TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
188       B.Add(aCompound, anOriginal.Located(aLocRes));
189     }
190     aShape = aCompound;
191   }
192   else if (aType == TRANSLATE_2D) {
193     Standard_Integer nbtimes1 = TI.GetNbIter1(), nbtimes2 = TI.GetNbIter2();
194     Standard_Real DX, DY, DZ,  step1 = TI.GetStep1(),  step2 = TI.GetStep2();
195     gp_Vec aVec;
196     Handle(GEOM_Function) aVector = TI.GetVector();
197     if(aVector.IsNull()) return 0;
198     TopoDS_Shape aV = aVector->GetValue();
199     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
200     TopoDS_Edge anEdge = TopoDS::Edge(aV);
201
202     gp_Vec Vec1(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
203     Vec1.Normalize();
204
205     Handle(GEOM_Function) aVector2 = TI.GetVector2();
206     if(aVector2.IsNull()) return 0;
207     aV = aVector2->GetValue();
208     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
209     anEdge = TopoDS::Edge(aV);
210
211     gp_Vec Vec2(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
212     Vec2.Normalize();
213
214     TopoDS_Compound aCompound;
215     BRep_Builder B;
216     B.MakeCompound( aCompound );
217
218     TopLoc_Location aLocOrig = anOriginal.Location();
219     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
220
221     for (int i = 0; i < nbtimes1; i++) {
222       for (int j = 0; j < nbtimes2; j++) {
223         DX = i * step1 * Vec1.X() + j * step2 * Vec2.X();
224         DY = i * step1 * Vec1.Y() + j * step2 * Vec2.Y();
225         DZ = i * step1 * Vec1.Z() + j * step2 * Vec2.Z();
226         aVec.SetCoord( DX, DY, DZ );
227         aTrsf.SetTranslation(aVec);
228         //NPAL18620: performance problem: multiple locations are accumulated
229         //           in shape and need a great time to process
230         //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
231         //B.Add(aCompound, aBRepTransformation.Shape());
232         TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
233         B.Add(aCompound, anOriginal.Located(aLocRes));
234       }
235     }
236    aShape = aCompound;
237   }
238   else return 0;
239
240
241   if (aShape.IsNull()) return 0;
242
243   aFunction->SetValue(aShape);
244
245   log.SetTouched(Label());
246
247   return 1;
248 }
249
250
251 //=======================================================================
252 //function :  GEOMImpl_TranslateDriver_Type_
253 //purpose  :
254 //=======================================================================
255 Standard_EXPORT Handle_Standard_Type& GEOMImpl_TranslateDriver_Type_()
256 {
257
258   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
259   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
260   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
261   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
262   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
263   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
264
265   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
266   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_TranslateDriver",
267                                                          sizeof(GEOMImpl_TranslateDriver),
268                                                          1,
269                                                          (Standard_Address)_Ancestors,
270                                                          (Standard_Address)NULL);
271
272   return _aType;
273 }
274
275 //=======================================================================
276 //function : DownCast
277 //purpose  :
278 //=======================================================================
279 const Handle(GEOMImpl_TranslateDriver) Handle(GEOMImpl_TranslateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
280 {
281   Handle(GEOMImpl_TranslateDriver) _anOtherObject;
282
283   if (!AnObject.IsNull()) {
284      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_TranslateDriver))) {
285        _anOtherObject = Handle(GEOMImpl_TranslateDriver)((Handle(GEOMImpl_TranslateDriver)&)AnObject);
286      }
287   }
288
289   return _anOtherObject;
290 }