Back to index

salome-geom  6.5.0
GEOMImpl_TranslateDriver.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include <Standard_Stream.hxx>
00024 
00025 #include <GEOMImpl_TranslateDriver.hxx>
00026 #include <GEOMImpl_ITranslate.hxx>
00027 #include <GEOMImpl_ITransformOperations.hxx>
00028 #include <GEOMImpl_Types.hxx>
00029 #include <GEOM_Function.hxx>
00030 
00031 #include <ShapeFix_Shape.hxx>
00032 #include <ShapeFix_ShapeTolerance.hxx>
00033 
00034 #include <BRep_Tool.hxx>
00035 #include <BRep_Builder.hxx>
00036 #include <BRepCheck_Analyzer.hxx>
00037 #include <BRepBuilderAPI_Transform.hxx>
00038 
00039 #include <TopoDS.hxx>
00040 #include <TopoDS_Shape.hxx>
00041 #include <TopoDS_Edge.hxx>
00042 #include <TopoDS_Compound.hxx>
00043 #include <TopAbs.hxx>
00044 #include <TopExp.hxx>
00045 #include <TopoDS_Vertex.hxx>
00046 #include <TopoDS_Edge.hxx>
00047 
00048 #include <gp_Trsf.hxx>
00049 #include <gp_Pnt.hxx>
00050 #include <gp_Vec.hxx>
00051 
00052 //=======================================================================
00053 //function : GetID
00054 //purpose  :
00055 //=======================================================================
00056 const Standard_GUID& GEOMImpl_TranslateDriver::GetID()
00057 {
00058   static Standard_GUID aTranslateDriver("FF1BBB03-5D14-4df2-980B-3A668264EA16");
00059   return aTranslateDriver;
00060 }
00061 
00062 
00063 //=======================================================================
00064 //function : GEOMImpl_TranslateDriver
00065 //purpose  :
00066 //=======================================================================
00067 
00068 GEOMImpl_TranslateDriver::GEOMImpl_TranslateDriver()
00069 {
00070 }
00071 
00072 //=======================================================================
00073 //function : Execute
00074 //purpose  :
00075 //=======================================================================
00076 Standard_Integer GEOMImpl_TranslateDriver::Execute(TFunction_Logbook& log) const
00077 {
00078   if (Label().IsNull()) return 0;
00079   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00080 
00081   if (aFunction.IsNull()) return 0;
00082 
00083   GEOMImpl_ITranslate TI (aFunction);
00084   gp_Trsf aTrsf;
00085   gp_Pnt aP1, aP2;
00086   Standard_Integer aType = aFunction->GetType();
00087 
00088   Handle(GEOM_Function) anOriginalFunction = TI.GetOriginal();
00089   if (anOriginalFunction.IsNull()) return 0;
00090   TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
00091   if (anOriginal.IsNull()) return 0;
00092 
00093   if (aType == TRANSLATE_TWO_POINTS || aType == TRANSLATE_TWO_POINTS_COPY) {
00094     Handle(GEOM_Function) aPoint1 = TI.GetPoint1();
00095     Handle(GEOM_Function) aPoint2 = TI.GetPoint2();
00096     if(aPoint1.IsNull() || aPoint2.IsNull()) return 0;
00097     TopoDS_Shape aV1 = aPoint1->GetValue();
00098     TopoDS_Shape aV2 = aPoint2->GetValue();
00099     if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
00100     if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
00101 
00102     aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
00103     aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
00104 
00105     aTrsf.SetTranslation(aP1, aP2);
00106     //NPAL18620: performance problem: multiple locations are accumulated
00107     //           in shape and need a great time to process
00108     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
00109     //aShape = aTransformation.Shape();
00110     TopLoc_Location aLocOrig = anOriginal.Location();
00111     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
00112     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
00113     aShape = anOriginal.Located(aLocRes);
00114   }
00115   else if (aType == TRANSLATE_VECTOR || aType == TRANSLATE_VECTOR_COPY) {
00116     Handle(GEOM_Function) aVector = TI.GetVector();
00117     if(aVector.IsNull()) return 0;
00118     TopoDS_Shape aV = aVector->GetValue();
00119     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
00120     TopoDS_Edge anEdge = TopoDS::Edge(aV);
00121 
00122     aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
00123     aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
00124 
00125     aTrsf.SetTranslation(aP1, aP2);
00126     //NPAL18620: performance problem: multiple locations are accumulated
00127     //           in shape and need a great time to process
00128     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
00129     //aShape = aTransformation.Shape();
00130     TopLoc_Location aLocOrig = anOriginal.Location();
00131     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
00132     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
00133     aShape = anOriginal.Located(aLocRes);
00134   }
00135   else if (aType == TRANSLATE_VECTOR_DISTANCE) {
00136     Handle(GEOM_Function) aVector = TI.GetVector();
00137     double aDistance = TI.GetDistance();
00138     if(aVector.IsNull()) return 0;
00139     TopoDS_Shape aV = aVector->GetValue();
00140     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
00141     TopoDS_Edge anEdge = TopoDS::Edge(aV);
00142 
00143     aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
00144     aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
00145 
00146     gp_Vec aVec (aP1, aP2);
00147     aVec.Normalize();
00148     aTrsf.SetTranslation(aVec * aDistance);
00149 
00150     TopLoc_Location aLocOrig = anOriginal.Location();
00151     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
00152     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
00153     aShape = anOriginal.Located(aLocRes);
00154   }
00155   else if (aType == TRANSLATE_XYZ || aType == TRANSLATE_XYZ_COPY) {
00156     gp_Vec aVec (TI.GetDX(), TI.GetDY(), TI.GetDZ());
00157     aTrsf.SetTranslation(aVec);
00158     //NPAL18620: performance problem: multiple locations are accumulated
00159     //           in shape and need a great time to process
00160     //BRepBuilderAPI_Transform aTransformation (anOriginal, aTrsf, Standard_False);
00161     //aShape = aTransformation.Shape();
00162     TopLoc_Location aLocOrig = anOriginal.Location();
00163     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
00164     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
00165     aShape = anOriginal.Located(aLocRes);
00166   }
00167   else if (aType == TRANSLATE_1D) {
00168     Standard_Real DX, DY, DZ, step = TI.GetStep1();
00169     Standard_Integer nbtimes = TI.GetNbIter1();
00170     gp_Vec aVec;
00171     TopoDS_Compound aCompound;
00172     BRep_Builder B;
00173     B.MakeCompound( aCompound );
00174 
00175     Handle(GEOM_Function) aVector = TI.GetVector();
00176     if(aVector.IsNull()) return 0;
00177     TopoDS_Shape aV = aVector->GetValue();
00178     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
00179     TopoDS_Edge anEdge = TopoDS::Edge(aV);
00180 
00181     gp_Vec Vec(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
00182     Vec.Normalize();
00183 
00184     TopLoc_Location aLocOrig = anOriginal.Location();
00185     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
00186 
00187     for (int i = 0; i < nbtimes; i++) {
00188       DX = i * step * Vec.X();
00189       DY = i * step * Vec.Y();
00190       DZ = i * step * Vec.Z();
00191       aVec.SetCoord( DX, DY, DZ );
00192       aTrsf.SetTranslation(aVec);
00193       //NPAL18620: performance problem: multiple locations are accumulated
00194       //           in shape and need a great time to process
00195       //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
00196       //B.Add(aCompound, aTransformation.Shape());
00197       TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
00198       B.Add(aCompound, anOriginal.Located(aLocRes));
00199     }
00200     aShape = aCompound;
00201     //aShape = GEOMImpl_ITransformOperations::TranslateShape1D(anOriginal, &TI);
00202   }
00203   else if (aType == TRANSLATE_2D) {
00204     Standard_Integer nbtimes1 = TI.GetNbIter1(), nbtimes2 = TI.GetNbIter2();
00205     Standard_Real DX, DY, DZ,  step1 = TI.GetStep1(),  step2 = TI.GetStep2();
00206     gp_Vec aVec;
00207     Handle(GEOM_Function) aVector = TI.GetVector();
00208     if(aVector.IsNull()) return 0;
00209     TopoDS_Shape aV = aVector->GetValue();
00210     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
00211     TopoDS_Edge anEdge = TopoDS::Edge(aV);
00212 
00213     gp_Vec Vec1(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
00214     Vec1.Normalize();
00215 
00216     Handle(GEOM_Function) aVector2 = TI.GetVector2();
00217     if(aVector2.IsNull()) return 0;
00218     aV = aVector2->GetValue();
00219     if(aV.IsNull() || aV.ShapeType() != TopAbs_EDGE) return 0;
00220     anEdge = TopoDS::Edge(aV);
00221 
00222     gp_Vec Vec2(BRep_Tool::Pnt(TopExp::FirstVertex(anEdge)), BRep_Tool::Pnt(TopExp::LastVertex(anEdge)));
00223     Vec2.Normalize();
00224 
00225     TopoDS_Compound aCompound;
00226     BRep_Builder B;
00227     B.MakeCompound( aCompound );
00228 
00229     TopLoc_Location aLocOrig = anOriginal.Location();
00230     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
00231 
00232     for (int i = 0; i < nbtimes1; i++) {
00233       for (int j = 0; j < nbtimes2; j++) {
00234         DX = i * step1 * Vec1.X() + j * step2 * Vec2.X();
00235         DY = i * step1 * Vec1.Y() + j * step2 * Vec2.Y();
00236         DZ = i * step1 * Vec1.Z() + j * step2 * Vec2.Z();
00237         aVec.SetCoord( DX, DY, DZ );
00238         aTrsf.SetTranslation(aVec);
00239         //NPAL18620: performance problem: multiple locations are accumulated
00240         //           in shape and need a great time to process
00241         //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
00242         //B.Add(aCompound, aBRepTransformation.Shape());
00243         TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
00244         B.Add(aCompound, anOriginal.Located(aLocRes));
00245       }
00246     }
00247     aShape = aCompound;
00248     //aShape = GEOMImpl_ITransformOperations::TranslateShape2D(anOriginal, &TI);
00249   }
00250   else return 0;
00251 
00252   if (aShape.IsNull()) return 0;
00253 
00254   BRepCheck_Analyzer ana (aShape, Standard_True);
00255   if (!ana.IsValid()) {
00256     ShapeFix_ShapeTolerance aSFT;
00257     aSFT.LimitTolerance(aShape,Precision::Confusion(),Precision::Confusion());
00258     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
00259     aSfs->SetPrecision(Precision::Confusion());
00260     aSfs->Perform();
00261     aShape = aSfs->Shape();
00262 
00263     ana.Init(aShape, Standard_False);
00264     if (!ana.IsValid())
00265       Standard_ConstructionError::Raise("Scaling aborted : algorithm has produced an invalid shape result");
00266   }
00267 
00268   aFunction->SetValue(aShape);
00269 
00270   log.SetTouched(Label());
00271 
00272   return 1;
00273 }
00274 
00275 
00276 //=======================================================================
00277 //function :  GEOMImpl_TranslateDriver_Type_
00278 //purpose  :
00279 //=======================================================================
00280 Standard_EXPORT Handle_Standard_Type& GEOMImpl_TranslateDriver_Type_()
00281 {
00282 
00283   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00284   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00285   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00286   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
00287   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00288   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00289 
00290   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00291   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_TranslateDriver",
00292                                                          sizeof(GEOMImpl_TranslateDriver),
00293                                                          1,
00294                                                          (Standard_Address)_Ancestors,
00295                                                          (Standard_Address)NULL);
00296 
00297   return _aType;
00298 }
00299 
00300 //=======================================================================
00301 //function : DownCast
00302 //purpose  :
00303 //=======================================================================
00304 const Handle(GEOMImpl_TranslateDriver) Handle(GEOMImpl_TranslateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
00305 {
00306   Handle(GEOMImpl_TranslateDriver) _anOtherObject;
00307 
00308   if (!AnObject.IsNull()) {
00309      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_TranslateDriver))) {
00310        _anOtherObject = Handle(GEOMImpl_TranslateDriver)((Handle(GEOMImpl_TranslateDriver)&)AnObject);
00311      }
00312   }
00313 
00314   return _anOtherObject;
00315 }