SoFCSelection and App::FeaturePython

Need help, or want to share a macro? Post here!
realthunder
Posts: 1535
Joined: Tue Jan 03, 2017 10:55 am

Re: SoFCSelection and App::FeaturePython

Postby realthunder » Sun May 24, 2020 2:01 am

tomkcook wrote:
Sat May 23, 2020 8:10 pm
One more thing to add to this: I've found that if I take the last gist I posted and replace the SoIndexedPointSet objects with SoTranslation and SoSphere objects, then those sphere objects show correct highlight and selection behaviour. So it is something particular about SoIndexedPointSet and SoIndexedLineSet that means the highlight and selection behaviour doesn't work for them.
You need to change the style of highlight to 'EMISSIVE_DIFFUSE'.

Code: Select all

sel1.style = 'EMISSIVE_DIFFUSE'

By default, the style is 'EMISSIVE', which means the highlight is achieved by overriding the emissive color, this is so that the original diffuse color can still show through. I guess the coin SoIndexedPoint/LineSet simply ignore emissive color when rendering.

BTW, I made a mistake before in my getDetailPath() function, it should be something like

Code: Select all

    def getDetailPath(self, subname, path, append):
        vobj = self.ViewObject
        if append:
            path.append(vobj.RootNode)
            path.append(vobj.SwitchNode)

        path.append(self.parent)
        sub = Part.splitSubname(subname)[-1]
        if sub == 'Atom1':
            path.append(self.sel1)
        elif sub == 'Atom2':
            path.append(self.sel2)
        elif sub == 'Line':
            path.append(self.sel3)
        return True
The problem of the original code won't show up until you create a link to your object.
Try Assembly3 (latest version 0.11) along with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
tomkcook
Posts: 79
Joined: Wed Jul 20, 2016 3:39 pm

Re: SoFCSelection and App::FeaturePython

Postby tomkcook » Sun May 24, 2020 8:07 pm

Thank you for your help and I'm so sorry to be a pain - but that doesn't work for me.

Here's my attach function:

Code: Select all

    def attach(self, obj):
        material = coin.SoMaterial()
        material.diffuseColor = (1.0, 0.0, 0.0)
        drawStyle = coin.SoDrawStyle()
        drawStyle.pointSize.setValue(10)
        drawStyle.style = coin.SoDrawStyle.LINES

        self.coords = coin.SoCoordinate3()
        self.coords.point.setValues(0, 2, [FreeCAD.Vector(0, 0, 0), FreeCAD.Vector(1, 0, 0)])
        parent = coin.SoType.fromName('SoFCSelection').createInstance()
        self.parent = parent

        parent += self.coords
        parent += drawStyle
        parent += material

        sel1 = coin.SoType.fromName('SoFCSelection').createInstance()
        sel1.style = 'EMISSIVE_DIFFUSE'
        p1 = coin.SoType.fromName('SoIndexedPointSet').createInstance()
        p1.coordIndex.set1Value(0, 0)
        sel1 += p1
        parent += sel1

        sel2 = coin.SoType.fromName('SoFCSelection').createInstance()
        sel2.style = 'EMISSIVE_DIFFUSE'
        p2 = coin.SoType.fromName('SoIndexedPointSet').createInstance()
        p2.coordIndex.set1Value(0, 1)
        sel2 += p2
        parent += sel2

        sel3 = coin.SoType.fromName('SoFCSelection').createInstance()
        sel3.style = 'EMISSIVE_DIFFUSE'
        p3 = coin.SoType.fromName('SoIndexedLineSet').createInstance()
        p3.coordIndex.setValues(0, 2, [0, 1])
        sel3 += p3
        parent += sel3

        for mode in self.getDisplayModes(obj):
            obj.addDisplayMode(parent, mode)

        self.sel1 = sel1
        self.sel2 = sel2
        self.sel3 = sel3
        self.constructed = True
        self.updateData(obj.Object, 'p2')
This still displays no preselection or selection behaviour.
realthunder
Posts: 1535
Joined: Tue Jan 03, 2017 10:55 am

Re: SoFCSelection and App::FeaturePython

Postby realthunder » Sun May 24, 2020 11:18 pm

tomkcook wrote:
Sun May 24, 2020 8:07 pm
Thank you for your help and I'm so sorry to be a pain - but that doesn't work for me.
Just need a single line change. Change the type of self.parent to SoGroup, it'll work. If you have other display mode, then use SoSeparator instead.
Try Assembly3 (latest version 0.11) along with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
tomkcook
Posts: 79
Joined: Wed Jul 20, 2016 3:39 pm

Re: SoFCSelection and App::FeaturePython

Postby tomkcook » Thu May 28, 2020 2:51 pm

Brilliant. Thank you yet again.

One more thing I've found, for anyone who comes after me: Adding the same node as the root for multiple display modes using addDisplayMode(node, 'name') makes the highlight/selection behaviour go quite weird. The first element you mouse over will highlight, and if you then select it then it will select as well. But if you move the mouse over an element and then away, nothing will then highlight or select until you click on an element and then click on the background. The fix to this is to use separate root elements for each display mode, with all the same children added to each one.