OPAL (Object Oriented Parallel Accelerator Library)  2024.1
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"
21 #include "PyOpal/PyCore/Globals.h"
23 
24 //using namespace boost::python;
25 namespace PyOpal {
26 namespace PyLineNS {
27 
28 template <>
29 std::vector<PyOpalObjectNS::AttributeDef> PyOpalObjectNS::PyOpalObject<TBeamline<Element> >::attributes = {
30  {"L", "length", "", PyOpalObjectNS::DOUBLE},
31  {"ORIGIN", "origin", "", PyOpalObjectNS::STRING},
32  {"ORIENTATION", "orientation", "", PyOpalObjectNS::STRING},
33  {"X", "x", "", PyOpalObjectNS::DOUBLE},
34  {"Y", "y", "", PyOpalObjectNS::DOUBLE},
35  {"Z", "z", "", PyOpalObjectNS::DOUBLE},
36  {"THETA", "theta", "", PyOpalObjectNS::DOUBLE},
37  {"PHI", "phi", "", PyOpalObjectNS::DOUBLE},
38  {"PSI", "psi", "", PyOpalObjectNS::DOUBLE}
39 };
40 
41 
45  PyLine aLine;
46  auto lineClass = aLine.make_class("Line");
47  // https://docs.python.org/3/library/collections.abc.html
48  lineClass
49  .def("__len__", &PyLine::getLength)
50  .def("__getitem__", &PyLine::getElement)
51  .def("__setitem__", &PyLine::setElement)
52  //.def("__delitem__", &PyLine::removeElement)
53  .def("append", &PyLine::append);
54  lineClass.def("register", &PyLine::registerObject);
55  lineClass.def("get_opal_name", &PyLine::getName);
56  lineClass.def("set_opal_name", &PyLine::setName);
57  aLine.addGetOpalElement(lineClass);
58 
59  // line is dependent on opal_element; all line elements are stored as
60  // abstract opal_elements and we need boost to know how to do the
61  // translation
62  PyObject* mod = PyImport_ImportModule("pyopal.elements.opal_element");
63  if (mod == nullptr) {
64  PyErr_Print();
65  }
66 }
67 
68 }
69 }
boost::python::class_< PyLine > make_class(const char *className)
void Initialise()
Definition: Globals.cpp:50
void append(boost::python::object element)
BOOST_PYTHON_MODULE(line)
Definition: PyLine.cpp:42
boost::python::object getElement(int i)
void setName(std::string name)
int getLength() const
Definition: PyLine.h:95
void registerObject()
void setElement(int i, boost::python::object element)
std::string getName() const
void addGetOpalElement(PYCLASS &pyclass)
Definition: PyOpalObject.h:799