Skip to content

Commit

Permalink
App: add New APIs for future Link function
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
realthunder authored and wwmayer committed Aug 17, 2019
1 parent 3fcbf71 commit ff1d1cd
Show file tree
Hide file tree
Showing 31 changed files with 1,762 additions and 172 deletions.
52 changes: 51 additions & 1 deletion src/App/Application.cpp
Expand Up @@ -148,6 +148,7 @@ using namespace boost::program_options;
# include <new>
#endif

FC_LOG_LEVEL_INIT("App",true,true);

//using Base::GetConsole;
using namespace Base;
Expand Down Expand Up @@ -249,7 +250,7 @@ init_freecad_module(void)
#endif

Application::Application(std::map<std::string,std::string> &mConfig)
: _mConfig(mConfig), _pActiveDoc(0)
: _mConfig(mConfig), _pActiveDoc(0), _objCount(-1)
{
//_hApp = new ApplicationOCC;
mpcPramManager["System parameter"] = _pcSysParamMngr;
Expand Down Expand Up @@ -481,6 +482,8 @@ bool Application::closeDocument(const char* name)
std::unique_ptr<Document> delDoc (pos->second);
DocMap.erase( pos );

_objCount = -1;

// Trigger observers after removing the document from the internal map.
signalDeletedDocument();

Expand Down Expand Up @@ -704,6 +707,51 @@ std::string Application::getHelpDir()
#endif
}

int Application::checkLinkDepth(int depth, bool no_throw) {
if(_objCount<0) {
_objCount = 0;
for(auto &v : DocMap)
_objCount += v.second->countObjects();
}
if(depth > _objCount+2) {
const char *msg = "Link recursion limit reached. "
"Please check for cyclic reference.";
if(no_throw) {
FC_ERR(msg);
return 0;
}else
throw Base::RuntimeError(msg);
}
return _objCount+2;
}

std::set<DocumentObject *> Application::getLinksTo(
const DocumentObject *obj, int options, int maxCount) const
{
std::set<DocumentObject *> links;
if(!obj) {
for(auto &v : DocMap) {
v.second->getLinksTo(links,obj,options,maxCount);
if(maxCount && (int)links.size()>=maxCount)
break;
}
} else {
std::set<Document*> docs;
for(auto o : obj->getInList()) {
if(o && o->getNameInDocument() && docs.insert(o->getDocument()).second) {
o->getDocument()->getLinksTo(links,obj,options,maxCount);
if(maxCount && (int)links.size()>=maxCount)
break;
}
}
}
return links;
}

bool Application::hasLinksTo(const DocumentObject *obj) const {
return !getLinksTo(obj,0,1).empty();
}

