HierarchyObjectInterface Manual

内容表

关于

maxon::HierarchyObjectInterface is a base interface used to create elements that can be organized in a tree like structure. A parent element can have one or many child elements. A child element can have only one parent but none, one or many siblings.

A parent object can organize its child elements in multiple sub-branches.

Hierarchy Interface

The hierarchy interface provides functions to insert an element into a given tree structure:

// This example creates several elements and inserts them under the "root" element. // At the end, the order of elements is checked.

// create "root" element const TreeElementRef root = TreeElementFactory().Create( "root" _s) iferr_return ; // create child elements const TreeElementRef elementA = TreeElementFactory().Create( "Element A" _s) iferr_return ; const TreeElementRef elementB = TreeElementFactory().Create( "Element B" _s) iferr_return ; const TreeElementRef elementC = TreeElementFactory().Create( "Element C" _s) iferr_return ;

// insert first element under the root element elementC.InsertAsLastChildOf(root) iferr_return ;

// insert second element as a sibling under the root element elementB.InsertBefore(elementC) iferr_return ;

// insert third element under the root element root.InsertChildAsFirst(elementA) iferr_return ;

// check element order

const auto children = root.GetChildren() iferr_return ; for ( const auto & child : children) { const TreeElementRef treeElement = maxon::Cast<TreeElementRef>(child); const maxon::String name = treeElement.GetName(); DiagnosticOutput ( "Child Element: @" , name); }

Child elements can be replaced or removed:

// This example creates multiple elements, inserts them as // child elements of the "root" element and removes them again.

// create "root" element const TreeElementRef root = TreeElementFactory().Create( "root" _s) iferr_return ;

// create child elements const TreeElementRef elementA = TreeElementFactory().Create( "Element A" _s) iferr_return ; const TreeElementRef elementB = TreeElementFactory().Create( "Element B" _s) iferr_return ; const TreeElementRef elementC = TreeElementFactory().Create( "Element C" _s) iferr_return ;

// insert child elements root.InsertChildAsFirst(elementA) iferr_return ; root.InsertChildAsFirst(elementB) iferr_return ;

// replace element elementB.Replace(elementC) iferr_return ;

// remove element elementA.Remove();

// check remaining element const maxon::HierarchyObjectRef<> firstChild = root.GetFirstChild(); const TreeElementRef firstElement = maxon::Cast<TreeElementRef>(firstChild); DiagnosticOutput ( "First Child: @" , firstElement.GetName());

Within a given hierarchy level one can navigate with:

// This example gets the first child element of the given "root" element // and loops through all remaining child elements.

// get first child element maxon::HierarchyObjectRef<> child = root.GetFirstChild(); while (child) { // check type of child if (child.IsInstanceOf<TreeElementRef>()) { const TreeElementRef treeElement = maxon::Cast<TreeElementRef>(child); DiagnosticOutput ( "Tree Element: @" , treeElement.GetName()); }

// get next child child = child.GetNext(); }

The parent of an element is accessed with:

The child elements of a parent object are accessed with:

// This example inserts and accesses child elements in different branches.

// define branch IDs const maxon::ConstDataPtr branchA = maxon::ConstDataPtr ( "a" ); const maxon::ConstDataPtr branchB = maxon::ConstDataPtr ( "b" );

// insert child elements into branches root.InsertChildAsFirst(elementA, branchA) iferr_return ; root.InsertChildAsFirst(elementB, branchB) iferr_return ;

// get child elements from branches const maxon::HierarchyObjectRef<> childA = root.GetFirstChild(branchA); const maxon::HierarchyObjectRef<> childB = root.GetFirstChild(branchB);

These observables are called on certain events:

To navigate through a given hierarchy one can use these utility functions:

// This example traverses over all child elements of the given root element and the root element itself. maxon::TraverseMeAndChildren<TreeElementRef>(root, []( const TreeElementRef& child, const TreeElementRef& parent, const maxon::ConstDataPtr & branch, maxon::Int depth) -> maxon::Result<maxon::Bool> { DiagnosticOutput ( "Tree Element: @" , child.GetName()); return true ; }) iferr_return ;

