App: add New APIs for future Link function

DocumentObject:

* getSubObject(): the most important API for Link to work with
  hierarchies. The function is a inspired from and replaces the
  getPySubObjects(). It returns a child object following a dot separated
  subname reference, and can optionally return accumulated
  transformation, and/or a python object of the refered
  sub-object/element. The default implementation here is to look for
  link type property, and search for the referenced object. This patch also
  include other specialized implementation of this API, such as
  (GeoFeature)GroupExtension (through extensionGetSubObject()),
  PartDesign::Body, and so on. A link type object is expected to
  call the linked object's getSubObject() for resolving.

* getSubObjectList(): helper function to return a list of object
  referenced in the given subname.

* getSubObjects(): return a list of subname references of all children
  objects. The purpose of this function is similar to
  ViewProvider::claimChildren().  Container type object is expected to
  implement this function.  The reason it returns subname references
  instead of just object is to allow the container to skip hierarchies.
  For example, the Assembly3 container uses this to skip the constraint
  and element group.

* getLinkedObject(), obtain the linked object, and optionally with the
  accumulated transformation. It is expected to return a linked object
  or the object itself if it is not a link. In case there are multiple
  levels of linking involved, this function allows the caller to retrieve
  the linked object recursively.

* hasChildElement(), set/isElementVisible(), controls the children
  visibility for a group type object. Because the child object may be
  claimed by other objects, it is essential to have independent control
  of children visibilities. These APIs are designed to abstract how
  group manages the child visibility. For performance reason, these
  function are meant to control only the immediate child object.

* resolve(), helper function to parse subname reference and resolve the
  final object, and optionally the immediate parent of the final object,
  the final object reference name (for calling `set/isElementVisible()`),
  and the subname reference if there is one.

* touch(), add optional argument 'noRecompute' for better backward
  compatibility with the NoRecompute flag. By default, touch() causes
  recompute unless noRecompute is true

* signalChanged/signalBeforeChange, two new signal for tracking changes
  of a specific object.

* getViewProviderNameOverride(), return a string of the view provider
  type of this object. This allows Python class to override the view
  provider of an object. This feature will be used by ViewProviderLink
  which is designed to work with any object that has LinkBaseExtension.

* canLinkProperties(), will be used by Gui::PropertyView to display
  linked object properties together with the object's own properties.

* redirectSubname(), will be used by Gui::Tree to allow an object to
  redirect selection to some other object when (pre)selected in the tree
  view.

* Visibility, new property serve as the same purpose as view provider
  property of the same name. It is added here so that App namespace
  code can check for visibility without Gui module. This is useful,
  for example, when constructing a compound shape of a container that
  respects the children visibility.

* (has)hasHiddenMarker(), return or check for a special sub-element
  name used as marker for overriding sub-object visibility. Will be
  used by Gui::ViewProvider, it is put here for the same reason as
  adding Visibility property.

* getID(), return object internal identifier. Each object is now
  assigned an integer identifier that is unique within its containing
  document.

Document:

* ShowHidden, new property to tell tree view whether to show hidden
  object items.

* signalTouchedObject, new signal triggered when manually touch an
  object when calling its touch() function

* getObjectByID(), get object by its identifier

* addObject() is modified to allow overriding view provider

* has/getLinksTo(), helper function to obtain links to a given object.

Application:

* checkLinkDepth(), helper function to check recursive depth for link
  traversal. The depth is checked against the total object count of
  all opened documents. The count (_objCount) is internally updated
  whenever object is added or removed.

* has/getLinksTo(), same as Document::has/getLinksTo() but return links
  from all opened documents.

GroupExtension/OriginGroupExtension/DatumFeature/DatumCS/Part::Feature:
implement sepcialized getSubObject/getSubObjects().
This commit is contained in:
Zheng, Lei
2019-06-24 16:30:08 +08:00
committed by wmayer
parent 2d9ca92594
commit 6731e83bf5
31 changed files with 1762 additions and 172 deletions

View File

@@ -26,6 +26,8 @@
#ifndef _PreComp_
#endif
#include "OCCError.h"
#include "PartPyCXX.h"
#include "DatumFeature.h"
@@ -58,6 +60,31 @@ TopoDS_Shape Datum::getShape() const
return sh.getShape();
}
App::DocumentObject *Datum::getSubObject(const char *subname,
PyObject **pyObj, Base::Matrix4D *pmat, bool transform, int depth) const
{
// For the sake of simplicity, we don't bother to check for subname, just
// return the shape as it is, because a datum object only holds shape with
// one single geometry element.
(void)subname;
(void)depth;
if(pmat && transform)
*pmat *= Placement.getValue().toMatrix();
if(!pyObj)
return const_cast<Datum*>(this);
Base::PyGILStateLocker lock;
PY_TRY {
TopoShape ts(getShape().Located(TopLoc_Location()));
if(pmat && !ts.isNull())
ts.transformShape(*pmat,false,true);
*pyObj = Py::new_reference_to(shape2pyshape(ts.getShape()));
return const_cast<Datum*>(this);
} PY_CATCH_OCC
}
Base::Vector3d Datum::getBasePoint () const {
return Placement.getValue().getPosition();
}

