Pivy: getSelection method??

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
JMG
Posts: 288
Joined: Wed Dec 25, 2013 9:32 am
Location: Spain
Contact:

Pivy: getSelection method??

Post by JMG »

Hi,

I'm playing around with voxels (https://github.com/JMG1/VoxelWorkbench), and there are a lot of them, making it laggy. Currently, this workbench uses the Part.makeCube to create each voxel geometry, that is included inside a list that creates a shape with Part.makeCompound( list ) function.

I've tried to implement the same functionality using coin3d soCube, but I don't know how to get the mouse selection of this pure coin3d objects.

If I place a cube in the scenegraph, like this example:

Code: Select all

from pivy import coin

sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
col = coin.SoBaseColor()
col.rgb=(1,0,0)
cub = coin.SoCube()
myCustomNode = coin.SoSeparator()
myCustomNode.addChild(col)
myCustomNode.addChild(cub)
sg.addChild(myCustomNode) 
How could I retrieve at mouse click the cube object, or the selected face, or point in the 3d space, or anything related to this geometry?

I've have investigated pivy, coin3d and some inventor mentor codes, without success.


Regards,
Javier.
FreeCAD scripts, animations, experiments and more: http://linuxforanengineer.blogspot.com.es/
Open source CNC hot wire cutter project (NiCr): https://github.com/JMG1/NiCr
Exploded Assembly Workbench: https://github.com/JMG1/ExplodedAssembly
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Pivy: getSelection method??

Post by looo »

Is it about selection or also highlighting?

I think you have some options:

1. use the freecad selection:
there you should use a SoBrepFaceSet. https://forum.freecadweb.org/viewtopic. ... =10#p35071 I think this is possible from python, but not that easy.

2. use an eventcallback for sending rays on mouse move or mouse press. Maybe you have a look at our implementation of drag-able points https://github.com/booya-at/freecad_gli ... y#L220L229

3. use a special selection node to do the selection. And a action node to trigger a function when a face was selected.
https://github.com/looooo/pivy/blob/mas ... nCB.py#L65

Maybe you have to create the cube from faces to distinguish between them. I don't know if this is possible with a SoCube.
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Pivy: getSelection method??

Post by wmayer »

You can try two things:

1. Instead of creating a new shape for each box try to use a compound. This way you only exactly one shape in the scene graph which makes the rendering much faster. But the downside is that the creation of the compound shape may take longer. So, you have to test if the performance boost in rendering outweighs the slower creation of the shapes

2. When working only with pure Inventor nodes then try out this:

Code: Select all

from pivy import coin

sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
col = coin.SoBaseColor()
col.rgb=(1,0,0)
cub = coin.SoCube()
myCustomNode = coin.SoSeparator()
myCustomNode.addChild(col)
myCustomNode.addChild(cub)
sg.addChild(myCustomNode) 

view = FreeCADGui.ActiveDocument.ActiveView
viewer=view.getViewer()
ray=coin.SoRayPickAction(viewer.getSoRenderManager().getViewportRegion())
x,y=view.getCursorPos()
ray.setPoint(coin.SbVec2s(x,y))

ray.apply(viewer.getSoRenderManager().getSceneGraph())
pp=ray.getPickedPoint()
path=pp.getPath()
path.getTail()
pp.getNormal().getValue()

det=pp.getDetail()
cubedet=coin.cast(det,"SoCubeDetail")
cubedet.getPart()
JMG
Posts: 288
Joined: Wed Dec 25, 2013 9:32 am
Location: Spain
Contact:

Re: Pivy: getSelection method??

Post by JMG »

Thanks!!

I'll try this weekend, specially the pure inventor code :)
FreeCAD scripts, animations, experiments and more: http://linuxforanengineer.blogspot.com.es/
Open source CNC hot wire cutter project (NiCr): https://github.com/JMG1/NiCr
Exploded Assembly Workbench: https://github.com/JMG1/ExplodedAssembly
Post Reply