[tool] Curved lines  SOLVED

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: Daniel - PDF-XChange, PDF-XChange Support, Vasyl - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

Post Reply
Mathew
User
Posts: 560
Joined: Thu Jun 19, 2014 7:30 pm

[tool] Curved lines  SOLVED

Post by Mathew »

A built-in tool to draw curved lines is in the works (viewtopic.php?t=41916) but until then:

This tool will convert a polyline, polygon or rectangle into a curved line, either as a polyline, or as an ink annotation. It allows you to set a 'radius' to limit the size of the curve.

Background
There isn't any easy way to add curved markups (annotations) besides trying to approximate a curve by drawing a polyline with many points. The application has a built-in way to draw curves in the ink tool, but they are quite difficult to control.

This tool takes a polyline and draws quadratic curves between the start point, midpoints of interior lines, and end point; so it's more intuitive where the curves are.

The tool also allows editing of the curve by adjusting the original control line (if you delete the original control line, it will remember the coordinates of that line temporarily and recreate if you select the curved line and run the Poly to Curve tool, so you can still edit it, but only until you close the file).

Installation
Extract the zip file, and save the files into the Javascripts folder either at
%APPDATA%\Tracker Software\PDFXEditor\3.0\JavaScripts\
or at
%PROGRAMFILES%\Tracker Software\PDF Editor\Javascripts\
curvedPoly v1.11.zip
(18.67 KiB) Downloaded 24 times

On restarting PDFX-Change, it will add a button to the Add-on tools to quickly convert a polyline into a curve (select the polyline first, then this tool - it will draw the curve and ends in edit mode. Delete the control line or close the progress dialog to stop editing):
image(4).png
image(4).png (1.19 KiB) Viewed 7728 times
It will also add a menu item in the Home ribbon (and in Comments menu for classic UI) that makes the curve and directly opens the dialog:
image(8).png
image(8).png (1.26 KiB) Viewed 7377 times
Select the polyline (you can select one or more to edit at the same time) to use as the control points for the curved line:
image(1).png
image(1).png (16.37 KiB) Viewed 7835 times
Run the Poly to Curve tool to generate a curved line from it. It converts the polylines to curves, copies all the properties from the lines to the curves, and converts the original polyline to dashed blue/green color; then shows a dialog:
image.png
Markup Type
  • Select either polyline or ink annotation type for the curve. The polyline option uses a polyline (or polygon for closed shapes), and will allow you to individually edit points on the lines. The ink option makes a single object similar to the pencil tool and doesn't show the individual control points. Either of them can be edited with this tool, but the latter cannot have arrows.
Max Radius
  • 'Auto' radius means it calculates the distance between the corner point and the midpoint of the line on each side at each corner. To round corners by a specific amount, enter a radius (in points). For example, to round the corners of a rectangle, enter 18 for the radius and it will only round the corners, similar to entering 0.25in for the radius in properties (18pt / 72ppi = 0.25in).

    The dialog box updates a bit erratically, so you may need to select inside the edit box after editing to see the change.
Delete Control Lines
  • If selected, it will delete the original control line and just leave the generated curve. If not checked, when you select "OK" you keep both the control line and the curve:
    image(2).png
    image(2).png (20.7 KiB) Viewed 7835 times
    Once the original control line is deleted, you can still edit the curve only until the file is closed.

    To edit a curve, select it and run the tool or you can edit it when you first create it.
Edit
  • If you select "Edit" the dialog closes and the selection tool is enabled. Now you can edit the original control line and the curve will follow:
    image(6).png
    image(6).png (21.79 KiB) Viewed 7585 times
    To end editing, you can either just delete the control line, or there's a progress bar in the lower right corner: Click the X to close it and go back to the dialog:
    image(7).png
    image(7).png (2.96 KiB) Viewed 7760 times
It also works with polygons - the hatch tool will recognize the curves and hatch inside them fine:
image(3).png

Options
Ink annotations have built-in smoothing of lines, so the curved line can be generated with much fewer points. It may print better, and doesn't increase the file size as much. But you can't have arrows on it, and the hatch tool will only approximately match the boundary (it uses straight lines between control points).

By default, the tool generates a polygon or polyline with at most 20 points per curve and ink with 6 points per curve. To change the number of points and how smooth the curve is, edit lines 59-60 to set the number of points you want. It also will reduce the number of points used to draw the curve based on a factor * square root of the line thickness. Line 61 can be set to zero to prevent this:

