Back to index

salome-geom  6.5.0
GEOMImpl_PolylineDriver.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_PolylineDriver.hxx>
00026 #include <GEOMImpl_IPolyline.hxx>
00027 #include <GEOMImpl_Types.hxx>
00028 #include <GEOM_Function.hxx>
00029 
00030 #include <TColgp_Array1OfPnt.hxx>
00031 #include <BRepBuilderAPI_MakeVertex.hxx>
00032 #include <BRepBuilderAPI_MakePolygon.hxx>
00033 #include <BRep_Tool.hxx>
00034 #include <TopoDS.hxx>
00035 #include <TopoDS_Shape.hxx>
00036 #include <TopoDS_Vertex.hxx>
00037 #include <TopoDS_Wire.hxx>
00038 #include <TopAbs.hxx>
00039 #include <TopExp.hxx>
00040 
00041 #include <Precision.hxx>
00042 #include <gp_Pnt.hxx>
00043 
00044 //=======================================================================
00045 //function : GetID
00046 //purpose  :
00047 //======================================================================= 
00048 const Standard_GUID& GEOMImpl_PolylineDriver::GetID()
00049 {
00050   static Standard_GUID aPolylineDriver("FF1BBB31-5D14-4df2-980B-3A668264EA16");
00051   return aPolylineDriver; 
00052 }
00053 
00054 
00055 //=======================================================================
00056 //function : GEOMImpl_PolylineDriver
00057 //purpose  : 
00058 //=======================================================================
00059 GEOMImpl_PolylineDriver::GEOMImpl_PolylineDriver() 
00060 {
00061 }
00062 
00063 //=======================================================================
00064 //function : Execute
00065 //purpose  :
00066 //======================================================================= 
00067 Standard_Integer GEOMImpl_PolylineDriver::Execute(TFunction_Logbook& log) const
00068 {
00069   if (Label().IsNull()) return 0;    
00070   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00071 
00072   GEOMImpl_IPolyline aCI (aFunction);
00073   Standard_Integer aType = aFunction->GetType();
00074   
00075   TopoDS_Shape aShape;
00076 
00077   if (aType == POLYLINE_POINTS) {
00078 
00079     bool useCoords = aCI.GetConstructorType() == COORD_CONSTRUCTOR;
00080     TColgp_Array1OfPnt points(1, (useCoords ? aCI.GetLength() : 1) );
00081     if(useCoords) {
00082       Handle(TColStd_HArray1OfReal) aCoordsArray = aCI.GetCoordinates();
00083       int anArrayLength = aCoordsArray->Length();
00084       for (int i = 0, j = 1; i <= (anArrayLength-3); i += 3) {
00085        gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
00086        points.SetValue(j,aPnt);
00087        j++;
00088       } 
00089     }
00090 
00091     int aLen = aCI.GetLength();
00092     int ind = 1;
00093     BRepBuilderAPI_MakePolygon aMakePoly;
00094     for (; ind <= aLen; ind++)
00095     {
00096       if(useCoords) {
00097        aMakePoly.Add(BRepBuilderAPI_MakeVertex(points.Value(ind)));
00098       } else {
00099        Handle(GEOM_Function) aRefPoint = aCI.GetPoint(ind);
00100        TopoDS_Shape aShapePnt = aRefPoint->GetValue();
00101        if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
00102          Standard_TypeMismatch::Raise
00103            ("Polyline creation aborted : arguments are not a vertexes");
00104          return 0;
00105        }
00106        if (aShapePnt.ShapeType() == TopAbs_VERTEX) {
00107          aMakePoly.Add(TopoDS::Vertex(aShapePnt));
00108          //if (!aMakePoly.Added()) return 0;
00109        }
00110       }
00111     }
00112     // Compare first and last point coordinates and close polyline if it's the same.
00113     if ( aLen > 2 ) {
00114       TopoDS_Vertex aV1;
00115       if( useCoords ) {
00116        aV1 = BRepBuilderAPI_MakeVertex(points.Value(1));
00117       } else {
00118        Handle(GEOM_Function) aFPoint = aCI.GetPoint(1);
00119        TopoDS_Shape aFirstPnt = aFPoint->GetValue();
00120        aV1 = TopoDS::Vertex(aFirstPnt);
00121       }
00122       
00123       TopoDS_Vertex aV2;
00124       if( useCoords ) {
00125        aV2 = BRepBuilderAPI_MakeVertex(points.Value(aLen));
00126       } else {
00127        Handle(GEOM_Function) aLPoint = aCI.GetPoint(aLen);
00128        TopoDS_Shape aLastPnt = aLPoint->GetValue();
00129        aV2 = TopoDS::Vertex(aLastPnt);
00130       }
00131       
00132       if ( (!aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2)) ||
00133            aCI.GetIsClosed())
00134         aMakePoly.Close();
00135     }
00136     
00137     if (aMakePoly.IsDone()) {
00138       aShape = aMakePoly.Wire();
00139     }
00140   }
00141   else {
00142   }
00143 
00144   if (aShape.IsNull()) return 0;
00145   
00146   aFunction->SetValue(aShape);
00147   
00148   log.SetTouched(Label()); 
00149   
00150   return 1;    
00151 }
00152 
00153 
00154 //=======================================================================
00155 //function :  GEOMImpl_PolylineDriver_Type_
00156 //purpose  :
00157 //======================================================================= 
00158 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PolylineDriver_Type_()
00159 {
00160 
00161   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00162   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00163   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00164   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
00165   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00166   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00167  
00168 
00169   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00170   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PolylineDriver",
00171                                                          sizeof(GEOMImpl_PolylineDriver),
00172                                                          1,
00173                                                          (Standard_Address)_Ancestors,
00174                                                          (Standard_Address)NULL);
00175 
00176   return _aType;
00177 }
00178 
00179 //=======================================================================
00180 //function : DownCast
00181 //purpose  :
00182 //======================================================================= 
00183 const Handle(GEOMImpl_PolylineDriver) Handle(GEOMImpl_PolylineDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
00184 {
00185   Handle(GEOMImpl_PolylineDriver) _anOtherObject;
00186 
00187   if (!AnObject.IsNull()) {
00188      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PolylineDriver))) {
00189        _anOtherObject = Handle(GEOMImpl_PolylineDriver)((Handle(GEOMImpl_PolylineDriver)&)AnObject);
00190      }
00191   }
00192 
00193   return _anOtherObject ;
00194 }