+ implement PropertyMaterialList

This commit is contained in:
wmayer 2016-05-07 14:41:24 +02:00
parent d8ad50d11d
commit 317b447f88
5 changed files with 217 additions and 0 deletions

View File

@ -1123,6 +1123,7 @@ void Application::initTypes(void)
App ::PropertyColor ::init(); App ::PropertyColor ::init();
App ::PropertyColorList ::init(); App ::PropertyColorList ::init();
App ::PropertyMaterial ::init(); App ::PropertyMaterial ::init();
App ::PropertyMaterialList ::init();
App ::PropertyPath ::init(); App ::PropertyPath ::init();
App ::PropertyFile ::init(); App ::PropertyFile ::init();
App ::PropertyFileIncluded ::init(); App ::PropertyFileIncluded ::init();

View File

@ -66,8 +66,11 @@ FeatureTest::FeatureTest()
ConstraintFloat.setConstraints(&floatPercent); ConstraintFloat.setConstraints(&floatPercent);
App::Color c; App::Color c;
App::Material mat;
ADD_PROPERTY(Colour ,(c) ); ADD_PROPERTY(Colour ,(c) );
ADD_PROPERTY(ColourList ,(c) ); ADD_PROPERTY(ColourList ,(c) );
ADD_PROPERTY(Material ,(mat));
ADD_PROPERTY(MaterialList,(mat));
ADD_PROPERTY(Distance,(47.11f) ); ADD_PROPERTY(Distance,(47.11f) );
ADD_PROPERTY(Angle ,(3.0f) ); ADD_PROPERTY(Angle ,(3.0f) );

View File

@ -55,6 +55,8 @@ public:
App::PropertyColor Colour; App::PropertyColor Colour;
App::PropertyColorList ColourList; App::PropertyColorList ColourList;
App::PropertyMaterial Material;
App::PropertyMaterialList MaterialList;
// special types // special types
App::PropertyDistance Distance; App::PropertyDistance Distance;

View File

@ -2530,4 +2530,163 @@ void PropertyMaterial::Paste(const Property &from)
hasSetValue(); 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<int>(_lValueList.size());
}
void PropertyMaterialList::setValue(const Material& lValue)
{
aboutToSetValue();
_lValueList.resize(1);
_lValueList[0] = lValue;
hasSetValue();
}
void PropertyMaterialList::setValues(const std::vector<Material>& values)
{
aboutToSetValue();
_lValueList = values;
hasSetValue();
}
PyObject *PropertyMaterialList::getPyObject(void)
{
Py::Tuple tuple(getSize());
for (int i = 0; i<getSize(); i++) {
tuple.setItem(i, Py::asObject(new MaterialPy(new Material(_lValueList[i]))));
}
return Py::new_reference_to(tuple);
}
void PropertyMaterialList::setPyObject(PyObject *value)
{
if (PyObject_TypeCheck(value, &(MaterialPy::Type))) {
setValue(*static_cast<MaterialPy*>(value)->getMaterialPtr());
}
else if (PyList_Check(value) || PyTuple_Check(value)) {
Py::Sequence list(value);
std::vector<Material> materials;
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
if (PyObject_TypeCheck((*it).ptr(), &(MaterialPy::Type))) {
materials.push_back(*static_cast<MaterialPy*>(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() << "<MaterialList file=\"" << writer.addFile(getName(), this) << "\"/>" << 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<App::Material>::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<Material> values(uCt);
uint32_t value; // must be 32 bit long
float valueF;
for (std::vector<App::Material>::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<const PropertyMaterialList&>(from)._lValueList;
hasSetValue();
}
unsigned int PropertyMaterialList::getMemSize(void) const
{
return static_cast<unsigned int>(_lValueList.size() * sizeof(Material));
}

View File

@ -962,6 +962,58 @@ private:
Material _cMat; 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<Material>& values);
const std::vector<Material> &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<Material> _lValueList;
};
} // namespace App } // namespace App