Author Topic: distribute point along a defined range in a curve  (Read 1456 times)

0 Members and 1 Guest are viewing this topic.

Shay Gaghe

  • Newt
  • Posts: 89
distribute point along a defined range in a curve
« on: July 12, 2015, 07:23:49 AM »
I wrote a code to distribute point  along a defined range in a curve. i want the points to be distributed evenly.
i tried to divide the distance between the start and end parameters by 90% percent of it,
i did it because i wanted my routine to be scalable and fit all curve lengths.

points are not evenly distributed and go beyond the end parameter.

cant figur out what is going wrong.

Code: [Select]
(defun destribute (obj strPrm endPrm  /  d w ppt ppts d pv x)
  (setvar "osmode" 2048)
  (if (setq obj (vlax-ename->vla-object obj))
      (progn
      (setq d (vlax-curve-getDistAtParam obj strPrm)                         ;_get the distance from curv start to strPrm
    w (- (vlax-curve-getDistAtParam obj endPrm) d)                   ;_get the distance from strPrm to endPrm
    x (/ w (/ (* w 90)100)))                                         ;_divide w by 90 % of it
      (setq pv strPrm)
     (while (< pv endPrm)                                            ;_while pv is smaller than endPrm
     
       (setq pv (vlax-curve-getParamAtDist obj d))                   ;_get the parameter point  at d
       (setq d (+ d x))                                              ;_add pv to list
       (setq ppts (cons (vlax-curve-getPointAtParam obj pv) ppts))   ;_walk d x units as d
       
       

)
      )
    )
 
  ppts
  )

please help
S


kpblc

  • Bull Frog
  • Posts: 396
Re: distribute point along a defined range in a curve
« Reply #1 on: July 12, 2015, 09:30:27 AM »
Perhaps the reason is
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq value 12)
  2. 12
  3. _$ (/ (* value 90) 100)
  4. 10
  5. _$ (/ (* value 90.) 100)
  6. 10.8
Sorry for my English.

hmspe

  • Bull Frog
  • Posts: 362
Re: distribute point along a defined range in a curve
« Reply #2 on: July 12, 2015, 10:29:46 AM »
Are the values for strPrm and endPrm really parameters?  See http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/what-is-a-the-parameter-of-a-curve/td-p/896825 and http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/vlax-curve-getparam-functions-what-are-they/td-p/789993

If you are using points to specify the ends of the range you want points in I would do something like this
Code: [Select]
(defun distribute (object start_point end_point point_count / point_list start_param
                   end_param temp start_dist span interval counter points)
  (if (vle-enamep object)
    (setq object (vlax-ename->vla-object object))
  )
  (if object
    (progn
      (setq point_list (list
                         (vlax-curve-getStartPoint object)
                         (vlax-curve-getEndPoint object)
                       )
      )
      (if (equal start_point (car point_list) 1E-5)
        (setq start_param (vlax-curve-getStartParam object))
        (if (equal start_point (cadr point_list) 1E-5)
          (setq start_param (vlax-curve-getEndParam object))
          (setq start_param (vlax-curve-getParamAtPoint
                              object
                                (vlax-curve-getClosestPointTo object start_point)
                            )
          )
        )
      ) 
      (if (equal end_point (car point_list) 1E-5)
        (setq end_param (vlax-curve-getStartParam object))
        (if (equal end_point (cadr point_list) 1E-5)
          (setq end_param (vlax-curve-getEndParam object))
          (setq end_param (vlax-curve-getParamAtPoint
                              object
                                (vlax-curve-getClosestPointTo object end_point)
                            )
          )
        )
      ) 
      (if (and start_param
               end_param
          )
        (if (> start_param end_param)
          (setq temp        end_param
                end_param   start_param
                start_param temp
          )
        )
      )
      (setq start_dist (vlax-curve-getDistAtParam object start_param)
            span       (- (vlax-curve-getDistAtParam object end_param)
                          start_dist
                       )   
            interval   (/ span (1- point_count))
      )
      (setq counter 0)
      (while (< counter point_count)
        (setq temp   (vlax-curve-getParamAtDist object (+ start_dist (* interval counter)))
              points (cons (vlax-curve-getPointAtParam object temp) points)
        )
        (setq counter (1+ counter))
      )
      (setq points (reverse points))
    )
  )
  points
)

(defun c:test ( / object start_param end_param span point1 point2)
  (setq object      (car (entsel "\nSelect object: "))
        object      (vlax-ename->vla-object object)
        start_param (vlax-curve-getStartParam object)
        end_param   (vlax-curve-getEndParam object)
        span        (vlax-curve-getDistAtParam object end_param)
        point1      (vlax-curve-getPointAtDist object (/ span 3.0))
        point2      (vlax-curve-getPointAtDist object (* (/ span 3.0) 2.0))
  )
  (distribute object point1 point2 7)
"Science is the belief in the ignorance of experts." - Richard Feynman

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: distribute point along a defined range in a curve
« Reply #3 on: July 13, 2015, 04:04:36 AM »
@ Shay:
I think there are two issues:
1.
The test expression of the while loop tests the *old* value of pv.
This can easily be fixed:
Change this:
Code: [Select]
(while (< pv endPrm)
  (setq pv (vlax-curve-getparamatdist obj d))
  ...
)
To this:
Code: [Select]
(while (< (setq pv (vlax-curve-getparamatdist obj d)) endPrm)
  ...
)
2.
The point are evenly distributed *along* the curve. So to check the output you should not measure the linear distance from point to point.