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