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