Code: Select all

  const minCurvePoints = 20; // number of points to use in polyline
  const minInkPoints = 6;    // number of points to use in ink
  const ptSpacingFactor = 1.5;    // minimum spacing between curve points in points is determined by multiplying this * sqrt(linewidth). Set to zero to disable. 

tl;dr
The shape appears approximately circular, but a quadratic curve has a varying radius that grows as it approaches the line on each end. It's more obvious at sharp than oblique angles.

Initially I thought I could just take the vertices from the polygon or polyline and put them into an ink annotation, because PXCE has built-in smoothing for that. But the way the ink annotation handles curves is not intuitive to me - it's some type of Bezier I guess, but I couldn't get it to behave without basically drawing most of the points.

By default the script calculates 6 points per curve for ink (pencil) annotations - more points than that for ink annotations didn't seem to have much effect. I still can't figure out how to get a perfectly smooth transition at the beginning and end of a polygon if using ink, so converted polygons will have a very small dimple:
image(5).png
Editing works by setting up an interval timer that watches for changes to the control line, and redraws the curve after each change. It uses xutils for the improved timeout and interval functions. The only non-modal dialog that javascript has access to (as far as I know) is the thermometer progress dialog, so I used that as a way to end the interval. It also times out after about 40 seconds, and gives an option to stop editing.

Limitations
  • I've not come up with a way to save the information from the original control line when the file is saved and closed. I currently assign a custom property 'curveData' to the ink annotation but that doesn't get saved with the file.
  • After editing a polyline, "Cancel" doesn't undo any changes - the built-in 'undo' function should be able to step you back through, though.
  • If you close the document while curve edit is running PXE immediately stops the repeating interval checking, so you'll be left with the progress dialog box stuck in the lower right corner. Run the tool again to clear it.
Last edited by Mathew on Sat Apr 05, 2025 5:00 pm, edited 48 times in total.
User avatar
Dimitar - PDF-XChange
Site Admin
Posts: 2191
Joined: Mon Jan 15, 2018 9:01 am

Re: [tool] Curved lines

Post by Dimitar - PDF-XChange »

Thank you for your effort, Mathew.

We appreciate it.
Mathew
User
Posts: 560
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Curved lines

Post by Mathew »

I found a way to edit the curves, so the tool now has an option to edit.
curvedPoly v1.0.zip
(11.08 KiB) Downloaded 102 times
Changes in v1.0
  • Add a dialog to show curves and allows edits or cancel and switch curve type ink/polyline,
  • allow multiple curves to be edited,
  • don't try to draw curve to curve
[edit] updated to 1.0.1 to fix bugs when no control line selected
Mathew
User
Posts: 560
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Curved lines

Post by Mathew »

.. and another edit - I added a quick tool to just take the polyline, convert it into curved and delete the original, because that's what I do the most. One can still edit with the Poly to Curve tool.
curvedPoly v1.1.zip
Extract and put files into the Javascripts folder. See first post for details
(11.44 KiB) Downloaded 101 times
Select the polyline:
image.png
Use the "Curve" button in the Add-on toolbar
image(1).png
image(1).png (1.2 KiB) Viewed 7730 times
And it results in a curved line:
image(2).png
Until the pdf is closed, you can still edit the curve using the other tool (see first post above)
image(3).png
image(3).png (1.53 KiB) Viewed 7727 times
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 10862
Joined: Wed Jan 03, 2018 6:52 pm

[tool] Curved lines

Post by Daniel - PDF-XChange »

:)
Dan McIntyre - Support Technician
PDF-XChange Co. LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
Mathew
User
Posts: 560
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Curved lines

Post by Mathew »

I keep fiddling with this tool. I think this version is a slight improvement:
curvedPoly v1.2.zip
See first post for detailed instructions.
(11.79 KiB) Downloaded 93 times
Changes
  • moved location of menu item to 'Comments' (classic UI) and comment tools on 'Home' ribbon,
  • also change color of control line not curve,
  • edit mode goes to 'ok' if control line deleted & 'cancel' if curve deleted,
  • quick command ends at 'edit' mode,
  • dancing ants option: If you want to see the dashes on control lines moving during editing set line 49 to true. However, this option adds hundreds of undos, so I really do not recommend it. It's more a novelty that I just wanted to try out:

    Code: Select all

      const dancingAnts = true; // true to show dancing ants on edit line
