FreeCAD

Форк
0
/
Automation.py 
98 строк · 2.5 Кб
1
# FreeCAD TemplatePyMod module  
2
# (c) 2010 Werner Mayer LGPL
3

4
"""
5
The module can be executed with:
6
FreeCAD -P <path_to_file> Automation.py
7
FreeCADCmd -P <path_to_file> Automation.py
8
"""
9

10
import FreeCAD, Part
11
import os
12
import tempfile
13

14
def makeSnapshotWithGui():
15
	from PySide import QtGui
16
	import FreeCADGui
17

18
	def getMainWindow():
19
		toplevel = QtGui.QApplication.topLevelWidgets()
20
		for i in toplevel:
21
			if i.metaObject().className() == "Gui::MainWindow":
22
				return i
23
		raise RuntimeError("No main window found")
24

25
	mw=getMainWindow()
26
	mw.hide()
27
	#mw.showMinimized()
28

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

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

44
def makeSnapshotWithoutGui():
45
	from pivy import coin
46

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

51
	# load it into a buffer
52
	inp=coin.SoInput()
53
	try:
54
		inp.setBuffer(iv)
55
	except Exception:
56
		tempPath = tempfile.gettempdir()
57
		fileName = tempPath + os.sep + "cone.iv"
58
		file = open(fileName, "w")
59
		file.write(iv)
60
		file.close()
61
		inp.openFile(fileName)
62

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

69
	# add light and camera so that the rendered geometry is visible
70
	root = coin.SoSeparator()
71
	light = coin.SoDirectionalLight()
72
	cam = coin.SoOrthographicCamera()
73
	root.addChild(cam)
74
	root.addChild(light)
75
	root.addChild(data)
76

77
	# do the rendering now
78
	axo = coin.SbRotation(-0.353553, -0.146447, -0.353553, -0.853553)
79
	viewport=coin.SbViewportRegion(400,400)
80
	cam.orientation.setValue(axo)
81
	cam.viewAll(root,viewport)
82
	off=coin.SoOffscreenRenderer(viewport)
83
	root.ref()
84
	off.render(root)
85
	root.unref()
86

87
	# export the image, PS is always available
88
	off.writeToPostScript("crystal.ps")
89

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

95
if FreeCAD.GuiUp:
96
	makeSnapshotWithGui()
97
else:
98
	makeSnapshotWithoutGui()
99

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

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

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

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