chiark / gitweb /
ef3435d2bd5939f2567d759cf3e10f6137bb2b29
[cura.git] /
1 """
2 This page is in the table of contents.
3 Postscript is an export canvas plugin to export the canvas to a postscript file.
4
5 When the export menu item in the file menu in an analyze viewer tool, like skeinlayer or skeiniso is clicked, the postscript dialog will be displayed.  When the 'Export to Postscript' button on that dialog is clicked, the canvas will be exported as a postscript file.  If the 'Postscript Program' is set to a program name, the postscript file will be sent to that program to be opened.  The default is gimp, the Gnu Image Manipulation Program (Gimp), which is open source, can open postscript and save in a variety of formats.  It is available at:
6 http://www.gimp.org/
7
8 If furthermore the 'File Extension' is set to a file extension, the postscript file will be sent to the program, along with the file extension for the converted output.  The default is blank because some systems do not have an image conversion program; if you have or will install an image conversion program, a common 'File Extension' is png.  A good open source conversion program is Image Magick, which is available at:
9 http://www.imagemagick.org/script/index.php
10
11 An export canvas plugin is a script in the export_canvas_plugins folder which has the function getNewRepository, and which has a repository class with the functions setCanvasFileNameSuffix to set variables and execute to save the file.  It is meant to be run from an analyze viewer tool, like skeinlayer or skeiniso.  To ensure that the plugin works on platforms which do not handle file capitalization properly, give the plugin a lower case name.
12
13 """
14
15
16 from __future__ import absolute_import
17 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
18 import __init__
19
20 from fabmetheus_utilities import archive
21 from fabmetheus_utilities import gcodec
22 from fabmetheus_utilities import settings
23 from skeinforge_application.skeinforge_utilities import skeinforge_profile
24 import cStringIO
25 import os
26 import sys
27
28
29 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
30 __date__ = '$Date: 2008/21/04 $'
31 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
32
33
34 def getNewRepository():
35         'Get new repository.'
36         return PostscriptRepository()
37
38
39 class PostscriptRepository:
40         "A class to handle the export settings."
41         def __init__(self):
42                 "Set the default settings, execute title & settings fileName."
43                 skeinforge_profile.addListsToCraftTypeRepository(
44                         'skeinforge_application.skeinforge_plugins.analyze_plugins.export_canvas_plugins.postscript.html', self)
45                 self.fileExtension = settings.StringSetting().getFromValue('File Extension:', self, '')
46                 self.postscriptProgram = settings.StringSetting().getFromValue('Postscript Program:', self, 'gimp')
47
48         def execute(self):
49                 "Convert to postscript button has been clicked. Export the canvas as a postscript file."
50                 postscriptFileName = archive.getFilePathWithUnderscoredBasename( self.fileName, self.suffix )
51                 boundingBox = self.canvas.bbox( settings.Tkinter.ALL ) # tuple (w, n, e, s)
52                 boxW = boundingBox[0]
53                 boxN = boundingBox[1]
54                 boxWidth = boundingBox[2] - boxW
55                 boxHeight = boundingBox[3] - boxN
56                 print('Exported postscript file saved as ' + postscriptFileName )
57                 self.canvas.postscript( file = postscriptFileName, height = boxHeight, width = boxWidth, pageheight = boxHeight, pagewidth = boxWidth, x = boxW, y = boxN )
58                 fileExtension = self.fileExtension.value
59                 postscriptProgram = self.postscriptProgram.value
60                 if postscriptProgram == '':
61                         return
62                 postscriptFilePath = '"' + os.path.normpath( postscriptFileName ) + '"' # " to send in file name with spaces
63                 shellCommand = postscriptProgram
64                 print('')
65                 if fileExtension == '':
66                         shellCommand += ' ' + postscriptFilePath
67                         print('Sending the shell command:')
68                         print(shellCommand)
69                         commandResult = os.system(shellCommand)
70                         if commandResult != 0:
71                                 print('It may be that the system could not find the %s program.' % postscriptProgram )
72                                 print('If so, try installing the %s program or look for another one, like the Gnu Image Manipulation Program (Gimp) which can be found at:' % postscriptProgram )
73                                 print('http://www.gimp.org/')
74                         return
75                 shellCommand += ' ' + archive.getFilePathWithUnderscoredBasename( postscriptFilePath, '.' + fileExtension + '"')
76                 print('Sending the shell command:')
77                 print(shellCommand)
78                 commandResult = os.system(shellCommand)
79                 if commandResult != 0:
80                         print('The %s program could not convert the postscript to the %s file format.' % ( postscriptProgram, fileExtension ) )
81                         print('Try installing the %s program or look for another one, like Image Magick which can be found at:' % postscriptProgram )
82                         print('http://www.imagemagick.org/script/index.php')
83
84         def setCanvasFileNameSuffix( self, canvas, fileName, suffix ):
85                 "Set the canvas and initialize the execute title."
86                 self.canvas = canvas
87                 self.executeTitle = 'Export to Postscript'
88                 self.fileName = fileName
89                 self.suffix = suffix + '.ps'
90
91
92 def main():
93         "Display the file or directory dialog."
94         settings.startMainLoopFromConstructor(getNewRepository())
95
96 if __name__ == "__main__":
97         main()