Get absolute coordinates for rotation of Inkscape SVG elements - python

I am using wxPython to prototype some display interfaces for a project that will eventually become an embedded design. Since we are iterating with a graphics designer, it is much easier to let them work in a WYSIWYG editor like Inkscape than constantly rewrite 2D graphics drawing calls. For elements that need animation or dynamic text, the SVG elements are easily identifiable by ID so that they can be programatically updated before rendering. The rendering flow looks like this:
Inkscape SVG as XML --> Python modification of XML --> Drawing onto wxPython buffered canvas using Cairo and pyRSVG
I'd like to animate a pivoting needle around a fixed axis for a gauge. To define a needle, the graphics designer draws the graphical "needle" and a "needle_axis" element which is a small circle. Then, I thought I could set the transform property using the desired angle and the center coordinates of the "needle_axis." However, since the relative positions could have been affected because of other transforms, determining the absolute location of the rotation axis is non-trivial.
I cannot seem to find a Python library for determining absolute coordinates of an element, and it feels like reinventing the wheel to try an implement the part of the SVG spec. necessary to calculate it. The rsvg API mentions rsvg_handle_get_dimensions_sub and arsvg_handle_get_position_sub functions to get the dimensions and positions of elements, but I can't find an API for pyRSVG.
Is there a better way to find the rotation axis than manually calculating it? An example Inkscape SVG file and test snippet are available as a gist.

