diff --git a/src/App/DocumentObserver.cpp b/src/App/DocumentObserver.cpp index 8115960f50..be3d1b7178 100644 --- a/src/App/DocumentObserver.cpp +++ b/src/App/DocumentObserver.cpp @@ -248,10 +248,8 @@ const std::string &DocumentObjectT::getPropertyName() const { std::string DocumentObjectT::getPropertyPython() const { std::stringstream str; - str << "FreeCAD.getDocument('" << document - << "').getObject('" << object - << "')"; - if(property.size()) + str << getObjectPython(); + if (property.size()) str << '.' << property; return str.str(); } @@ -400,6 +398,99 @@ std::vector SubObjectT::getSubObjectList() const { // ----------------------------------------------------------------------------- +PropertyListT::PropertyListT() + : toPython("None") +{ +} + +PropertyListT::PropertyListT(DocumentObject *obj) + : PropertyListT() +{ + if (obj) { + std::ostringstream str; + DocumentObjectT objT(obj); + str << objT.getObjectPython(); + + toPython = str.str(); + } +} + +PropertyListT::PropertyListT(DocumentObject *obj, const std::vector& subNames) + : PropertyListT() +{ + if (obj) { + std::ostringstream str; + DocumentObjectT objT(obj); + str << "(" << objT.getObjectPython() << ",["; + for(const auto& it : subNames) + str << "'" << it << "',"; + str << "])"; + + toPython = str.str(); + } +} + +PropertyListT::PropertyListT(const std::vector& objs) + : PropertyListT() +{ + if (!objs.empty()) { + std::stringstream str; + str << "["; + for (std::size_t i = 0; i < objs.size(); i++) { + if (i > 0) + str << ", "; + + App::DocumentObject* obj = objs[i]; + if (obj) { + DocumentObjectT objT(obj); + str << objT.getObjectPython(); + } + else { + str << "None"; + } + } + + str << "]"; + } +} + +PropertyListT::PropertyListT(const std::vector& objs, const std::vector& subNames) + : PropertyListT() +{ + if (!objs.empty() && objs.size() == subNames.size()) { + std::stringstream str; + str << "["; + for (std::size_t i = 0; i < subNames.size(); i++) { + if (i>0) + str << ",("; + else + str << "("; + + App::DocumentObject* obj = objs[i]; + if (obj) { + DocumentObjectT objT(obj); + str << objT.getObjectPython(); + } + else { + str << "None"; + } + + str << ","; + str << "'" << subNames[i] << "'"; + str << ")"; + } + + str << "]"; + } +} + +std::string PropertyListT::getPropertyPython() const +{ + return toPython; +} + +// ----------------------------------------------------------------------------- + class DocumentWeakPtrT::Private { public: Private(App::Document* doc) : _document(doc) { diff --git a/src/App/DocumentObserver.h b/src/App/DocumentObserver.h index 1a163d37c2..21416b96e2 100644 --- a/src/App/DocumentObserver.h +++ b/src/App/DocumentObserver.h @@ -211,6 +211,34 @@ private: std::string subname; }; +/** + * The PropertyListT class is a helper class to create Python statements for proprty links. + */ +class AppExport PropertyListT +{ +public: + /*! Constructor */ + PropertyListT(); + + /*! Constructor */ + PropertyListT(DocumentObject *obj); + + /*! Constructor */ + PropertyListT(DocumentObject *obj, const std::vector& subNames); + + /*! Constructor */ + PropertyListT(const std::vector& objs); + + /*! Constructor */ + PropertyListT(const std::vector& objs, const std::vector& subNames); + + /*! Get the property as Python command. */ + std::string getPropertyPython() const; + +private: + std::string toPython; +}; + /** * @brief The DocumentWeakPtrT class */