File size: 2,581 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# SPDX-License-Identifier: LGPL-2.1-or-later

# FreeCAD TemplatePyMod module  
# (c) 2010 Werner Mayer LGPL

"""

The module can be executed with:

FreeCAD -P <path_to_file> Automation.py

FreeCADCmd -P <path_to_file> Automation.py

"""

import FreeCAD, Part
import os
import tempfile

def makeSnapshotWithGui():
	from PySide import QtGui
	import FreeCADGui

	def getMainWindow():
		toplevel = QtGui.QApplication.topLevelWidgets()
		for i in toplevel:
			if i.metaObject().className() == "Gui::MainWindow":
				return i
		raise RuntimeError("No main window found")

	mw=getMainWindow()
	mw.hide()
	#mw.showMinimized()

	# Create a test geometry and add it to the document
	obj=Part.makeCone(10,8,10)
	doc = FreeCAD.newDocument()
	Part.show(obj)

	# switch off animation so that the camera is moved to the final position immediately
	view = FreeCADGui.getDocument(doc.Name).activeView()
	view.setAnimationEnabled(False)
	view.viewAxometric()
	view.fitAll()
	view.saveImage('crystal.png',800,600,'Current')
	FreeCAD.closeDocument(doc.Name)
	# close the application
	QtGui.QApplication.quit()

def makeSnapshotWithoutGui():
	from pivy import coin

	# create a test geometry and create an IV representation as string
	box=Part.makeCone(10,8,10)
	iv=box.writeInventor()

	# load it into a buffer
	inp=coin.SoInput()
	try:
		inp.setBuffer(iv)
	except Exception:
		tempPath = tempfile.gettempdir()
		fileName = tempPath + os.sep + "cone.iv"
		file = open(fileName, "w")
		file.write(iv)
		file.close()
		inp.openFile(fileName)

	# 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("crystal.ps")

	# Other formats are only available if simage package is installed
	if off.isWriteSupported("PNG"):
		print("Save as PNG")
		off.writeToFile("crystal.png","PNG")

if FreeCAD.GuiUp:
	makeSnapshotWithGui()
else:
	makeSnapshotWithoutGui()