Salome HOME
ENV: Windows porting.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_TranslateDriver.cxx
1
2 #include <Standard_Stream.hxx>
3
4 #include <GEOMImpl_TranslateDriver.hxx>
5 #include <GEOMImpl_ITranslate.hxx>
6 #include <GEOMImpl_Types.hxx>
7 #include <GEOM_Function.hxx>
8 #include <gp_Trsf.hxx>
9 #include <gp_Pnt.hxx>
10 #include <gp_Vec.hxx>
11 #include <BRepBuilderAPI_Transform.hxx>
12 #include <BRep_Builder.hxx>
13 #include <TopoDS.hxx>
14 #include <TopoDS_Shape.hxx>
15 #include <TopoDS_Edge.hxx>
16 #include <TopoDS_Compound.hxx>
17 #include <TopAbs.hxx>
18 #include <TopExp.hxx>
19 #include <TopoDS_Vertex.hxx>
20 #include <TopoDS_Edge.hxx>
21 #include <BRep_Tool.hxx>
22
23 //=======================================================================
24 //function : GetID
25 //purpose  :
26 //======================================================================= 
27 const Standard_GUID& GEOMImpl_TranslateDriver::GetID()
28 {
29   static Standard_GUID aTranslateDriver("FF1BBB03-5D14-4df2-980B-3A668264EA16");
30   return aTranslateDriver; 
31 }
32
33
34 //=======================================================================
35 //function : GEOMImpl_TranslateDriver
36 //purpose  : 
37 //=======================================================================
38
39 GEOMImpl_TranslateDriver::GEOMImpl_TranslateDriver() 
40 {
41 }
42
43 //=======================================================================
44 //function : Execute
45 //purpose  :
46 //======================================================================= 
47 Standard_Integer GEOMImpl_TranslateDriver::Execute(TFunction_Logbook& log) const
48 {
49   if(Label().IsNull())  return 0;    
50   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
51
52   if(aFunction.IsNull()) return 0;
53
54   GEOMImpl_ITranslate TI(aFunction);
55   gp_Trsf aTrsf;
56   gp_Pnt aP1, aP2;
57   Standard_Integer aType = aFunction->GetType();
58
59   Handle(GEOM_Function) anOriginalFunction = TI.GetOriginal();
60   if(anOriginalFunction.IsNull()) return 0;
61   TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
62   if(anOriginal.IsNull()) return 0;
63
64   if(aType == TRANSLATE_TWO_POINTS || aType == TRANSLATE_TWO_POINTS_COPY) {
65     Handle(GEOM_Function) aPoint1 = TI.GetPoint1();
66     Handle(GEOM_Function) aPoint2 = TI.GetPoint2();
67     if(aPoint1.IsNull() || aPoint2.IsNull()) return 0;
68     TopoDS_Shape aV1 = aPoint1->GetValue();
69     TopoDS_Shape aV2 = aPoint2->GetValue();
70     if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
71     if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
72
73     aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
74     aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
75
76     aTrsf.SetTranslation(aP1, aP2);
77     BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
78     aShape = aTransformation.Shape();
79
80   } else if(aType == TRANSLATE_VECTOR || aType == TRANSLATE_VECTOR_COPY) {
81     Handle(GEOM_Function) aVector = TI.GetVector();
82     if(aVector.IsNull()) return 0;
83     TopoDS_Shape aV = aVector->GetValue();
84     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
85     TopoDS_Edge anEdge = TopoDS::Edge(aV);
86
87     aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
88     aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
89
90     aTrsf.SetTranslation(aP1, aP2);
91     BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
92     aShape = aTransformation.Shape();
93   }
94   else if(aType == TRANSLATE_XYZ || aType == TRANSLATE_XYZ_COPY) {
95     gp_Vec aVec(TI.GetDX(), TI.GetDY(), TI.GetDZ());
96     aTrsf.SetTranslation(aVec);
97     BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
98     aShape = aTransformation.Shape();
99   }
100   else if(aType == TRANSLATE_1D) {
101     Standard_Real DX, DY, DZ, step = TI.GetStep1();
102     Standard_Integer nbtimes = TI.GetNbIter1();
103     gp_Vec myVec ;
104     TopoDS_Compound aCompound;
105     BRep_Builder B;
106     B.MakeCompound( aCompound );
107
108     Handle(GEOM_Function) aVector = TI.GetVector();
109     if(aVector.IsNull()) return 0;
110     TopoDS_Shape aV = aVector->GetValue();
111     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
112     TopoDS_Edge anEdge = TopoDS::Edge(aV);
113
114     gp_Vec Vec(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
115     Vec.Normalize();
116
117     for (int i = 0; i < nbtimes; i++ ) {
118       DX = i * step * Vec.X() ;
119       DY = i * step * Vec.Y() ;
120       DZ = i * step * Vec.Z() ;
121       myVec.SetCoord( DX, DY, DZ ) ;
122       aTrsf.SetTranslation(myVec) ;
123       BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False) ;
124       B.Add(aCompound , aTransformation.Shape() );
125     }
126     aShape = aCompound;   
127   }
128   else if(aType == TRANSLATE_2D) {
129     Standard_Integer nbtimes1 = TI.GetNbIter1(), nbtimes2 = TI.GetNbIter2();
130     Standard_Real DX, DY, DZ,  step1 = TI.GetStep1(),  step2 = TI.GetStep2();
131     gp_Vec myVec ;
132     Handle(GEOM_Function) aVector = TI.GetVector();
133     if(aVector.IsNull()) return 0;
134     TopoDS_Shape aV = aVector->GetValue();
135     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
136     TopoDS_Edge anEdge = TopoDS::Edge(aV);
137
138     gp_Vec Vec1(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
139     Vec1.Normalize();
140
141     Handle(GEOM_Function) aVector2 = TI.GetVector2();
142     if(aVector2.IsNull()) return 0;
143     aV = aVector2->GetValue();
144     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
145     anEdge = TopoDS::Edge(aV);
146
147     gp_Vec Vec2(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
148     Vec2.Normalize();
149
150     TopoDS_Compound aCompound;
151     BRep_Builder B;
152     B.MakeCompound( aCompound );
153     
154     for (int i = 0; i < nbtimes1; i++ ) {
155       for (int j = 0; j < nbtimes2; j++ ) {
156         DX = i * step1 * Vec1.X() + j * step2 * Vec2.X() ;
157         DY = i * step1 * Vec1.Y() + j * step2 * Vec2.Y() ;
158         DZ = i * step1 * Vec1.Z() + j * step2 * Vec2.Z() ;
159         myVec.SetCoord( DX, DY, DZ ) ;
160         aTrsf.SetTranslation(myVec) ;
161         BRepBuilderAPI_Transform myBRepTransformation(anOriginal, aTrsf, Standard_False) ;
162         B.Add(aCompound , myBRepTransformation.Shape() );
163       }
164     }
165    aShape = aCompound;  
166   }
167   else return 0;
168
169  
170
171   if (aShape.IsNull()) return 0;
172
173   aFunction->SetValue(aShape);
174
175   log.SetTouched(Label()); 
176
177   return 1;
178 }
179
180
181 //=======================================================================
182 //function :  GEOMImpl_TranslateDriver_Type_
183 //purpose  :
184 //======================================================================= 
185 Standard_EXPORT Handle_Standard_Type& GEOMImpl_TranslateDriver_Type_()
186 {
187
188   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
189   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
190   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
191   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
192   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
193   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
194  
195
196   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
197   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_TranslateDriver",
198                                                          sizeof(GEOMImpl_TranslateDriver),
199                                                          1,
200                                                          (Standard_Address)_Ancestors,
201                                                          (Standard_Address)NULL);
202
203   return _aType;
204 }
205
206 //=======================================================================
207 //function : DownCast
208 //purpose  :
209 //======================================================================= 
210
211 const Handle(GEOMImpl_TranslateDriver) Handle(GEOMImpl_TranslateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
212 {
213   Handle(GEOMImpl_TranslateDriver) _anOtherObject;
214
215   if (!AnObject.IsNull()) {
216      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_TranslateDriver))) {
217        _anOtherObject = Handle(GEOMImpl_TranslateDriver)((Handle(GEOMImpl_TranslateDriver)&)AnObject);
218      }
219   }
220
221   return _anOtherObject ;
222 }
223
224