File: save_selection.py

package info (click to toggle)
sketch 0.6.13-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,284 kB
  • ctags: 8,453
  • sloc: python: 34,711; ansic: 16,543; makefile: 83; sh: 26
file content (67 lines) | stat: -rw-r--r-- 2,576 bytes parent folder | download | duplicates (4)
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
# Sketch - A Python-based interactive drawing program
# Copyright (C) 2000 by Bernhard Herzog
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import os
from Sketch import UI, Document, PostScriptDevice
import Sketch.Scripting

def selection_as_document(document):
    # Get a copy of the currently selected objects as a group
    # If no object is selected the method returns None 
    selection = document.CopyForClipboard()

    if selection is not None:
        # create a new document
        seldoc = Document(create_layer = 1)
        # and insert the group
        seldoc.Insert(selection)
        # The group is now the selected object in the document. Ungroup
        # it.
        seldoc.UngroupSelected()
        return seldoc
    return None

def get_ps_filename(context):
    dir = context.document.meta.directory
    if not dir:
        dir = os.getcwd()
    name = context.document.meta.filename
    name, ext = os.path.splitext(name)
    name = name + '.eps'
    app = context.application
    filename = app.GetSaveFilename(title = "Save Selection As PostScript",
                                   filetypes = UI.skapp.psfiletypes,
                                   initialdir = dir,
                                   initialfile = name)
    return filename

def save_selection_as_ps(context):
    seldoc = selection_as_document(context.document)
    if seldoc is not None:
        filename = get_ps_filename(context)
        if filename:
            bbox = seldoc.BoundingRect(visible = 0, printable = 1)
	    ps_dev = PostScriptDevice(filename, as_eps = 1,
				      bounding_box = tuple(bbox),
				      document = seldoc)
	    seldoc.Draw(ps_dev)
	    ps_dev.Close()

Sketch.Scripting.AddFunction('save_selection_as_ps', 'Save Selection As EPS',
                             save_selection_as_ps,
                             script_type = Sketch.Scripting.AdvancedScript)