Author Topic: Convert2PDF Update  (Read 4161 times)

Agastar

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1878
    • View Profile
Convert2PDF Update
« on: August 28, 2016, 08:13:02 PM »
This project is going to take me some time to get this complete but I did want to give an update on where I am.

It's going slower than I'd like but that's mainly due to some limitations in the Libraries I chose for this project. For instance, the PDF library was developed mainly for creating PDFs from .net and currently only supports exporting the JPG images that are stored in PDFs. The other types like BMP, PNG, TIFF, and others are left to people like me to figure out how to implement.

The good part is I'm learning about these image types, the bad is I feel like I'm wasting time. There are other .net libraries that read PDFs but they are either not available for use with commercial software or require you to implement the image extraction and conversion.

I will look into more libraries since I'm not looking to reinvent the wheel here. Exporting images should be a fundamental part of the PDF library. The part that needs to be the focus of this project is building the glue that brings these separate libraries together. The PDF library should handle getting the objects from the PDF while the DXF library should handle writing the DXF files and the SVG library for reading SVG files. There is plenty to do to take the objects from PDF and SVG and use the DXF library to write it all out, without having to add the missing pieces to these libraries.

The only reason I've stuck with the PDF library is because it is pretty easy to work with otherwise and well, some of the other libraries are missing the same thing. Hopefully the image extraction should be complete soon and I can get back to translating the PDF and SVG objects to DXF.

Once the images are done, I will focus on converting the paths to DXF drawing objects. Next will be text followed by tracing high contrast images.

I will update once the image extraction is done.

Agastar

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1878
    • View Profile
Re: Convert2PDF Update
« Reply #1 on: August 30, 2016, 06:25:20 AM »
Well, I gave up on PdfSharp and switched to iTextSharp. They allow you to use their library as long as you publish the source code of you app which I plan to do so this should be ok.

I am able to extract the images in a PDF document and save them to my hard drive which means I can also return the image data in a function. I'm also able to get the coordinates of the image in the PDF as rendered. This means that when you view a pdf document, the location of the image(s) on the page(s) is able to be retrieved and used to place the image(s) or any traces of the image(s) in the correct location of the DXF file.

This is important because drawings can be stored as images in PDF files so if you have a mixture of vectors and images that all look like drawings, you need the traces of the images to be in the correct location in reference to the other lines or text that you may have. This is also important for maintaining the correct alignment when you have a mixture of vectors and rasters (ie photos and lines or text) that need to stay relative to each other.

Well, vectors and text are next on the list and I'll post an update here as I go.

Administrator

  • Administrator
  • Hero Member
  • *****
  • Posts: 2095
  • Pilot Instructor
    • View Profile
Re: Convert2PDF Update
« Reply #2 on: August 30, 2016, 07:11:13 AM »
Thanks @Agastar for your hard work...
Ralph--Admin -- support_BenCutLaser_us
http://www.BenCutLaser.us/BenCutLaserSetup1.8.4a.exe.zip
http://www.BenCutLaser.us/BCLVirtualLaserMachineSetup1.8.2.exe.zip
BCL Paid license: $39.95
VLM Paid license: $19.95 (stand alone version)
via Paypal to bclpp@primemail.com

Zax

  • T2Laser
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3812
    • View Profile
    • T2Laser
Re: Convert2PDF Update
« Reply #3 on: August 30, 2016, 07:12:08 AM »
Thanks @Agastar for your hard work...

I second that.

Agastar

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1878
    • View Profile
Re: Convert2PDF Update
« Reply #4 on: August 30, 2016, 07:32:40 AM »
My pleasure :)

Agastar

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1878
    • View Profile
Re: Convert2PDF Update
« Reply #5 on: August 31, 2016, 05:20:19 PM »
I spent some time today working on the path objects in PDFs and converting them to dxf objects. It still needs a LOT of work but I was able to produce something that would load into BCL.

