1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_TranslateDriver.hxx>
26 #include <GEOMImpl_ITranslate.hxx>
27 #include <GEOMImpl_ITransformOperations.hxx>
28 #include <GEOMImpl_Types.hxx>
29 #include <GEOM_Function.hxx>
31 #include <ShapeFix_Shape.hxx>
32 #include <ShapeFix_ShapeTolerance.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRepCheck_Analyzer.hxx>
37 #include <BRepBuilderAPI_Transform.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS_Compound.hxx>
45 #include <TopoDS_Vertex.hxx>
46 #include <TopoDS_Edge.hxx>
48 #include <gp_Trsf.hxx>
52 //=======================================================================
55 //=======================================================================
56 const Standard_GUID& GEOMImpl_TranslateDriver::GetID()
58 static Standard_GUID aTranslateDriver("FF1BBB03-5D14-4df2-980B-3A668264EA16");
59 return aTranslateDriver;
63 //=======================================================================
64 //function : GEOMImpl_TranslateDriver
66 //=======================================================================
68 GEOMImpl_TranslateDriver::GEOMImpl_TranslateDriver()
72 //=======================================================================
75 //=======================================================================
76 Standard_Integer GEOMImpl_TranslateDriver::Execute(TFunction_Logbook& log) const
78 if (Label().IsNull()) return 0;
79 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
81 if (aFunction.IsNull()) return 0;
83 GEOMImpl_ITranslate TI (aFunction);
86 Standard_Integer aType = aFunction->GetType();
88 Handle(GEOM_Function) anOriginalFunction = TI.GetOriginal();
89 if (anOriginalFunction.IsNull()) return 0;
90 TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
91 if (anOriginal.IsNull()) return 0;
93 if (aType == TRANSLATE_TWO_POINTS || aType == TRANSLATE_TWO_POINTS_COPY) {
94 Handle(GEOM_Function) aPoint1 = TI.GetPoint1();
95 Handle(GEOM_Function) aPoint2 = TI.GetPoint2();
96 if(aPoint1.IsNull() || aPoint2.IsNull()) return 0;
97 TopoDS_Shape aV1 = aPoint1->GetValue();
98 TopoDS_Shape aV2 = aPoint2->GetValue();
99 if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
100 if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
102 aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
103 aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
105 aTrsf.SetTranslation(aP1, aP2);
106 //NPAL18620: performance problem: multiple locations are accumulated
107 // in shape and need a great time to process
108 //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
109 //aShape = aTransformation.Shape();
110 TopLoc_Location aLocOrig = anOriginal.Location();
111 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
112 TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
113 aShape = anOriginal.Located(aLocRes);
115 else if (aType == TRANSLATE_VECTOR || aType == TRANSLATE_VECTOR_COPY) {
116 Handle(GEOM_Function) aVector = TI.GetVector();
117 if(aVector.IsNull()) return 0;
118 TopoDS_Shape aV = aVector->GetValue();
119 if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
120 TopoDS_Edge anEdge = TopoDS::Edge(aV);
122 aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
123 aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
125 aTrsf.SetTranslation(aP1, aP2);
126 //NPAL18620: performance problem: multiple locations are accumulated
127 // in shape and need a great time to process
128 //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
129 //aShape = aTransformation.Shape();
130 TopLoc_Location aLocOrig = anOriginal.Location();
131 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
132 TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
133 aShape = anOriginal.Located(aLocRes);
135 else if (aType == TRANSLATE_VECTOR_DISTANCE) {
136 Handle(GEOM_Function) aVector = TI.GetVector();
137 double aDistance = TI.GetDistance();
138 if(aVector.IsNull()) return 0;
139 TopoDS_Shape aV = aVector->GetValue();
140 if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
141 TopoDS_Edge anEdge = TopoDS::Edge(aV);
143 aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
144 aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
146 gp_Vec aVec (aP1, aP2);
148 aTrsf.SetTranslation(aVec * aDistance);
150 TopLoc_Location aLocOrig = anOriginal.Location();
151 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
152 TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
153 aShape = anOriginal.Located(aLocRes);
155 else if (aType == TRANSLATE_XYZ || aType == TRANSLATE_XYZ_COPY) {
156 gp_Vec aVec (TI.GetDX(), TI.GetDY(), TI.GetDZ());
157 aTrsf.SetTranslation(aVec);
158 //NPAL18620: performance problem: multiple locations are accumulated
159 // in shape and need a great time to process
160 //BRepBuilderAPI_Transform aTransformation (anOriginal, aTrsf, Standard_False);
161 //aShape = aTransformation.Shape();
162 TopLoc_Location aLocOrig = anOriginal.Location();
163 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
164 TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
165 aShape = anOriginal.Located(aLocRes);
167 else if (aType == TRANSLATE_1D) {
168 Standard_Real DX, DY, DZ, step = TI.GetStep1();
169 Standard_Integer nbtimes = TI.GetNbIter1();
171 TopoDS_Compound aCompound;
173 B.MakeCompound( aCompound );
175 Handle(GEOM_Function) aVector = TI.GetVector();
176 gp_Vec Vec = gp::DX();
177 if (!aVector.IsNull()) {
178 TopoDS_Shape aV = aVector->GetValue();
179 if (aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
180 TopoDS_Edge anEdge = TopoDS::Edge(aV);
181 Vec = gp_Vec(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
185 TopLoc_Location aLocOrig = anOriginal.Location();
186 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
188 for (int i = 0; i < nbtimes; i++) {
189 DX = i * step * Vec.X();
190 DY = i * step * Vec.Y();
191 DZ = i * step * Vec.Z();
192 aVec.SetCoord( DX, DY, DZ );
193 aTrsf.SetTranslation(aVec);
194 //NPAL18620: performance problem: multiple locations are accumulated
195 // in shape and need a great time to process
196 //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
197 //B.Add(aCompound, aTransformation.Shape());
198 TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
199 B.Add(aCompound, anOriginal.Located(aLocRes));
202 //aShape = GEOMImpl_ITransformOperations::TranslateShape1D(anOriginal, &TI);
204 else if (aType == TRANSLATE_2D) {
205 Standard_Integer nbtimes1 = TI.GetNbIter1(), nbtimes2 = TI.GetNbIter2();
206 Standard_Real DX, DY, DZ, step1 = TI.GetStep1(), step2 = TI.GetStep2();
207 Handle(GEOM_Function) aVector = TI.GetVector();
208 Handle(GEOM_Function) aVector2 = TI.GetVector2();
210 gp_Vec Vec1 = gp::DX();
211 gp_Vec Vec2 = gp::DY();
213 if (!aVector.IsNull()) {
214 TopoDS_Shape aV = aVector->GetValue();
215 if (aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
216 TopoDS_Edge anEdge = TopoDS::Edge(aV);
217 Vec1 = gp_Vec(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
221 if (!aVector2.IsNull()) {
222 TopoDS_Shape aV = aVector2->GetValue();
223 if (aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
224 TopoDS_Edge anEdge = TopoDS::Edge(aV);
225 Vec2 = gp_Vec(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
229 TopoDS_Compound aCompound;
231 B.MakeCompound(aCompound);
233 TopLoc_Location aLocOrig = anOriginal.Location();
234 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
237 for (int i = 0; i < nbtimes1; i++) {
238 for (int j = 0; j < nbtimes2; j++) {
239 DX = i * step1 * Vec1.X() + j * step2 * Vec2.X();
240 DY = i * step1 * Vec1.Y() + j * step2 * Vec2.Y();
241 DZ = i * step1 * Vec1.Z() + j * step2 * Vec2.Z();
242 aVec.SetCoord( DX, DY, DZ );
243 aTrsf.SetTranslation(aVec);
244 //NPAL18620: performance problem: multiple locations are accumulated
245 // in shape and need a great time to process
246 //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
247 //B.Add(aCompound, aBRepTransformation.Shape());
248 TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
249 B.Add(aCompound, anOriginal.Located(aLocRes));
253 //aShape = GEOMImpl_ITransformOperations::TranslateShape2D(anOriginal, &TI);
257 if (aShape.IsNull()) return 0;
259 BRepCheck_Analyzer ana (aShape, Standard_True);
260 if (!ana.IsValid()) {
261 ShapeFix_ShapeTolerance aSFT;
262 aSFT.LimitTolerance(aShape,Precision::Confusion(),Precision::Confusion());
263 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
264 aSfs->SetPrecision(Precision::Confusion());
266 aShape = aSfs->Shape();
268 ana.Init(aShape, Standard_False);
270 Standard_ConstructionError::Raise("Scaling aborted : algorithm has produced an invalid shape result");
273 aFunction->SetValue(aShape);
275 log.SetTouched(Label());
280 //================================================================================
282 * \brief Returns a name of creation operation and names and values of creation parameters
284 //================================================================================
286 bool GEOMImpl_TranslateDriver::
287 GetCreationInformation(std::string& theOperationName,
288 std::vector<GEOM_Param>& theParams)
290 if (Label().IsNull()) return 0;
291 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
293 GEOMImpl_ITranslate aCI( function );
294 Standard_Integer aType = function->GetType();
297 case TRANSLATE_TWO_POINTS:
298 case TRANSLATE_TWO_POINTS_COPY:
299 theOperationName = "TRANSLATION";
300 AddParam( theParams, "Object", aCI.GetOriginal() );
301 AddParam( theParams, "Point 1", aCI.GetPoint1() );
302 AddParam( theParams, "Point 2", aCI.GetPoint2() );
304 case TRANSLATE_VECTOR:
305 case TRANSLATE_VECTOR_COPY:
306 theOperationName = "TRANSLATION";
307 AddParam( theParams, "Object", aCI.GetOriginal() );
308 AddParam( theParams, "Vector", aCI.GetVector() );
310 case TRANSLATE_VECTOR_DISTANCE:
311 theOperationName = "TRANSLATION";
312 AddParam( theParams, "Object", aCI.GetOriginal() );
313 AddParam( theParams, "Vector", aCI.GetVector() );
314 AddParam( theParams, "Distance", aCI.GetDistance() );
317 case TRANSLATE_XYZ_COPY:
318 theOperationName = "TRANSLATION";
319 AddParam( theParams, "Object", aCI.GetOriginal() );
320 AddParam( theParams, "Dx", aCI.GetDX() );
321 AddParam( theParams, "Dy", aCI.GetDY() );
322 AddParam( theParams, "Dz", aCI.GetDZ() );
325 theOperationName = "MUL_TRANSLATION";
326 AddParam( theParams, "Main Object", aCI.GetOriginal() );
327 AddParam( theParams, "Vector", aCI.GetVector(), "DX" );
328 AddParam( theParams, "Step", aCI.GetStep1() );
329 AddParam( theParams, "Nb. Times", aCI.GetNbIter1() );
332 theOperationName = "MUL_TRANSLATION";
333 AddParam( theParams, "Main Object", aCI.GetOriginal() );
334 AddParam( theParams, "Vector U", aCI.GetVector(), "DX" );
335 AddParam( theParams, "Vector V", aCI.GetVector2(), "DY" );
336 AddParam( theParams, "Step U", aCI.GetStep1() );
337 AddParam( theParams, "Nb. Times U", aCI.GetNbIter1() );
338 AddParam( theParams, "Step V", aCI.GetStep2() );
339 AddParam( theParams, "Nb. Times V", aCI.GetNbIter2() );
348 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_TranslateDriver,GEOM_BaseDriver);
349 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_TranslateDriver,GEOM_BaseDriver);