FreeCAD

Форк
0
/
make_snapshot.py 
80 строк · 1.9 Кб
1
# (c) 2022 Werner Mayer LGPL
2

3
"""
4
The module can be used from another module with:
5

6
from make_snapshot import make_snapshot
7
make_snapshot("file.stl", "image.png")
8

9
This other module then can be used with:
10

11
FreeCAD -c make_snapshot_from_file.py
12

13
or
14

15
FreeCADCmd make_snapshot_from_file.py
16
"""
17

18
import FreeCAD
19
import FreeCADGui
20
import os
21
import importlib
22
import sys
23
from PySide2 import QtGui
24

25

26
def init_gui():
27
    try:
28
        FreeCADGui.setupWithoutGUI()
29
    except Exception as e:
30
        pass
31

32

33
def make_snapshot(input_file, output_file, size=48):
34
    from pivy import coin
35

36
    ext = os.path.splitext(input_file)[1][1:]
37
    mod = FreeCAD.getImportType(ext)
38
    if len(mod) == 0:
39
        print("Cannot load file {}".format(input_file))
40
        return
41

42
    # use the first listed module
43
    module = importlib.import_module(mod[0])
44
    module.open(input_file)
45

46
    doc = FreeCAD.ActiveDocument
47
    if doc is None:
48
        print("No active document")
49
        return
50

51
    init_gui()
52
    nodes = [FreeCADGui.subgraphFromObject(obj) for obj in doc.Objects]
53

54
    # add light and camera so that the rendered geometry is visible
55
    root = coin.SoSeparator()
56
    light = coin.SoDirectionalLight()
57
    cam = coin.SoOrthographicCamera()
58
    root.addChild(cam)
59
    root.addChild(light)
60
    for node in nodes:
61
        root.addChild(node)
62

63
    # do the rendering now
64
    axo = coin.SbRotation(-0.353553, -0.146447, -0.353553, -0.853553)
65
    width = size
66
    height = size
67
    viewport = coin.SbViewportRegion(width, height)
68
    cam.orientation.setValue(axo)
69
    cam.viewAll(root, viewport)
70
    off = FreeCADGui.SoQtOffscreenRenderer(width, height)
71
    off.setBackgroundColor(1, 1, 1)
72
    root.ref()
73

74
    # A QGuiApplication is needed to create an OpenGL context
75
    if QtGui.QGuiApplication.instance() is None:
76
        app = QtGui.QGuiApplication(sys.argv)
77

78
    off.render(root)
79
    off.writeToImage(output_file)
80
    root.unref()
81

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.