Back to index

salome-geom  6.5.0
GEOMImpl_RotateDriver.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_RotateDriver.hxx>
00026 #include <GEOMImpl_IRotate.hxx>
00027 #include <GEOMImpl_Types.hxx>
00028 #include <GEOM_Function.hxx>
00029 #include <gp_Trsf.hxx>
00030 #include <gp_Pnt.hxx>
00031 #include <gp_Vec.hxx>
00032 #include <gp_Dir.hxx>
00033 #include <gp_Ax1.hxx>
00034 #include <BRepBuilderAPI_Transform.hxx>
00035 #include <TopoDS.hxx>
00036 #include <TopoDS_Vertex.hxx>
00037 #include <TopoDS_Shape.hxx>
00038 #include <TopoDS_Compound.hxx>
00039 #include <TopAbs.hxx>
00040 #include <TopExp.hxx>
00041 #include <TopoDS_Vertex.hxx>
00042 #include <TopoDS_Edge.hxx>
00043 #include <BRep_Tool.hxx>
00044 #include <BRep_Builder.hxx>
00045 #include <GeomAPI_ProjectPointOnCurve.hxx>
00046 #include <Geom_Line.hxx>
00047 #include <GProp_GProps.hxx>
00048 #include <BRepGProp.hxx>
00049 #include <Precision.hxx>
00050 
00051 //=======================================================================
00052 //function : GetID
00053 //purpose  :
00054 //=======================================================================
00055 const Standard_GUID& GEOMImpl_RotateDriver::GetID()
00056 {
00057   static Standard_GUID aRotateDriver("FF1BBB56-5D14-4df2-980B-3A668264EA16");
00058   return aRotateDriver;
00059 }
00060 
00061 
00062 //=======================================================================
00063 //function : GEOMImpl_RotateDriver
00064 //purpose  :
00065 //=======================================================================
00066 
00067 GEOMImpl_RotateDriver::GEOMImpl_RotateDriver()
00068 {
00069 }
00070 
00071 //=======================================================================
00072 //function : Execute
00073 //purpose  :
00074 //=======================================================================
00075 Standard_Integer GEOMImpl_RotateDriver::Execute(TFunction_Logbook& log) const
00076 {
00077   if (Label().IsNull()) return 0;
00078   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00079 
00080   if (aFunction.IsNull()) return 0;
00081 
00082   GEOMImpl_IRotate RI(aFunction);
00083   gp_Trsf aTrsf;
00084   gp_Pnt aCP, aP1, aP2;
00085   Standard_Integer aType = aFunction->GetType();
00086   Handle(GEOM_Function) anOriginalFunction = RI.GetOriginal();
00087   if (anOriginalFunction.IsNull()) return 0;
00088   TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
00089   if (anOriginal.IsNull()) return 0;
00090 
00091   if (aType == ROTATE || aType == ROTATE_COPY) {
00092     Handle(GEOM_Function) anAxis = RI.GetAxis();
00093     if (anAxis.IsNull()) return 0;
00094     TopoDS_Shape A = anAxis->GetValue();
00095     if (A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
00096     TopoDS_Edge anEdge = TopoDS::Edge(A);
00097 
00098     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
00099     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
00100     gp_Dir aDir(gp_Vec(aP1, aP2));
00101     gp_Ax1 anAx1(aP1, aDir);
00102     Standard_Real anAngle = RI.GetAngle();
00103     if (fabs(anAngle) < Precision::Angular()) anAngle += 2.*M_PI; // NPAL19665,19769
00104     aTrsf.SetRotation(anAx1, anAngle);
00105 
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); // gp_Trsf::Multiply() has a bug
00113     aTrsfOrig.PreMultiply(aTrsf);
00114     TopLoc_Location aLocRes (aTrsfOrig);
00115     aShape = anOriginal.Located(aLocRes);
00116   }
00117   else if (aType ==  ROTATE_THREE_POINTS || aType == ROTATE_THREE_POINTS_COPY) {
00118     Handle(GEOM_Function) aCentPoint = RI.GetCentPoint();
00119     Handle(GEOM_Function) aPoint1 = RI.GetPoint1();
00120     Handle(GEOM_Function) aPoint2 = RI.GetPoint2();
00121     if(aCentPoint.IsNull() || aPoint1.IsNull() || aPoint2.IsNull()) return 0;
00122     TopoDS_Shape aCV = aCentPoint->GetValue();
00123     TopoDS_Shape aV1 = aPoint1->GetValue();
00124     TopoDS_Shape aV2 = aPoint2->GetValue();
00125     if(aCV.IsNull() || aCV.ShapeType() != TopAbs_VERTEX) return 0;
00126     if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
00127     if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
00128 
00129     aCP = BRep_Tool::Pnt(TopoDS::Vertex(aCV));
00130     aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
00131     aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
00132 
00133     gp_Vec aVec1 (aCP, aP1);
00134     gp_Vec aVec2 (aCP, aP2);
00135     gp_Dir aDir (aVec1 ^ aVec2);
00136     gp_Ax1 anAx1 (aCP, aDir);
00137     Standard_Real anAngle = aVec1.Angle(aVec2);
00138     if (fabs(anAngle) < Precision::Angular()) anAngle += 2.*M_PI; // NPAL19665
00139     aTrsf.SetRotation(anAx1, anAngle);
00140     //NPAL18620: performance problem: multiple locations are accumulated
00141     //           in shape and need a great time to process
00142     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
00143     //aShape = aTransformation.Shape();
00144     TopLoc_Location aLocOrig = anOriginal.Location();
00145     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
00146     //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
00147     aTrsfOrig.PreMultiply(aTrsf);
00148     TopLoc_Location aLocRes (aTrsfOrig);
00149     aShape = anOriginal.Located(aLocRes);
00150   }
00151   else if (aType == ROTATE_1D) {
00152     //Get direction
00153     Handle(GEOM_Function) anAxis = RI.GetAxis();
00154     if(anAxis.IsNull()) return 0;
00155     TopoDS_Shape A = anAxis->GetValue();
00156     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
00157     TopoDS_Edge anEdge = TopoDS::Edge(A);
00158 
00159     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
00160     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
00161     gp_Dir D(gp_Vec(aP1, aP2));
00162 
00163     gp_Ax1 AX1(aP1, D);
00164 
00165     Standard_Integer nbtimes = RI.GetNbIter1();
00166     Standard_Real angle = 360.0/nbtimes;
00167 
00168     TopoDS_Compound aCompound;
00169     BRep_Builder B;
00170     B.MakeCompound( aCompound );
00171 
00172     TopLoc_Location aLocOrig = anOriginal.Location();
00173     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
00174 
00175     for (int i = 0; i < nbtimes; i++ ) {
00176       if (i == 0) { // NPAL19665
00177         B.Add(aCompound, anOriginal);
00178       }
00179       else {
00180         aTrsf.SetRotation(AX1, i*angle*M_PI/180.);
00181         //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
00182         gp_Trsf aTrsfNew (aTrsfOrig);
00183         aTrsfNew.PreMultiply(aTrsf);
00184         TopLoc_Location aLocRes (aTrsfNew);
00185         B.Add(aCompound, anOriginal.Located(aLocRes));
00186       }
00187       //NPAL18620: performance problem: multiple locations are accumulated
00188       //           in shape and need a great time to process
00189       //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
00190       //B.Add(aCompound, aBRepTransformation.Shape());
00191     }
00192 
00193     aShape = aCompound;
00194   }
00195   else if (aType == ROTATE_2D) {
00196     //Get direction
00197     Handle(GEOM_Function) anAxis = RI.GetAxis();
00198     if(anAxis.IsNull()) return 0;
00199     TopoDS_Shape A = anAxis->GetValue();
00200     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
00201     TopoDS_Edge anEdge = TopoDS::Edge(A);
00202     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
00203     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
00204     gp_Dir D(gp_Vec(aP1, aP2));
00205 
00206     gp_Ax1 AX1(aP1, D);
00207 
00208     gp_Trsf aTrsf1;
00209     gp_Trsf aTrsf2;
00210 
00211     gp_XYZ aDir2 = RI.GetDir2(); // can be set by previous execution
00212     if (aDir2.Modulus() < gp::Resolution()) {
00213       // Calculate direction as vector from the axis to the shape's center
00214       gp_Pnt P1;
00215       GProp_GProps System;
00216 
00217       if (anOriginal.ShapeType() == TopAbs_VERTEX) {
00218         P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
00219       }
00220       else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
00221         BRepGProp::LinearProperties(anOriginal, System);
00222         P1 = System.CentreOfMass();
00223       }
00224       else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
00225         BRepGProp::SurfaceProperties(anOriginal, System);
00226         P1 = System.CentreOfMass();
00227       }
00228       else {
00229         BRepGProp::VolumeProperties(anOriginal, System);
00230         P1 = System.CentreOfMass();
00231       }
00232 
00233       Handle(Geom_Line) Line = new Geom_Line(AX1);
00234       GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line );
00235       gp_Pnt P2 = aPrjTool.NearestPoint();
00236 
00237       if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
00238 
00239       aDir2 = gp_XYZ(P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z());
00240 
00241       // Attention: this abnormal action is done for good working of
00242       // TransformLikeOther(), used by RestoreSubShapes functionality
00243       RI.SetDir2(aDir2);
00244     }
00245 
00246     gp_Vec Vec (aDir2);
00247     Vec.Normalize();
00248 
00249     Standard_Integer nbtimes2 = RI.GetNbIter2();
00250     Standard_Integer nbtimes1 = RI.GetNbIter1();
00251     Standard_Real step = RI.GetStep();
00252     Standard_Real ang = RI.GetAngle();
00253 
00254     TopLoc_Location aLocOrig = anOriginal.Location();
00255     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
00256 
00257     gp_Vec aVec;
00258     TopoDS_Compound aCompound;
00259     BRep_Builder B;
00260     B.MakeCompound( aCompound );
00261 
00262     Standard_Real DX, DY, DZ;
00263 
00264     for (int i = 0; i < nbtimes2; i++ ) {
00265       DX = i * step * Vec.X();
00266       DY = i * step * Vec.Y();
00267       DZ = i * step * Vec.Z();
00268       aVec.SetCoord( DX, DY, DZ );
00269       aTrsf1.SetTranslation(aVec);
00270 
00271       for (int j = 0; j < nbtimes1; j++ ) {
00272         if (j == 0) { // NPAL19665
00273           TopLoc_Location aLocRes (aTrsf1 * aTrsfOrig);
00274           B.Add(aCompound, anOriginal.Located(aLocRes));
00275         }
00276         else {
00277           aTrsf2.SetRotation(AX1, j*ang*M_PI/180.);
00278           //TopLoc_Location aLocRes (aTrsf2 * aTrsf1 * aTrsfOrig); // gp_Trsf::Multiply() has a bug
00279           gp_Trsf aTrsfNew (aTrsfOrig);
00280           aTrsfNew.PreMultiply(aTrsf1);
00281           aTrsfNew.PreMultiply(aTrsf2);
00282           TopLoc_Location aLocRes (aTrsfNew);
00283           B.Add(aCompound, anOriginal.Located(aLocRes));
00284         }
00285         //NPAL18620: performance problem: multiple locations are accumulated
00286         //           in shape and need a great time to process
00287         //BRepBuilderAPI_Transform aBRepTrsf1 (anOriginal, aTrsf1, Standard_False);
00288         //BRepBuilderAPI_Transform aBRepTrsf2 (aBRepTrsf1.Shape(), aTrsf2, Standard_False);
00289         //B.Add(aCompound, aBRepTrsf2.Shape());
00290       }
00291     }
00292 
00293     aShape = aCompound;
00294   }
00295   else return 0;
00296 
00297 
00298   if (aShape.IsNull()) return 0;
00299 
00300   aFunction->SetValue(aShape);
00301 
00302   log.SetTouched(Label());
00303 
00304   return 1;
00305 }
00306 
00307 
00308 //=======================================================================
00309 //function :  GEOMImpl_RotateDriver_Type_
00310 //purpose  :
00311 //=======================================================================
00312 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
00313 {
00314   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00315   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00316   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00317   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
00318   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00319   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00320 
00321   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00322   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RotateDriver",
00323                                                          sizeof(GEOMImpl_RotateDriver),
00324                                                          1,
00325                                                          (Standard_Address)_Ancestors,
00326                                                          (Standard_Address)NULL);
00327 
00328   return _aType;
00329 }
00330 
00331 //=======================================================================
00332 //function : DownCast
00333 //purpose  :
00334 //=======================================================================
00335 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
00336 {
00337   Handle(GEOMImpl_RotateDriver) _anOtherObject;
00338 
00339   if (!AnObject.IsNull()) {
00340      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
00341        _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
00342      }
00343   }
00344 
00345   return _anOtherObject;
00346 }