Jump to content

Need a Lisp to write the length of a line


sofiane

Recommended Posts

Hello everyone, 

 

I really need a lisp please, to write the length of a line, when a draw it, add 1m and round up,

image.thumb.png.f92dab2196d8dbf2dd9a6d97881f0245.png

 

 

When i draw the blue line, where there "XM" i need the length of the line so here 631mm and add 1000m so total 1631mm. But I want to round up, so i want to write here 2m

 

Can someone help me please

 

Thanks

Edited by sofiane
Link to comment
Share on other sites

  • sofiane changed the title to Need a Lisp to write the length of a line

As a start, very rough. Like devitg need dwg.

 

(defun c:d1-2 ( / p1 p2 d)
(setq p1 (getpoint "\nPick point 1 ")
      p2 (getpoint "\nPick point 2 ")
)
(command "line" p1 p2 "")
(setq d (/ (+ 1000.0 (distance p1 p2)) 1000.0))
(setq intd (fix d))
(setq frac (- d (fix d)))
(if (< frac 0.5)
(progn
(setq frac 0.5)
(setq d (+ intd frac))
)
)
(setq txt (rtos d 2 0))
(setq p1 (getpoint "\nPick point for text "))
(command "text" p1 "50" "0" (rtos d 2 0))
(princ)
)
(c:d1-2)

 

Edited by BIGAL
Link to comment
Share on other sites

On 8/21/2021 at 4:36 AM, BIGAL said:

As a start, very rough. Like devitg need dwg.

 


(defun c:d1-2 ( / p1 p2 d)
(setq p1 (getpoint "\nPick point 1 ")
      p2 (getpoint "\nPick point 2 ")
)
(command "line" p1 p2 "")
(setq d (/ (+ 1000.0 (distance p1 p2)) 1000.0))
(setq intd (fix d))
(setq frac (- d (fix d)))
(if (< frac 0.5)
(progn
(setq frac 0.5)
(setq d (+ intd frac))
)
)
(setq txt (rtos d 2 0))
(setq p1 (getpoint "\nPick point for text "))
(command "text" p1 "50" "0" (rtos d 2 0))
(princ)
)
(c:d1-2)

 

HI Bigal thanks for your code, i tried it and i think it's working no ?? Can we add "m" for meter at the end of the text please ? 

Do you see another amelioration ? Thanks a lot !

Link to comment
Share on other sites

I tried to understand what you develop and i didn't understand this part

 

Quote

(setq intd (fix d)) 
(setq frac (- d (fix d)))
(if (< frac 0.5)
(progn
(setq frac 0.5)
(setq d (+ intd frac)) ) )

 

Can you explain me please

Edited by sofiane
Link to comment
Share on other sites

 

I just thought of an improvement that would also save me time.

 

I would like to be able to count the lengths of everything i just traced.

 

I don't know how to do it? Instead of writing a text, maybe insert a 2m, 3m etc... block and then count all the similar ? 

 

image.thumb.png.3eb824b0b1c2cc782b5953297713bab4.png

Link to comment
Share on other sites

2 hours ago, sofiane said:

Can you explain me please

(setq intd (fix d)) 
(setq frac (- d (fix d)))
(if (< frac 0.5)
(progn
(setq frac 0.5)
(setq d (+ intd frac)))) 

 

I think this basically rounds 1631mm to 2m like you asked.

 

This gives you total length of everything you select and outputs the length to the command prompt. it also copies the length to the clipboard so you can Ctrl+ V & paste it somewhere if you want.

 

