70001180 How to get a polyline segment from point

Article 70001180
Type HowTo
Product WebJS
Version 7
Date Added 9/29/2017
Fixed 7.7012.0.7 (10/3/2017)
Submitted by Brendan Fry

Summary

How can i get the segment of a polyline when i click somewhere on it

Solution

In version 7012.0.7 a new method vdgeo.GetPlineSegmentIndexFromPoint(object point, object [] vertexes, bool isClosed, object extrusionVector) has been exported.
point: A passed point in world coordinates
vertexes: An array of the vertexes of the polyline which we usually get from pline.VertexList.Items
isClosed: True if it is closed or False if it is open.If not passed it will be False
extrusionVector: An array of [X,Y,Z] which is a vector that is perpendicular to the plain of the polyline.Usually we get it from the polyline.If not passed it will get the [0,0,1] value.
Below is an example where on mouse down event when we click somewhere close to a polyline we can find
the segment with the start and the end point of the polyline which is more close to the point that we have clicked .

     function _vdmousedown(e) {

            if (e.target.ActiveAction().IsStarted()) return; //no user waiting action is active
            var entity = e.target.GetEntityFromPoint(e.xPix, e.yPix); //scan for an entity under the pick point
            if (!entity || entity._t !== vdConst.vdPolyline_code) return; //check if entity exist and is a polyline type
            var index = vdgeo.GetPlineSegmentIndexFromPoint([e.x, e.y, e.z], entity.VertexList.Items, entity.Flag == 1, entity.ExtrusionVector); //get the picked index near the pick point
            if (index == -1) return; 
            //an index was found.Print the result
            var sp, ep; 
            sp = entity.VertexList.Items[index]; 
            if (index == entity.VertexList.Items.length - 1) ep = entity.VertexList.Items[0];//in case passed polyline is closed
            else ep = entity.VertexList.Items[index + 1];
            alert("Segment " + index + "\nbetween first point X=" + sp[0].toFixed(3).toString() + "   Y=" + sp[1].toFixed(3).toString() + "   Z=" + sp[2].toFixed(3).toString() + "   Bulge=" + sp[3].toFixed(3).toString()
            + "\nand second point X=" + ep[0].toFixed(3).toString() + "   Y=" + ep[1].toFixed(3).toString() + "   Z=" + ep[2].toFixed(3).toString() + "   Bulge=" + ep[3].toFixed(3).toString()); 

      }

Send comments on this topic.