If I understood correctly, you have the needle's coordinates and the rotation axis coordinates, and you want to programatically change the needle's "angle" and then change the needle's shape in the SVG file accordingly?
I think this is a really bad idea: making changes to the SVG, saving and then rendering the "new SVG" will take too much ressources and is exactly why such a simple task seems complicated.
Instead, parse your SVG once (using lxml for instance, it's a piece of cake to extract coordinates just by doing some XPATH queries) and build your shapes into a model (a Needle class for instance, with the paths and center) and then simply rotate that needle using Cairo transforms. They already provide a rotate function that will do what you're looking for!

Related

Automaticly inserting text into dxf file with python

I‘m trying to automatically insert text into dxf contours using python. I have a bulk of dxf files for lasercutting. Often we want to engrave the partnumber into the sheetmetal part.
My attempt is to make a square box Where the length and with equals the text height and width. After to Programm found a place where it is outside the innerconturs and inside the outer contour I want to fill the box with text. I tried to abstract the contours with a polygon and start checking if it fits or not, which kind of works ok. Not finish completely yet.
I wondered if there is some sort of library /tool that has this function because computing time is quite high atm before putting more afford into the Programm or anyone has an easier approach than mine.
The next release of ezdxf (v0.16 is in beta now), has a new text2path add-on, which uses Matplotlib to render text strings as path objects. This path objects can be placed as:
POLYLINE and SPLINE entities to preserves the smooth curves
flattened to POLYLINE or LWPOLYLINE entities, which consist only of straight lines
HATCH entities with or without spline edges
flatten the paths into simple vertices
There are some examples in the https://github.com/mozman/ezdxf/tree/master/examples/addons folder (*_to_path.py).
It is possible to transform the path objects by a Matrix44 transformation and there even exist a function to fit some paths into a box: fit_paths_into_box().
For additional questions, you can use the discussions board at github.

Do I need to use OpenGL to draw at the pixel by pixel level (Python). Is there a way I can do such a thing without using a code library?

I have written some code in Python which allows 3D objects to be defined in 3D object space and mapped onto a 2D screen. Currently the finished 2D polygons are drawn on the screen using the PyGame library, which works effectively, but I would like to go the full way and write code myself to complete the drawing operations PyGame does for me. This means I would like to manually control the drawing of each pixel on the screen, with the use of GPU support to accelerate the entire rendering process. From some reading it seems OpenGL is suitable for this sort of thing, but I'm not sure what the complete purpose of OpenGL is and whether I could achieve what I am trying to do in a better way. Do I really need to use OpenGL? Or is there another way for me to directly access my GPU to draw at the pixel by pixel level?
It sounds like OpenGL's programmable shaders are what you're looking for (in particular fragment shaders). They run massively parallel on a pixel-by-pixel basis, in the sense that basically you write a function that takes a single pixel location and computes its color. Note that this means that the individual pixels can't exchange information, though there are certain ways around that.
(Technically when I said "pixel" I meant "fragment", which is sort of a generalized version of a pixel.)

Matplotlib export text as line elements in Python

I have to build some rudimentary CAD Tool in Python based on matplotlib for handling the display of the content.
After all the parts have been put together, the whole layout shall be exported as line elements (basically just tuples of the start / end coordinates of the lines, e.g. [x1,y1,x2,y2]) and just points.
So far I have all the basic gemoetric stuff implemented, but I cannot figure out how to implement text properly. To be able to use different fonts etc. I want to use the text capabilities of matplotlib, but I can't find a way to export the text properly from matplotlib.
Is there a way to get a vectorized output right away? Or at least an array of the plotted text?
After some days of struggling, I found a way to get the outline of the text: https://github.com/rougier/freetype-py , more precisely the example https://github.com/rougier/freetype-py/blob/master/examples/glyph-vector.py
If you just want to get the outline as an vector array, you can delete everything after line 78 and do this:
path = Path(VERTS, CODES)
outline = path.to_polygons()
This will give you an array of polygons, and each polygon is again an array of points (x,y) of the polygon.
Though it was some trouble to get freetype running on windows and I still have not figured out how to make it portable, I think I will stick with this solution, because it is fast, reliable and allows one to use all the nice system fonts.

Easiest way to parse simple SVG into Python, applying all transforms

I'd like to draw a few simple objects in Inkscape (lines, circles, rectangles), group them, move them around, scale, rotate, make copies, etc. and then I need something which will let me load the SVG into Python and iterate over all these shapes, getting the relevant attributes (for circles: centre and radius; for lines: the two end-points; etc.).
I've installed and tried svg-utils, pysvg, svgfig and read about inkex, and while all of these seem to allow me to iterate through the XML structure, with varying degrees of awkwardness, as far as I can see none of them apply the transforms to the elements. So, if I draw a line from (0,0) to (1,1), group it, move it to (100,100), then its XML tag is still going to say (0,0) to (1,1), but its real position is computed by applying the transform in its containing group, to these end-points.
I don't want to write all this transform-application code myself, because that would be re-inventing the bicycle. But I need help finding a convenient existing bicycle...
One likely useful route is to find an exporter into a simple format, which would already have had to solve all these problems. Here is an example I found: http://en.wikipedia.org/wiki/SK1_%28program%29#Supported_formats
But which of the export formats listed there is likely to be the simplest?

Draw a map with gps markers and save this image to file

I have some GPS position recordings, which I want to draw into a map (e.g. OpenStreetMap) and save this as an image file.
I'm using python. I've found osm-gps-map as a candidate library to do the visualization, but apparantly the API lacks some functionality to satisfy all of my needs. What I need is a library that does:
draw stuff into a map (e.g. an OSM-based map), i.e. a list of GPS positions.
automatically adjust view (scale/position) to fit in all drawn positions
save this view to an image file
I seriously want to avoid being forced to manually make a screenshot from a widget or a browser window. And if possible I also want to avoid to implement the necessary projection functionality by myself (as in here). Does anyone know a library/toolchain that provides the desired functionality (if possible for use by Python)?
You may want to consider using Basemap for matplotlib. Here is a blog post describing how to use this package with OSM.
There are examples of how to draw custom GPS points on top of OSM background map using Matplotlib Basemap, Matplotlib or Cairo in GeoTiler project: https://github.com/wrobell/geotiler/tree/master/examples.

Categories

Resources