Who can tell me how to use python commands to add custom nodes to the structure tree

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
x70
Posts: 14
Joined: Mon Oct 05, 2020 3:42 pm

Who can tell me how to use python commands to add custom nodes to the structure tree

Post by x70 »

Who can tell me how to use python to add custom items to the treepanel,give me some examples!
thx!
edi
Posts: 481
Joined: Fri Jan 17, 2020 1:32 pm

Re: Who can tell me how to use python commands to add custom nodes to the structure tree

Post by edi »

Open a document, switch to Part WB, start the script

Code: Select all

# 1. create object
Obj = App.ActiveDocument.addObject('Part::FeaturePython','MyPart')
# 2. define properties
Obj.addProperty('App::PropertyLength','A').A=5.0
# 3.create the shape 
Shape1 = Part.makeBox(10.0,10.0,10.0)
Shape2 = Part.makeBox(10.0,10.0,10.0)
Shape2.Placement.move(App.Vector(Obj.A,Obj.A,Obj.A))
Obj.Shape=Shape1.fuse(Shape2)
# 4. make it visible
Obj.ViewObject.Proxy=0
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Who can tell me how to use python commands to add custom nodes to the structure tree

Post by openBrain »

x70 wrote: Wed Nov 04, 2020 3:09 pm Who can tell me how to use python to add custom items to the treepanel,give me some examples!
thx!
ATM, that's just a read-the-doc question :
FeaturePython Objects
Scripted objects
x70
Posts: 14
Joined: Mon Oct 05, 2020 3:42 pm

Re: Who can tell me how to use python commands to add custom nodes to the structure tree

Post by x70 »

openBrain wrote: Wed Nov 04, 2020 4:53 pm
x70 wrote: Wed Nov 04, 2020 3:09 pm Who can tell me how to use python to add custom items to the treepanel,give me some examples!
thx!
ATM, that's just a read-the-doc question :
FeaturePython Objects
Scripted objects
thx!
but I want to add ChildItem to FeaturePython Object in tree view,I cannot find any example.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Who can tell me how to use python commands to add custom nodes to the structure tree

Post by openBrain »

x70 wrote: Tue Nov 17, 2020 3:23 pm thx!
but I want to add ChildItem to FeaturePython Object in tree view,I cannot find any example.
Maybe https://forum.freecadweb.org/viewtopic. ... 34#p194209 :?:
x70
Posts: 14
Joined: Mon Oct 05, 2020 3:42 pm

Re: Who can tell me how to use python commands to add custom nodes to the structure tree

Post by x70 »

openBrain wrote: Tue Nov 17, 2020 3:36 pm
x70 wrote: Tue Nov 17, 2020 3:23 pm thx!
but I want to add ChildItem to FeaturePython Object in tree view,I cannot find any example.
Maybe https://forum.freecadweb.org/viewtopic. ... 34#p194209 :?:
thank you very much,but I try to script like this:
parent=App.ActiveDocument.addObject("App::FeaturePython","Parent")
child=App.ActiveDocument.addObject("App::FeaturePython","Child")
child.addProperty("App::PropertyLink","LinkToParent")
child.LinkToParent=parent
parent and child are in the same level, bu I want to make the tree like:

doc
..parent
.......child1
.......child2
...........subchild1
...........subchild2
...............subsubchild
.......child3
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Who can tell me how to use python commands to add custom nodes to the structure tree

Post by openBrain »

Do your parent viewprovider implements the claimChildren() function as in the example ?
x70
Posts: 14
Joined: Mon Oct 05, 2020 3:42 pm

Re: Who can tell me how to use python commands to add custom nodes to the structure tree

Post by x70 »

openBrain wrote: Wed Nov 18, 2020 3:50 pm Do your parent viewprovider implements the claimChildren() function as in the example ?
great! thank u very much.
but,more a question,How to set doubleClicked Event with every treeItem?Is threre any Examples?
thx!
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Who can tell me how to use python commands to add custom nodes to the structure tree

Post by openBrain »

x70 wrote: Thu Nov 19, 2020 12:55 pm great! thank u very much.
but,more a question,How to set doubleClicked Event with every treeItem?Is threre any Examples?
thx!
Have a look at the viewprovider API : https://www.freecadweb.org/api/d3/db3/c ... vider.html

You can implement the 'doubleClicked' method or have a look at the 'edit methods'. ;)
Post Reply