OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
PyLine.cpp
Go to the documentation of this file.
1//
2// Python API for the Line
3//
4// Copyright (c) 2023, Chris Rogers, STFC Rutherford Appleton Laboratory, Didcot, UK
5//
6// This file is part of OPAL.
7//
8// OPAL is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12//
13// You should have received a copy of the GNU General Public License
14// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
15//
16
17#include <Python.h>
18
19#include "Lines/Line.h"
23
24// using namespace boost::python;
25namespace PyOpal {
26 template <>
27 std::vector<PyOpalObjectNS::AttributeDef>
29 {"L", "length", "", PyOpalObjectNS::DOUBLE},
30 {"ORIGIN", "origin", "", PyOpalObjectNS::STRING},
31 {"ORIENTATION", "orientation", "", PyOpalObjectNS::STRING},
32 {"X", "x", "", PyOpalObjectNS::DOUBLE},
33 {"Y", "y", "", PyOpalObjectNS::DOUBLE},
34 {"Z", "z", "", PyOpalObjectNS::DOUBLE},
35 {"THETA", "theta", "", PyOpalObjectNS::DOUBLE},
36 {"PHI", "phi", "", PyOpalObjectNS::DOUBLE},
37 {"PSI", "psi", "", PyOpalObjectNS::DOUBLE}};
38
39 namespace PyLineNS {
40
44 PyLine aLine;
45 auto lineClass = aLine.make_class("Line");
46 // https://docs.python.org/3/library/collections.abc.html
47 lineClass.def("__len__", &PyLine::getLength)
48 .def("__getitem__", &PyLine::getElement)
49 .def("__setitem__", &PyLine::setElement)
50 //.def("__delitem__", &PyLine::removeElement)
51 .def("append", &PyLine::append);
52 lineClass.def("register", &PyLine::registerObject);
53 lineClass.def("get_opal_name", &PyLine::getName);
54 lineClass.def("set_opal_name", &PyLine::setName);
55 aLine.addGetOpalElement(lineClass);
56
57 // line is dependent on opal_element; all line elements are stored as
58 // abstract opal_elements and we need boost to know how to do the
59 // translation
60 PyObject* mod = PyImport_ImportModule("pyopal.elements.opal_element");
61 if (mod == nullptr) {
62 PyErr_Print();
63 }
64 }
65
66 } // namespace PyLineNS
67} // namespace PyOpal
PyLine_< TBeamline< FlaggedElmPtr > > PyLine
Definition PyLine.h:33
void Initialise()
Definition Globals.cpp:50
BOOST_PYTHON_MODULE(line)
Definition PyLine.cpp:41
void addGetOpalElement(PYCLASS &pyclass)
void append(boost::python::object element)
void setElement(int i, boost::python::object element)
boost::python::class_< PyLine > make_class(const char *className)
boost::python::object getElement(int i)