Offscreen rendering thumbnails of .stp files

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
steph31
Posts: 1
Joined: Fri Jan 11, 2019 8:43 pm

Offscreen rendering thumbnails of .stp files

Post by steph31 »

Hello,
I am a newbye in the world of freecad, I am just beginning so excuse my lack of knowledge and thanks for this nice piece of software.

What I want to do is server side rendering of thumbnails (jpeg or png) of step files. As it will be server side I would rather have it headless with offscreen rendering, so using purely FreecadCmd.exe.

My first attempt with FreecadCmd was in testing this simple script, particularily (makeSnapshotWithoutGui()), which does half of the job (i.e rendering in offscreen with FreecadCmd.exe):
https://github.com/FreeCAD/FreeCAD/blob ... omation.py

Then suppressing everything but the offscreen branch, I get:
Exception while processing file: Automation.py [SoInput_setBuffer expected 3 arguments, got 2]
I searched in the forum and found this https://forum.freecadweb.org/viewtopic. ... 28#p112001
stating that this defect shall be fixed, but it seems not to be.

Then, as I am not interested in creating a cone but would rather need to load a .stp file I tried to load a .stp using
box = FreeCAD.loadFile("mypart.stp"), and I got following exception:
Exception while processing file: Automation.py [Cannot load Gui module in console application.]

So, I guess I can not do this way which would require to use FreeCad.exe rather than FreecadCmd.exe.

Do you know if what I am trying to do is possible ? Can somebody assist ?
In particular, I have no idea in passing the step geometry to coin.
Any help would be much welcomed :o).
Many thanks for your support,

OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.13509 (Git)
Build type: Release
Branch: releases/FreeCAD-0-17
Hash: 0258808ccb6ba3bd5ea9312f79cd023f1a8671b7
Python version: 2.7.14
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.2.0
Locale: English/UnitedKingdom (en_GB)
wmayer
Founder
Posts: 20246
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Offscreen rendering thumbnails of .stp files

Post by wmayer »

I guess that the setBuffer issue is fixed in the source code but your version still uses the old binaries. Anyway, the setBuffer method is just a way to keep everything in RAM but it's also possible to export the Inventor structure to a file and then read it in again.

Here is a slightly modified script to get the thumbnails:

Code: Select all

import os
import FreeCAD
import Part
from pivy import coin

def makeSnapshotWithoutGui(stepfilename):
    # determine output file names
    ivfilename = os.path.splitext(stepfilename)[0] + ".iv"
    psfilename = os.path.splitext(stepfilename)[0] + ".ps"
    pngfilename = os.path.splitext(stepfilename)[0] + ".png"

    # open a STEP file
    shape=Part.read(stepfilename)
    f=open(ivfilename,"w")
    f.write(shape.writeInventor())
    f.close()

    # convert to iv format
    inp=coin.SoInput()
    inp.openFile(ivfilename)

    # and create a scenegraph
    data = coin.SoDB.readAll(inp)
    base = coin.SoBaseColor()
    base.rgb.setValue(0.6,0.7,1.0)
    data.insertChild(base,0)

    # add light and camera so that the rendered geometry is visible
    root = coin.SoSeparator()
    light = coin.SoDirectionalLight()
    cam = coin.SoOrthographicCamera()
    root.addChild(cam)
    root.addChild(light)
    root.addChild(data)

    # do the rendering now
    axo = coin.SbRotation(-0.353553, -0.146447, -0.353553, -0.853553)
    viewport=coin.SbViewportRegion(400,400)
    cam.orientation.setValue(axo)
    cam.viewAll(root,viewport)
    off=coin.SoOffscreenRenderer(viewport)
    root.ref()
    off.render(root)
    root.unref()

    # export the image, PS is always available
    off.writeToPostScript(psfilename)

    # Other formats are only available if simage package is installed
    if off.isWriteSupported("PNG"):
        off.writeToFile(pngfilename,"PNG")
Now run it with

Code: Select all

makeSnapshotWithoutGui("C:/Temp/mystepfile.stp")
The function will create the files C:/Temp/mystepfile.iv, C:/Temp/mystepfile.ps and if simage is installed also C:/Temp/mystepfile.png.
Post Reply