Copy contents of 3D view to clipboard

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
User avatar
r-frank
Posts: 2181
Joined: Thu Jan 24, 2013 6:26 pm
Location: Möckmühl, Germany
Contact:

Copy contents of 3D view to clipboard

Postby r-frank » Sun Jul 24, 2016 10:01 am

Hello.

Since we have "Tools > save picture" to save the contents of the 3D view to a file i thought it would
be nice to have the possibility to copy the contents of the 3D view directly to the clipboard
with click or a keyboard shortcut (for inserting it directly into another program).

I did not find a macro so i thought i would ask here for a trick to achieve that or if somebody would
be able to provide the few lines of magic python code to do this.

Thanks for your help.

Roland
Deutsche FreeCAD Tutorials auf Youtube
My GrabCAD FreeCAD-Projects
FreeCAD lessons for beginners in english

Native german speaker - so apologies for my english, no offense intended :)
wmayer
Site Admin
Posts: 16633
Joined: Thu Feb 19, 2009 10:32 am

Re: Copy contents of 3D view to clipboard

Postby wmayer » Sun Jul 24, 2016 11:32 am

The only difficulty I have is to find the corresponding gl widget to the active view.

Code: Select all

from PySide import QtGui
from PySide import QtOpenGL

mw=Gui.getMainWindow()

gl=mw.findChildren(QtOpenGL.QGLWidget)
glw=gl[0] # just use the first element

i=glw.grabFrameBuffer()

cb=QtGui.qApp.clipboard()
cb.setImage(i)
Alternatively you can create a image in the temp folder, load it and set it to the clipboard. This way the active view issue is solved.

Code: Select all

import tempfile
import os
from PySide import QtGui

fn = tempfile.gettempdir() + os.path.sep + "image.png"
view=Gui.ActiveDocument.ActiveView
size=view.getSize()
view.saveImage(fn,size[0],size[1])

img=QtGui.QImage(fn, "PNG")
cb=QtGui.qApp.clipboard()
cb.setImage(img)
User avatar
microelly2
Posts: 4690
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Copy contents of 3D view to clipboard

Postby microelly2 » Sun Jul 24, 2016 11:52 am

wow, can I make the background transparent too?
User avatar
r-frank
Posts: 2181
Joined: Thu Jan 24, 2013 6:26 pm
Location: Möckmühl, Germany
Contact:

Re: Copy contents of 3D view to clipboard

Postby r-frank » Sun Jul 24, 2016 12:12 pm

Way cool ... thanks a lot, this makes things a lot easier ...
Deutsche FreeCAD Tutorials auf Youtube
My GrabCAD FreeCAD-Projects
FreeCAD lessons for beginners in english

Native german speaker - so apologies for my english, no offense intended :)
wmayer
Site Admin
Posts: 16633
Joined: Thu Feb 19, 2009 10:32 am

Re: Copy contents of 3D view to clipboard

Postby wmayer » Sun Jul 24, 2016 8:46 pm

microelly2 wrote:wow, can I make the background transparent too?
After experimenting quite a lot I finally found one way.
First just some background information: in FreeCAD we have altogether three ways for off-screen rendering.
  • One way is to directly use View3DInventorViewer::renderToFramebuffer
  • the class SoQtOffscreenRenderer (which supports FBO and pbuffers based on Qt)
  • and Coin's old SoOffscreenRendering class which mostly works for bad graphic drivers where the previous two methods mostly fail
I found now a solution for the first method but had to make a little change in the source code. See git commit 1f3c2ef

Then you also have to slightly adjust the Python code:

Code: Select all

view.saveImage(fn,size[0],size[1],"transparent")
I hope that transparent background colors should also be possible for the second method because it basically uses the same Qt functionality. Only for method three I don't see a way to achieve this.

So, if you want to test method 1 you first have to fetch the latest sources. After building FreeCAD start it and open the parameter editor. There go to Preferences > Document and create the boolean key "DisablePBuffers" if it doesn't exist yet and set its value to "true".

Then run the Python code again.
User avatar
microelly2
Posts: 4690
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Copy contents of 3D view to clipboard

Postby microelly2 » Sun Jul 24, 2016 8:55 pm

thank you, i see a new nice opportunity to get animation movies.
wmayer
Site Admin
Posts: 16633
Joined: Thu Feb 19, 2009 10:32 am

Re: Copy contents of 3D view to clipboard

Postby wmayer » Sun Jul 24, 2016 9:29 pm

Update:
As said before the class SoQtOffscreenRenderer can either use FBO or pbuffer for the rendering and the default mode is set to pbuffer if it's supported by the system. Unfortunately, in this mode it doesn't seem to support transparent backgrounds. However, if pbuffers are not supported or when FBO is explicitly set then transparent background colors work out of the box.

So, the only reliable way to have transparent background is to follow the advice from my previous post.
triplus
Posts: 9475
Joined: Mon Dec 12, 2011 4:45 pm

Re: Copy contents of 3D view to clipboard

Postby triplus » Sun Jul 24, 2016 9:32 pm

wmayer wrote:The only difficulty I have is to find the corresponding gl widget to the active view.

Code: Select all

from PySide import QtGui
from PySide import QtOpenGL

mw=Gui.getMainWindow()

mdi = mw.findChild(QtGui.QMdiArea)
sw = mdi.currentSubWindow().widget()
gl = sw.findChild(QtOpenGL.QGLWidget)

if gl:
    i=gl.grabFrameBuffer()
    cb=QtGui.qApp.clipboard()
    cb.setImage(i)
else:
    pass
mario52
Posts: 3217
Joined: Wed May 16, 2012 2:13 pm

Re: Copy contents of 3D view to clipboard

Postby mario52 » Mon Jul 25, 2016 9:06 am

hi
no macros works

macro 1 wmayer : (Gimp) no data image in clipboard
macro 2 wmayer : save to disk but (Gimp) no data image in clipboard
macro 3 triplus : (Gimp) no data image in clipboard

OS: Windows 8 (10)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.16.6700 (Git)
Build type: Release
Branch: releases/FreeCAD-0-16
Hash: 7b925d11aa69ac405b423635adb1e2833f18a817
Python version: 2.7.8
Qt version: 4.8.6
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting, Dialog creation. My macros on Gist.github.
wmayer
Site Admin
Posts: 16633
Joined: Thu Feb 19, 2009 10:32 am

Re: Copy contents of 3D view to clipboard

Postby wmayer » Mon Jul 25, 2016 9:35 am

It looks like Gimp expects this in a certain format. When using e.g. InkScape or MS Paint it works flawlessly. After pasting into one of the two latter and then copy from there pasting into Gimp also works.