Jump to content

The middle distance between the two Plines


Recommended Posts

Posted

If possible,

calculate the average distance between two Plines
As the attached picture

Capture.PNG

Posted (edited)

Is the green polyline straight (I suppose not)?

If it isn't, what is the average based on? 

 

Maybe you mean average according to the x-axis? I see that your distance line is vertical, so that suggests to base it on the x-axis

 

Can these two ever intersect?

Is the green polyline always  bigger than the blue one (both to the left and to the right)?
 

Edited by Emmanuel Delay
  • Like 1
Posted

The Green Line is not straight 

It is taller than the blue line

As is attached

 

Is it possible to calculate the area space between the two lines?

And divide the value of the area ON  the ((horizontal)) length of the short blue line

 

average distance.dwg

Posted (edited)

I think this works.

Make sure you keep to the specs of your last post.  (Don't reverse the role of the top and down polyline, don't let these polylines intersect, ...)

 

Command AD (for average distance)

 

What it does: it reads all vertices of both polylines (within the range of the blue polyline).  It draws vertical xlines, calculates the insersect points of the xlines to the polylines.

Now we have trapeziums.  Calculate sum area, divide by total horizontal distance

 

Feel free to comment out (foreach .. (entdel x) ) to see the xlines;

 

 

;; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/intersection-point-list/td-p/2065100
;; find insert points
(defun intpoints (obj1 obj2 / result intvar pt)
    (vl-load-com)
    (if (and obj1 obj2)
        (progn
            (setq intvar (vlax-invoke obj1 'Intersectwith obj2 0))
            (while (caddr intvar)
                (setq pt (list (car intvar) (cadr intvar) (caddr intvar)))
                (setq result (cons pt result))
                (setq intvar (cdddr intvar))
            )
        )
        (princ "\nSelection error..")
    )
    (if result
        (reverse result)
    )
)


(defun Line (p1 p2)
 (entmakex (list (cons 0 "LINE")
                 (cons 10 p1)
                 (cons 11 p2)
)))

(defun xLine (pt vec)
  (entmakex (list (cons 0 "XLINE")
                  (cons 100 "AcDbEntity")
                  (cons 100 "AcDbXline")
                  (cons 10 pt)
                  (cons 11 vec))))
                  

;; returns the vertices of a polyline.
;; The left endpoint is returned first, so sometimes the points get returned reversed                   
(defun vertices_xsorted (ent / vertex_lst)
  (setq vertex_lst nil)
  (foreach dp ent
    (if (= (car dp) 10)
     (setq vertex_lst (append vertex_lst (list (cdr dp))))
    )
  )
  ;; sort, maake sure the first point is on the left
  (if (< (nth 0 (nth 0 vertex_lst)) (nth 0 (last vertex_lst)) )
    vertex_lst
    (reverse vertex_lst)
  )
)

;; returns sorted x-values ...
(defun get_xvalues (top_pts bottom_pts minx maxx / pt xvalues)
  (setq xvalues (list))
  (foreach pt top_pts
    (if (and (<= (nth 0 pt) maxx) (>= (nth 0 pt) minx))
      (setq xvalues (append xvalues (list (nth 0 pt) )))
    )
  )
  (foreach pt bottom_pts
    (if (and (<= (nth 0 pt) maxx) (>= (nth 0 pt) minx))
      (setq xvalues (append xvalues (list (nth 0 pt) )))
    )
  )
  (vl-sort xvalues '<)
)


(defun c:ad ( / result pline1 pline2 top_pts bottom_pts minx maxx xvalues xlines x vlines i ins1 ins2 ins3 ins4 surfacesum)
  (setq pline1 (entsel "\nSelect the (green) top polyline: "))
  (setq pline2 (entsel "\nSelect the (blue) bottom polyline: "))

  ;; read all vertices
  (setq top_pts (vertices_xsorted (entget (car pline1))))
  (setq bottom_pts (vertices_xsorted (entget (car pline2))))
 
  ;; now collect all endpoints/vertices, both on the blue and green polyline
  ;; no need to get the vertices of the green polyline outside of the range of the blue polyline
  ;; we only need the x-value
  (setq minx (nth 0 (nth 0 bottom_pts)))
  (setq maxx (nth 0 (last bottom_pts)))

  (setq xvalues (get_xvalues top_pts bottom_pts minx maxx))
  ;; draw vertical xlines.  
  (setq xlines (list))
  (foreach x xvalues
    (setq xlines (append xlines (list
      (xLine (list x 0.0) (list 0.0 1.0 0.0))
    )))
  )
  ;; Now we have a series of trapeziums
  ;; sum the surfaces, divide by total horizontal distance
  (setq
    i 0
    surfacesum 0.0
  )
 
  (repeat (- (length xlines) 1)
    (setq ins1
      (intpoints
        (vlax-ename->vla-object (nth i xlines))
        (vlax-ename->vla-object (car pline1))
      )
    )
    (setq ins2
      (intpoints
        (vlax-ename->vla-object (nth i xlines))
        (vlax-ename->vla-object (car pline2))
      )
    )
    
    (setq ins3
      (intpoints
        (vlax-ename->vla-object (nth (+ i 1) xlines))
        (vlax-ename->vla-object (car pline1))
      )
    )
    (setq ins4
      (intpoints
        (vlax-ename->vla-object (nth (+ i 1) xlines))
        (vlax-ename->vla-object (car pline2))
      )
    )
    (setq surfacesum (+ surfacesum
      (*
        (+
          (distance (nth 0 ins1) (nth 0 ins2))                                                  ;; dist1
          (/ (- (distance (nth 0 ins3) (nth 0 ins4)) (distance (nth 0 ins1) (nth 0 ins2)) ) 2)  ;; (dist2 - dist1) / 2
        )
        (- (nth (+ i 1) xvalues ) (nth i xvalues))                                              ;; horizontal dist
      )
    ))
    
    (setq i (+ i 1))
  )
 
  ;; The result
  (setq result (/ surfacesum (- (last xvalues) (nth 0 xvalues))) )  
  ;; remove the X-lines
  (foreach x xlines
    (entdel x)
  )

  (princ "\nTotal surface: ")
  (princ surfacesum)
  (princ "\nAverage vertical distance: ")
  (princ result)
 
  (princ)
)
Edited by Emmanuel Delay
Posted

Thank you very much Emmanuel Delay

The lisp works as I want and well
Thanks again

  • Like 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...