Notifications
Clear all

[Closed] How get a edge length?

Hi…
When i selected a edge , I use the code to get the length of th edge.
< getEdgeSelection.Length > but it not work.

6 Replies
2 Replies
(@bobo)
Joined: 11 months ago

Posts: 0

You will have to get the vertices at the edge’s ends, read their positions and calculate their distance.

(@perfectboy)
Joined: 11 months ago

Posts: 0

Thanks Bobo…
I understand the thought you told me,but maybe it have a little diffcult for me.
I read the help doc, but didn’t find how to get the two vertices at the edge.

Hi, here is a piece of code to start you up. Take a look at “Editable Poly” in the MaxScript Reference to find many other methods.

(
    function getEdgeLength theEditablePoly iEdge =
    (
        -- get vertexes defining the edge into an Array
        local aiEdgeVerts = polyOp.getEdgeVerts theEditablePoly iEdge
    
        -- get vertexes positions into an Array
        local ap3VertPosition = for iVert in aiEdgeVerts collect
            polyOp.getVert theEditablePoly iVert
        
        -- return the distance between vertexes
        return (distance ap3VertPosition[1] ap3VertPosition[2])
    )
    
    -- create a Plane and convert it to Editable Poly
    local thePlane = convertToPoly (Plane())
    
    -- get and print the length of edge 1
    format "Length of edge 1 is: %
" (getEdgeLength thePlane 1)
)
  • Enrico

Thanks SyncViewS,it’s useful very much.
When i select an Edge on one box ,and run the script, it crash.


Rollout caption "Edge"
 (
  button click1 "Check" width:150 height:25 pos:[10,10]
  label lab1 "EdgeLength:" width:130 height:25 pos:[10,52]
	 edittext editx "" fieldWidth:53 pos:[100,50] labelOnTop:false
  
	function getEdgeLength theEditablePoly iEdge =
	(
		local aiEdgeVerts = polyOp.getEdgeVerts theEditablePoly iEdge
	
		local ap3VertPosition = for iVert in aiEdgeVerts collect
			   polyOp.getVert theEditablePoly iVert
		
		return (distance ap3VertPosition[1] ap3VertPosition[2])
	)
 
	on click1 pressed do
   (
	ep = Edit_Poly()
	el= format "%
" (getEdgeLength ep.Selection)
	editx.text = el
	)
)
newroll=newrolloutfloater "-MyTool" 180 300
addRollout caption newroll

Hi, you made a little mess
If you want to get the edge length from an editable poly, you need to get it from the scene, check if the selection is valid, then do your calculations. Here is a script: create a box, convert it to editable poly, go to edge sub level and select one edge, then press the “Check” button.

Rollout caption "Edge"
(
    button click1 "Check" width:150 height:25 pos:[10,10]
    label lab1 "EdgeLength" width:130 height:25 pos:[10,52]
    edittext editx "" fieldWidth:53 pos:[100,50] labelOnTop:false
    
    function getEdgeLength theEditablePoly iEdge =
    (
        local aiEdgeVerts = polyOp.getEdgeVerts theEditablePoly iEdge
        
        local ap3VertPosition = for iVert in aiEdgeVerts collect
        polyOp.getVert theEditablePoly iVert
        
        return (distance ap3VertPosition[1] ap3VertPosition[2])
    )
    
    on click1 pressed do
    (
        -- check if current selection is a single Editable Poly
        if (selection.count == 1) and ((classOf selection[1]) == Editable_Poly) do
        (
            theEPoly = selection[1]
            
            -- get current edge selection in Editable Poly
            baEdge = polyOp.getEdgeSelection theEPoly
            
            -- if one edge is selected, write its length to the edit text field
            if (baEdge.numberSet == 1) then
                editx.text = (getEdgeLength selection[1] (baEdge as Array)[1]) as String
            else
                editx.text = ""
        )
    )
)

newroll = newrolloutfloater "-MyTool" 180 300
addRollout caption newroll
  • Enrico

Hi…Dear SyncViewS! Sorry my confused code.
With your help, i have finished my little study in script, i think there’re more error in my script, but i possessed the mothod how to get the length…

Thank you very much!