;;----------------------------------------------------------------------;;
;; ADD UP TOTAL LENGHTS OF SELECTED OBJECTS & ADDS TO CLIPBOARD
(defun C:TLEN (/ e ss len i)
  (setq len 0.0 ss (ssget '((0 . "LINE,SPLINE,LWPOLYLINE,POLYLINE,ARC,CIRCLE,ELLIPSE"))))
  (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
    (setq len (+ len (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e))))
  )
  (princ (strcat "\nTotal Length = " (rtos len 2 3)))
  (vlax-invoke (vlax-get (vlax-get (vlax-create-object "htmlfile") 'ParentWindow) 'ClipBoardData) 'setData "TEXT" (rtos len 2 3))
  ;above copys to clipboard
  (princ)
)

 

Link to comment
Share on other sites

12 minutes ago, mhupp said:

(setq intd (fix d)) 
(setq frac (- d (fix d)))
(if (< frac 0.5)
(progn
(setq frac 0.5)
(setq d (+ intd frac)))) 

 

I think this basically rounds 1631mm to 2m like you asked.

 

 

Ok perfect, i want to learn autolisp, and i tried to understand the code. Thanks

Link to comment
Share on other sites

14 minutes ago, mhupp said:

This gives you total length of everything you select and outputs the length to the command prompt. it also copies the length to the clipboard so you can Ctrl+ V & paste it somewhere if you want.

 


;;----------------------------------------------------------------------;;
;; ADD UP TOTAL LENGHTS OF SELECTED OBJECTS & ADDS TO CLIPBOARD
(defun C:TLEN (/ e ss len i)
  (setq len 0.0 ss (ssget '((0 . "LINE,SPLINE,LWPOLYLINE,POLYLINE,ARC,CIRCLE,ELLIPSE"))))
  (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
    (setq len (+ len (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e))))
  )
  (princ (strcat "\nTotal Length = " (rtos len 2 3)))
  (vlax-invoke (vlax-get (vlax-get (vlax-create-object "htmlfile") 'ParentWindow) 'ClipBoardData) 'setData "TEXT" (rtos len 2 3))
  ;above copys to clipboard
  (princ)
)

 

 

 

Thanks for this code, but i don't really need this.

 

I have the first part of what I want, it's perfect => draw a line and have the length displayed.

 

I would like to have a summary of the texts, with a quantitative. I say texts, because it will be the real length. I don't want the length of the lines i drawed. 

Link to comment
Share on other sites

11 minutes ago, sofiane said:

 

 

Ok perfect, i want to learn autolisp, and i tried to understand the code. Thanks

First place to start LISP 

 

https://ronleigh.com/autolisp/

 

Despite it say OBSOLETE , it is actual for the very start to learn LISP , like your first learning book when you start School  at your  5 or 6 years old 

Link to comment
Share on other sites

24 minutes ago, devitg said:

If the distance is , guess 1.05 , would it be show as 2 ?

 

Yes exactly, something simple. I tried the code from BIGAL and it's working. 

 

my 2nd wish is now to count.

Link to comment
Share on other sites

29 minutes ago, devitg said:

put text on a dedicated layer . 

then do a ssget    

 

 

 

 

 

an ssget ?

Imagine that I have several lines drawn. A hundred for example.

Each line has a text, with "its length". The one I asked for, that is with 1 meter and rounded up.

 

Normally my lines never exceed 12 meters.

My wish is to have a table next to it, 2 columns, 13 lines (corresponding to the lengths from 2 to 12 m).

The idea is to indicate directly, the number of lines 3m, 4m etc... that I have.

a lisp that counts directly.

Link to comment
Share on other sites

when you want to apply a LISP , you have to think in Lisp . As LISP only obey order, it can not THINK . 

 

So all such text 3m 2m 12m ,shall be in a own layer   linelength ,  or there shall not be any other text not related with the lengths 

  (setq lengths-ss (ssget  "X" '(  (0 . "TEXT") ( 1 . "#m")))

(setq lengths-ss (ssget "X" '( (0 . "TEXT") ( 1 . "#m") (8 . " linelength"))))

 

So it will hold all text with digit and the m at layer named  linelength

 

 

 

 

 

Edited by devitg
Link to comment
Share on other sites

Devitg is correct put the text on 1 layer so you would use something like this.

 

(setq ss "X" (list (cons 0 "TEXT")(cons 8 "Mylayername")(cons 410 (getvar 'ctab))))
so now have all the text objects
would make a list of all the length
((2)(3)(4)(2)(2)(3)............
Then sort them 
((2)(2)(2)(2)(3)(3)(4)(4).....
Then do a count of matching objects
((2 4)(3 2)(4 2)...........
Put in table
All done

Need to think about it. 

 

Something confusing Units is set to feet but exemple is 685 long is this mm so want length in metres ? 0.685 round to 2.

Link to comment
Share on other sites

11 hours ago, BIGAL said:

Devitg is correct put the text on 1 layer so you would use something like this.

 


(setq ss "X" (list (cons 0 "TEXT")(cons 8 "Mylayername")(cons 410 (getvar 'ctab))))
so now have all the text objects
would make a list of all the length
((2)(3)(4)(2)(2)(3)............
Then sort them 
((2)(2)(2)(2)(3)(3)(4)(4).....
Then do a count of matching objects
((2 4)(3 2)(4 2)...........
Put in table
All done

Need to think about it. 

 

Something confusing Units is set to feet but exemple is 685 long is this mm so want length in metres ? 0.685 round to 2.

 

Yes, i'm working in mm and want after a txt in meter. 

Your code to draw a line and have a txt with meter is working great. 

Like i said, i need another part. I want a table, by piece, with number flexible

 

Here an example of my plan

 

image.thumb.png.f7acf9da051502c6b911ec118c4e07c3.png

 

 

 

I draw all the line with the lisp. Know i want a table like this 

 

image.thumb.png.e0f8173e108ab3b10325aaa275368e68.png

 

 

I'm making it manually right now, but i want to save time, and make it automatically we'll be great ! 

 

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