Info Polyline Area

This forum is for 'Work-in-Progress' QCAD user developed script Add Ons and Plug-ins.

Please use this forum to request/submit your script Add-Ons & Plug-in idea's.

Moderators: andrew, Husky, J-J

Post Reply
riverbuoy
Senior Member
Posts: 121
Joined: Thu Oct 03, 2013 5:37 pm

Info Polyline Area

Post by riverbuoy » Mon May 09, 2016 6:24 pm

Hi,

I have added an option to the information menu to print the area and circumference (length) of a polyline. Recently, Andrew added an 'area of polyline' property to QCAD Pro only, suggesting that he doesn't want this to be shown in the community edition. I don't have a problem with this, but I am posting this because users of the community edition can calculate the area of a polyline by using the two tools 'Info->Polygonal Area' and 'Info->Arc/Circle/Ellipse Area'. By noting down the polygonal area and adding or subtracting the sector area of arcs, the total area can be calculated. This tool does this automatically for the user. (It doesn't display the area in the property editor.)
When you start the tool, it prompts you to select a polyline. When selected it prints the area and circumference of the polyline. (The circumference is the 'length' shown in the property editor.) It continues to prompt you to select a polyline until you press the right mouse button, or press the 'escape' key.
This tool doesn't check that the polyline is closed, nor does it check if the polyline crosses itself. If the polyline is not closed, it will print an area which will be correct if the polyline only has straight line segments. If it has arcs, the area will only be correct if all the arc areas are to be subtracted from the total area. If you want to see the correct value, temporarily close the polyline, run this tool, and then open the polyline again. If the polyline crosses itself then all bets are off; the area is almost certainly wrong. In this case, try breaking the segments into smaller segments that do not cross. (This may not be possible, in which case case try creating more than one polyline.)
If you are using QCAD Pro, you will notice, when the polyline has arcs, that the area shown by this tool is slightly different than the area displayed in the property editor. This is because, I believe, Andrew is calculating the area of the arcs by replacing the arcs with many small straight line segments. This can cause the area to be either slightly larger (when subtracting arc areas), or slightly smaller (when adding arc areas). This tool calculates the sector area of the arcs, and then adds or subtracts them as required.
The attached zip file contains a directory called 'InfoPolylineArea'. Extract this, and place it in the 'scripts/Information' directory.

I hope this doesn't upset Andrew too much.

riverbuoy
Attachments
InfoPolylineArea.zip
(13.81 KiB) Downloaded 731 times

User avatar
andrew
Site Admin
Posts: 9019
Joined: Fri Mar 30, 2007 6:07 am

Re: Info Polyline Area

Post by andrew » Mon May 09, 2016 7:50 pm

riverbuoy wrote:I hope this doesn't upset Andrew too much.
Absolutely not :) Community contributions are of course always welcome and this is a great one. The polyline area calculation should really go into RPolyline.cpp if you have no objections. This way it would be available to QCAD CE and QCAD Pro for all polyline area calculations.

There are some issues with this algorithm though. But I think it's only the condition that decides whether an arc sector area is added or subtracted. Areas of polylines like the one attached seem to be off because of this.
Screen Shot 2016-05-09 at 20.44.04.png
Screen Shot 2016-05-09 at 20.44.04.png (15.56 KiB) Viewed 15455 times
My suggested fix for the relevant parts of InfoPolylineArea.prototype.getArea would be:
var plReversed = (this.shape.getOrientation()===RS.CW);

    // arc segments
    if (this.shape.hasArcSegments()) {
        for (var i = 0; i < nvert; i++) {
            if (this.shape.isArcSegmentAt(i)) {
                var arcseg = this.shape.getSegmentAt(i);
                arcarea = this.getSectorArea(arcseg);

                // get mid point of chord
                var startpt = arcseg.getStartPoint();
                var endpt = arcseg.getEndPoint();
                var cen = new RVector((startpt.x + endpt.x) / 2.0, (startpt.y + endpt.y) / 2.0);
                if (arcseg.isReversed()===plReversed) {
                    // arc has same orientation as polyline: add
                    area = area + arcarea;
                }
                else {
                    // arc has opposite orientation of polyline: subtract
                    area = area - arcarea;
                }
            } // isArcSegmentAt
        } // vertices loop
    } // hasArcSegments
What do you think? I didn't test this extensively yet, but I sure hope there's no fundamental flaw with the idea. This would be much better than the arc segmentation currently used.
Attachments
area.dxf
(104.35 KiB) Downloaded 716 times

riverbuoy
Senior Member
Posts: 121
Joined: Thu Oct 03, 2013 5:37 pm

Re: Info Polyline Area

Post by riverbuoy » Tue May 10, 2016 6:17 pm

Hi Andrew,
andrew wrote:The polyline area calculation should really go into RPolyline.cpp if you have no objections.
I have no objections. Glad you liked it.
andrew wrote:There are some issues with this algorithm though.
I had tried to think of an example where this wouldn't work. You're example never even occurred to me. However, if I had thought of it, this might never have seen the light of day, as I doubt I would have thought of using the arc's direction to decide whether to add or subtract the area.

You're fix seems to be the perfect solution. I have tried it on several polylines, and so far it has performed flawlessly. I have made three small changes.

Firstly, I added a check at the start of the getArea function which checks if the polyline is geometrically closed. If not, it sets closed to true, and then just before returning it sets closed back to false. This means the correct area will be returned whether the polyline is closed or not. The only issue would be if the polyline crossed over itself, the returned value would, almost certainly, be wrong.

The second change is at line 103. I change the point where the text is added. Change:-
var c = this.getCenter();
to
var c = this.shape.getLastVertex();
getCenter was producing some odd positions.

Thirdly, I removed the lines which are calculating the mid point of the chord.

I have attached the changed file.

riverbuoy
Attachments
InfoPolylineArea.js
(6.69 KiB) Downloaded 720 times

User avatar
andrew
Site Admin
Posts: 9019
Joined: Fri Mar 30, 2007 6:07 am

Re: Info Polyline Area

Post by andrew » Wed May 11, 2016 11:40 am

Thanks. This should all be implemented now mostly in RPolyline.cpp and RArc.cpp.

Post Reply

Return to “QCAD 'Script Add-On & Plug-in challenge' - Work in Progress”