I imported the Polyline4.dxf file from BCL into Inkscape, offset it from 0,0 by 5mm and exported it as a PDF. Then I ran my test code against the Polyline4.pdf file which I have attached to this post. Next I opened it up in BCL to see if it would display the drawing and it did.

The attached Polyline4.dxf is the output from my code. If you open it up in BCL you will see that the same drawing objects are not used and that it is currently made up of polylines and splines. The funny thing is that when reviewing the debug output, the way it is stored in the PDF would allow me to build it almost exactly as it was in the original DXF file.

Here is the debug output from reading the PDF. Ignore the figures right after the text. The coordinates after the --> is the rendered coordinates and the ones that matter.  When you open a PDF, the PDF reader will "render" the contents which means that it can apply scaling, rotating, cropping, etc... The first figures are the raw coordinates and the ones after --> are the "rendered" coordinates.
Quote
Move to (14.578, 827.313)
--> at  (14.578, 14.57678)
Line to (14.578, 742.27)
--> at  (14.578, 99.61975)
Stroke
Move to (42.926, 713.926)
--> at  (42.926, 127.9637)
Curve along (27.27, 713.926) (14.578, 726.617) (14.578, 742.27)
--> at  (27.27, 127.9637) (14.578, 115.2728) (14.578, 99.61975)
Stroke
Move to (42.926, 713.926)
--> at  (42.926, 127.9637)
Line to (127.965, 713.926)
--> at  (127.965, 127.9637)
Line to (127.965, 770.617)
--> at  (127.965, 71.27277)
Stroke
Move to (71.273, 827.313)
--> at  (71.273, 14.57678)
Line to (14.578, 827.313)
--> at  (14.578, 14.57678)
Stroke
Move to (127.965, 770.617)
--> at  (127.965, 71.27277)
Curve along (96.652, 770.617) (71.273, 796) (71.273, 827.313)
--> at  (96.652, 71.27277) (71.273, 45.88977) (71.273, 14.57678)
Stroke
Move to (57.098, 742.27)
--> at  (57.098, 99.61975)
Curve along (57.102, 734.441) (50.754, 728.094) (42.926, 728.094)
--> at  (57.102, 107.4488) (50.754, 113.7958) (42.926, 113.7958)
Curve along (35.098, 728.094) (28.75, 734.441) (28.754, 742.27)
--> at  (35.098, 113.7958) (28.75, 107.4488) (28.754, 99.61975)
Curve along (28.75, 750.102) (35.098, 756.449) (42.926, 756.449)
--> at  (28.75, 91.78778) (35.098, 85.4408) (42.926, 85.4408)
Curve along (50.754, 756.449) (57.102, 750.102) (57.098, 742.27)
--> at  (50.754, 85.4408) (57.102, 91.78778) (57.098, 99.61975)
Close
Move to (57.098, 742.27)
--> at  (57.098, 99.61975)
Stroke
Move to (113.793, 742.27)
--> at  (113.793, 99.61975)
Curve along (113.793, 734.441) (107.449, 728.094) (99.617, 728.094)
--> at  (113.793, 107.4488) (107.449, 113.7958) (99.617, 113.7958)
Curve along (91.789, 728.094) (85.441, 734.441) (85.445, 742.27)
--> at  (91.789, 113.7958) (85.441, 107.4488) (85.445, 99.61975)
Curve along (85.441, 750.102) (91.789, 756.449) (99.617, 756.449)
--> at  (85.441, 91.78778) (91.789, 85.4408) (99.617, 85.4408)
Curve along (107.449, 756.449) (113.793, 750.102) (113.793, 742.27)
--> at  (107.449, 85.4408) (113.793, 91.78778) (113.793, 99.61975)
Close

Move to (113.793, 742.27)
--> at  (113.793, 99.61975)
Stroke
Rectangle at (42.926, 770.617) with size (28.348, 28.348)
--> at (42.926, 71.27277) (71.274, 71.27277) (71.274, 42.92474) (42.926, 42.92474)
Stroke

