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