1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include <Standard_Stream.hxx>
23 #include <GEOMImpl_TranslateDriver.hxx>
24 #include <GEOMImpl_ITranslate.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
28 #include <ShapeFix_Shape.hxx>
29 #include <ShapeFix_ShapeTolerance.hxx>
31 #include <BRep_Tool.hxx>
32 #include <BRep_Builder.hxx>
33 #include <BRepCheck_Analyzer.hxx>
34 #include <BRepBuilderAPI_Transform.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Compound.hxx>
42 #include <TopoDS_Vertex.hxx>
43 #include <TopoDS_Edge.hxx>
45 #include <gp_Trsf.hxx>
49 //=======================================================================
52 //=======================================================================
53 const Standard_GUID& GEOMImpl_TranslateDriver::GetID()
55 static Standard_GUID aTranslateDriver("FF1BBB03-5D14-4df2-980B-3A668264EA16");
56 return aTranslateDriver;
60 //=======================================================================
61 //function : GEOMImpl_TranslateDriver
63 //=======================================================================
65 GEOMImpl_TranslateDriver::GEOMImpl_TranslateDriver()
69 //=======================================================================
72 //=======================================================================
73 Standard_Integer GEOMImpl_TranslateDriver::Execute(TFunction_Logbook& log) const
75 if (Label().IsNull()) return 0;
76 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
78 if (aFunction.IsNull()) return 0;
80 GEOMImpl_ITranslate TI(aFunction);
83 Standard_Integer aType = aFunction->GetType();
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;
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;
99 aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
100 aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
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);
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);
119 aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
120 aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
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);
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);
140 aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
141 aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
143 gp_Vec aVec (aP1, aP2);
145 aTrsf.SetTranslation(aVec * aDistance);
147 TopLoc_Location aLocOrig = anOriginal.Location();
148 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
149 TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
150 aShape = anOriginal.Located(aLocRes);
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);
164 else if (aType == TRANSLATE_1D) {
165 Standard_Real DX, DY, DZ, step = TI.GetStep1();
166 Standard_Integer nbtimes = TI.GetNbIter1();
168 TopoDS_Compound aCompound;
170 B.MakeCompound( aCompound );
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);
178 gp_Vec Vec(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
181 TopLoc_Location aLocOrig = anOriginal.Location();
182 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
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));
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();
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);
209 gp_Vec Vec1(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
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);
218 gp_Vec Vec2(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
221 TopoDS_Compound aCompound;
223 B.MakeCompound( aCompound );
225 TopLoc_Location aLocOrig = anOriginal.Location();
226 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
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));
247 if (aShape.IsNull()) return 0;
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());
256 aShape = aSfs->Shape();
258 ana.Init(aShape, Standard_False);
260 Standard_ConstructionError::Raise("Scaling aborted : algorithm has produced an invalid shape result");
263 aFunction->SetValue(aShape);
265 log.SetTouched(Label());
271 //=======================================================================
272 //function : GEOMImpl_TranslateDriver_Type_
274 //=======================================================================
275 Standard_EXPORT Handle_Standard_Type& GEOMImpl_TranslateDriver_Type_()
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);
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),
289 (Standard_Address)_Ancestors,
290 (Standard_Address)NULL);
295 //=======================================================================
296 //function : DownCast
298 //=======================================================================
299 const Handle(GEOMImpl_TranslateDriver) Handle(GEOMImpl_TranslateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
301 Handle(GEOMImpl_TranslateDriver) _anOtherObject;
303 if (!AnObject.IsNull()) {
304 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_TranslateDriver))) {
305 _anOtherObject = Handle(GEOMImpl_TranslateDriver)((Handle(GEOMImpl_TranslateDriver)&)AnObject);
309 return _anOtherObject;