View File

@@ -49,11 +49,13 @@ public:
virtual const char* getViewProviderName(void) const = 0;
/// Return a shape including Placement representing the datum feature
TopoDS_Shape getShape() const;
virtual TopoDS_Shape getShape() const;
/// Returns a point of the feature it counts as it's base
virtual Base::Vector3d getBasePoint () const;
virtual App::DocumentObject *getSubObject(const char *subname, PyObject **pyObj,
Base::Matrix4D *mat, bool transform, int depth) const override;
protected:
void onDocumentRestored();
void handleChangedPropertyName(Base::XMLReader &reader, const char* TypeName, const char* PropName);

View File

@@ -42,7 +42,8 @@
# include <Bnd_Box.hxx>
# include <BRepBndLib.hxx>
# include <BRepExtrema_DistShapeShape.hxx>
# include <BRepAdaptor_Curve.hxx>
# include <TopoDS.hxx>
# include <GProp_GProps.hxx>
# include <BRepGProp.hxx>
# include <gce_MakeLin.hxx>
@@ -59,13 +60,18 @@
#include <Base/Stream.h>
#include <Base/Placement.h>
#include <Base/Rotation.h>
#include <App/Application.h>
#include <App/FeaturePythonPyImp.h>
#include <App/Document.h>
#include "PartPyCXX.h"
#include "PartFeature.h"
#include "PartFeaturePy.h"
#include "TopoShapePy.h"
using namespace Part;
FC_LOG_LEVEL_INIT("Part",true,true);
PROPERTY_SOURCE(Part::Feature, App::GeoFeature)
@@ -112,20 +118,74 @@ PyObject *Feature::getPyObject(void)
return Py::new_reference_to(PythonObject);
}
std::vector<PyObject *> Feature::getPySubObjects(const std::vector<std::string>& NameVec) const
App::DocumentObject *Feature::getSubObject(const char *subname,
PyObject **pyObj, Base::Matrix4D *pmat, bool transform, int depth) const
{
try {
std::vector<PyObject *> temp;
for (std::vector<std::string>::const_iterator it=NameVec.begin();it!=NameVec.end();++it) {
PyObject *obj = Shape.getShape().getPySubShape((*it).c_str());
if (obj)
temp.push_back(obj);
}
return temp;
// having '.' inside subname means it is referencing some children object,
// instead of any sub-element from ourself
if(subname && !Data::ComplexGeoData::isMappedElement(subname) && strchr(subname,'.'))
return App::DocumentObject::getSubObject(subname,pyObj,pmat,transform,depth);
if(pmat && transform)
*pmat *= Placement.getValue().toMatrix();
if(!pyObj) {
#if 0
if(subname==0 || *subname==0 || Shape.getShape().hasSubShape(subname))
return const_cast<Feature*>(this);
return nullptr;
#else
// TopoShape::hasSubShape is kind of slow, let's cut outself some slack here.
return const_cast<Feature*>(this);
#endif
}
catch (Standard_Failure&) {
//throw Py::ValueError(e.GetMessageString());
return std::vector<PyObject *>();
try {
TopoShape ts(Shape.getShape());
bool doTransform = pmat && *pmat!=ts.getTransform();
if(doTransform) {
ts.setShape(ts.getShape().Located(TopLoc_Location()),false);
ts.initCache(1);
}
if(subname && *subname && !ts.isNull())
ts = ts.getSubTopoShape(subname,true);
if(doTransform && !ts.isNull()) {
static int sCopy = -1;
if(sCopy<0) {
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
"User parameter:BaseApp/Preferences/Mod/Part/General");
sCopy = hGrp->GetBool("CopySubShape",false)?1:0;
}
bool copy = sCopy?true:false;
if(!copy) {
// Work around OCC bug on transforming circular edge with an
// offseted surface. The bug probably affect other shape type,
// too.
TopExp_Explorer exp(ts.getShape(),TopAbs_EDGE);
if(exp.More()) {
auto edge = TopoDS::Edge(exp.Current());
exp.Next();
if(!exp.More()) {
BRepAdaptor_Curve curve(edge);
copy = curve.GetType() == GeomAbs_Circle;
}
}
}
ts.transformShape(*pmat,copy,true);
}
*pyObj = Py::new_reference_to(shape2pyshape(ts));
return const_cast<Feature*>(this);
}catch(Standard_Failure &e) {
std::ostringstream str;
Standard_CString msg = e.GetMessageString();
str << typeid(e).name() << " ";
if (msg) {str << msg;}
else {str << "No OCCT Exception Message";}
str << ": " << getFullName();
if (subname)
str << '.' << subname;
FC_ERR(str.str());
return 0;
}
}

View File

@@ -31,6 +31,8 @@
#include <App/PropertyGeo.h>
// includes for findAllFacesCutBy()
#include <TopoDS_Face.hxx>
#include <BRep_Builder.hxx>
#include <TopoDS_Compound.hxx>
class gp_Dir;
class BRepBuilderAPI_MakeShape;
@@ -63,10 +65,12 @@ public:
virtual const App::PropertyComplexGeoData* getPropertyOfGeometry() const;
virtual PyObject* getPyObject(void);
virtual std::vector<PyObject *> getPySubObjects(const std::vector<std::string>&) const;
TopLoc_Location getLocation() const;
virtual DocumentObject *getSubObject(const char *subname, PyObject **pyObj,
Base::Matrix4D *mat, bool transform, int depth) const override;
protected:
/// recompute only this object
virtual App::DocumentObjectExecReturn *recompute(void);

View File

@@ -497,3 +497,29 @@ PyObject *Body::getPyObject(void)
}
return Py::new_reference_to(PythonObject);
}
std::vector<std::string> Body::getSubObjects(int reason) const {
if(reason==GS_SELECT && !showTip)
return Part::BodyBase::getSubObjects(reason);
return {};
}
App::DocumentObject *Body::getSubObject(const char *subname,
PyObject **pyObj, Base::Matrix4D *pmat, bool transform, int depth) const
{
if(!pyObj || showTip ||
(subname && !Data::ComplexGeoData::isMappedElement(subname) && strchr(subname,'.')))
return Part::BodyBase::getSubObject(subname,pyObj,pmat,transform,depth);
// We return the shape only if there are feature visibile inside
for(auto obj : Group.getValues()) {
if(obj->Visibility.getValue() &&
obj->isDerivedFrom(PartDesign::Feature::getClassTypeId()))
{
return Part::BodyBase::getSubObject(subname,pyObj,pmat,transform,depth);
}
}
if(pmat && transform)
*pmat *= Placement.getValue().toMatrix();
return const_cast<Body*>(this);
}

