Jump to content

LISP to get length of single line


martyn3200

Recommended Posts

I have searched high and low for an answer to this probably simple question. How do I get the length of a line in LISP? The best I could come up with was using the LIST command in VLISP to get the object properties, but as this is a command and not a native lisp function am I right in thinking you cannot manipulate the lisp with CADR NTH etc.? Forgive my naivety. Any help will be much appreciated. I'm sure I'm missing something simple.

Link to comment
Share on other sites

There are lisp routines that will return line lengths.

 

Output from one such routine.

 

LineLengthMessage.JPG

 

The lisp routine was written by Ravikumar Vermana and posted in his blog on October 21, 2011. Do you need to see the code?

Link to comment
Share on other sites

If the OP only wants the length of a single line what's the need for lisp in the first place? Isn't the length just as easily obtained via the LIST command or clicking on the line then looking at Quick Properties or Properties? Sometimes people request lisp routines where there doesn't seem to be a real need for them.

Link to comment
Share on other sites

If the OP only wants the length of a single line what's the need for lisp in the first place? Isn't the length just as easily obtained via the LIST command or clicking on the line then looking at Quick Properties or Properties? .

 

Most of people start asking for a very bit piece of codes at the beginning and that request would become very soon as a big ball of snow rolling down from a high mountain . :lol:

I think you are aware enough of these things and we might have this imaginary sight come true in this thread , who knows ? :rofl:

Link to comment
Share on other sites

Thanks for the responses guys. I have seen the lisp routines that gives you the lengths of multiple lines, but I am looking for just a simple bit of code to get the length of just one line to use in a lisp routine I'm working on.

 

(defun c:thirdline () ;routine to get the length of a line, divide it by 3 then print the result

 

(setq sset (ssget))

(setq len (***GET LINE LENGTH***))

(princ (/ len 3))

(princ)

)

Link to comment
Share on other sites

Have a play with this :)

 

(defun c:foo ( / s e )
 (princ "\n Pick on one line to get its length :")
 (if (setq s (ssget "_+.:S:E" '((0 . "LINE"))))
     (princ (strcat "\n Total Length is : " (rtos (distance (cdr (assoc 10 (setq e (entget (ssname s 0))))) (cdr (assoc 11 e))) 2)))
   )
 (princ)
 )

Link to comment
Share on other sites

As a primer to create a robust function :

 

 

[b][color=BLACK]([/color][/b]defun linelength [b][color=FUCHSIA]([/color][/b]e / ed p10 p11 d2 d3[b][color=FUCHSIA])[/color][/b]
[b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]= [b][color=MAROON]([/color][/b]type e[b][color=MAROON])[/color][/b] 'ENAME[b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]setq ed [b][color=MAROON]([/color][/b]entget e[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]= [color=#2f4f4f]"LINE"[/color] [b][color=MAROON]([/color][/b]cdr [b][color=GREEN]([/color][/b]assoc 0 ed[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]setq p10 [b][color=MAROON]([/color][/b]cdr [b][color=GREEN]([/color][/b]assoc 10 ed[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
           p11 [b][color=MAROON]([/color][/b]cdr [b][color=GREEN]([/color][/b]assoc 11 ed[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]setq d3 [b][color=MAROON]([/color][/b]distance p10 p11[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]setq d2 [b][color=MAROON]([/color][/b]distance [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]car p10[b][color=BLUE])[/color][/b] [b][color=BLUE]([/color][/b]cadr p10[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                        [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]car p11[b][color=BLUE])[/color][/b] [b][color=BLUE]([/color][/b]cadr p11[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
d3[b][color=BLACK])[/color][/b]

[color=#8b4513]; [b][color=BLACK]([/color][/b]defun       [b][color=BLACK])[/color][/b] : define a local function[/color]
[color=#8b4513];  [b][color=BLACK]([/color][/b]e /          : supply 1 parameter to the function[/color]
[color=#8b4513];  / ed p10 p11 d2 d3[b][color=BLACK])[/color][/b] : declare  variables local to this function only[/color]
[color=#8b4513];  [b][color=BLACK]([/color][/b]and        [b][color=BLACK])[/color][/b] : inclusive testing - continue evaluating until a nil is returned or test complete[/color]
[color=#8b4513];  [b][color=BLACK]([/color][/b]type       [b][color=BLACK])[/color][/b] : test for the the type of the parameter[/color]
[color=#8b4513];  [b][color=BLACK]([/color][/b]entget     [b][color=BLACK])[/color][/b] : get an entity definition[/color]
[color=#8b4513];  [b][color=BLACK]([/color][/b]distance   [b][color=BLACK])[/color][/b] : calculate the true distance between 2 points[/color]
[color=#8b4513];                : if both points have X Y & Z values, return the 3D distance[/color]
[color=#8b4513];                : if either point has only X & Y values, return the 2D distance[/color]
[color=#8b4513];  d3            : return the 3D value or nil if an error occurred[/color]

 

 

Usage :

 

Draw a line ->

[b][color=BLACK]([/color][/b]prin1 [b][color=FUCHSIA]([/color][/b]linelength [b][color=NAVY]([/color][/b]entlast[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

LINE Point values are always stored in WCS so translations are not required

 

 

HTH -David

Edited by David Bethel
Link to comment
Share on other sites

Nice one guys. Managed to get it working with the following code. David, I'm afraid a lot of you response went over my head. My working knowledge of AutoLISP is fairly limited!

 

Anyway, the following code sufficed in the end:

 

(defun c:thirdline ( / s e ) ;routine to get the length of a line, divide it by 3 then print the result

 (if (setq s (ssget "_+.:S:E" '((0 . "LINE"))))
     (setq s1 (rtos (distance (cdr (assoc 10 (setq e (entget (ssname s 0))))) (cdr (assoc 11 e))) 2))
   )
 (setq s2 (atof s1))
 (setq s3 (/ s2 3))
 (princ s3)
 (princ)
 )

Link to comment
Share on other sites

;routine to get the length of a line, divide it by 3 then print the result

 

vlisp

(defun c:div3 (/ e)
 (if (and (setq e (car (entsel "\nPick line..\n")))
   (vlax-property-available-p (setq e (vlax-ename->vla-object e)) 'length)
   ) 
   (princ (rtos (/(vla-get-length e)3.0)2 3))
   (princ "\noops.. ")
   ) 
 (princ)
 ) 
(vl-load-com)

 

 

Have a play with this :)

(defun c:foo ......)

Tharwat as usual quick and simple :thumbsup:

 

Should we note that the length result is in 3D ?

some people working in 2D may use any lisp without study the codes.

if screwed up , lisper gets blamed? :danger:

Link to comment
Share on other sites

Tharwat as usual quick and simple :thumbsup:

 

Thank you hanhphuc :)

 

if screwed up , lisper gets blamed? :danger:

 

The user would be screwed up if they did not pick a line or an object that does not have a length property ;)

Link to comment
Share on other sites

Martyn3200 have a look at "dxf codes" for an object there are a series of code that return properties of an object. AS an Example for a line

0 object type Line

8 layer

10 start point

11 end point

 

0 object type Block

2 name of block

8 layer

10 insertion point

 

In VL download Dumpit.lsp and load it, pick your line you will see a list of various information like 'length pick a pline and 'AREA will return just that using code above. Using a COND you can have one program to return lots of info depending on the object type, ready for you to use what about 'Angle.

Link to comment
Share on other sites

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...