This block would be a line
Quote
Move to (14.578, 827.313)
--> at  (14.578, 14.57678)
Line to (14.578, 742.27)
--> at  (14.578, 99.61975)
Stroke

while this block could be turned into a polyline. This is the line that is highlighted in the attached image.
Quote
Move to (42.926, 713.926)
--> at  (42.926, 127.9637)
Line to (127.965, 713.926)
--> at  (127.965, 127.9637)
Line to (127.965, 770.617)
--> at  (127.965, 71.27277)
Stroke

The same idea seems to hold true for "Curve along" so it may be possible to convert these to circles and arcs without having to get a lot of math involved to see if the curve is symmetrical or not. More testing needs to be done to be sure.

The other cool part is that the netDxf library has built in conversions from one shape to another. So basically, I can take an arc, circle, or spline and convert it to a polyline like this...
Quote
var spline = new Spline();
...
spline.ToPolyline(10);

Still a ton more work to do but I thought I'd share what I have so far.

Administrator

  • Administrator
  • Hero Member
  • *****
  • Posts: 2095
  • Pilot Instructor
    • View Profile
Re: Convert2PDF Update
« Reply #6 on: August 31, 2016, 06:05:41 PM »
To confirm, yes, I too was also able to load in the Polyline4.dxf file into BCL (see attachment). It came in perfectly fine.

I took one of the 'circles' which was actually made up of 4 Splines (shaped as Arcs) and assigned a laser tool to them and then made them a 'group' to represent a circle, and then used the FC/IC/LC tags to tell the Gcode Generation engine to treat these 4 objects as one continuous object (a circle).

So, excellent work @Agastar, this will work fine for BCL...!!!

As a side note: What I would do with this .dxf file is manually trace over those 4 splines with a circle object in my CAD program and then delete the 4 splines. This doesn't take much manual work and inside BCL there is only object to work with vs 4 objects -- however -- this isn't necessary to do at all...you can still work with the 4 splines as I did in the attachment. I'm just saying, if you feel like it, you can make the .dxf conversion a little more efficient by doing something like that manually but it isn't mandatory to make the conversion work...!!!
« Last Edit: August 31, 2016, 06:07:49 PM by Administrator »
Ralph--Admin -- support_BenCutLaser_us
http://www.BenCutLaser.us/BenCutLaserSetup1.8.4a.exe.zip
http://www.BenCutLaser.us/BCLVirtualLaserMachineSetup1.8.2.exe.zip
BCL Paid license: $39.95
VLM Paid license: $19.95 (stand alone version)
via Paypal to bclpp@primemail.com

Agastar

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1878
    • View Profile
Re: Convert2PDF Update
« Reply #7 on: August 31, 2016, 06:54:01 PM »
Thanks!

Yes I do plan on reducing the number of objects. My first test file was a rectangle, line, and a spline and after getting that to work I decided to try the Polyline4.dxf file. Honestly I was surprised it actually worked lol. This is bare bones code at this point but I do plan on coding it to recognize arcs, circles, and other basic shapes.

The patterns I'm seeing in the debug output will help me write code to recognize these patterns and translate them into these other shapes.This is where trying a bunch of different PDFs will help me learn how these shapes are drawn in PDFs.

With the built in functionality to convert to polylines this will make it easier to work with BCL and T2laser without much more code.

Thanks for the feedback.

Agastar

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1878
    • View Profile
Re: Convert2PDF Update
« Reply #8 on: August 31, 2016, 06:59:04 PM »
I'll post another file once I get the arcs and circles implemented.

Zax

  • T2Laser
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3812
    • View Profile
    • T2Laser
Re: Convert2PDF Update
« Reply #9 on: September 01, 2016, 04:59:54 PM »
I've added circles, arcs, ellipses (with angles) and splines to T2Laser DXF import.


