Back to index

salome-geom  6.5.0
GEOMImpl_ScaleDriver.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_ScaleDriver.hxx>
00026 #include <GEOMImpl_IScale.hxx>
00027 #include <GEOMImpl_Types.hxx>
00028 #include <GEOM_Function.hxx>
00029 
00030 #include <ShapeFix_Shape.hxx>
00031 #include <ShapeFix_ShapeTolerance.hxx>
00032 
00033 #include <BRepBuilderAPI_Transform.hxx>
00034 #include <BRepBuilderAPI_GTransform.hxx>
00035 #include <BRep_Tool.hxx>
00036 #include <BRepAlgo.hxx>
00037 #include <BRepCheck_Analyzer.hxx>
00038 
00039 #include <TopAbs.hxx>
00040 #include <TopExp.hxx>
00041 #include <TopoDS.hxx>
00042 #include <TopoDS_Shape.hxx>
00043 #include <TopoDS_Vertex.hxx>
00044 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
00045 
00046 #include <Precision.hxx>
00047 #include <gp_Pnt.hxx>
00048 #include <gp_Trsf.hxx>
00049 #include <gp_GTrsf.hxx>
00050 
00051 //=======================================================================
00052 //function : GetID
00053 //purpose  :
00054 //======================================================================= 
00055 const Standard_GUID& GEOMImpl_ScaleDriver::GetID()
00056 {
00057   static Standard_GUID aScaleDriver("FF1BBB52-5D14-4df2-980B-3A668264EA16");
00058   return aScaleDriver; 
00059 }
00060 
00061 
00062 //=======================================================================
00063 //function : GEOMImpl_ScaleDriver
00064 //purpose  : 
00065 //=======================================================================
00066 GEOMImpl_ScaleDriver::GEOMImpl_ScaleDriver() 
00067 {
00068 }
00069 
00070 //=======================================================================
00071 //function : Execute
00072 //purpose  :
00073 //======================================================================= 
00074 Standard_Integer GEOMImpl_ScaleDriver::Execute(TFunction_Logbook& log) const
00075 {
00076   if (Label().IsNull()) return 0;    
00077   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00078 
00079   GEOMImpl_IScale aCI (aFunction);
00080   Standard_Integer aType = aFunction->GetType();
00081 
00082   TopoDS_Shape aShape;
00083 
00084   if (aType == SCALE_SHAPE || aType == SCALE_SHAPE_COPY) {
00085     Handle(GEOM_Function) aRefShape = aCI.GetShape();
00086     TopoDS_Shape aShapeBase = aRefShape->GetValue();
00087     if (aShapeBase.IsNull()) return 0;
00088 
00089     gp_Pnt aP (0,0,0);
00090     Handle(GEOM_Function) aRefPoint = aCI.GetPoint();
00091     if (!aRefPoint.IsNull()) {
00092       TopoDS_Shape aShapePnt = aRefPoint->GetValue();
00093       if (aShapePnt.IsNull()) return 0;
00094       if (aShapePnt.ShapeType() != TopAbs_VERTEX) return 0;
00095       aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
00096     }
00097 
00098     // Bug 6839: Check for standalone (not included in faces) degenerated edges
00099     TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
00100     TopExp::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, aEFMap);
00101     Standard_Integer i, nbE = aEFMap.Extent();
00102     for (i = 1; i <= nbE; i++) {
00103       TopoDS_Shape anEdgeSh = aEFMap.FindKey(i);
00104       if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeSh))) {
00105         const TopTools_ListOfShape& aFaces = aEFMap.FindFromIndex(i);
00106         if (aFaces.IsEmpty())
00107           Standard_ConstructionError::Raise
00108             ("Scaling aborted : cannot scale standalone degenerated edge");
00109       }
00110     }
00111 
00112     // Perform Scaling
00113     gp_Trsf aTrsf;
00114     aTrsf.SetScale(aP, aCI.GetFactor());
00115     BRepBuilderAPI_Transform aBRepTrsf (aShapeBase, aTrsf, Standard_False);
00116     aShape = aBRepTrsf.Shape();
00117   }
00118   else if (aType == SCALE_SHAPE_AXES || aType == SCALE_SHAPE_AXES_COPY) {
00119     Handle(GEOM_Function) aRefShape = aCI.GetShape();
00120     TopoDS_Shape aShapeBase = aRefShape->GetValue();
00121     if (aShapeBase.IsNull()) return 0;
00122 
00123     bool isP = false;
00124     gp_Pnt aP (0,0,0);
00125     Handle(GEOM_Function) aRefPoint = aCI.GetPoint();
00126     if (!aRefPoint.IsNull()) {
00127       TopoDS_Shape aShapePnt = aRefPoint->GetValue();
00128       if (aShapePnt.IsNull()) return 0;
00129       if (aShapePnt.ShapeType() != TopAbs_VERTEX) return 0;
00130       aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
00131       isP = true;
00132     }
00133 
00134     // Bug 6839: Check for standalone (not included in faces) degenerated edges
00135     TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
00136     TopExp::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, aEFMap);
00137     Standard_Integer i, nbE = aEFMap.Extent();
00138     for (i = 1; i <= nbE; i++) {
00139       TopoDS_Shape anEdgeSh = aEFMap.FindKey(i);
00140       if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeSh))) {
00141         const TopTools_ListOfShape& aFaces = aEFMap.FindFromIndex(i);
00142         if (aFaces.IsEmpty())
00143           Standard_ConstructionError::Raise
00144             ("Scaling aborted : cannot scale standalone degenerated edge");
00145       }
00146     }
00147 
00148     // Perform Scaling
00149     gp_GTrsf aGTrsf;
00150     gp_Mat rot (aCI.GetFactorX(), 0, 0,
00151                 0, aCI.GetFactorY(), 0,
00152                 0, 0, aCI.GetFactorZ());
00153     aGTrsf.SetVectorialPart(rot);
00154 
00155     if (isP) {
00156       gp_Pnt anO (0,0,0);
00157       if (anO.Distance(aP) > Precision::Confusion()) {
00158         gp_GTrsf aGTrsfP0;
00159         aGTrsfP0.SetTranslationPart(anO.XYZ() - aP.XYZ());
00160         gp_GTrsf aGTrsf0P;
00161         aGTrsf0P.SetTranslationPart(aP.XYZ());
00162         //aGTrsf = aGTrsf0P * aGTrsf * aGTrsfP0;
00163         aGTrsf = aGTrsf0P.Multiplied(aGTrsf);
00164         aGTrsf = aGTrsf.Multiplied(aGTrsfP0);
00165       }
00166     }
00167 
00168     BRepBuilderAPI_GTransform aBRepGTrsf (aShapeBase, aGTrsf, Standard_False);
00169     if (!aBRepGTrsf.IsDone())
00170       Standard_ConstructionError::Raise("Scaling not done");
00171     aShape = aBRepGTrsf.Shape();
00172   }
00173   else {
00174   }
00175 
00176   if (aShape.IsNull()) return 0;
00177 
00178   BRepCheck_Analyzer ana (aShape, Standard_False);
00179   if (!ana.IsValid()) {
00180     ShapeFix_ShapeTolerance aSFT;
00181     aSFT.LimitTolerance(aShape,Precision::Confusion(),Precision::Confusion());
00182     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
00183     aSfs->SetPrecision(Precision::Confusion());
00184     aSfs->Perform();
00185     aShape = aSfs->Shape();
00186 
00187     ana.Init(aShape, Standard_False);
00188     if (!ana.IsValid())
00189       Standard_ConstructionError::Raise("Scaling aborted : algorithm has produced an invalid shape result");
00190   }
00191 
00192   aFunction->SetValue(aShape);
00193 
00194   log.SetTouched(Label()); 
00195 
00196   return 1;    
00197 }
00198 
00199 
00200 //=======================================================================
00201 //function :  GEOMImpl_ScaleDriver_Type_
00202 //purpose  :
00203 //======================================================================= 
00204 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ScaleDriver_Type_()
00205 {
00206 
00207   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00208   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00209   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00210   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
00211   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00212   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00213  
00214 
00215   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00216   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ScaleDriver",
00217                                                          sizeof(GEOMImpl_ScaleDriver),
00218                                                          1,
00219                                                          (Standard_Address)_Ancestors,
00220                                                          (Standard_Address)NULL);
00221 
00222   return _aType;
00223 }
00224 
00225 //=======================================================================
00226 //function : DownCast
00227 //purpose  :
00228 //======================================================================= 
00229 const Handle(GEOMImpl_ScaleDriver) Handle(GEOMImpl_ScaleDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
00230 {
00231   Handle(GEOMImpl_ScaleDriver) _anOtherObject;
00232 
00233   if (!AnObject.IsNull()) {
00234      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ScaleDriver))) {
00235        _anOtherObject = Handle(GEOMImpl_ScaleDriver)((Handle(GEOMImpl_ScaleDriver)&)AnObject);
00236      }
00237   }
00238 
00239   return _anOtherObject;
00240 }