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

Labeling lines with their lengths,

18 REPLIES 18
SOLVED
Reply
Message 1 of 19
JamaL9722060
6923 Views, 18 Replies

Labeling lines with their lengths,

Labeling lines with their lengths,

 

I got the attached lisp file from the web which automatically labels the lines with their length. Could you please help in resulting the values of the length in the absolute values of the length instead of saying that the length is in inch. i.e, i  need to remove the inch sign (") from the suffix of the length.

 

Thank you in advance,

 

Best

 

Jamal

 

Clip_410.jpg

 

---------------------------
Jamal Numan
18 REPLIES 18
Message 2 of 19
3wood
in reply to: JamaL9722060

Please try ADDLINES.vlx and set the Units to meters.

 

3wood

CAD KITS

Message 3 of 19
JamaL9722060
in reply to: 3wood

sorry, i don't know how to use "ADDLINES.vlx"

 

just i need to play a bit with the lisp file to have the length in meter.

 

best

 

Jamal

---------------------------
Jamal Numan
Message 4 of 19
Kent1Cooper
in reply to: JamaL9722060


@JamaL9722060 wrote:

Labeling lines with their lengths,

 

.... Could you please help in resulting the values of the length in the absolute values of the length instead of saying that the length is in inch. i.e, i  need to remove the inch sign (") from the suffix of the length.

.... 


Change this:

 

(setq tStr (rtos (vla-get-length Obj) 3 2)

 

to this:

 

(setq tStr (rtos (vla-get-length Obj) 2 2)

 

to have it use decimal units instead of Engineering units.  If your drawing unit is a Meter, so that it's measuring the length in Meters, that change should be all you need.  See the AutoLISP Reference under (rtos).

Kent Cooper, AIA
Message 5 of 19
JamaL9722060
in reply to: Kent1Cooper

many thanks kent.

 

i did it but still it gives wrong length! what might be the problem?

 

Clip_411.jpg

 

---------------------------
Jamal Numan
Message 6 of 19
Kent1Cooper
in reply to: JamaL9722060


@JamaL9722060 wrote:

many thanks kent.

 

i did it but still it gives wrong length! what might be the problem?

.... 


A closer look at the code reveals that it's putting a 1 onto the beginning of the 2.00 actual value.  I think if you did more lines, the next one would have a 2 tacked onto the beginning of it, and the next one a 3, etc.  I haven't studied it enough to know what that might be for (I'm guessing to sequence the lengths, but it seems to be used strangely), but I think you can get rid of it.  Using your image for the values mentioned:

 

....

;;  (setq x 1) ; ELIMINATE THIS [no use for the X variable except for that tacked-on initial character]
....

    (setq tStr (rtos (vla-get-length Obj) 2 2) ; THIS [original 3 changed to a 2] RETURNS THE "2.00" PART
....

;;  (setq rnum (rtos X 2 0)) ; ELIMINATE THIS [TURNS THAT INTEGER 1 INTO A STRING "1"]
;;  (setq tstr (strcat " " rnum  tStr)) ; ELIMINATE THIS [joins a space and "1" and "2.00" together] 
....

;;  (setq x (+ x 1)) ; ELIMINATE THIS

 

I would think if the intent is to put sequence numbers before the length values, it would put the space between rnum and tStr to separate them, but again, I haven't dug very deeply into it.  But eliminating that (strcat) should leave the "2.00" alone as the text content.

Kent Cooper, AIA
Message 7 of 19
Hallex
in reply to: JamaL9722060

Jamal

Here is working program I use

See if this good enough on your end

{code}

;;------------------------------------ DIMLP.LSP ----------------------------------;;

;; fixo () 2012 * all rights released
;; edited 3/3/12

(defun C:DIMLP(/ *error* acsp adoc ang curve deriv en mid mp ppt1 ppt2 prex sset txh txt txt1 txtpt1)
 
    (defun *error* (msg)
      (vla-endundomark (vla-get-activedocument
              (vlax-get-acad-object))
       )
    (cond ((or (not msg)
        (member msg '("console break" "Function cancelled" "quit / exit abort"))
        )
    )
   ((princ (strcat "\nError: " msg)))
   )

    (princ)
    )
 
(setq adoc (vla-get-activedocument
              (vlax-get-acad-object))
      acsp (vla-get-block(vla-get-activelayout adoc)))
 

  (vla-startundomark adoc )
 
(setq txh (getvar "dimtxt")
     
      prex (getvar "dimdec")

      )
 
(while (not sset)
 
    (setq sset (ssget '((0 . "*LINE")))
  
   )
  )
 
(while (setq en (ssname sset 0))
 
  (setq curve (vlax-ename->vla-object en))
 
  (setq txt (if (= (getvar "measurement") 0)
      
       (rtos (vla-get-length curve) 3 2)
      
       (rtos (vla-get-length curve) 2 prex))

 )
 
  (setq mid (/ (abs (- (vlax-curve-getendparam curve)
                           (vlax-curve-getstartparam curve))) 2.)
 
 mp (vlax-curve-getpointatparam curve mid)

 deriv  (vlax-curve-getfirstderiv
   curve
   (vlax-curve-getparamatpoint curve mp))
 )

  (if (zerop (cadr deriv))
    (setq ang 0)
    (setq ang (- (/ pi 2) (atan (/ (car deriv) (cadr deriv)))))
    )

    (if (< (/ pi 2) ang (* pi 1.5))
    (setq ang (+ pi ang))
    )
  (setq ppt1 (polar mp (+ ang (/ pi 2)) (* txh 0.5))
 )
 
  (setq txtpt1  (vlax-3d-point (trans ppt1 1 0)))

  (setq txt1 (vla-addtext acsp txt txtpt1 txh))
 
  (vla-put-alignment txt1 acalignmentbottomcenter)
 
  (vla-put-textalignmentpoint txt1 txtpt1)
 
  (vla-put-insertionpoint txt1 (vla-get-textalignmentpoint txt1))
 
  (vla-put-rotation txt1 ang)

(ssdel en sset)

  )
 
  (*error* nil)
 
  (princ)
  )
(princ "\n\t---\tStart command with \"DIMLP\"\t---")
(princ)
(or (vl-load-com)
    (princ))
;;------------------------------------ code end ----------------------------------;;

{code}

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 8 of 19
JamaL9722060
in reply to: Hallex

Thank you guys for the help.

 

I’m not sure guys if your time permits to send me the lisp file (that does the labeling) with the correct code. I got totally frustrated in trying to do it. It seems not to work with me.

 

Appreciated

 

Jamal

---------------------------
Jamal Numan
Message 9 of 19
stevesfr
in reply to: JamaL9722060

Message #7 works like a charm here !   just perfect.

Message 10 of 19
JamaL9722060
in reply to: stevesfr

Perfect. finally it did work. just i needed to replace the "2" by "3" as Kent advised to have the kength without the inch sign.

 

(rtos (vla-get-length curve) 2 2)

 

many thanx Hallex. that's great.

 

best

 

Jamal

---------------------------
Jamal Numan
Message 11 of 19
Hallex
in reply to: JamaL9722060

Glad you got it to work

Happy computing 🙂

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 12 of 19
Hallex
in reply to: stevesfr

Thanks for the support bro

Cheers 🙂

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 13 of 19
JamaL9722060
in reply to: JamaL9722060

Hi Hallex,

 

It is me again.

 

Could you please indicate how can I modify the code so that:

 

  1. I can add some prefix and suffix to the length. For example, I need the number to appear as L=3.5m
  2. How to have the length under the line or to the right of the line?
  3. How to control the gap between the line and the length text? To be for example 0.15 meter

 

Many thanks

 

Jamal

 

Clip_428.jpg

 

Clip_429.jpg

 

Clip_430.jpg

 

---------------------------
Jamal Numan
Message 14 of 19
Hallex
in reply to: JamaL9722060

Try this code instead

{code}

;;------------------------------------ DIMLP.LSP ----------------------------------;;

;; fixo () 2012 * all rights released ;; edited 3/3/12 ;; edited 3/5/12

(defun C:DIMLP(/ *error* acsp adoc ang curve deriv en mid mp ppt1 ppt2 prex sset txh txt txt1 txtpt1)       (defun *error* (msg)       (vla-endundomark (vla-get-activedocument               (vlax-get-acad-object))        )     (cond ((or (not msg)         (member msg '("console break" "Function cancelled" "quit / exit abort"))         )     )    ((princ (strcat "\nError: " msg)))    )

    (princ)     )   (setq adoc (vla-get-activedocument               (vlax-get-acad-object))       acsp (vla-get-block(vla-get-activelayout adoc)))

  (vla-startundomark adoc )   (setq txh (getvar "dimtxt")             prex (getvar "dimdec")

      )   (while (not sset)       (setq sset (ssget '((0 . "*LINE")))       )   )   (while (setq en (ssname sset 0))     (setq curve (vlax-ename->vla-object en))     (setq txt (if (= (getvar "measurement") 0)               (rtos (vla-get-length curve) 3 2)               (rtos (vla-get-length curve) 2 prex))

 )     (setq mid (/ (abs (- (vlax-curve-getendparam curve)                            (vlax-curve-getstartparam curve))) 2.)    mp (vlax-curve-getpointatparam curve mid)

 deriv  (vlax-curve-getfirstderiv    curve    (vlax-curve-getparamatpoint curve mp))  )

  (if (zerop (cadr deriv))     (setq ang 0)     (setq ang (- (/ pi 2) (atan (/ (car deriv) (cadr deriv)))))     )

    (if (< (/ pi 2) ang (* pi 1.5))     (setq ang (+ pi ang))     ) ;;;  (setq ppt1 (polar mp (+ ang (/ pi 2)) (* txh 0.5)) ;;; )   (setq ppt1 (polar mp (+ ang (/ pi 2)) 0.15)  )   (setq txtpt1  (vlax-3d-point (trans ppt1 1 0)))

;;;  (setq txt1 (vla-addtext acsp txt txtpt1 txh))

  (setq txt1 (vla-addtext acsp (strcat "L=" (strcat txt "m")) txtpt1 txh))     (vla-put-alignment txt1 acalignmentbottomcenter)     (vla-put-textalignmentpoint txt1 txtpt1)     (vla-put-insertionpoint txt1 (vla-get-textalignmentpoint txt1))     (vla-put-rotation txt1 ang)

(ssdel en sset)

  )     (*error* nil)     (princ)   ) (princ "\n\t---\tStart command with \"DIMLP\"\t---") (princ) (or (vl-load-com)     (princ)) ;;------------------------------------ code end ----------------------------------;;

{code}

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 15 of 19
3wood
in reply to: JamaL9722060

Please try ADDLINES.vlx, LAYERS.vlx and ALTEXT.vlx, use command APPLOAD to load vlx file if you don't know how to use vlx file in AutoCAD.

Following steps below to quickly label all lines as you expected.

1. Set up a new layer and set it as the current layer.

2. Set up proper Units and Precision (use command UNITS)

3. Use command ADDLINES to add length notation to all lines/plines. In the set up dialogue, do not select "Show bearing" and "Segment number", input a negative number in "Gap between text and object", you may need try different gap values to get desired result. Ensure "Orientation" is "Aligned"

4. Use command LI in LAYERS.vlx to isolate the new layer.

5. Use command ALTEXT to add prefix "L = "and suffix " m" to all new notations.

6. Use command LAYERP to retrieve previous layer status.

The result is exactly what you need, with prefix and suffix, with length notation at the bottom and right side of the object, and also you can control the gap by adjusting the gap value in Step 3.

 

3wood

CAD KITS

 

 

Message 16 of 19
JamaL9722060
in reply to: Hallex

Hi Hallex,

 

the code provided sounds not to work. i got the error shown in the screenshot below

 

best

 

Jamal

 

Clip_434.jpg

---------------------------
Jamal Numan
Message 17 of 19
Hallex
in reply to: JamaL9722060

Try again I added hard code gap value of 0.15

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 18 of 19
phanaem
in reply to: JamaL9722060

As an alternative you can use a Dynamic Block.

Take care of scale.  The correct way is to use it at scale 1, uniform.

It has a huge advantage if you need to extract data and put it in a table.

 

Sample.PNG

Message 19 of 19
JamaL9722060
in reply to: Hallex

Many thanks Hallex.

 

The lisp file working very will but it does still show the inch sign (") and doesn't show the length UNDER or to the RIGHT of the measured line.

 

how can we decrease/increase the gap?

 

best

 

Jamal

 

Clip_453.jpg

---------------------------
Jamal Numan

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

Post to forums  

Autodesk Design & Make Report

”Boost