Mathew
User
Posts: 560
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Curved lines

Post by Mathew »

... still fiddling with it. I added a radius option. Enter a maximum radius or 'auto' to calculate curves between midpoints. The update when typing into the radius box is a bit erratic, so you may need to use the mouse to select inside the input box after typing to get it to update the preview:
image.png
image(1).png
curvedPoly v1.3.zip
See first post for detailed instructions.
(12.47 KiB) Downloaded 88 times
Last edited by Mathew on Mon Dec 02, 2024 6:44 pm, edited 1 time in total.
User avatar
Paul - PDF-XChange
Site Admin
Posts: 7356
Joined: Wed Mar 25, 2009 10:37 pm
Contact:

Re: [tool] Curved lines

Post by Paul - PDF-XChange »

Hi, Mathew

this is truly a thing of beauty.

Just gorgeous. Thank you for this.
Best regards

Paul O'Rorke
PDF-XChange Support
http://www.pdf-xchange.com
Mathew
User
Posts: 560
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Curved lines

Post by Mathew »

Thank you @Paul :)

A slight change to properly calculate points from radius (!)
[Edit] revised some code to use v0.4 of xutil.setInterval() - no change to how the tool works.
curvedPoly v1.3.2.zip
See first post for usage
(7.33 KiB) Downloaded 112 times
Last edited by Mathew on Wed Dec 04, 2024 1:11 am, edited 1 time in total.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19777
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: [tool] Curved lines

Post by Stefan - PDF-XChange »

Hello Mathew,

Thanks for the updated script and improved points calculations!

Kind regards,
Stefan
Mathew
User
Posts: 560
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Curved lines

Post by Mathew »

I can't leave this one alone ;) I fixed some bugs and made it work better when drawing as ink annotations.
curvedPoly v1.5.zip
See first post for usage.
(14.51 KiB) Downloaded 93 times
image.png
Changes since v1.3
  • optimize curve point spacing based on line thickness
  • fix bug when document closed or switched with edit mode running
  • draw ink as individual curves and lines
  • fix bug adding start/end point in polygon control line
User avatar
Dimitar - PDF-XChange
Site Admin
Posts: 2191
Joined: Mon Jan 15, 2018 9:01 am

Re: [tool] Curved lines

Post by Dimitar - PDF-XChange »

Thank you for your effort, Mathew.
xchange dude
User
Posts: 2
Joined: Thu Aug 01, 2024 7:05 pm

Re: [tool] Curved lines

Post by xchange dude »

Works wonderfully.
Jerry
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19777
Joined: Mon Jan 12, 2009 8:07 am
Contact:

[tool] Curved lines

Post by Stefan - PDF-XChange »

:)
Mathew
User
Posts: 560
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Curved lines

Post by Mathew »

Updated above so that if you move the curve, and edit again, it will move the control line to the correct place (doesn't work if the rotation is changed through). It also tries hard to find a control line if you duplicate a curve: Works so long as the polyline points or rotation isn't changed on the copied curve before running the tool on the copied curve. If you set a radius and/or curve type (polyline or ink) it won't overwrite them when editing (doesn't save this info with the file still, so only works until the file is closed.)

[edit] Minor change to use the center of the rect instead of a corner for positioning the control line. Much better but still not great for rotated curves. Fixed some bugs.

I tried a demo (thanks Loki@99 for pointing to LICEcap)...
curve demo.gif
curvedPoly v1.9.zip
(16.66 KiB) Downloaded 55 times
Changes since v1.5
v1.9
  • Use center of curve's .rect property for base point,
  • comparison of curves is slightly more error tolerant,
  • fix bug not getting curve type in edit mode
  • also update rotation during edit
v1.8
  • save max radius with curve
  • get type from curve
  • fix bug properly cancelling multiple intervals (resulted in multiple dialogs on edit of multiple curves)
v1.7
  • handle moving the ink annotation (saves the control line based at 0,0)
  • debug ink curves
v1.6
  • draw ink continuous if no radius given (smoother line)
Last edited by Mathew on Mon Feb 17, 2025 7:30 pm, edited 5 times in total.
User avatar
Dimitar - PDF-XChange
Site Admin
Posts: 2191
Joined: Mon Jan 15, 2018 9:01 am

