/[thuban]/branches/WIP-pyshapelib-bramz/Extensions/svgexport/svgsaver.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Extensions/svgexport/svgsaver.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2388 - (hide annotations)
Mon Nov 15 16:27:41 2004 UTC (20 years, 3 months ago) by bernhard
Original Path: trunk/thuban/Extensions/svgexport/svgsaver.py
File MIME type: text/x-python
File size: 3406 byte(s)
* Extensions/svgexport/: Minor improvements to doc strings.

1 bh 2074 # Copyright (c) 2001, 2002, 2003 by Intevation GmbH
2     # Authors:
3     # Markus Rechtien <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     __version__ = "$Revision$"
9     # $Source$
10     # $Id$
11    
12    
13     """
14     Classes to write a session in SVG format
15     """
16    
17    
18     # Needed wx-toolkit classes
19 bernhard 2344 from wxPython.wx import wxFileDialog, wxSAVE, wxOVERWRITE_PROMPT, wxID_OK, \
20     wxOK, wxICON_HAND
21 bh 2074
22     # We need os.path
23     import os
24    
25     # use _() already now for all strings that may later be translated
26     from Thuban import _
27    
28     # Import SVG related classes
29 bernhard 2344 from svgmapwriter import VirtualDC, SVGRenderer, SVGMapWriterError
30 bh 2074
31    
32     def write_to_svg(context):
33 bernhard 2388 '''Export data depending on the set properties.
34    
35     This is the main export funcation.
36 bh 2074 '''
37     canvas = context.mainwindow.canvas
38     file = None
39     map = canvas.Map()
40    
41     if hasattr(canvas, "export_path"):
42     export_path = canvas.export_path
43     else:
44     export_path="."
45     # Get the file the session shall be written to
46     dlg = wxFileDialog(canvas, _("Write SVG"), export_path, "",
47     "Scalable Vector Graphics (*.svg)|*.svg",
48     wxSAVE|wxOVERWRITE_PROMPT)
49    
50     response = dlg.ShowModal()
51     if response == wxID_OK:
52     file = dlg.GetPath()
53     else: # Do nothing if choice was interrupted.
54     return 0
55    
56     # If the user selected a file
57     if file and map is not None:
58     canvas.export_path = os.path.dirname(file)
59     # Initialize some dimensions and calculate the map bounds
60     width, height = canvas.GetSizeTuple()
61     llx, lly = canvas.win_to_proj(0, height)
62     urx, ury = canvas.win_to_proj(width, 0)
63     mapwidth, mapheight = ((urx - llx), (ury - lly))
64     mapregion = (llx, lly, mapwidth, mapheight)
65    
66     # Get all selected layers and shapes that should be written as SVG
67     selected_layer = canvas.selection.SelectedLayer()
68     selected_shapes = canvas.selection.SelectedShapes()
69 bernhard 2344 try:
70     dc = VirtualDC(file, (mapwidth, mapheight, ))
71     dc.BeginExport()
72     # map scale offset region
73     renderer = SVGRenderer(dc, map, 1.0, (0 - min(llx, urx),
74     0 + max(lly, ury)), mapregion)
75     # Render the map
76     renderer.RenderMap(selected_layer, selected_shapes)
77     dc.EndExport()
78     except SVGMapWriterError, inst:
79     context.mainwindow.RunMessageBox(_("Error: SVG not written!"),
80     text=_("Could not write SVG because: ")+ str(inst),
81     flags= wxOK | wxICON_HAND)
82     # delete partly writting file
83     os.remove(file)
84 bh 2074
85    
86 bernhard 2344
87 bh 2074 # Thuban has named commands which can be registered in the central
88     # instance registry.
89     from Thuban.UI.command import registry, Command
90    
91     # The instance of the main menu of the Thuban application
92     # See Thuban/UI/menu.py for the API of the Menu class
93     from Thuban.UI.mainwindow import main_menu
94    
95     # create a new command and register it
96     registry.Add(Command('write_to_svg', _('Write SVG Map'), write_to_svg,
97     helptext = _('Export the a map into a SVG file')))
98    
99 jan 2208 # find the extensions menu (create it anew if not found)
100     extensions_menu = main_menu.FindOrInsertMenu('extensions', _('E&xtensions'))
101    
102 bh 2074 # finally bind the new command with an entry in the extensions menu
103     extensions_menu.InsertItem('write_to_svg')

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26