diff --git a/src/App/ComplexGeoData.cpp b/src/App/ComplexGeoData.cpp index dc61854a80..7af8e84c24 100644 --- a/src/App/ComplexGeoData.cpp +++ b/src/App/ComplexGeoData.cpp @@ -148,3 +148,8 @@ void ComplexGeoData::getFaces(std::vector &Points, (void)Accuracy; (void)flags; } + +bool ComplexGeoData::getCenterOfGravity(Base::Vector3d&) const +{ + return false; +} diff --git a/src/App/ComplexGeoData.h b/src/App/ComplexGeoData.h index 4260c044ed..6b1058d819 100644 --- a/src/App/ComplexGeoData.h +++ b/src/App/ComplexGeoData.h @@ -145,6 +145,11 @@ public: /** Get faces from object with given accuracy */ virtual void getFaces(std::vector &Points,std::vector &faces, float Accuracy, uint16_t flags=0) const; + /** Get the center of gravity + * If this method is implemented then true is returned and the center of gravity. + * The default implementation only returns false. + */ + virtual bool getCenterOfGravity(Base::Vector3d& center) const; //@} protected: diff --git a/src/App/GeoFeature.cpp b/src/App/GeoFeature.cpp index cc55fe77d7..fc2e766b81 100644 --- a/src/App/GeoFeature.cpp +++ b/src/App/GeoFeature.cpp @@ -53,3 +53,8 @@ void GeoFeature::transformPlacement(const Base::Placement &transform) plm = transform * plm; this->Placement.setValue(plm); } + +const PropertyComplexGeoData* GeoFeature::getPropertyOfGeometry() const +{ + return nullptr; +} diff --git a/src/App/GeoFeature.h b/src/App/GeoFeature.h index f022ef63a2..9ca50968dc 100644 --- a/src/App/GeoFeature.h +++ b/src/App/GeoFeature.h @@ -54,6 +54,13 @@ public: * @param transform (input). */ 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 + * a mesh object the Mesh property and so on. + * The default implementation returns null. + */ + virtual const PropertyComplexGeoData* getPropertyOfGeometry() const; }; } //namespace App diff --git a/src/Mod/Mesh/App/MeshFeature.h b/src/Mod/Mesh/App/MeshFeature.h index 52c36b503b..c1bf8f2e42 100644 --- a/src/Mod/Mesh/App/MeshFeature.h +++ b/src/Mod/Mesh/App/MeshFeature.h @@ -77,6 +77,9 @@ public: virtual const char* getViewProviderName(void) const { return "MeshGui::ViewProviderMeshFaceSet"; } + virtual const App::PropertyComplexGeoData* getPropertyOfGeometry() const { + return &Mesh; + } /// handles the MeshPy object virtual PyObject* getPyObject(void); diff --git a/src/Mod/Part/App/PartFeature.cpp b/src/Mod/Part/App/PartFeature.cpp index 2034935821..5b132e9d9f 100644 --- a/src/Mod/Part/App/PartFeature.cpp +++ b/src/Mod/Part/App/PartFeature.cpp @@ -238,6 +238,11 @@ const char* Feature::getViewProviderName(void) const { return "PartGui::ViewProviderPart"; } +const App::PropertyComplexGeoData* Feature::getPropertyOfGeometry() const +{ + return &Shape; +} + // --------------------------------------------------------- PROPERTY_SOURCE(Part::FilletBase, Part::Feature) diff --git a/src/Mod/Part/App/PartFeature.h b/src/Mod/Part/App/PartFeature.h index f1284b2723..7484c890f4 100644 --- a/src/Mod/Part/App/PartFeature.h +++ b/src/Mod/Part/App/PartFeature.h @@ -60,6 +60,7 @@ public: /// returns the type name of the ViewProvider virtual const char* getViewProviderName(void) const; + virtual const App::PropertyComplexGeoData* getPropertyOfGeometry() const; virtual PyObject* getPyObject(void); virtual std::vector getPySubObjects(const std::vector&) const; diff --git a/src/Mod/Part/App/TopoShape.cpp b/src/Mod/Part/App/TopoShape.cpp index 32eb152d58..da9731beff 100644 --- a/src/Mod/Part/App/TopoShape.cpp +++ b/src/Mod/Part/App/TopoShape.cpp @@ -63,6 +63,7 @@ # include # include # include +# include # include # include # include @@ -94,6 +95,7 @@ # include # include # include +# include # include # include # include @@ -928,6 +930,36 @@ Base::BoundBox3d TopoShape::getBoundBox(void) const return box; } +bool TopoShape::getCenterOfGravity(Base::Vector3d& center) const +{ + if (_Shape.IsNull()) + return false; + + // Computing of CentreOfMass + gp_Pnt pnt; + + if (_Shape.ShapeType() == TopAbs_VERTEX) { + pnt = BRep_Tool::Pnt(TopoDS::Vertex(_Shape)); + } + else { + GProp_GProps prop; + if (_Shape.ShapeType() == TopAbs_EDGE || _Shape.ShapeType() == TopAbs_WIRE) { + BRepGProp::LinearProperties(_Shape, prop); + } + else if (_Shape.ShapeType() == TopAbs_FACE || _Shape.ShapeType() == TopAbs_SHELL) { + BRepGProp::SurfaceProperties(_Shape, prop); + } + else { + BRepGProp::VolumeProperties(_Shape, prop); + } + + pnt = prop.CentreOfMass(); + } + + center.Set(pnt.X(), pnt.Y(), pnt.Z()); + return true; +} + void TopoShape::Save (Base::Writer & ) const { } diff --git a/src/Mod/Part/App/TopoShape.h b/src/Mod/Part/App/TopoShape.h index 00f5ad1cfc..36146d9d72 100644 --- a/src/Mod/Part/App/TopoShape.h +++ b/src/Mod/Part/App/TopoShape.h @@ -81,6 +81,7 @@ public: Base::Matrix4D getTransform(void) const; /// Bound box from the CasCade shape Base::BoundBox3d getBoundBox(void)const; + virtual bool getCenterOfGravity(Base::Vector3d& center) const; static void convertTogpTrsf(const Base::Matrix4D& mtrx, gp_Trsf& trsf); static void convertToMatrix(const gp_Trsf& trsf, Base::Matrix4D& mtrx); //@} diff --git a/src/Mod/Points/App/PointsFeature.h b/src/Mod/Points/App/PointsFeature.h index 763341ad5e..00cca571b0 100644 --- a/src/Mod/Points/App/PointsFeature.h +++ b/src/Mod/Points/App/PointsFeature.h @@ -69,6 +69,10 @@ public: virtual const char* getViewProviderName(void) const { return "PointsGui::ViewProviderScattered"; } + + virtual const App::PropertyComplexGeoData* getPropertyOfGeometry() const { + return &Points; + } protected: void onChanged(const App::Property* prop); //@}