//================================================================
// Function : DrawSurface                           
// Purpose  : displays a given geometric surface in 3d viewer
//            (creates a finite face and displays it)
//================================================================
Handle_AIS_InteractiveObject OCCDemo_Presentation::drawSurface
                                  (const Handle_Geom_Surface& theSurface,
                                   const Quantity_Color& theColor,
                                   const Standard_Boolean toDisplay)
{
  Standard_Real u1, u2, v1, v2;
  theSurface->Bounds(u1,u2,v1,v2);
  fixParam(u1);
  fixParam(u2);
  fixParam(v1);
  fixParam(v2);

  Handle_AIS_Shape aGraphicSurface = 
    new AIS_Shape(BRepBuilderAPI_MakeFace (theSurface, u1, u2, v1, v2));

  getAISContext()->SetMaterial(aGraphicSurface, Graphic3d_NOM_PLASTIC, toDisplay);
  getAISContext()->SetColor(aGraphicSurface, theColor, toDisplay);
  if (toDisplay) {
    if (FitMode){
		getAISContext()->Display (aGraphicSurface, Standard_False);
		COCCDemoDoc::Fit();
	}
	else
		getAISContext()->Display (aGraphicSurface);
  }
  
  return aGraphicSurface;
}
PyObject* GeometrySurfacePy::toShape(PyObject *args)
{
    Handle_Geom_Geometry g = getGeometryPtr()->handle();
    Handle_Geom_Surface s = Handle_Geom_Surface::DownCast(g);
    try {
        if (!s.IsNull()) {
            double u1,u2,v1,v2;
            s->Bounds(u1,u2,v1,v2);
            if (!PyArg_ParseTuple(args, "|dddd", &u1,&u2,&v1,&v2))
                return 0;
            BRepBuilderAPI_MakeFace mkBuilder(s, u1, u2, v1, v2
#if OCC_VERSION_HEX >= 0x060502
              , Precision::Confusion()
#endif
            );
            TopoDS_Shape sh = mkBuilder.Shape();
            return new TopoShapeFacePy(new TopoShape(sh));
        }
    }
    catch (Standard_Failure) {
        Handle_Standard_Failure e = Standard_Failure::Caught();
        PyErr_SetString(PartExceptionOCCError, e->GetMessageString());
        return 0;
    }

    PyErr_SetString(PartExceptionOCCError, "Geometry is not a surface");
    return 0;
}
PyObject* GeometrySurfacePy::bounds(PyObject * args)
{
    if (!PyArg_ParseTuple(args, ""))
        return 0;

    Handle_Geom_Surface surf = Handle_Geom_Surface
        ::DownCast(getGeometryPtr()->handle());
    Py::Tuple bound(4);
    Standard_Real u1,u2,v1,v2;
    surf->Bounds(u1,u2,v1,v2);
    bound.setItem(0,Py::Float(u1));
    bound.setItem(1,Py::Float(u2));
    bound.setItem(2,Py::Float(v1));
    bound.setItem(3,Py::Float(v2));
    return Py::new_reference_to(bound);
}
// constructor method
int PlateSurfacePy::PyInit(PyObject* args, PyObject* kwds)
{
    static char* kwds_Parameter[] = {"Surface","Points","Curves","Degree",
        "NbPtsOnCur","NbIter","Tol2d","Tol3d","TolAng","TolCurv","Anisotropie",NULL};

    PyObject* surface = 0;
    PyObject* points = 0;
    PyObject* curves = 0;
    int Degree = 3;
    int NbPtsOnCur = 10;
    int NbIter = 3;
    double Tol2d = 0.00001;
    double Tol3d = 0.0001;
    double TolAng = 0.01;
    double TolCurv = 0.1;
    PyObject* Anisotropie = Py_False;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O!OOiiiddddO!", kwds_Parameter,
        &(GeometryPy::Type), &surface, &points, &curves,
        &Degree, &NbPtsOnCur, &NbIter, &Tol2d, &Tol3d, &TolAng, &TolCurv,
        &PyBool_Type,&Anisotropie))
        return -1;

    if (!surface && !points && !curves) {
        PyErr_SetString(PyExc_ValueError, "set points or curves as constraints");
        return -1;
    }

    Handle_Geom_Surface surf;
    if (surface) {
        GeometryPy* pcGeo = static_cast<GeometryPy*>(surface);
        surf = Handle_Geom_Surface::DownCast
            (pcGeo->getGeometryPtr()->handle());
        if (surf.IsNull()) {
            PyErr_SetString(PyExc_TypeError, "geometry is not a surface");
            return -1;
        }
    }

    try {
        GeomPlate_BuildPlateSurface buildPlate(Degree, NbPtsOnCur, NbIter, Tol2d, Tol3d, TolAng, TolCurv,
            PyObject_IsTrue(Anisotropie) ? Standard_True : Standard_False);
        if (!surf.IsNull()) {
            buildPlate.LoadInitSurface(surf);

            if (!points && !curves) {
                Standard_Real U1,U2,V1,V2;
                surf->Bounds(U1,U2,V1,V2);
                buildPlate.Add(new GeomPlate_PointConstraint(surf->Value(U1,V1),0));
                buildPlate.Add(new GeomPlate_PointConstraint(surf->Value(U1,V2),0));
                buildPlate.Add(new GeomPlate_PointConstraint(surf->Value(U2,V1),0));
                buildPlate.Add(new GeomPlate_PointConstraint(surf->Value(U2,V2),0));
            }
        }

        if (points) {
            Py::Sequence list(points);
            for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
                Base::Vector3d vec = Py::Vector(*it).toVector();
                Handle(GeomPlate_PointConstraint) PCont = new GeomPlate_PointConstraint(gp_Pnt(vec.x,vec.y,vec.z),0);
                buildPlate.Add(PCont);
            }
        }

        if (curves) {
            Py::Sequence list(curves);
            for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
                //TODO
            }
        }

        buildPlate.Perform();
        getGeomPlateSurfacePtr()->setHandle(buildPlate.Surface());
        return 0;
    }
    catch (Standard_Failure) {
        Handle_Standard_Failure e = Standard_Failure::Caught();
        PyErr_SetString(PartExceptionOCCError, e->GetMessageString());
        return -1;
    }
}