Sunday, April 29, 2018

Import vector data into illustrator


I have a program that generates thousands of vector points (just coordinates), the format is not fixed, but it could be something like this:


(2.4785|77.01)
(78.8|9.88)
(45.33|0.2)


I am able to format/convert this coordinates any way necessary.


My question is: How can I import these coordinate data into Illustrator as vector points, so I can work with them properly (give them a stroke, scale them etc.).


The result could be something like this, but this is pixel data. I want something similar to that, only as a vector.



Edit: Each coordinate should result in one dot/circle being rendered in Illustrator, so maybe each coordinate should be made into a line with identical beginning and end in illustrator?


It is a little hard to see in the screenshot, but the lines actually consist of many individual dots which are not always touching.


I generated the dot coordinates with a little java code, if anyone is interested in it, just PM me.


Solution: Jackson Hyde suggested SVG as a possible format in his answer, which turns out to work perfectly. I just generate this SVG file and then import it into Illustrator. There I can select all points and add a stroke and do some other fancy stuff.






xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events"
version="1.1" baseProfile="full"
width="20px" height="20px">



[...]




Answer



As a alternate to SVG you could generate a EPS file instead, its less verbose than SVG and has less caveats. Here's a quick intro:


http://paulbourke.net/dataformats/postscript/



EPS is a framed form of postscript meant to be included in other postscript jobs/desktop publishing applications. To handle this it needs a header to define the bounding box etc. The header would look as follows:


%!PS-Adobe-3.0 EPSF-3.0 
%%BoundingBox: 0 0 100 100
%%Title: Demo for GD.SE

%%Creator: Janne Ojala
%%CreationDate: 2014-06-23
%%EndComments

This info is strictly not necessary for illustrator, as illustrator is a postscript engine. Nevertheless, you may want to include it so the file can be used as is without illustrator. Using a header also makes it easier to use in illustrator. The Bounding box defines the size of your artwork, coordinates lower left, upper right.


Next you need to handle your data for ease you may want to add a simple routine for drawing a single point:


/P {newpath 1 0 360 arc fill} def %draw circle with radius of 1

Now all you need is to define each point as follows:


15.23 2.25 P

8.1234 7.85 P
...

Finally your eps needs a end marker like this


%%EOF

Done. In general EPS is one of the easiest formats to generate, you could actually omit all the P calls by iterating over an array, file or binary data but ill leave this as it is for time being.


Workflow


Here is a trick that you can use for visualizing the data while your developing the EPS file. After all something can go wrong and its useful to have some feedback:




  • Place the EPS file in Illustrator


Now each time you edit the EPS file and jump back to illustrator it will ask you you want to update. If you click yes, you get instant feedback, and if you have an error you get a blank image. You can expand it when your ready.


Other resources



  1. Online postscript 2 reference

  2. Ghostscript a software postscript engine, for debugging, conversion to PDF etc.


Alternate functions in EPS


The function could be replaced by:



1 setlinecap
/P {newpath moveto 0 0 rlineto stroke} def

for the same kind of 0 size line approach. you can add a:


1 -1 scale 

and use negative bounding box for top left measurement directions.





In addition to EPS and SVG another easy transfer format is DXF. The DXF spec can be found here:




PDF is also relatively easy to generate in this specific case. Tough the easiest way would be to distill the EPS with acrobat or ghost script. Generating the data tables and check-sums is a bit of extra work but other than that its almost as straightforward as generating a EPS file. PDF Spec can be found here:



A manually crafted pdf sample, this uses the same idea as described in the the EPS and SVG answers containing the 2 demo points. Search for % graphics here for the relevant section (if you paste this in a text file that has PDF set as extension it should just work).


The problem with PDF is is generating the lengths of the sections for the XREF table which is a bit painful to do by hand. This is why I avoid PDF in quick cases like this. Granted, adobe will ignore the tags if the PDF is just one page. So ignoring those you may generate a technically incorrect PDF file that still just works. Fun example exploiting this phenomena can be found here.


No comments:

Post a Comment

technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...