View File

@@ -120,6 +120,13 @@ public:
PyObject *getPyObject(void) override;
virtual std::vector<std::string> getSubObjects(int reason=0) const override;
virtual App::DocumentObject *getSubObject(const char *subname,
PyObject **pyObj, Base::Matrix4D *pmat, bool transform, int depth) const override;
void setShowTip(bool enable) {
showTip = enable;
}
protected:
virtual void onSettingDocument() override;
@@ -146,6 +153,7 @@ protected:
private:
boost::signals2::scoped_connection connection;
bool showTip = false;
};
} //namespace PartDesign

View File

@@ -28,6 +28,8 @@
# include <gp_Pln.hxx>
#endif
#include <Mod/Part/App/PartPyCXX.h>
#include <Mod/Part/App/OCCError.h>
#include "DatumCS.h"
using namespace PartDesign;
@@ -77,3 +79,32 @@ Base::Vector3d CoordinateSystem::getZAxis()
rot.multVec(Base::Vector3d(0,0,1), normal);
return normal;
}
App::DocumentObject *CoordinateSystem::getSubObject(const char *subname,
PyObject **pyObj, Base::Matrix4D *pmat, bool transform, int) const
{
if(pmat && transform)
*pmat *= Placement.getValue().toMatrix();
if(!pyObj)
return const_cast<CoordinateSystem*>(this);
gp_Dir dir(0,0,1);
if(subname) {
if(strcmp(subname,"X")==0)
dir = gp_Dir(1,0,0);
else if(strcmp(subname,"Y")==0)
dir = gp_Dir(0,1,0);
}
Base::PyGILStateLocker lock;
PY_TRY {
BRepBuilderAPI_MakeFace builder(gp_Pln(gp_Pnt(0,0,0), dir));
Part::TopoShape ts(builder.Shape());
if(pmat)
ts.transformShape(*pmat,false,true);
*pyObj = Py::new_reference_to(Part::shape2pyshape(ts));
return const_cast<CoordinateSystem*>(this);
} PY_CATCH_OCC
}

View File

@@ -44,6 +44,9 @@ public:
Base::Vector3d getXAxis();
Base::Vector3d getYAxis();
Base::Vector3d getZAxis();
virtual App::DocumentObject *getSubObject(const char *subname,
PyObject **pyObj, Base::Matrix4D *pmat, bool transform, int depth) const override;
};
} //namespace PartDesign