Re: [tool] Curved lines

Post by Dimitar - PDF-XChange »

Thank you for your effort, Mathew.
Mathew
User
Posts: 560
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Curved lines

Post by Mathew »

:)

... found some bugs I'd introduced. Revised again v1.9 :oops:
image.png
image.png (27.99 KiB) Viewed 5240 times
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 10862
Joined: Wed Jan 03, 2018 6:52 pm

[tool] Curved lines

Post by Daniel - PDF-XChange »

:)
Dan McIntyre - Support Technician
PDF-XChange Co. LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
Mathew
User
Posts: 560
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Curved lines

Post by Mathew »

v1.10 uploaded above. More bug fixes.
curvedPoly v1.10.zip
See first post for details
(16.45 KiB) Downloaded 47 times
  • slight change on decision whether to redraw for changed radius,
  • copy radius also when copying control line from another curve,
  • fix bug when copying rectangle control line,
  • deal with inReplyTo bug [PXCE build 394]
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19777
Joined: Mon Jan 12, 2009 8:07 am
Contact:

[tool] Curved lines

Post by Stefan - PDF-XChange »

:)
BLEService
User
Posts: 1
Joined: Tue Mar 11, 2025 3:00 pm

Re: [tool] Curved lines

Post by BLEService »

Hi There,
Can making/using curved lines be added to Xchange Plus? I work for an electrical contractor and we would use this feature all the time.
I cannot figure out how to make a curved line.

I've read through so many forum posts about it but the screenshots are not helping lol

Really need some help with this, please and thank you!
User avatar
Paul - PDF-XChange
Site Admin
Posts: 7356
Joined: Wed Mar 25, 2009 10:37 pm
Contact:

Re: [tool] Curved lines

Post by Paul - PDF-XChange »

Hi, BLEService

it may be that such a tool can be added in time, but given the awesome nature of Mathew's tool it is not likely to happen right away.

Is there something we can help you with in regards using this add-on tool for the job?
Best regards

Paul O'Rorke
PDF-XChange Support
http://www.pdf-xchange.com
Mathew
User
Posts: 560
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Curved lines

Post by Mathew »

BLEService wrote: Tue Mar 11, 2025 3:06 pm I've read through so many forum posts about it but the screenshots are not helping lol
For my tools I put all the instructions and the most recent version of the tool in the first post of the topic (generally). I'm sorry you're having trouble with it: Is there something specific that isn't explained well?

My guess: Installation is generally the most difficult thing when people are installing a custom javascript tool the first time:
  1. Download the tool. It's saved as a zip file.
  2. Find/make the destination JavaScripts folder. Two ways to do this:
    1. Open the JavaScript console by pressing CTRL+J or going to Form > JavaScript Console:
      image.png
      Click on the buttom "App-Level Scripts…"
      image(1).png
    2. or paste the following into the windows explorer location: %APPDATA%\Tracker Software\PDFXEditor\3.0 and if there's a folder "JavaScripts" open it, or if not, make a folder with that name
  3. Go to the zip file you downloaded. Windows explorer allows you to open zip files directly by double clicking on it. Select all the files inside it, and copy them
    image(2).png
  4. Go back to the JavaScripts folder you found in step 2 and paste the files into that folder.
  5. Restart PDF XChange
@Paul thank you for your kind words! Made my day :)
Mathew
User
Posts: 560
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Curved lines

Post by Mathew »

v1.11 updated above. This is code reconfiguring in anticipation of using the built-in timeout functions. If v1.10 is working fine for you, no need to update.
curvedPoly v1.11.zip
See first post for details
(18.67 KiB) Downloaded 24 times

Changes since v1.10
  • remove separate timeout to simplify;
  • change interval functions to recursive timeout;
  • add showCountdown option. This updates the thermometer in the lower right until it asks "Do you want more time?" again. I find it annoying, so disabled it, but if you want it, set showCountdown = true on line 65

    Code: Select all

      const showCountdown = true;    // true to show the timer counting down until the next 'do you need more time?'
    
User avatar
Dimitar - PDF-XChange
Site Admin
Posts: 2191
Joined: Mon Jan 15, 2018 9:01 am

Re: [tool] Curved lines

Post by Dimitar - PDF-XChange »

Thank you for your effort, Mathew.

It is really appreciated.
Post Reply