ParameterManager & Application::GetSystemParameter(void)
{
return *_pcSysParamMngr;
Expand Down Expand Up @@ -1009,11 +1057,13 @@ void Application::slotChangedDocument(const App::Document& doc, const Property&
void Application::slotNewObject(const App::DocumentObject&O)
{
this->signalNewObject(O);
_objCount = -1;
}

void Application::slotDeletedObject(const App::DocumentObject&O)
{
this->signalDeletedObject(O);
_objCount = -1;
}

void Application::slotBeforeChangeObject(const DocumentObject& O, const Property& Prop)
Expand Down
50 changes: 48 additions & 2 deletions src/App/Application.h
Expand Up @@ -28,6 +28,7 @@
#include <boost/signals2.hpp>

#include <vector>
#include <deque>

#include <Base/PyObjectBase.h>
#include <Base/Parameter.h>
Expand All @@ -48,6 +49,16 @@ class DocumentObject;
class ApplicationObserver;
class Property;

enum GetLinkOption {
/// Get all links (both directly and in directly) linked to the given object
GetLinkRecursive = 1,
/// Get link array element instead of the array
GetLinkArrayElement = 2,
/// Get linked object instead of the link, no effect if GetLinkRecursive
GetLinkedObject = 4,
/// Get only external links, no effect if GetLinkRecursive
GetLinkExternal = 8,
};


/** The Application
Expand Down Expand Up @@ -124,6 +135,8 @@ class AppExport Application
boost::signals2::signal<void (const Document&)> signalUndoDocument;
/// signal on redo in document
boost::signals2::signal<void (const Document&)> signalRedoDocument;
/// signal on show hidden items
boost::signals2::signal<void (const Document&)> signalShowHidden;
//@}


Expand Down Expand Up @@ -274,6 +287,34 @@ class AppExport Application
static std::string getHelpDir();
//@}

/** @name Link handling */
//@{

/** Check for link recursion depth
*
* @param depth: current depth
* @param no_throw: whether to throw exception
*
* @return Return the maximum remaining depth.
*
* The function uses an internal count of all objects in all documents as
* the limit of recursion depth.
*/
int checkLinkDepth(int depth, bool no_throw=true);

/** Return the links to a given object
*
* @param obj: the linked object. If NULL, then all links are returned.
* @param option: @sa App::GetLinkOptions
* @param maxCount: limit the number of links returned, 0 means no limit
*/
std::set<DocumentObject*> getLinksTo(
const DocumentObject *, int options, int maxCount=0) const;

/// Check if there is any link to the given object
bool hasLinksTo(const DocumentObject *obj) const;
//@}

friend class App::Document;

protected:
Expand Down Expand Up @@ -315,7 +356,6 @@ class AppExport Application
static ParameterManager *_pcUserParamMngr;
//@}


//---------------------------------------------------------------------
// python exports goes here +++++++++++++++++++++++++++++++++++++++++++
//---------------------------------------------------------------------
Expand Down Expand Up @@ -353,7 +393,10 @@ class AppExport Application
static PyObject *sSetLogLevel (PyObject *self,PyObject *args);
static PyObject *sGetLogLevel (PyObject *self,PyObject *args);

static PyMethodDef Methods[];
static PyObject *sCheckLinkDepth (PyObject *self,PyObject *args);
static PyObject *sGetLinksTo (PyObject *self,PyObject *args);

static PyMethodDef Methods[];

friend class ApplicationObserver;

Expand Down Expand Up @@ -401,6 +444,9 @@ class AppExport Application
std::map<std::string,std::string> &_mConfig;
App::Document* _pActiveDoc;

// for estimate max link depth
int _objCount;

static Base::ConsoleObserverStd *_pConsoleObserverStd;
static Base::ConsoleObserverFile *_pConsoleObserverFile;
};
Expand Down
44 changes: 43 additions & 1 deletion src/App/ApplicationPy.cpp
Expand Up @@ -35,6 +35,7 @@
#include "Document.h"
#include "DocumentPy.h"
#include "DocumentObserverPython.h"
#include "DocumentObjectPy.h"

// FreeCAD Base header
#include <Base/Interpreter.h>
Expand Down Expand Up @@ -144,7 +145,12 @@ PyMethodDef Application::Methods[] = {
"'level' can either be string 'Log', 'Msg', 'Wrn', 'Error', or an integer value"},
{"getLogLevel", (PyCFunction) Application::sGetLogLevel, METH_VARARGS,
"getLogLevel(tag) -- Get the log level of a string tag"},

{"checkLinkDepth", (PyCFunction) Application::sCheckLinkDepth, METH_VARARGS,
"checkLinkDepth(depth) -- check link recursion depth"},
{"getLinksTo", (PyCFunction) Application::sGetLinksTo, METH_VARARGS,
"getLinksTo(obj,options=0,maxCount=0) -- return the objects linked to 'obj'\n\n"
"options: 1: recursive, 2: check link array. Options can combine.\n"
"maxCount: to limit the number of links returned\n"},
{NULL, NULL, 0, NULL} /* Sentinel */
};

Expand Down Expand Up @@ -763,4 +769,40 @@ PyObject *Application::sGetLogLevel(PyObject * /*self*/, PyObject *args)
} PY_CATCH;
}

PyObject *Application::sCheckLinkDepth(PyObject * /*self*/, PyObject *args)
{
short depth = 0;
if (!PyArg_ParseTuple(args, "h", &depth))
return NULL;

PY_TRY {
return Py::new_reference_to(Py::Int(GetApplication().checkLinkDepth(depth,false)));
}PY_CATCH;
}

PyObject *Application::sGetLinksTo(PyObject * /*self*/, PyObject *args)
{
PyObject *pyobj = Py_None;
int options = 0;
short count = 0;
if (!PyArg_ParseTuple(args, "|Oih",&pyobj,&options, &count))
return NULL;

PY_TRY {
DocumentObject *obj = 0;
if(pyobj!=Py_None) {
if(!PyObject_TypeCheck(pyobj,&DocumentObjectPy::Type)) {
PyErr_SetString(PyExc_TypeError, "Expect the first argument of type document object");
return 0;
}
obj = static_cast<DocumentObjectPy*>(pyobj)->getDocumentObjectPtr();
}
auto links = GetApplication().getLinksTo(obj,options,count);
Py::Tuple ret(links.size());
int i=0;
for(auto o : links)
ret.setItem(i++,Py::Object(o->getPyObject(),true));
return Py::new_reference_to(ret);
}PY_CATCH;
}

0 comments on commit ff1d1cd

Please sign in to comment.