Matplotlib export text as line elements in Python - 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.

Related

Generate schematic (geographic) diagram from graph

I would like to know how best to generate a schematic diagram, something like this, from a graph (created using the Python NetworkX library) that contains the latitude and longitude of each node (city) and the lines connecting them in the Indian railway network.
The cities (nodes) should be located reasonably close to their actual position, but not necessarily exactly. I am OK with using the plate carrée projection that simply maps lat/long onto X/Y in the diagram.
The rail lines (edges) can be straight lines or even curves if it fits better.
On the diagram should be displayed the cities (preferably as dots) along with a short (max 4 characters) label for each, the lines connecting them, and a single label for each line (the given example has quite long labels for the lines).
Preferably the amount of manual tweaking of coordinates to get things to fit should be minimised.
Using Graphviz was my first idea. But I don't know how well neato/fdp (required for fixed positioning of nodes) will perform with large numbers of nodes/edges. Also, making Graphviz display labels separately outside the nodes (rather than inline) seems to need a lot of manual positioning of each label, which would be pretty boring. Is there any better way to get this kind of layout?
Doable (https://forum.graphviz.org/t/another-stupid-graphviz-trick-geographic-graphs/256), but does not seem to use many Graphviz features. In addition to tools mentioned in the link, maybe consider pikchr (https://pikchr.org/home/doc/trunk/homepage.md)

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.

Displaying a grid of letters with coordinates on axes

I'm making a simple word game simulation in Python and need a way to visualise a grid of coordinates. The input would be a simple 2D array with either '' or a character in each spot.
I need each spot in the grid to either be blank or have one letter. The x and y axes need to have arbitrary start points, such as -20. It seems like Matplotlib might do what I want, but having looked around on a bunch of Stackoverflow questions and Matplotlib help pages, I can't find what I need.
The question here partly has what I need:
Show the values in the grid using matplotlib
Except I want no colour, the values are single characters, and the axis labels need to allow arbitrary start points.
Does anyone know whether Matplotlib is the right library to do this sort of thing, or if I should try something else? Performance matters but it's not the most important thing. I don't need any interactivity with the display window, it's purely read only.

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?

Get absolute coordinates for rotation of Inkscape SVG elements

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!

Categories

Resources