00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <BRep_Builder.hxx>
00027 # include <BRep_Tool.hxx>
00028 # include <BRepTools.hxx>
00029 # include <BRepBuilderAPI_MakeFace.hxx>
00030 # include <ShapeAnalysis.hxx>
00031 # include <BRepAdaptor_Surface.hxx>
00032 # include <BRepLProp_SLProps.hxx>
00033 # include <BRepOffsetAPI_MakeOffset.hxx>
00034 # include <Geom_BezierSurface.hxx>
00035 # include <Geom_BSplineSurface.hxx>
00036 # include <Geom_Plane.hxx>
00037 # include <Geom_CylindricalSurface.hxx>
00038 # include <Geom_ConicalSurface.hxx>
00039 # include <Geom_SphericalSurface.hxx>
00040 # include <Geom_ToroidalSurface.hxx>
00041 # include <Handle_Geom_Surface.hxx>
00042 # include <TopoDS.hxx>
00043 # include <TopoDS_Face.hxx>
00044 # include <TopoDS_Wire.hxx>
00045 # include <gp_Pnt2d.hxx>
00046 # include <gp_Pln.hxx>
00047 # include <gp_Cylinder.hxx>
00048 # include <gp_Cone.hxx>
00049 # include <gp_Sphere.hxx>
00050 # include <gp_Torus.hxx>
00051 #endif
00052
00053 #include <BRepTopAdaptor_FClass2d.hxx>
00054 #include <BRepPrimAPI_MakeHalfSpace.hxx>
00055 #include <BRepGProp.hxx>
00056 #include <GProp_GProps.hxx>
00057 #include <BRepLProp_SurfaceTool.hxx>
00058 #include <BRepGProp_Face.hxx>
00059 #include <GeomLProp_SLProps.hxx>
00060
00061 #include <Base/VectorPy.h>
00062 #include <Base/GeometryPyCXX.h>
00063
00064 #include "TopoShape.h"
00065 #include "TopoShapeSolidPy.h"
00066 #include "TopoShapeWirePy.h"
00067 #include "TopoShapeFacePy.h"
00068 #include "TopoShapeFacePy.cpp"
00069
00070 #include "BezierSurfacePy.h"
00071 #include "BSplineSurfacePy.h"
00072 #include "PlanePy.h"
00073 #include "CylinderPy.h"
00074 #include "ConePy.h"
00075 #include "SpherePy.h"
00076 #include "OffsetSurfacePy.h"
00077 #include "SurfaceOfRevolutionPy.h"
00078 #include "SurfaceOfExtrusionPy.h"
00079 #include "ToroidPy.h"
00080
00081 using namespace Part;
00082
00083
00084 std::string TopoShapeFacePy::representation(void) const
00085 {
00086 std::stringstream str;
00087 str << "<Face object at " << getTopoShapePtr() << ">";
00088
00089 return str.str();
00090 }
00091
00092 PyObject *TopoShapeFacePy::PyMake(struct _typeobject *, PyObject *, PyObject *)
00093 {
00094
00095 return new TopoShapeFacePy(new TopoShape);
00096 }
00097
00098
00099 int TopoShapeFacePy::PyInit(PyObject* args, PyObject* )
00100 {
00101 PyObject *pW;
00102 if (PyArg_ParseTuple(args, "O!", &(Part::TopoShapePy::Type), &pW)) {
00103 try {
00104 const TopoDS_Shape& sh = static_cast<Part::TopoShapePy*>(pW)->getTopoShapePtr()->_Shape;
00105 if (sh.IsNull()) {
00106 PyErr_SetString(PyExc_Exception, "cannot create face out of empty wire");
00107 return -1;
00108 }
00109
00110 if (sh.ShapeType() == TopAbs_WIRE) {
00111 BRepBuilderAPI_MakeFace mkFace(TopoDS::Wire(sh));
00112 getTopoShapePtr()->_Shape = mkFace.Face();
00113 return 0;
00114 }
00115 else if (sh.ShapeType() == TopAbs_FACE) {
00116 getTopoShapePtr()->_Shape = sh;
00117 return 0;
00118 }
00119 }
00120 catch (Standard_Failure) {
00121 Handle_Standard_Failure e = Standard_Failure::Caught();
00122 PyErr_SetString(PyExc_Exception, e->GetMessageString());
00123 return -1;
00124 }
00125 }
00126
00127 PyErr_Clear();
00128 PyObject *surf, *bound=0;
00129 if (PyArg_ParseTuple(args, "O!|O!", &(GeometryPy::Type), &surf, &(PyList_Type), &bound)) {
00130 try {
00131 Handle_Geom_Surface S = Handle_Geom_Surface::DownCast
00132 (static_cast<GeometryPy*>(surf)->getGeometryPtr()->handle());
00133 if (S.IsNull()) {
00134 PyErr_SetString(PyExc_TypeError, "geometry is not a valid surface");
00135 return -1;
00136 }
00137
00138 BRepBuilderAPI_MakeFace mkFace(S);
00139 if (bound) {
00140 Py::List list(bound);
00141 for (Py::List::iterator it = list.begin(); it != list.end(); ++it) {
00142 PyObject* item = (*it).ptr();
00143 if (PyObject_TypeCheck(item, &(Part::TopoShapePy::Type))) {
00144 const TopoDS_Shape& sh = static_cast<Part::TopoShapePy*>(item)->getTopoShapePtr()->_Shape;
00145 if (sh.ShapeType() == TopAbs_WIRE)
00146 mkFace.Add(TopoDS::Wire(sh));
00147 else {
00148 PyErr_SetString(PyExc_TypeError, "shape is not a wire");
00149 return -1;
00150 }
00151 }
00152 else {
00153 PyErr_SetString(PyExc_TypeError, "item is not a shape");
00154 return -1;
00155 }
00156 }
00157 }
00158
00159 getTopoShapePtr()->_Shape = mkFace.Face();
00160 return 0;
00161 }
00162 catch (Standard_Failure) {
00163 Handle_Standard_Failure e = Standard_Failure::Caught();
00164 PyErr_SetString(PyExc_Exception, e->GetMessageString());
00165 return -1;
00166 }
00167 }
00168
00169 PyErr_Clear();
00170 if (PyArg_ParseTuple(args, "O!", &(PyList_Type), &bound)) {
00171 try {
00172 std::vector<TopoDS_Wire> wires;
00173 Py::List list(bound);
00174 for (Py::List::iterator it = list.begin(); it != list.end(); ++it) {
00175 PyObject* item = (*it).ptr();
00176 if (PyObject_TypeCheck(item, &(Part::TopoShapePy::Type))) {
00177 const TopoDS_Shape& sh = static_cast<Part::TopoShapePy*>(item)->getTopoShapePtr()->_Shape;
00178 if (sh.ShapeType() == TopAbs_WIRE)
00179 wires.push_back(TopoDS::Wire(sh));
00180 else
00181 Standard_Failure::Raise("shape is not a wire");
00182 }
00183 else
00184 Standard_Failure::Raise("shape is not a wire");
00185 }
00186
00187 if (!wires.empty()) {
00188 BRepBuilderAPI_MakeFace mkFace(wires.front());
00189 for (std::vector<TopoDS_Wire>::iterator it = wires.begin()+1; it != wires.end(); ++it)
00190 mkFace.Add(*it);
00191 getTopoShapePtr()->_Shape = mkFace.Face();
00192 return 0;
00193 }
00194 else {
00195 Standard_Failure::Raise("no wires in list");
00196 }
00197 }
00198 catch (Standard_Failure) {
00199 Handle_Standard_Failure e = Standard_Failure::Caught();
00200 PyErr_SetString(PyExc_Exception, e->GetMessageString());
00201 return -1;
00202 }
00203 }
00204
00205 PyErr_SetString(PyExc_Exception, "wire or list of wires expected");
00206 return -1;
00207 }
00208
00209 PyObject* TopoShapeFacePy::makeOffset(PyObject *args)
00210 {
00211 float dist;
00212 if (!PyArg_ParseTuple(args, "f",&dist))
00213 return 0;
00214 const TopoDS_Face& f = TopoDS::Face(getTopoShapePtr()->_Shape);
00215
00216 BRepOffsetAPI_MakeOffset mkOffset(f);
00217 mkOffset.Perform(dist);
00218
00219 return new TopoShapePy(new TopoShape(mkOffset.Shape()));
00220 }
00221
00222 PyObject* TopoShapeFacePy::valueAt(PyObject *args)
00223 {
00224 double u,v;
00225 if (!PyArg_ParseTuple(args, "dd",&u,&v))
00226 return 0;
00227
00228 const TopoDS_Face& f = TopoDS::Face(getTopoShapePtr()->_Shape);
00229
00230 BRepAdaptor_Surface adapt(f);
00231 BRepLProp_SLProps prop(adapt,u,v,0,Precision::Confusion());
00232 const gp_Pnt& V = prop.Value();
00233 return new Base::VectorPy(new Base::Vector3d(V.X(),V.Y(),V.Z()));
00234 }
00235
00236 PyObject* TopoShapeFacePy::normalAt(PyObject *args)
00237 {
00238 double u,v;
00239 if (!PyArg_ParseTuple(args, "dd",&u,&v))
00240 return 0;
00241
00242 const TopoDS_Face& f = TopoDS::Face(getTopoShapePtr()->_Shape);
00243 BRepAdaptor_Surface adapt(f);
00244
00245 BRepLProp_SLProps prop(adapt,u,v,1,Precision::Confusion());
00246 if (prop.IsNormalDefined()) {
00247 gp_Pnt pnt; gp_Vec vec;
00248
00249 BRepGProp_Face(f).Normal(u,v,pnt,vec);
00250 return new Base::VectorPy(new Base::Vector3d(vec.X(),vec.Y(),vec.Z()));
00251 }
00252 else {
00253 PyErr_SetString(PyExc_Exception, "normal not defined");
00254 return 0;
00255 }
00256 }
00257
00258 PyObject* TopoShapeFacePy::tangentAt(PyObject *args)
00259 {
00260 double u,v;
00261 if (!PyArg_ParseTuple(args, "dd",&u,&v))
00262 return 0;
00263
00264 gp_Dir dir;
00265 Py::Tuple tuple(2);
00266 const TopoDS_Face& f = TopoDS::Face(getTopoShapePtr()->_Shape);
00267 BRepAdaptor_Surface adapt(f);
00268
00269 BRepLProp_SLProps prop(adapt,u,v,1,Precision::Confusion());
00270 if (prop.IsTangentUDefined()) {
00271 prop.TangentU(dir);
00272 tuple.setItem(0, Py::Vector(Base::Vector3d(dir.X(),dir.Y(),dir.Z())));
00273 }
00274 else {
00275 PyErr_SetString(PyExc_Exception, "tangent in u not defined");
00276 return 0;
00277 }
00278 if (prop.IsTangentVDefined()) {
00279 prop.TangentV(dir);
00280 tuple.setItem(1, Py::Vector(Base::Vector3d(dir.X(),dir.Y(),dir.Z())));
00281 }
00282 else {
00283 PyErr_SetString(PyExc_Exception, "tangent in v not defined");
00284 return 0;
00285 }
00286
00287 return Py::new_reference_to(tuple);
00288 }
00289
00290 PyObject* TopoShapeFacePy::curvatureAt(PyObject *args)
00291 {
00292 double u,v;
00293 if (!PyArg_ParseTuple(args, "dd",&u,&v))
00294 return 0;
00295
00296 Py::Tuple tuple(2);
00297 const TopoDS_Face& f = TopoDS::Face(getTopoShapePtr()->_Shape);
00298 BRepAdaptor_Surface adapt(f);
00299
00300 BRepLProp_SLProps prop(adapt,u,v,2,Precision::Confusion());
00301 if (prop.IsCurvatureDefined()) {
00302 tuple.setItem(0, Py::Float(prop.MinCurvature()));
00303 tuple.setItem(1, Py::Float(prop.MaxCurvature()));
00304 }
00305 else {
00306 PyErr_SetString(PyExc_Exception, "curvature not defined");
00307 return 0;
00308 }
00309
00310 return Py::new_reference_to(tuple);
00311 }
00312
00313 PyObject* TopoShapeFacePy::derivative1At(PyObject *args)
00314 {
00315 double u,v;
00316 if (!PyArg_ParseTuple(args, "dd",&u,&v))
00317 return 0;
00318
00319 Py::Tuple tuple(2);
00320 const TopoDS_Face& f = TopoDS::Face(getTopoShapePtr()->_Shape);
00321 BRepAdaptor_Surface adapt(f);
00322
00323 try {
00324 BRepLProp_SLProps prop(adapt,u,v,1,Precision::Confusion());
00325 const gp_Vec& vecU = prop.D1U();
00326 tuple.setItem(0, Py::Vector(Base::Vector3d(vecU.X(),vecU.Y(),vecU.Z())));
00327 const gp_Vec& vecV = prop.D1V();
00328 tuple.setItem(1, Py::Vector(Base::Vector3d(vecV.X(),vecV.Y(),vecV.Z())));
00329 return Py::new_reference_to(tuple);
00330 }
00331 catch (Standard_Failure) {
00332 Handle_Standard_Failure e = Standard_Failure::Caught();
00333 PyErr_SetString(PyExc_Exception, e->GetMessageString());
00334 return 0;
00335 }
00336 }
00337
00338 PyObject* TopoShapeFacePy::derivative2At(PyObject *args)
00339 {
00340 double u,v;
00341 if (!PyArg_ParseTuple(args, "dd",&u,&v))
00342 return 0;
00343
00344 Py::Tuple tuple(2);
00345 const TopoDS_Face& f = TopoDS::Face(getTopoShapePtr()->_Shape);
00346 BRepAdaptor_Surface adapt(f);
00347
00348 try {
00349 BRepLProp_SLProps prop(adapt,u,v,2,Precision::Confusion());
00350 const gp_Vec& vecU = prop.D2U();
00351 tuple.setItem(0, Py::Vector(Base::Vector3d(vecU.X(),vecU.Y(),vecU.Z())));
00352 const gp_Vec& vecV = prop.D2V();
00353 tuple.setItem(1, Py::Vector(Base::Vector3d(vecV.X(),vecV.Y(),vecV.Z())));
00354 return Py::new_reference_to(tuple);
00355 }
00356 catch (Standard_Failure) {
00357 Handle_Standard_Failure e = Standard_Failure::Caught();
00358 PyErr_SetString(PyExc_Exception, e->GetMessageString());
00359 return 0;
00360 }
00361 }
00362
00363 PyObject* TopoShapeFacePy::isPartOfDomain(PyObject *args)
00364 {
00365 double u,v;
00366 if (!PyArg_ParseTuple(args, "dd",&u,&v))
00367 return 0;
00368
00369 const TopoDS_Face& face = TopoDS::Face(getTopoShapePtr()->_Shape);
00370
00371 double tol;
00372
00373 tol = Precision::Confusion();
00374 try {
00375
00376
00377
00378
00379 BRepTopAdaptor_FClass2d CL(face,tol);
00380 TopAbs_State state = CL.Perform(gp_Pnt2d(u,v));
00381 if (state == TopAbs_ON || state == TopAbs_IN) {
00382 Py_INCREF(Py_True);
00383 return Py_True;
00384 }
00385 else {
00386 Py_INCREF(Py_False);
00387 return Py_False;
00388 }
00389 }
00390 catch (Standard_Failure) {
00391 Handle_Standard_Failure e = Standard_Failure::Caught();
00392 PyErr_SetString(PyExc_Exception, e->GetMessageString());
00393 return 0;
00394 }
00395 }
00396
00397 PyObject* TopoShapeFacePy::makeHalfSpace(PyObject *args)
00398 {
00399 PyObject* pPnt;
00400 if (!PyArg_ParseTuple(args, "O!",&(Base::VectorPy::Type),&pPnt))
00401 return 0;
00402
00403 try {
00404 Base::Vector3d pt = Py::Vector(pPnt,false).toVector();
00405 BRepPrimAPI_MakeHalfSpace mkHS(TopoDS::Face(this->getTopoShapePtr()->_Shape), gp_Pnt(pt.x,pt.y,pt.z));
00406 return new TopoShapeSolidPy(new TopoShape(mkHS.Solid()));
00407 }
00408 catch (Standard_Failure) {
00409 Handle_Standard_Failure e = Standard_Failure::Caught();
00410 PyErr_SetString(PyExc_Exception, e->GetMessageString());
00411 return 0;
00412 }
00413 }
00414
00415 PyObject* TopoShapeFacePy::setTolerance(PyObject *args)
00416 {
00417 double tol;
00418 if (!PyArg_ParseTuple(args, "d", &tol))
00419 return 0;
00420 BRep_Builder aBuilder;
00421 const TopoDS_Face& f = TopoDS::Face(getTopoShapePtr()->_Shape);
00422 aBuilder.UpdateFace(f, tol);
00423 Py_Return;
00424 }
00425
00426 Py::Object TopoShapeFacePy::getSurface() const
00427 {
00428 const TopoDS_Face& f = TopoDS::Face(getTopoShapePtr()->_Shape);
00429 BRepAdaptor_Surface adapt(f);
00430 switch(adapt.GetType())
00431 {
00432 case GeomAbs_Plane:
00433 {
00434 GeomPlane* plane = new GeomPlane();
00435 Handle_Geom_Plane this_surf = Handle_Geom_Plane::DownCast
00436 (plane->handle());
00437 this_surf->SetPln(adapt.Plane());
00438 return Py::Object(new PlanePy(plane),true);
00439 }
00440 case GeomAbs_Cylinder:
00441 {
00442 GeomCylinder* cylinder = new GeomCylinder();
00443 Handle_Geom_CylindricalSurface this_surf = Handle_Geom_CylindricalSurface::DownCast
00444 (cylinder->handle());
00445 this_surf->SetCylinder(adapt.Cylinder());
00446 return Py::Object(new CylinderPy(cylinder),true);
00447 }
00448 case GeomAbs_Cone:
00449 {
00450 GeomCone* cone = new GeomCone();
00451 Handle_Geom_ConicalSurface this_surf = Handle_Geom_ConicalSurface::DownCast
00452 (cone->handle());
00453 this_surf->SetCone(adapt.Cone());
00454 return Py::Object(new ConePy(cone),true);
00455 }
00456 case GeomAbs_Sphere:
00457 {
00458 GeomSphere* sphere = new GeomSphere();
00459 Handle_Geom_SphericalSurface this_surf = Handle_Geom_SphericalSurface::DownCast
00460 (sphere->handle());
00461 this_surf->SetSphere(adapt.Sphere());
00462 return Py::Object(new SpherePy(sphere),true);
00463 }
00464 case GeomAbs_Torus:
00465 {
00466 GeomToroid* toroid = new GeomToroid();
00467 Handle_Geom_ToroidalSurface this_surf = Handle_Geom_ToroidalSurface::DownCast
00468 (toroid->handle());
00469 this_surf->SetTorus(adapt.Torus());
00470 return Py::Object(new ToroidPy(toroid),true);
00471 }
00472 case GeomAbs_BezierSurface:
00473 {
00474 GeomBezierSurface* surf = new GeomBezierSurface(adapt.Bezier());
00475 return Py::Object(new BezierSurfacePy(surf),true);
00476 }
00477 case GeomAbs_BSplineSurface:
00478 {
00479 GeomBSplineSurface* surf = new GeomBSplineSurface(adapt.BSpline());
00480 return Py::Object(new BSplineSurfacePy(surf),true);
00481 }
00482 case GeomAbs_SurfaceOfRevolution:
00483 {
00484 Handle_Geom_Surface s = BRep_Tool::Surface(f);
00485 Handle_Geom_SurfaceOfRevolution rev = Handle_Geom_SurfaceOfRevolution::DownCast(s);
00486 if (!rev.IsNull()) {
00487 GeomSurfaceOfRevolution* surf = new GeomSurfaceOfRevolution(rev);
00488 return Py::Object(new SurfaceOfRevolutionPy(surf),true);
00489 }
00490 }
00491 case GeomAbs_SurfaceOfExtrusion:
00492 {
00493 Handle_Geom_Surface s = BRep_Tool::Surface(f);
00494 Handle_Geom_SurfaceOfLinearExtrusion ext = Handle_Geom_SurfaceOfLinearExtrusion::DownCast(s);
00495 if (!ext.IsNull()) {
00496 GeomSurfaceOfExtrusion* surf = new GeomSurfaceOfExtrusion(ext);
00497 return Py::Object(new SurfaceOfExtrusionPy(surf),true);
00498 }
00499 }
00500 case GeomAbs_OffsetSurface:
00501 {
00502 Handle_Geom_Surface s = BRep_Tool::Surface(f);
00503 Handle_Geom_OffsetSurface off = Handle_Geom_OffsetSurface::DownCast(s);
00504 if (!off.IsNull()) {
00505 GeomOffsetSurface* surf = new GeomOffsetSurface(off);
00506 return Py::Object(new OffsetSurfacePy(surf),true);
00507 }
00508 }
00509 case GeomAbs_OtherSurface:
00510 break;
00511 }
00512
00513 throw Py::TypeError("undefined surface type");
00514 }
00515
00516 Py::Tuple TopoShapeFacePy::getParameterRange(void) const
00517 {
00518 const TopoDS_Face& f = TopoDS::Face(getTopoShapePtr()->_Shape);
00519 BRepAdaptor_Surface adapt(f);
00520 double u1 = adapt.FirstUParameter();
00521 double u2 = adapt.LastUParameter();
00522 double v1 = adapt.FirstVParameter();
00523 double v2 = adapt.LastVParameter();
00524
00525 Py::Tuple t(4);
00526 t.setItem(0, Py::Float(u1));
00527 t.setItem(1, Py::Float(u2));
00528 t.setItem(2, Py::Float(v1));
00529 t.setItem(3, Py::Float(v2));
00530 return t;
00531 }
00532
00533 Py::Object TopoShapeFacePy::getWire(void) const
00534 {
00535 TopoDS_Shape clSh = getTopoShapePtr()->_Shape;
00536 if (clSh.ShapeType() == TopAbs_FACE) {
00537 TopoDS_Face clFace = (TopoDS_Face&)clSh;
00538 TopoDS_Wire clWire = ShapeAnalysis::OuterWire(clFace);
00539 return Py::Object(new TopoShapeWirePy(new TopoShape(clWire)),true);
00540 }
00541 else
00542 throw "Internal error, TopoDS_Shape is not a face!";
00543
00544 return Py::Object();
00545 }
00546
00547 Py::Object TopoShapeFacePy::getCenterOfMass(void) const
00548 {
00549 GProp_GProps props;
00550 BRepGProp::SurfaceProperties(getTopoShapePtr()->_Shape, props);
00551 gp_Pnt c = props.CentreOfMass();
00552 return Py::Vector(Base::Vector3d(c.X(),c.Y(),c.Z()));
00553 }
00554
00555 PyObject *TopoShapeFacePy::getCustomAttributes(const char* attr) const
00556 {
00557 return 0;
00558 }
00559
00560 int TopoShapeFacePy::setCustomAttributes(const char* attr, PyObject *obj)
00561 {
00562 return 0;
00563 }