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