Skip to content

Commit

Permalink
ShapeMaterial becomes part of Part::Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
davesrocketshop committed Mar 2, 2024
1 parent 20444f1 commit b0e6e96
Show file tree
Hide file tree
Showing 31 changed files with 1,184 additions and 1,453 deletions.
83 changes: 52 additions & 31 deletions src/App/GeoFeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

#include <App/GeoFeaturePy.h>

#include "ElementNamingUtils.h"
#include "GeoFeature.h"
#include "GeoFeatureGroupExtension.h"
#include "ElementNamingUtils.h"


using namespace App;
Expand All @@ -42,12 +42,12 @@ PROPERTY_SOURCE(App::GeoFeature, App::DocumentObject)

GeoFeature::GeoFeature()
{
ADD_PROPERTY_TYPE(Placement,(Base::Placement()),nullptr,Prop_NoRecompute,nullptr);
ADD_PROPERTY_TYPE(Placement, (Base::Placement()), nullptr, Prop_NoRecompute, nullptr);
}

GeoFeature::~GeoFeature() = default;

void GeoFeature::transformPlacement(const Base::Placement &transform)
void GeoFeature::transformPlacement(const Base::Placement& transform)
{
Base::Placement plm = this->Placement.getValue();
plm = transform * plm;
Expand All @@ -61,7 +61,7 @@ Base::Placement GeoFeature::globalPlacement() const
auto ext = group->getExtensionByType<GeoFeatureGroupExtension>();
return ext->globalGroupPlacement() * Placement.getValue();
}
return Placement.getValue();
return Placement.getValue();
}

const PropertyComplexGeoData* GeoFeature::getPropertyOfGeometry() const
Expand All @@ -73,67 +73,88 @@ PyObject* GeoFeature::getPyObject()
{
if (PythonObject.is(Py::_None())) {
// ref counter is set to 1
PythonObject = Py::Object(new GeoFeaturePy(this),true);
PythonObject = Py::Object(new GeoFeaturePy(this), true);
}
return Py::new_reference_to(PythonObject);
}


std::pair<std::string,std::string> GeoFeature::getElementName(
const char *name, ElementNameType type) const
std::pair<std::string, std::string> GeoFeature::getElementName(const char* name,
ElementNameType type) const
{
(void)type;

std::pair<std::string,std::string> ret;
if(!name)
std::pair<std::string, std::string> ret;
if (!name) {
return ret;
}

ret.second = name;
return ret;
}

