py::Object read ( py::Object self, py::Tuple args ) try { std::istream& stream = *static_cast<std::istream*> (py::TypeBuilder::get_baton(self)); std::ostringstream payload; if ( args.size() == 1 ) { char data[1024]; long size = py::Int(args[0]); for (; (size > 0); size -= stream.gcount()) { stream.read(data, std::min(size, long(sizeof(data)))); payload.write(data, stream.gcount()); } } else { payload << stream.rdbuf(); } return (py::Bytes(payload.str())); } catch ( const std::bad_cast& ) { std::cerr << "Bad cast!" << std::endl; return (py::None()); }
py::Object write ( py::Object self, py::Tuple args ) { std::ostream& stream = *static_cast<std::ostream*> (py::TypeBuilder::get_baton(self)); if (args.size() == 1) { stream << (std::string)py::Bytes(args[0]); } return (py::None()); }
py::Object writelines ( py::Object self, py::Tuple args ) { std::ostream& stream = *static_cast<std::ostream*> (py::TypeBuilder::get_baton(self)); if (args.size() == 1) { py::Iterator iterator(args[0]); while (iterator.next()) { stream << (std::string)py::Bytes(iterator.item()); } } return (py::None()); }
Py::Object openBrowser(const Py::Tuple& args) { const char* url; if (!PyArg_ParseTuple(args.ptr(), "s",&url)) throw Py::Exception(); WebGui::BrowserView* pcBrowserView; pcBrowserView = new WebGui::BrowserView(Gui::getMainWindow()); pcBrowserView->setWindowTitle(QObject::tr("Browser")); pcBrowserView->resize(400, 300); pcBrowserView->load(url); Gui::getMainWindow()->addWindow(pcBrowserView); return Py::None(); }
Py::Object _transforms_module::new_bbox (const Py::Tuple &args) { _VERBOSE("_transforms_module::new_bbox "); args.verify_length(2); if (!Point::check(args[0])) throw Py::TypeError("Point(p1,p2) expected a Point for p1"); if (!Point::check(args[1])) throw Py::TypeError("Point(p1,p2) expected a Point for p2"); Point* ll = static_cast<Point*>(args[0].ptr()); Point* ur = static_cast<Point*>(args[1].ptr()); return Py::asObject(new Bbox(ll, ur) ); }
Py::Object openBrowserHTML(const Py::Tuple& args) { const char* HtmlCode; const char* BaseUrl; const char* TabName = "Browser"; if (! PyArg_ParseTuple(args.ptr(), "ss|s",&HtmlCode,&BaseUrl,&TabName)) throw Py::Exception(); WebGui::BrowserView* pcBrowserView = 0; pcBrowserView = new WebGui::BrowserView(Gui::getMainWindow()); pcBrowserView->resize(400, 300); pcBrowserView->setHtml(QString::fromUtf8(HtmlCode),QUrl(QString::fromLatin1(BaseUrl)),QString::fromUtf8(TabName)); Gui::getMainWindow()->addWindow(pcBrowserView); return Py::None(); }
Py::Object writeProjectFile(const Py::Tuple& args) { char *fromPython; if (! PyArg_ParseTuple(args.ptr(), "(s)", &fromPython)) throw Py::Exception(); std::ofstream fout; if (fromPython) fout.open(fromPython); else fout.open("FreeCAD.pov"); fout << FreeCAD ; fout.close(); return Py::None(); }
Py::Object FT2Font::get_charmap(const Py::Tuple & args) { _VERBOSE("FT2Font::get_charmap"); args.verify_length(0); FT_UInt index; Py::Dict charmap; FT_ULong code = FT_Get_First_Char(face, &index); while (code != 0) { //charmap[Py::Long((long) code)] = Py::Int((int) index); charmap[Py::Int((int) index)] = Py::Long((long) code); code = FT_Get_Next_Char(face, code, &index); } return charmap; }
Py::Object View3DInventorViewerPy::getSceneGraph(const Py::Tuple& args) { if (!PyArg_ParseTuple(args.ptr(), "")) throw Py::Exception(); try { SoNode* scene = _viewer->getSceneGraph(); PyObject* proxy = 0; proxy = Base::Interpreter().createSWIGPointerObj("pivy.coin", "SoSeparator *", (void*)scene, 1); scene->ref(); return Py::Object(proxy, true); } catch (const Base::Exception& e) { throw Py::Exception(e.what()); } }
Py::Object View3DInventorViewerPy::getSoRenderManager(const Py::Tuple& args) { if (!PyArg_ParseTuple(args.ptr(), "")) throw Py::Exception(); try { SoRenderManager* manager = _viewer->getSoRenderManager(); PyObject* proxy = 0; proxy = Base::Interpreter().createSWIGPointerObj("pivy.coin", "SoRenderManager *", (void*)manager, 0); return Py::Object(proxy, true); } catch (const Base::Exception& e) { throw Py::Exception(e.what()); } }
Py::Object ProgressIndicatorPy::next(const Py::Tuple& args) { int b=0; if (!PyArg_ParseTuple(args.ptr(), "|i",&b)) throw Py::Exception(); if (_seq.get()) { try { _seq->next(b ? true : false); } catch (const Base::AbortException&) { _seq.reset(); throw Py::Exception("abort progress indicator"); } } return Py::None(); }
Py::Object _path_module::point_in_path(const Py::Tuple& args) { args.verify_length(4); double x = Py::Float(args[0]); double y = Py::Float(args[1]); PathIterator path(args[2]); agg::trans_affine trans = py_to_agg_transformation_matrix(args[3].ptr(), false); if (::point_in_path(x, y, path, trans)) { return Py::Int(1); } return Py::Int(0); }
Py::Object _path_module::count_bboxes_overlapping_bbox(const Py::Tuple& args) { args.verify_length(2); Py::Object bbox = args[0]; Py::SeqBase<Py::Object> bboxes = args[1]; double ax0, ay0, ax1, ay1; double bx0, by0, bx1, by1; long count = 0; if (py_convert_bbox(bbox.ptr(), ax0, ay0, ax1, ay1)) { if (ax1 < ax0) std::swap(ax0, ax1); if (ay1 < ay0) std::swap(ay0, ay1); size_t num_bboxes = bboxes.size(); for (size_t i = 0; i < num_bboxes; ++i) { Py::Object bbox_b = bboxes[i]; if (py_convert_bbox(bbox_b.ptr(), bx0, by0, bx1, by1)) { if (bx1 < bx0) std::swap(bx0, bx1); if (by1 < by0) std::swap(by0, by1); if (!((bx1 <= ax0) || (by1 <= ay0) || (bx0 >= ax1) || (by0 >= ay1))) ++count; } else { throw Py::ValueError("Non-bbox object in bboxes list"); } } } else { throw Py::ValueError("First argument to count_bboxes_overlapping_bbox must be a Bbox object."); } return Py::Int(count); }
Py::Object _path_module::clip_path_to_rect(const Py::Tuple &args) { args.verify_length(3); PathIterator path(args[0]); Py::Object bbox_obj = args[1]; bool inside = Py::Int(args[2]); double x0, y0, x1, y1; if (!py_convert_bbox(bbox_obj.ptr(), x0, y0, x1, y1)) throw Py::TypeError("Argument 2 to clip_to_rect must be a Bbox object."); std::vector<Polygon> results; ::clip_to_rect(path, x0, y0, x1, y1, inside, results); npy_intp dims[2]; dims[1] = 2; PyObject* py_results = PyList_New(results.size()); if (!py_results) throw Py::RuntimeError("Error creating results list"); try { for (std::vector<Polygon>::const_iterator p = results.begin(); p != results.end(); ++p) { size_t size = p->size(); dims[0] = p->size(); PyArrayObject* pyarray = (PyArrayObject*)PyArray_SimpleNew(2, dims, PyArray_DOUBLE); for (size_t i = 0; i < size; ++i) { ((double *)pyarray->data)[2*i] = (*p)[i].x; ((double *)pyarray->data)[2*i+1] = (*p)[i].y; } if (PyList_SetItem(py_results, p - results.begin(), (PyObject *)pyarray) != -1) { throw Py::RuntimeError("Error creating results list"); } } } catch (...) { Py_XDECREF(py_results); throw; } return Py::Object(py_results, true); }
Py::Object triangulate(const Py::Tuple& args) { PyObject *pcObj; double searchRadius; double mu=2.5; if (!PyArg_ParseTuple(args.ptr(), "O!d|d", &(Points::PointsPy::Type), &pcObj, &searchRadius, &mu)) throw Py::Exception(); Points::PointsPy* pPoints = static_cast<Points::PointsPy*>(pcObj); Points::PointKernel* points = pPoints->getPointKernelPtr(); Mesh::MeshObject* mesh = new Mesh::MeshObject(); SurfaceTriangulation tria(*points, *mesh); tria.perform(searchRadius, mu); return Py::asObject(new Mesh::MeshPy(mesh)); }
Py::Object Image::apply_translation(const Py::Tuple& args) { _VERBOSE("Image::apply_translation"); args.verify_length(2); double tx = Py::Float(args[0]); double ty = Py::Float(args[1]); //printf("applying translation %1.2f, %1.2f\n", tx, ty); agg::trans_affine M = agg::trans_affine_translation(tx, ty); srcMatrix *= M; imageMatrix *= M; return Py::Object(); }
Py::Object Image::apply_scaling(const Py::Tuple& args) { _VERBOSE("Image::apply_scaling"); args.verify_length(2); double sx = Py::Float(args[0]); double sy = Py::Float(args[1]); //printf("applying scaling %1.2f, %1.2f\n", sx, sy); agg::trans_affine M = agg::trans_affine_scaling(sx, sy); srcMatrix *= M; imageMatrix *= M; return Py::Object(); }
Py::Object _transforms_module::new_interval (const Py::Tuple &args) { _VERBOSE("_transforms_module::new_interval "); args.verify_length(2); if (!LazyValue::check(args[0])) throw Py::TypeError("Interval(val1, val2) expected a LazyValue for val1"); if (!LazyValue::check(args[1])) throw Py::TypeError("Interval(val1, val2) expected a LazyValue for val2"); LazyValue* v1 = static_cast<LazyValue*>(args[0].ptr()); LazyValue* v2 = static_cast<LazyValue*>(args[1].ptr()); return Py::asObject(new Interval(v1, v2) ); }
Py::Object Transformation::seq_x_y(const Py::Tuple & args) { _VERBOSE("Transformation::seq_x_y"); args.verify_length(2); Py::SeqBase<Py::Object> x = args[0]; Py::SeqBase<Py::Object> y = args[1]; size_t Nx = x.length(); size_t Ny = y.length(); if (Nx!=Ny) throw Py::ValueError("x and y must be equal length sequences"); // evaluate the lazy objects try { if (!_frozen) eval_scalars(); } catch(...) { throw Py::ValueError("Domain error on Transformation::seq_x_y"); } Py::Tuple xo(Nx); Py::Tuple yo(Nx); for (size_t i=0; i< Nx; ++i) { double thisx = Py::Float(x[i]); double thisy = Py::Float(y[i]); try { this->operator()(thisx, thisy); } catch(...) { throw Py::ValueError("Domain error on Transformation::seq_x_y operator()(thisx, thisy)"); } xo[i] = Py::Float( xy.first ); yo[i] = Py::Float( xy.second ); } Py::Tuple ret(2); ret[0] = xo; ret[1] = yo; return ret; }
Py::Object View3DInventorViewerPy::setSceneGraph(const Py::Tuple& args) { PyObject* proxy; if (!PyArg_ParseTuple(args.ptr(), "O", &proxy)) throw Py::Exception(); void* ptr = 0; try { Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoNode *", proxy, &ptr, 0); SoNode* node = static_cast<SoNode*>(ptr); _viewer->setSceneGraph(node); return Py::None(); } catch (const Base::Exception& e) { throw Py::Exception(e.what()); } }
Py::Object TriContourGenerator::create_filled_contour(const Py::Tuple &args) { _VERBOSE("TriContourGenerator::create_filled_contour"); args.verify_length(2); double lower_level = (Py::Float)args[0]; double upper_level = (Py::Float)args[1]; clear_visited_flags(true); Contour contour; find_boundary_lines_filled(contour, lower_level, upper_level); find_interior_lines(contour, lower_level, false, true); find_interior_lines(contour, upper_level, true, true); return contour_to_segs_and_kinds(contour); }
Py::Object Transformation::inverse_xy_tup(const Py::Tuple & args) { _VERBOSE("Transformation::inverse_xy_tup"); args.verify_length(1); Py::Tuple tup = args[0]; double xin = Py::Float(tup[0]); double yin = Py::Float(tup[1]); if (!_frozen) eval_scalars(); inverse_api(xin, yin); Py::Tuple ret(2); ret[0] = Py::Float(xy.first); ret[1] = Py::Float(xy.second); return ret; }
Py::Object open(const Py::Tuple& args) { char* Name; if (!PyArg_ParseTuple(args.ptr(), "et","utf-8",&Name)) throw Py::Exception(); std::string EncodedName = std::string(Name); PyMem_Free(Name); //Base::Console().Log("Open in Part with %s",Name); Base::FileInfo file(EncodedName.c_str()); // extract extension if (file.extension().empty()) throw Py::RuntimeError("No file extension"); throw Py::RuntimeError("Unknown file extension"); //return Py::None(); }
Py::Object Bbox::contains(const Py::Tuple &args) { _VERBOSE("Bbox::contains"); args.verify_length(2); double x = Py::Float(args[0]); double y = Py::Float(args[1]); double minx = _ll->xval(); double miny = _ll->yval(); double maxx = _ur->xval(); double maxy = _ur->yval(); int inx = ( (x>=minx) && (x<=maxx) || (x>=maxx) && (x<=minx) ); if (!inx) return Py::Int(0); int iny = ( (y>=miny) && (y<=maxy) || (y>=maxy) && (y<=miny) ); return Py::Int(iny); }
Py::Object FT2Font::set_text(const Py::Tuple & args) { _VERBOSE("FT2Font::set_text"); args.verify_length(2); text = Py::String(args[0]); double angle = Py::Float(args[1]); angle = angle/360.0*2*3.14159; //this computes width and height in subpixels so we have to divide by 64 matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L ); matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L ); matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L ); matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L ); load_glyphs(); return Py::Object(); }
Py::Object ParameterGrpPy::getGroup(const Py::Tuple& args) { char *pstr; if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) throw Py::Exception(); // get the Handle of the wanted group Base::Reference<ParameterGrp> handle = _cParamGrp->GetGroup(pstr); if (handle.isValid()) { // crate a python wrapper class ParameterGrpPy *pcParamGrp = new ParameterGrpPy(handle); // increment the reff count return Py::asObject(pcParamGrp); } else { throw Py::RuntimeError("GetGroup failed"); } }
Py::Object FuncXY::inverse(const Py::Tuple &args) { _VERBOSE("FuncXY::inverse"); args.verify_length(2); double xin = Py::Float(args[0]); double yin = Py::Float(args[1]); std::pair<double, double> xy = this->inverse_api(xin, yin); Py::Tuple ret(2); double xout = xy.first; double yout = xy.second; ret[0] = Py::Float(xout); ret[1] = Py::Float(yout); return ret; };
/* ------------ module methods ------------- */ Py::Object _backend_agg_module::new_renderer (const Py::Tuple &args, const Py::Dict &kws) { if (args.length() != 3 ) { throw Py::RuntimeError("Incorrect # of args to RendererAgg(width, height, dpi)."); } int debug; if ( kws.hasKey("debug") ) debug = Py::Int( kws["debug"] ); else debug=0; int width = Py::Int(args[0]); int height = Py::Int(args[1]); double dpi = Py::Float(args[2]); return Py::asObject(new RendererAgg(width, height, dpi, debug)); }
Py::Object Transformation::xy_tup(const Py::Tuple & args) { _VERBOSE("Transformation::xy_tup"); args.verify_length(1); if (!_frozen) eval_scalars(); Py::SeqBase<Py::Object> xytup = args[0]; double x = Py::Float(xytup[0]); double y = Py::Float(xytup[1]); Py::Tuple out(2); this->operator()(x, y); out[0] = Py::Float( xy.first ); out[1] = Py::Float( xy.second ); return out; }
Py::Object Func::map(const Py::Tuple &args) { _VERBOSE("Func::map"); args.verify_length(1); double xin = Py::Float(args[0]); double xout; try { xout = this->operator()(xin); } catch(...) { throw Py::ValueError("Domain error on Func::map"); } return Py::Float(xout); };