// This example traverses over all child elements of the given root element but not the root element itself. maxon::TraverseChildren<TreeElementRef>(root, []( const TreeElementRef& child, const TreeElementRef& parent, const maxon::ConstDataPtr & branch, maxon::Int depth) -> maxon::Result<maxon::Bool> { DiagnosticOutput ( "Tree Element: @" , child.GetName()); return true ; }) iferr_return ;

It is also possible to iterate over all elements of a hierarchy using maxon::HierarchyObjectIterator :

// This example uses the HierarchyObjectIterator template to iterate over // all child elements of the given root element and the root element itself.

for ( const TreeElementRef& element : maxon::HierarchyObjectIterator<TreeElementRef> (root)) { DiagnosticOutput ( "Tree Element: @" , element.GetName()); }

Custom Implementations

A custom interface can be based on maxon::HierarchyObjectInterface . The resulting reference object can be organized in a tree. The default implementation of maxon::HierarchyObjectInterface is maxon::HierarchyObjectClass .

An interface based on maxon::HierarchyObjectInterface can implement maxon::HierarchyObjectInterface::ParentChanged() to be informed when the parent of the element has changed. This function must not be called by user code.

// This example shows an implementation of an interface based on HierarchyObjectInterface. // It implements ParentChanged() and accesses the hierarchy in GetParentName().

// implementation of TreeElementInterface class TreeElementImplementation : public maxon::Component <TreeElementImplementation, TreeElementInterface> { // use the default implementation "HierarchyObjectClass" MAXON_COMPONENT ( NORMAL , maxon::HierarchyObjectClass); public : MAXON_METHOD void SetName( maxon::String name) { _name = name; } MAXON_METHOD maxon::String GetName() const { return _name; }

// function that returns the name of the parent element MAXON_METHOD maxon::Result<maxon::String> GetParentName() const { // access parent element const auto parentObject = super.GetParent(); if (!parentObject) return maxon::String ();

// check type of parent element if (!parentObject.IsInstanceOf<TreeElementRef>()) return maxon::UnexpectedError( MAXON_SOURCE_LOCATION , "Parent object is no TreeElementRef." _s);

// get name of parent element const TreeElementRef treeElement = maxon::Cast<TreeElementRef>(parentObject); return treeElement.GetName(); } MAXON_METHOD void ParentChanged( maxon::Bool removed) { if (removed) return ;

// if a new parent is assigned, print the parent's name const auto parentObject = super.GetParent(); if (parentObject.IsInstanceOf<TreeElementRef>()) { const TreeElementRef treeElement = maxon::Cast<TreeElementRef>(parentObject); DiagnosticOutput ( "New Parent is @" , treeElement.GetName()); } }

// Factory method maxon::Result<void> FactoryInit(maxon::FactoryInterface::ConstPtr, maxon::String name) { self .SetName(name); return maxon::OK ; } private : maxon::String _name; };

延伸阅读

maxon::ComponentWithBase
定义: objectbase.h:2472
maxon::HierarchyObjectIterator
定义: hierarchyobject.h:28
MAXON_COMPONENT
#define MAXON_COMPONENT(KIND,...)
定义: objectbase.h:2036
maxon::String
定义: string.h:1197
maxon::OK
return OK
定义: apibase.h:2532
maxon::Bool
bool Bool
boolean type, possible values are only false/true, 8 bit
定义: apibase.h:177
iferr_return
#define iferr_return
定义: resultbase.h:1434
maxon::ConstDataPtr
定义: datatypebase.h:1730
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
定义: memoryallocationbase.h:66
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
maxon::HierarchyObjectRef
定义: hierarchyobject.h:18
maxon::Result
定义: apibase.h:314
MAXON_METHOD
#define MAXON_METHOD
定义: interfacebase.h:855
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
定义: apibase.h:184
SCULPTBRUSHMODE::NORMAL
@ NORMAL
Samples the surface as the user moves over it the SculptObject and returns a new hit point and normal...

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1