Gui/DocumentPyImp.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "PreCompiled.h"
00024
00025 #ifndef _PreComp_
00026 # include <sstream>
00027 #endif
00028
00029 #include <Base/Matrix.h>
00030 #include <Base/MatrixPy.h>
00031
00032 #include <App/Document.h>
00033
00034 #include "Document.h"
00035 #include "ViewProviderExtern.h"
00036
00037
00038 #include "DocumentPy.h"
00039 #include "DocumentPy.cpp"
00040
00041 using namespace Gui;
00042
00043
00044 std::string DocumentPy::representation(void) const
00045 {
00046 std::stringstream str;
00047 str << "<GUI Document object at " << getDocumentPtr() << ">";
00048
00049 return str.str();
00050 }
00051
00052
00053 PyObject* DocumentPy::show(PyObject *args)
00054 {
00055 char *psFeatStr;
00056 if (!PyArg_ParseTuple(args, "s;Name of the Feature to show have to be given!",&psFeatStr))
00057 return NULL;
00058
00059 PY_TRY {
00060 getDocumentPtr()->setShow(psFeatStr);
00061 Py_Return;
00062 } PY_CATCH;
00063 }
00064
00065 PyObject* DocumentPy::hide(PyObject *args)
00066 {
00067 char *psFeatStr;
00068 if (!PyArg_ParseTuple(args, "s;Name of the Feature to hide have to be given!",&psFeatStr))
00069 return NULL;
00070
00071 PY_TRY {
00072 getDocumentPtr()->setHide(psFeatStr);
00073 Py_Return;
00074 } PY_CATCH;
00075 }
00076
00077 PyObject* DocumentPy::setPos(PyObject *args)
00078 {
00079 char *psFeatStr;
00080 Base::Matrix4D mat;
00081 PyObject *pcMatObj;
00082 if (!PyArg_ParseTuple(args, "sO!;Name of the Feature and the transformation matrix have to be given!",
00083 &psFeatStr,
00084 &(Base::MatrixPy::Type), &pcMatObj))
00085 return NULL;
00086
00087 mat = static_cast<Base::MatrixPy*>(pcMatObj)->value();
00088
00089 PY_TRY {
00090 getDocumentPtr()->setPos(psFeatStr,mat);
00091 Py_Return;
00092 } PY_CATCH;
00093 }
00094
00095 PyObject* DocumentPy::setEdit(PyObject *args)
00096 {
00097 char *psFeatStr;
00098 int mod = 0;
00099 if (!PyArg_ParseTuple(args, "s|i;Name of the object to edit has to be given!",
00100 &psFeatStr,&mod))
00101 return NULL;
00102 App::DocumentObject * obj = getDocumentPtr()->getDocument()->getObject(psFeatStr);
00103 if (!obj) {
00104 PyErr_Format(PyExc_Exception, "No such object found in document: '%s'", psFeatStr);
00105 return 0;
00106 }
00107
00108 bool ok = getDocumentPtr()->setEdit(getDocumentPtr()->getViewProvider(obj),mod);
00109 if (!ok) {
00110 PyErr_Format(PyExc_Exception, "Failed to set object '%s' in edit mode", psFeatStr);
00111 return 0;
00112 }
00113
00114 Py_Return;
00115 }
00116
00117 PyObject* DocumentPy::resetEdit(PyObject *args)
00118 {
00119 if (!PyArg_ParseTuple(args, ";No arguments allowed"))
00120 return NULL;
00121 getDocumentPtr()->resetEdit();
00122
00123 Py_Return;
00124 }
00125
00126 PyObject* DocumentPy::addAnnotation(PyObject *args)
00127 {
00128 char *psAnnoName,*psFileName,*psModName=0;
00129 if (!PyArg_ParseTuple(args, "ss|s;Name of the Annotation and a file name have to be given!",&psAnnoName,&psFileName,&psModName))
00130 return NULL;
00131
00132 PY_TRY {
00133 ViewProviderExtern *pcExt = new ViewProviderExtern();
00134
00135 pcExt->setModeByFile(psModName?psModName:"Main",psFileName);
00136 pcExt->adjustDocumentName(getDocumentPtr()->getDocument()->getName());
00137
00138 getDocumentPtr()->setAnnotationViewProvider(psAnnoName,pcExt);
00139
00140 Py_Return;
00141
00142 } PY_CATCH;
00143 }
00144
00145 PyObject* DocumentPy::update(PyObject *args)
00146 {
00147 if (!PyArg_ParseTuple(args, ""))
00148 return NULL;
00149
00150 PY_TRY {
00151 getDocumentPtr()->onUpdate();
00152 Py_Return;
00153 } PY_CATCH;
00154 }
00155
00156 PyObject* DocumentPy::getObject(PyObject *args)
00157 {
00158 char *sName;
00159 if (!PyArg_ParseTuple(args, "s",&sName))
00160 return NULL;
00161
00162 PY_TRY {
00163 ViewProvider *pcView = getDocumentPtr()->getViewProviderByName(sName);
00164 if (pcView)
00165 return pcView->getPyObject();
00166 else {
00167 Py_Return;
00168 }
00169 } PY_CATCH;
00170 }
00171
00172 PyObject* DocumentPy::activeObject(PyObject *args)
00173 {
00174 if (!PyArg_ParseTuple(args, ""))
00175 return NULL;
00176
00177 PY_TRY {
00178 App::DocumentObject *pcFtr = getDocumentPtr()->getDocument()->getActiveObject();
00179 if (pcFtr) {
00180 ViewProvider *pcView = getDocumentPtr()->getViewProvider(pcFtr);
00181 return pcView->getPyObject();
00182 } else {
00183 Py_Return;
00184 }
00185 } PY_CATCH;
00186 }
00187
00188 PyObject* DocumentPy::activeView(PyObject *args)
00189 {
00190 if (!PyArg_ParseTuple(args, ""))
00191 return NULL;
00192
00193 PY_TRY {
00194 Gui::MDIView *pcView = getDocumentPtr()->getActiveView();
00195 if (pcView){
00196
00197 return pcView->getPyObject();
00198 } else {
00199 Py_Return;
00200 }
00201 } PY_CATCH;
00202 }
00203
00204 Py::Object DocumentPy::getActiveObject(void) const
00205 {
00206 App::DocumentObject *object = getDocumentPtr()->getDocument()->getActiveObject();
00207 if (object) {
00208 ViewProvider *viewObj = getDocumentPtr()->getViewProvider(object);
00209 return Py::Object(viewObj->getPyObject(), true);
00210 } else {
00211 return Py::None();
00212 }
00213 }
00214
00215 void DocumentPy::setActiveObject(Py::Object arg)
00216 {
00217 throw Py::AttributeError("'Document' object attribute 'ActiveObject' is read-only");
00218 }
00219
00220 Py::Object DocumentPy::getActiveView(void) const
00221 {
00222 Gui::MDIView *view = getDocumentPtr()->getActiveView();
00223 if (view) {
00224
00225 return Py::Object(view->getPyObject(), true);
00226 } else {
00227 return Py::None();
00228 }
00229 }
00230
00231 void DocumentPy::setActiveView(Py::Object arg)
00232 {
00233 throw Py::AttributeError("'Document' object attribute 'ActiveView' is read-only");
00234 }
00235
00236 Py::Object DocumentPy::getDocument(void) const
00237 {
00238 App::Document *doc = getDocumentPtr()->getDocument();
00239 if (doc) {
00240
00241 return Py::Object(doc->getPyObject(), true);
00242 } else {
00243 return Py::None();
00244 }
00245 }
00246
00247 PyObject *DocumentPy::getCustomAttributes(const char* attr) const
00248 {
00249
00250
00251
00252
00253
00254 if (this->ob_type->tp_dict == NULL) {
00255 if (PyType_Ready(this->ob_type) < 0)
00256 return 0;
00257 }
00258 PyObject* item = PyDict_GetItemString(this->ob_type->tp_dict, attr);
00259 if (item) return 0;
00260
00261 ViewProvider* obj = getDocumentPtr()->getViewProviderByName(attr);
00262 return (obj ? obj->getPyObject() : 0);
00263 }
00264
00265 int DocumentPy::setCustomAttributes(const char* attr, PyObject *)
00266 {
00267
00268
00269
00270
00271
00272 if (this->ob_type->tp_dict == NULL) {
00273 if (PyType_Ready(this->ob_type) < 0)
00274 return 0;
00275 }
00276 PyObject* item = PyDict_GetItemString(this->ob_type->tp_dict, attr);
00277 if (item) return 0;
00278 ViewProvider* obj = getDocumentPtr()->getViewProviderByName(attr);
00279 if (obj) {
00280 std::stringstream str;
00281 str << "'Document' object attribute '" << attr
00282 << "' must not be set this way" << std::ends;
00283 throw Py::AttributeError(str.str());
00284 }
00285
00286 return 0;
00287 }
00288
00289