[SOLVED] Draw vectors directly to 3D view using pivy + python

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
kbwbe
Veteran
Posts: 1052
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

[SOLVED] Draw vectors directly to 3D view using pivy + python

Post by kbwbe »

Hi,

i need to know how to draw vectors (complete arrow from a 3D Point to another, with color) directly to 3D view by python, also how to delete them later.
Does someone have a small sample ?

Thanks in advance.
Last edited by kbwbe on Wed Dec 02, 2020 4:43 pm, edited 1 time in total.
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.56, installable via FreeCAD's addon manager
Tutorial: gripper assembly https://www.youtube.com/watch?v=QMxcQ5tssWk
Documentation: https://www.freecadweb.org/wiki/A2plus_Workbench
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Draw vectors directly to 3D view using pivy + python

Post by Kunda1 »

This sounds like something @joel_graffs pivy-trackers project can do.
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Draw vectors directly to 3D view using pivy + python

Post by Kunda1 »

This sounds like something @joel_graffs pivy-trackers project can do.
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
HakanSeven12
Veteran
Posts: 1481
Joined: Wed Feb 06, 2019 10:30 pm

Re: Draw vectors directly to 3D view using pivy + python

Post by HakanSeven12 »

You can build an arrow by using lines. Here is my code to create lines.

Code: Select all

        # Contour features.
        cont_color = coin.SoBaseColor()
        cont_color.rgb = (1, 1, 0)
        cont_coords = coin.SoCoordinate3()
        cont_lines = coin.SoLineSet()
Then

Code: Select all

        cont_coords.point.values = pointlist
Then

Code: Select all

        # Contour root.
        contours = coin.SoSeparator()
        contours.addChild(cont_color)
        contours.addChild(cont_coords)
        contours.addChild(cont_lines)
Finally

Code: Select all

        sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
        sg.addChild(contours)
kbwbe
Veteran
Posts: 1052
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: Draw vectors directly to 3D view using pivy + python

Post by kbwbe »

HakanSeven12 wrote: Wed Dec 02, 2020 3:55 pm
Thank you very much, will try that.
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.56, installable via FreeCAD's addon manager
Tutorial: gripper assembly https://www.youtube.com/watch?v=QMxcQ5tssWk
Documentation: https://www.freecadweb.org/wiki/A2plus_Workbench
User avatar
HakanSeven12
Veteran
Posts: 1481
Joined: Wed Feb 06, 2019 10:30 pm

Re: Draw vectors directly to 3D view using pivy + python

Post by HakanSeven12 »

You are welcome. If you want to easily manage this view objects, I recommend you to create a feature python object.
kbwbe
Veteran
Posts: 1052
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: Draw vectors directly to 3D view using pivy + python

Post by kbwbe »

HakanSeven12 wrote: Wed Dec 02, 2020 4:22 pm
Thanks again. Your code helped very much.

I got it running, final script was:

Code: Select all

import FreeCADGui
import FreeCAD
from PySide import QtGui
from PySide import QtCore

from pivy import coin

#setup the contour
color = coin.SoBaseColor()
color.rgb = (1, 0, 0)

points=coin.SoCoordinate3()
lines=coin.SoLineSet()

points.point.values = ( (0,0,0),(10,10,10),(10,10,0) )


#feed data to separator
sep=coin.SoSeparator()
sep.addChild(points)
sep.addChild(color)
sep.addChild(lines)

#add separator to sceneGraph
sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
sg.addChild(sep)
HakanSeven12 wrote: Wed Dec 02, 2020 4:22 pm If you want to easily manage this view objects, I recommend you to create a feature python object.
I just want to avoid setting up feature python objects, as i need the lines for graphical debugging of my A2plus solver. Running the solver will probably generate a very big number of vectors, which could be to heavy for the FC document.
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.56, installable via FreeCAD's addon manager
Tutorial: gripper assembly https://www.youtube.com/watch?v=QMxcQ5tssWk
Documentation: https://www.freecadweb.org/wiki/A2plus_Workbench
User avatar
HakanSeven12
Veteran
Posts: 1481
Joined: Wed Feb 06, 2019 10:30 pm

Re: [SOLVED] Draw vectors directly to 3D view using pivy + python

Post by HakanSeven12 »

You can draw them under one fpo but that is your decision :) also you can change line width. Add it separator before lineset.

Code: Select all

        # Line style.
        line_style = coin.SoDrawStyle()
        line_style.style = coin.SoDrawStyle.LINES
        line_style.lineWidth = 2
kbwbe
Veteran
Posts: 1052
Joined: Tue Apr 10, 2018 3:12 pm
Location: Germany, near Köln (Cologne)

Re: [SOLVED] Draw vectors directly to 3D view using pivy + python

Post by kbwbe »

HakanSeven12 wrote: Thu Dec 03, 2020 6:48 am
Setting lineStype was a very good hint. Lines with width 1 are hard to see on a highres screen. Thanks :D
KBWBE

https://github.com/kbwbe/A2plus
latest release: v0.4.56, installable via FreeCAD's addon manager
Tutorial: gripper assembly https://www.youtube.com/watch?v=QMxcQ5tssWk
Documentation: https://www.freecadweb.org/wiki/A2plus_Workbench
User avatar
HakanSeven12
Veteran
Posts: 1481
Joined: Wed Feb 06, 2019 10:30 pm

Re: [SOLVED] Draw vectors directly to 3D view using pivy + python

Post by HakanSeven12 »

You are welcome ;)
Post Reply