DocumentObject *GeoFeature::resolveElement(DocumentObject *obj, const char *subname,
std::pair<std::string,std::string> &elementName, bool append,
ElementNameType type, const DocumentObject *filter,
const char **_element, GeoFeature **geoFeature)
DocumentObject* GeoFeature::resolveElement(DocumentObject* obj,
const char* subname,
std::pair<std::string, std::string>& elementName,
bool append,
ElementNameType type,
const DocumentObject* filter,
const char** _element,
GeoFeature** geoFeature)
{
if(!obj || !obj->isAttachedToDocument())
if (!obj || !obj->isAttachedToDocument()) {
return nullptr;
if(!subname)
}
if (!subname) {
subname = "";
const char *element = Data::findElementName(subname);
if(_element) *_element = element;
}
const char* element = Data::findElementName(subname);
if (_element) {
*_element = element;
}
auto sobj = obj->getSubObject(subname);
if(!sobj)
if (!sobj) {
return nullptr;
}
obj = sobj->getLinkedObject(true);
auto geo = dynamic_cast<GeoFeature*>(obj);
if(geoFeature)
if (geoFeature) {
*geoFeature = geo;
if(!obj || (filter && obj!=filter))
}
if (!obj || (filter && obj != filter)) {
return nullptr;
if(!element || !element[0]) {
if(append)
}
if (!element || !element[0]) {

Check warning on line 127 in src/App/GeoFeature.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic]

Check warning on line 127 in src/App/GeoFeature.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

implicit conversion 'char' -> bool [readability-implicit-bool-conversion]
if (append) {
elementName.second = Data::oldElementName(subname);
}
return sobj;
}

if(!geo || hasHiddenMarker(element)) {
if(!append)
if (!geo || hasHiddenMarker(element)) {
if (!append) {
elementName.second = element;
else
}
else {
elementName.second = Data::oldElementName(subname);
}
return sobj;
}
if(!append)
elementName = geo->getElementName(element,type);
else{
const auto &names = geo->getElementName(element,type);
std::string prefix(subname,element-subname);
if(!names.first.empty())
if (!append) {
elementName = geo->getElementName(element, type);
}
else {
const auto& names = geo->getElementName(element, type);
std::string prefix(subname, element - subname);
if (!names.first.empty()) {
elementName.first = prefix + names.first;
}
elementName.second = prefix + names.second;
}
return sobj;
}

App::Material GeoFeature::getMaterialAppearance() const
{
return App::Material(App::Material::DEFAULT);
}

Check warning on line 160 in src/App/GeoFeature.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

Could not find a newline character at the end of the file. [whitespace/ending_newline] [5]
58 changes: 37 additions & 21 deletions src/App/GeoFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
#define APP_GEOFEATURE_H

#include "DocumentObject.h"
#include "Material.h"
#include "PropertyGeo.h"


namespace App
{


/** Base class of all geometric document objects.
*/
class AppExport GeoFeature : public App::DocumentObject
class AppExport GeoFeature: public App::DocumentObject

Check warning on line 37 in src/App/GeoFeature.h

View workflow job for this annotation

GitHub Actions / Lint / Lint

class 'GeoFeature' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator [cppcoreguidelines-special-member-functions]
{
PROPERTY_HEADER_WITH_OVERRIDE(App::GeoFeature);

Check warning on line 39 in src/App/GeoFeature.h

View workflow job for this annotation

GitHub Actions / Lint / Lint

variable 'classTypeId' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

Expand All @@ -52,7 +52,7 @@ class AppExport GeoFeature : public App::DocumentObject
* was only called by alignment task (Edit->Alignment)
* @param transform (input).
*/
virtual void transformPlacement(const Base::Placement &transform);
virtual void transformPlacement(const Base::Placement& transform);
/**
* This method returns the main property of a geometric object that holds
* the actual geometry. For a part object this is the Shape property, for
Expand All @@ -66,14 +66,15 @@ class AppExport GeoFeature : public App::DocumentObject
*/
PyObject* getPyObject() override;

/// Specify the type of element name to return when calling getElementName()
enum ElementNameType {
/// Specify the type of element name to return when calling getElementName()
enum ElementNameType
{
/// Normal usage
Normal=0,
Normal = 0,
/// For importing
Import=1,
Import = 1,
/// For exporting
Export=2,
Export = 2,
};
/** Return the new and old style sub-element name
*
Expand All @@ -85,13 +86,13 @@ class AppExport GeoFeature : public App::DocumentObject
* This function currently is does nothing. The new style element name
* generation will be added in the next batch of patches.
*/
virtual std::pair<std::string,std::string> getElementName(
const char *name, ElementNameType type=Normal) const;
virtual std::pair<std::string, std::string> getElementName(const char* name,
ElementNameType type = Normal) const;

/** Resolve both the new and old style element name
*
* @param obj: top parent object
* @param subname: subname reference
* @param subname: subname reference
* @param elementName: output of a pair(newElementName,oldElementName)
* @param append: Whether to include subname prefix into the returned
* element name
Expand All @@ -102,27 +103,42 @@ class AppExport GeoFeature : public App::DocumentObject
*
* @return Return the owner object of the element
*/
static DocumentObject *resolveElement(App::DocumentObject *obj,
const char *subname, std::pair<std::string,std::string> &elementName,
bool append=false, ElementNameType type=Normal,
const DocumentObject *filter=nullptr,const char **element=nullptr, GeoFeature **geo=nullptr);
static DocumentObject* resolveElement(App::DocumentObject* obj,
const char* subname,
std::pair<std::string, std::string>& elementName,
bool append = false,
ElementNameType type = Normal,
const DocumentObject* filter = nullptr,
const char** element = nullptr,
GeoFeature** geo = nullptr);

/**
* @brief Calculates the placement in the global reference coordinate system
*
*
* In FreeCAD the GeoFeature placement describes the local placement of the object in its parent
* coordinate system. This is however not always the same as the global reference system. If the
* object is in a GeoFeatureGroup, hence in another local coordinate system, the Placement
* property does only give the local transformation. This function can be used to calculate the
* placement of the object in the global reference coordinate system taking all stacked local
* property does only give the local transformation. This function can be used to calculate the
* placement of the object in the global reference coordinate system taking all stacked local
* systems into account.
*
*
* @return Base::Placement The transformation from the global reference coordinate system
*/
Base::Placement globalPlacement() const;

/**
* @brief Virtual function to get an App::Material object describing the appearance
*
* The appearance properties are described by the underlying features material. This can not
* be accessed directly from within the Gui module. This virtal function will return a

Check warning on line 133 in src/App/GeoFeature.h

View workflow job for this annotation

GitHub Actions / Lint / Lint

virtal ==> virtual
* App::Material object describing the appearance properties of the material.
*
* @return App::Material the appearance properties of the object material
*/
virtual App::Material getMaterialAppearance() const;
};

} //namespace App
} // namespace App


#endif // APP_GEOFEATURE_H
#endif // APP_GEOFEATURE_H
51 changes: 48 additions & 3 deletions src/App/PropertyStandard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3345,6 +3345,10 @@ void PropertyMaterialList::Restore(Base::XMLReader& reader)
void PropertyMaterialList::SaveDocFile(Base::Writer& writer) const
{
Base::OutputStream str(writer.Stream());
// Write the version. Versions should be negative. A non-negative value is a count
// and should be processed as a V0
int32_t version = -1;
str << version;
uint32_t uCt = (uint32_t)getSize();
str << uCt;
for (const auto& it : _lValueList) {
Expand All @@ -3354,17 +3358,56 @@ void PropertyMaterialList::SaveDocFile(Base::Writer& writer) const
str << it.emissiveColor.getPackedValue();
str << it.shininess;
str << it.transparency;
// str << it.uuid.c_str();
}
}

void PropertyMaterialList::RestoreDocFile(Base::Reader& reader)
{
Base::InputStream str(reader);
uint32_t uCt = 0;
str >> uCt;
std::vector<Material> values(uCt);
int32_t version;
str >> version;
if (version < 0) {
RestoreDocFileV1(reader);
}
else {
uint32_t uCt = static_cast<uint32_t>(version);
RestoreDocFileV0(uCt, reader);
}
}

void PropertyMaterialList::RestoreDocFileV0(uint32_t count, Base::Reader& reader)
{
Base::InputStream str(reader);
std::vector<Material> values(count);
uint32_t value; // must be 32 bit long
float valueF;
for (auto& it : values) {
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);
}

void PropertyMaterialList::RestoreDocFileV1(Base::Reader& reader)
{
Base::InputStream str(reader);
uint32_t count = 0;
str >> count;
std::vector<Material> values(count);
uint32_t value; // must be 32 bit long
float valueF;
char valueS[37]; // UUID length is 36 including '-'s

Check warning on line 3410 in src/App/PropertyStandard.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

unused variable 'valueS' [-Wunused-variable]
for (auto& it : values) {
str >> value;
it.ambientColor.setPackedValue(value);
Expand All @@ -3378,6 +3421,8 @@ void PropertyMaterialList::RestoreDocFile(Base::Reader& reader)
it.shininess = valueF;
str >> valueF;
it.transparency = valueF;
// str >> valueS;
// it.uuid = valueS;
}
setValues(values);
}
Expand Down
3 changes: 3 additions & 0 deletions src/App/PropertyStandard.h
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,9 @@ class AppExport PropertyMaterialList: public PropertyListsT<Material>
Material getPyValue(PyObject*) const override;
void verifyIndex(int index) const;
void setSizeOne();

void RestoreDocFileV0(uint32_t count, Base::Reader& reader);
void RestoreDocFileV1(Base::Reader& reader);
};


Expand Down
3 changes: 3 additions & 0 deletions src/Gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ generate_from_xml(DocumentPy)
generate_from_xml(PythonWorkbenchPy)
generate_from_xml(ViewProviderPy)
generate_from_xml(ViewProviderDocumentObjectPy)
generate_from_xml(ViewProviderGeometryObjectPy)
generate_from_xml(ViewProviderExtensionPy)
generate_from_xml(WorkbenchPy)
generate_from_xml(SelectionObjectPy)
Expand All @@ -243,6 +244,7 @@ generate_from_py(FreeCADGuiInit GuiInitScript.h)
# The XML files
SET(FreeCADGui_XML_SRCS
ViewProviderDocumentObjectPy.xml
ViewProviderGeometryObjectPy.xml
ViewProviderPy.xml
ViewProviderExtensionPy.xml
PythonWorkbenchPy.xml
Expand Down Expand Up @@ -910,6 +912,7 @@ SET(Viewprovider_CPP_SRCS
ViewProviderDocumentObject.cpp
ViewProviderDocumentObjectGroup.cpp
ViewProviderDocumentObjectPyImp.cpp
ViewProviderGeometryObjectPyImp.cpp
ViewProviderDragger.cpp
ViewProviderExtern.cpp
ViewProviderFeature.cpp
Expand Down
Loading

0 comments on commit b0e6e96

Please sign in to comment.