Administrator

  • Administrator
  • Hero Member
  • *****
  • Posts: 2095
  • Pilot Instructor
    • View Profile
Re: Convert2PDF Update
« Reply #10 on: September 01, 2016, 05:58:15 PM »
Looking good @Zax...!!!
Ralph--Admin -- support_BenCutLaser_us
http://www.BenCutLaser.us/BenCutLaserSetup1.8.4a.exe.zip
http://www.BenCutLaser.us/BCLVirtualLaserMachineSetup1.8.2.exe.zip
BCL Paid license: $39.95
VLM Paid license: $19.95 (stand alone version)
via Paypal to bclpp@primemail.com

Zax

  • T2Laser
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3812
    • View Profile
    • T2Laser
Re: Convert2PDF Update
« Reply #11 on: September 01, 2016, 06:05:19 PM »
Thanks.

Circles, ellipses and arcs - including start and end angles took 2-3 hours and then I tackled splines which almost drove me crazy. 5 hours later it's nearly done but I need to test with many more files.

Agastar

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1878
    • View Profile
Re: Convert2PDF Update
« Reply #12 on: September 01, 2016, 08:12:32 PM »
lol, I know what you mean about this stuff driving you crazy lol. Man, the arcs were kicking my butt tonight. I had to convert the splines to arcs first and then trying to figure out which direction they were going to set the correct start and end angles was a bit crazy.

Well, I got circles and arcs done tonight finally and let me just say that was fun lol. I had to figure out how to take the cubic bezier points that is given to me from the PDF and plot the middle point of the path to see if that point along with the starting and ending points were symmetrical. Once that was working I just checked to see if 4 consecutive arcs were all symmetrical and if so I created a circle instead of 4 arcs. I started stubbing out the code to look for two consecutive symmetrical arcs in order to make them into half circle arcs instead of having two arcs back to back but I'll have to pick that up tomorrow.

I've attached the latest run for you guys.

Administrator

  • Administrator
  • Hero Member
  • *****
  • Posts: 2095
  • Pilot Instructor
    • View Profile
Re: Convert2PDF Update
« Reply #13 on: September 01, 2016, 08:51:53 PM »
BCL can read that latest file in OK AND the drawing objects are absolutely perfect, beautifully done -- but -- there are two issues with the file:

1. When ever any drawing object resides in negative space in the canvas -- any object is below -Y or left of -X axis, a popup dialog warns about that. I have many drawings that have some objects just slightly into negative space and it triggers the dialog. I get that dialog on your latest file despite all visible drawing objects clearly showing well into the positive space. So this tells me there is something still in negative space. Usually an object so small that you can't see it.

2. I took the drawing and loaded it into QCAD and deleted all the visible drawing objects and saved the file with a 'b' added onto the filename.
I opened it up in BCL and attached is the error dialog I saw. I don't know if or how you can check your .dxf file for 'extra' objects or objects too small to see with your eye when the file is loaded.

The main drawing objects are absolutely perfect...you hit it out of the ballpark...!!!
Ralph--Admin -- support_BenCutLaser_us
http://www.BenCutLaser.us/BenCutLaserSetup1.8.4a.exe.zip
http://www.BenCutLaser.us/BCLVirtualLaserMachineSetup1.8.2.exe.zip
BCL Paid license: $39.95
VLM Paid license: $19.95 (stand alone version)
via Paypal to bclpp@primemail.com

Agastar

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1878
    • View Profile
Re: Convert2PDF Update
« Reply #14 on: September 01, 2016, 09:29:47 PM »
I just noticed that you are up to 1.05 now and I'm still on 1.03. I don't get that pop up but if that is a new feature then that might explain why I'm not seeing it.

I'll check the count of DXF entities in code and see if it matches the count of objects I know about. It is quite possible that it is adding an empty entity that I missed somehow. I'll upgrade to the latest version too.

Thanks for checking out the file.