Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

running distance lisp

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
richie_hodgson
757 Views, 13 Replies

running distance lisp

Hi I am thinking aloud on this one and would like to see if I am on the right track

 

 (setq distlist (list' 0)); set first distance on list as 0
 (while
 (setq pt1 (nth pointslist)); get point n from list of points
 (setq pt2 (+1 nth pointslist));get next point from the list
 (setq dist (+dist (distance pt1 pt2))); work out the distance between the points and add it to the previous
 (setq distlist (cons dist)); add that distance to the list
 ) move nth to the next one
 (reverse distlist) make the list run the right direction

 

 

Richie
13 REPLIES 13
Message 2 of 14
pbejse
in reply to: richie_hodgson

Not sure what you want to do, Is this just an lisp exercise? What exactly is the end result? the Total length? 

 

 

Message 3 of 14
richie_hodgson
in reply to: pbejse

Should end up with a list like (  0 10 30 60 100 ), first join is 0 as it is the start point.  "joining" point 1 and point 2 from my list I get 10 as shown , "joining" point 2 and point 3 from my list i get 20 add this to 10 and I get 30 as shown this carries on till I get to the end of the points list. I want to do a further calculation based on that running distance list so I need each one not just the total. Note these are notional values as my points list comes from any selected polyline. 

Richie
Message 4 of 14
pbejse
in reply to: richie_hodgson

HYG [as per your posted code]

 

   (setq distlist (list '0)			; set first distance on list as 0
	 n	  0				; counter for nth function
	 dist	  0				; dist as a non nil value
   )

 (while (and
	(setq pt1 (nth  n pointslist)) 		; get point n from list of points
	
	(setq pt2 (nth (1+ n) pointslist)) 	; get next point from the list
		)
 	(setq dist (+ dist (distance pt1 pt2)))	; work out the distance between the points
   						; and add it to the previous
   
 	(setq distlist (cons dist distlist))	; add that distance to the list
   
	(setq n (1+ n)) 			; move nth to the next one
   )						; end while
  
 (reverse distlist) ;make the list run the right direction

 HTH

 

Message 5 of 14
richie_hodgson
in reply to: pbejse

Cool thanks thats just wonderfull.
Richie
Message 6 of 14
pbejse
in reply to: richie_hodgson


@richie_hodgson wrote:
Cool thanks thats just wonderfull.

You are welcome, there is a couple of ways to accomplish that desired result, heres one approach.

 

(setq distlist (cons (setq dist 0)
		 (mapcar '(lambda (p1 p2)
			    (setq dist (+ dist (distance p1 p2)))
			  )
			 pointslist
			 (cdr pointslist)
		 )
	   )
)

HTH

 

Message 7 of 14

Hi thanks, the second option looks interesting if it achieves the same thing. One thing I forgot is that the distance should be absolute, never negative so I am unsure where to put the ABS statement into the second code option
Richie
Message 8 of 14
pbejse
in reply to: richie_hodgson


@richie_hodgson wrote:
Hi thanks, the second option looks interesting if it achieves the same thing. One thing I forgot is that the distance should be absolute, never negative so I am unsure where to put the ABS statement into the second code option

I maybe wrong, but i dont see why the distance would result to a negative value, so there's no need for abs function, UNLESS, the value of dist is a negative value from the get-go. The code i posted should start wih 0. Can you give me an example of a negative value result. 

 

Message 9 of 14

Ok if for example pt2 was lower and to the left of pt1, would distance pt1 pt2 result in a negative
Richie
Message 10 of 14
pbejse
in reply to: richie_hodgson


@richie_hodgson wrote:
Ok if for example pt2 was lower and to the left of pt1, would distance pt1 pt2 result in a negative

No. not really, Look here:

 

(setq pt1 '(394.31 70.9552 0.0));<-- lower point


(setq pt2 '(600.145 315.343 0.0)):<-- higher point


(distance pt1 pt2) -----> 319.52


(distance pt2 pt1) -----> 319.52

Message 11 of 14
doni49
in reply to: pbejse

To expand on this just a little so you can understand a little better.....

 

 

A distance is just that a DISTANCE.  If it were to include a negative, that would indicate DIRECTION from a specific location. 



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 12 of 14


@richie_hodgson wrote:

Should end up with a list like (  0 10 30 60 100 ), first join is 0 as it is the start point.  "joining" point 1 and point 2 from my list I get 10 as shown , "joining" point 2 and point 3 from my list i get 20 add this to 10 and I get 30 as shown this carries on till I get to the end of the points list. I want to do a further calculation based on that running distance list so I need each one not just the total. Note these are notional values as my points list comes from any selected polyline


You can get that as a series of distances-so-far directly from the Polyline itself, rather than finding a batch of segment lengths as separate numbers and adding them up as you go along.  For a Lightweight Polyline:

 

(setq
  plent (car (entsel "\nSelect Polyline: "))
  verts (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget plent))); your points list
); setq
(foreach vert verts
  (setq distlist (cons (vlax-curve-getDistAtPoint plent vert) distlist)); put distance along the Polyline at each vertex into list
); foreach
(reverse distlist)

 

If you need them as integers as in your example [the above returns real numbers], that can be done in a couple of ways.  If you need to do it with "heavy" 2D or 3D Polylines, that can also be done with a different approach to the points list, but if you already have that list, you can plug it and the entity name in and just use the (foreach) part of the above.  And if you need the overall length at the end for a closed Polyline [the above would not include that], that can easily be added.

Kent Cooper, AIA
Message 13 of 14
richie_hodgson
in reply to: pbejse

Ok cool, just wasn't sure if the result of distance would always be positive. Thanks very much for your help
Richie
Message 14 of 14

Thats a nice alternative, since I have already selected the lwpoly in the previous code you sent me I can do a little tweaking.
Richie

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report