Back to index

salome-geom  6.5.0
GEOMImpl_3DSketcherDriver.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include <Standard_Stream.hxx>
00021 
00022 #include <GEOMImpl_3DSketcherDriver.hxx>
00023 #include <GEOMImpl_I3DSketcher.hxx>
00024 #include <GEOMImpl_Types.hxx>
00025 #include <GEOM_Function.hxx>
00026 
00027 #include <GEOMImpl_IMeasureOperations.hxx>
00028 
00029 // OCCT Includes
00030 #include <BRepBuilderAPI_MakePolygon.hxx>
00031 #include <BRepBuilderAPI_MakeVertex.hxx>
00032 #include <TopoDS.hxx>
00033 #include <TopoDS_Shape.hxx>
00034 #include <TopoDS_Wire.hxx>
00035 #include <gp_Pnt.hxx>
00036 
00037 #include <Standard_ConstructionError.hxx>
00038 
00039 //=======================================================================
00040 //function : GetID
00041 //purpose  :
00042 //=======================================================================
00043 const Standard_GUID& GEOMImpl_3DSketcherDriver::GetID()
00044 {
00045   static Standard_GUID a3DSketcherDriver("FF2BBB54-5D24-4df3-210B-3A678263EA26");
00046   return a3DSketcherDriver;
00047 }
00048 
00049 
00050 //=======================================================================
00051 //function : GEOMImpl_3DSketcherDriver
00052 //purpose  :
00053 //=======================================================================
00054 GEOMImpl_3DSketcherDriver::GEOMImpl_3DSketcherDriver()
00055 {
00056 }
00057 
00058 //=======================================================================
00059 //function : Execute
00060 //purpose  :
00061 //=======================================================================
00062 Standard_Integer GEOMImpl_3DSketcherDriver::Execute(TFunction_Logbook& log) const
00063 {
00064   if (Label().IsNull()) return 0;
00065   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00066   
00067   GEOMImpl_I3DSketcher aCI (aFunction);
00068 
00069   TopoDS_Shape aShape;
00070 
00071   Handle(TColStd_HArray1OfReal) aCoordsArray = aCI.GetCoordinates();
00072   int anArrayLength = aCoordsArray->Length();
00073 
00074   std::list<gp_Pnt> points;
00075   
00076   for (int i = 0; i <= (anArrayLength-3); i += 3) {
00077     gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
00078     if (points.empty() || aPnt.Distance(points.back()) > gp::Resolution())
00079       points.push_back(aPnt);
00080   }
00081 
00082   if ( points.size() == 1) { // Only Start Point
00083     BRepBuilderAPI_MakeVertex mkVertex (points.back());
00084     aShape = mkVertex.Shape();
00085   }
00086   else if ( points.size() > 1) { // Make Wire
00087     BRepBuilderAPI_MakePolygon aMakePoly;
00088     std::list<gp_Pnt>::iterator it;
00089     for (it = points.begin(); it != points.end(); ++it) {
00090       aMakePoly.Add(*it);
00091     }
00092 
00093     if (points.size() > 2 && 
00094        points.back().X() == points.front().X() && 
00095        points.back().Y() == points.front().Y() && 
00096        points.back().Z() == points.front().Z())
00097       aMakePoly.Close();
00098     
00099     if (aMakePoly.IsDone())
00100       aShape = aMakePoly.Wire();
00101   }
00102 
00103   if (aShape.IsNull()) return 0;
00104 
00105   aFunction->SetValue(aShape);
00106   log.SetTouched(Label());
00107   return 1;
00108 }
00109 
00110 
00111 //=======================================================================
00112 //function :  GEOMImpl_3DSketcherDriver_Type_
00113 //purpose  :
00114 //=======================================================================
00115 Standard_EXPORT Handle_Standard_Type& GEOMImpl_3DSketcherDriver_Type_()
00116 {
00117 
00118   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00119   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00120   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00121   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
00122   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00123   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00124 
00125 
00126   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00127   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_3DSketcherDriver",
00128                                                          sizeof(GEOMImpl_3DSketcherDriver),
00129                                                          1,
00130                                                          (Standard_Address)_Ancestors,
00131                                                          (Standard_Address)NULL);
00132 
00133   return _aType;
00134 }
00135 
00136 //=======================================================================
00137 //function : DownCast
00138 //purpose  :
00139 //=======================================================================
00140 const Handle(GEOMImpl_3DSketcherDriver) Handle(GEOMImpl_3DSketcherDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
00141 {
00142   Handle(GEOMImpl_3DSketcherDriver) _anOtherObject;
00143 
00144   if (!AnObject.IsNull()) {
00145      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_3DSketcherDriver))) {
00146        _anOtherObject = Handle(GEOMImpl_3DSketcherDriver)((Handle(GEOMImpl_3DSketcherDriver)&)AnObject);
00147      }
00148   }
00149 
00150   return _anOtherObject ;
00151 }