From 317b447f882af1f5e88c8c86f12a50d845eb683f Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 7 May 2016 14:41:24 +0200 Subject: [PATCH] + implement PropertyMaterialList --- src/App/Application.cpp | 1 + src/App/FeatureTest.cpp | 3 + src/App/FeatureTest.h | 2 + src/App/PropertyStandard.cpp | 159 +++++++++++++++++++++++++++++++++++ src/App/PropertyStandard.h | 52 ++++++++++++ 5 files changed, 217 insertions(+) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 23e19f2f0..b0214707a 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -1123,6 +1123,7 @@ void Application::initTypes(void) App ::PropertyColor ::init(); App ::PropertyColorList ::init(); App ::PropertyMaterial ::init(); + App ::PropertyMaterialList ::init(); App ::PropertyPath ::init(); App ::PropertyFile ::init(); App ::PropertyFileIncluded ::init(); diff --git a/src/App/FeatureTest.cpp b/src/App/FeatureTest.cpp index e735e821d..94f8beb41 100644 --- a/src/App/FeatureTest.cpp +++ b/src/App/FeatureTest.cpp @@ -66,8 +66,11 @@ FeatureTest::FeatureTest() ConstraintFloat.setConstraints(&floatPercent); App::Color c; + App::Material mat; ADD_PROPERTY(Colour ,(c) ); ADD_PROPERTY(ColourList ,(c) ); + ADD_PROPERTY(Material ,(mat)); + ADD_PROPERTY(MaterialList,(mat)); ADD_PROPERTY(Distance,(47.11f) ); ADD_PROPERTY(Angle ,(3.0f) ); diff --git a/src/App/FeatureTest.h b/src/App/FeatureTest.h index ba58f2436..eeb70f8ac 100644 --- a/src/App/FeatureTest.h +++ b/src/App/FeatureTest.h @@ -55,6 +55,8 @@ public: App::PropertyColor Colour; App::PropertyColorList ColourList; + App::PropertyMaterial Material; + App::PropertyMaterialList MaterialList; // special types App::PropertyDistance Distance; diff --git a/src/App/PropertyStandard.cpp b/src/App/PropertyStandard.cpp index a177806b8..33564e480 100644 --- a/src/App/PropertyStandard.cpp +++ b/src/App/PropertyStandard.cpp @@ -2530,4 +2530,163 @@ void PropertyMaterial::Paste(const Property &from) hasSetValue(); } +//************************************************************************** +// PropertyMaterialList +//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +TYPESYSTEM_SOURCE(App::PropertyMaterialList, App::PropertyLists); + +//************************************************************************** +// Construction/Destruction + +PropertyMaterialList::PropertyMaterialList() +{ + +} + +PropertyMaterialList::~PropertyMaterialList() +{ + +} + +//************************************************************************** +// Base class implementer + +void PropertyMaterialList::setSize(int newSize) +{ + _lValueList.resize(newSize); +} + +int PropertyMaterialList::getSize(void) const +{ + return static_cast(_lValueList.size()); +} + +void PropertyMaterialList::setValue(const Material& lValue) +{ + aboutToSetValue(); + _lValueList.resize(1); + _lValueList[0] = lValue; + hasSetValue(); +} + +void PropertyMaterialList::setValues(const std::vector& values) +{ + aboutToSetValue(); + _lValueList = values; + hasSetValue(); +} + +PyObject *PropertyMaterialList::getPyObject(void) +{ + Py::Tuple tuple(getSize()); + + for (int i = 0; i(value)->getMaterialPtr()); + } + else if (PyList_Check(value) || PyTuple_Check(value)) { + Py::Sequence list(value); + std::vector materials; + + for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { + if (PyObject_TypeCheck((*it).ptr(), &(MaterialPy::Type))) { + materials.push_back(*static_cast(value)->getMaterialPtr()); + } + } + + setValues(materials); + } + else { + std::string error = std::string("type must be 'Material', not "); + error += value->ob_type->tp_name; + throw Base::TypeError(error); + } +} + +void PropertyMaterialList::Save(Base::Writer &writer) const +{ + if (!writer.isForceXML()) { + writer.Stream() << writer.ind() << "" << std::endl; + } +} + +void PropertyMaterialList::Restore(Base::XMLReader &reader) +{ + reader.readElement("MaterialList"); + if (reader.hasAttribute("file")) { + std::string file(reader.getAttribute("file")); + + if (!file.empty()) { + // initate a file read + reader.addFile(file.c_str(), this); + } + } +} + +void PropertyMaterialList::SaveDocFile(Base::Writer &writer) const +{ + Base::OutputStream str(writer.Stream()); + uint32_t uCt = (uint32_t)getSize(); + str << uCt; + for (std::vector::const_iterator it = _lValueList.begin(); it != _lValueList.end(); ++it) { + str << it->ambientColor.getPackedValue(); + str << it->diffuseColor.getPackedValue(); + str << it->specularColor.getPackedValue(); + str << it->emissiveColor.getPackedValue(); + str << it->shininess; + str << it->transparency; + } +} + +void PropertyMaterialList::RestoreDocFile(Base::Reader &reader) +{ + Base::InputStream str(reader); + uint32_t uCt = 0; + str >> uCt; + std::vector values(uCt); + uint32_t value; // must be 32 bit long + float valueF; + for (std::vector::iterator it = values.begin(); it != values.end(); ++it) { + str >> value; + it->ambientColor.setPackedValue(value); + str >> value; + it->diffuseColor.setPackedValue(value); + str >> value; + it->specularColor.setPackedValue(value); + str >> value; + it->emissiveColor.setPackedValue(value); + str >> valueF; + it->shininess = valueF; + str >> valueF; + it->transparency = valueF; + } + setValues(values); +} + +Property *PropertyMaterialList::Copy(void) const +{ + PropertyMaterialList *p = new PropertyMaterialList(); + p->_lValueList = _lValueList; + return p; +} + +void PropertyMaterialList::Paste(const Property &from) +{ + aboutToSetValue(); + _lValueList = dynamic_cast(from)._lValueList; + hasSetValue(); +} + +unsigned int PropertyMaterialList::getMemSize(void) const +{ + return static_cast(_lValueList.size() * sizeof(Material)); +} diff --git a/src/App/PropertyStandard.h b/src/App/PropertyStandard.h index 18b9a2043..e0c109dff 100644 --- a/src/App/PropertyStandard.h +++ b/src/App/PropertyStandard.h @@ -962,6 +962,58 @@ private: Material _cMat; }; +/** Material properties +*/ +class AppExport PropertyMaterialList : public PropertyLists +{ + TYPESYSTEM_HEADER(); + +public: + + /** + * A constructor. + * A more elaborate description of the constructor. + */ + PropertyMaterialList(); + + /** + * A destructor. + * A more elaborate description of the destructor. + */ + virtual ~PropertyMaterialList(); + + virtual void setSize(int newSize); + virtual int getSize(void) const; + + /** Sets the property + */ + void setValue(const Material&); + + /// index operator + const Material& operator[] (const int idx) const { return _lValueList.operator[] (idx); } + + void set1Value(const int idx, const Material& value){ _lValueList.operator[] (idx) = value; } + + void setValues(const std::vector& values); + const std::vector &getValues(void) const{ return _lValueList; } + + virtual PyObject *getPyObject(void); + virtual void setPyObject(PyObject *); + + virtual void Save(Base::Writer &writer) const; + virtual void Restore(Base::XMLReader &reader); + + virtual void SaveDocFile(Base::Writer &writer) const; + virtual void RestoreDocFile(Base::Reader &reader); + + virtual Property *Copy(void) const; + virtual void Paste(const Property &from); + virtual unsigned int getMemSize(void) const; + +private: + std::vector _lValueList; +}; + } // namespace App