alexforencich / python-hpgl

Python HPGL parsing library
MIT License
18 stars 3 forks source link

Exception: Unknown HPGL command (SC) #4

Open blackbit42 opened 3 years ago

blackbit42 commented 3 years ago

I recently managed to interface my oscilloscope (HP 54542C) via GPIB and convinced it to provide a 'screenshot' (hardcopy is their term) to me.

Obviously I want to convert the file to some more convenient format.

Python-hpgl throws a UnicodeDecodeError at me.

Can you shed some light why that is, please?

plot.hpgl.txt

blackbit42 commented 3 years ago

Disregard, the file I have attached wasn't HPGL at all but ESC/P. I had to select a different printer device on the scope.

alexforencich commented 3 years ago

Oops! No worries. Did the HPGL convert correctly?

blackbit42 commented 3 years ago

Actually, no. I get 'Exception: Unknown HPGL command (SC)'. hp2xx digests it, the ke5fx thing too. I get different aspect ratios with the two, but still they seem to be able to understand the data. plot.hpgl.txt

alexforencich commented 3 years ago

Ah I see. I didn't have any reference HPGL with this command when I put the library together so I didn't implement it. I'll see what I can do. I just implemented it as a no-op and got a very strange result, and it looks like I may need to do some significant reworking for this command to work correctly. Can you post the output of one of the other programs so I have some idea of what the output should look like?

blackbit42 commented 3 years ago

plot2

alexforencich commented 3 years ago

Great, now to figure out why the output of python-hpgl is about five miles wide. Also, that's interesting, it looks like it's also plotting something as a bunch of dots with PD;PU; and python-hpgl is not outputting anything for that, looks like I need to fix that as well.

alexforencich commented 3 years ago

Alright, point drawing works now. There is something very wrong with text handling, and the output is mirrored, which I think the scale command is supposed to fix.

alexforencich commented 3 years ago

OK, why don't you give that a shot and let me know. I will likely need to rework things a bit more to bring out the page size setting and what not, but it's a start.

blackbit42 commented 3 years ago

Looks good! The hpgl file is processed just fine. The resulting .svg seems to be very large. I'll have to try to understand how to read the intended size out of the hpgl file and the svg and see what is correct.

alexforencich commented 3 years ago

The problem is that there isn't a nice way to get the intended size out of the HPGL file. HPGL is really a plotter control language, and the plotter has quite a bit of smarts itself. There are a number of HPGL "commands" that actually query information from the plotter. Also, by default, all of the coordinates are in the plotter's internal units, which is something like 40 increments per mm. And the page size can vary depending on how the plotter is configured. Most of the HPGL files I have just assume a particular size, and the result can be completely screwy as the text size is usually some percentage of the page size, so if the page size is large then the text ends up being far larger than expected. Interestingly, your HPGL file has a scale command at the top that defines basically a mapping of the whole page into user-specified units, so it can actually make use of the whole page. It looks like some of these plotter emulation programs/HPGL viewers actually inject one of these rescale commands before reading in the file to get a more reasonable result. I'm wondering if it would make sense to, say, parse the whole thing twice - once to determine the size, and then set an appropriate scale command and recompute everything.

blackbit42 commented 3 years ago

Sounds like a reasonable idea to me, but since my compreheniton of HPGL is practically non-existing I cannot really say if that is the straight-forward thing to do. Do you for instance know how ke5fx deals with scaling?

alexforencich commented 3 years ago

I honestly don't know, it would be interesting to look in to that. From looking at the config file, it seems like it just prepends a scale command, but I think that doesn't necessarily work all that well for all inputs - you may need to adjust that command depending on whatever generated the file.