/[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 2344 - (hide annotations)
Tue Sep 21 21:30:36 2004 UTC (20 years, 5 months ago) by bernhard
Original Path: trunk/thuban/Extensions/svgexport/svgsaver.py
File MIME type: text/x-python
File size: 3401 byte(s)
Improved the svgexport to only use unique ids. Will issues
an error message dialoge when two layer names are the same.
ShapeIDs are now added with a dash within the svg ids.

* Extensions/svgexport/svgmapwriter.py (SVGMapWriterError): New.
* Extensions/svgexport/test/test_svgmapwriter.py: Added imports
(TestSVGRenderer): New test class with test_make_in() and
test_check_for_layer_name_clash()
* Extensions/svgexport/svgmapwriter.py (SVGRenderer): Fixed __init__()
and draw_shape_layer_incrementally() to not use a baseid twice,
satisfying test_check_for_layer_name_clash()
(VirtualDC.make_id): Use a dash between baseit and id, satisfies
test_make_in().
* Extensions/svexport/svgsaver.py: Import SVGMapWriterError, wxOK
and wxICON_HAND.
(write_to_svg): Put dc and rendering in a try statement and on
catching SVGmapWriterError notify the user and delete the target file.
-------------------------------------------------------------------

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     '''The main export funtion - Exports the data depending
34     on the set properties.
35     '''
36     canvas = context.mainwindow.canvas
37     file = None
38     map = canvas.Map()
39    
40     if hasattr(canvas, "export_path"):
41     export_path = canvas.export_path
42     else:
43     export_path="."
44     # Get the file the session shall be written to
45     dlg = wxFileDialog(canvas, _("Write SVG"), export_path, "",
46     "Scalable Vector Graphics (*.svg)|*.svg",
47     wxSAVE|wxOVERWRITE_PROMPT)
48    
49     response = dlg.ShowModal()
50     if response == wxID_OK:
51     file = dlg.GetPath()
52     else: # Do nothing if choice was interrupted.
53     return 0
54    
55     # If the user selected a file
56     if file and map is not None:
57     canvas.export_path = os.path.dirname(file)
58     # Initialize some dimensions and calculate the map bounds
59     width, height = canvas.GetSizeTuple()
60     llx, lly = canvas.win_to_proj(0, height)
61     urx, ury = canvas.win_to_proj(width, 0)
62     mapwidth, mapheight = ((urx - llx), (ury - lly))
63     mapregion = (llx, lly, mapwidth, mapheight)
64    
65     # Get all selected layers and shapes that should be written as SVG
66     selected_layer = canvas.selection.SelectedLayer()
67     selected_shapes = canvas.selection.SelectedShapes()
68 bernhard 2344 try:
69     dc = VirtualDC(file, (mapwidth, mapheight, ))
70     dc.BeginExport()
71     # map scale offset region
72     renderer = SVGRenderer(dc, map, 1.0, (0 - min(llx, urx),
73     0 + max(lly, ury)), mapregion)
74     # Render the map
75     renderer.RenderMap(selected_layer, selected_shapes)
76     dc.EndExport()
77     except SVGMapWriterError, inst:
78     context.mainwindow.RunMessageBox(_("Error: SVG not written!"),
79     text=_("Could not write SVG because: ")+ str(inst),
80     flags= wxOK | wxICON_HAND)
81     # delete partly writting file
82     os.remove(file)
83 bh 2074
84    
85 bernhard 2344
86 bh 2074 # Thuban has named commands which can be registered in the central
87     # instance registry.
88     from Thuban.UI.command import registry, Command
89    
90     # The instance of the main menu of the Thuban application
91     # See Thuban/UI/menu.py for the API of the Menu class
92     from Thuban.UI.mainwindow import main_menu
93    
94     # create a new command and register it
95     registry.Add(Command('write_to_svg', _('Write SVG Map'), write_to_svg,
96     helptext = _('Export the a map into a SVG file')))
97    
98 jan 2208 # find the extensions menu (create it anew if not found)
99     extensions_menu = main_menu.FindOrInsertMenu('extensions', _('E&xtensions'))
100    
101 bh 2074 # finally bind the new command with an entry in the extensions menu
102     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