Jump to content

Need help with an Autolisp function to export a line length and then round up or down and then add a foot symbol to the exported text


Recommended Posts

Posted

Hello all! First post on here. I'm starting to dive into using autolisp commands, and while I can somewhat understand them I'm currently  too inexperienced to code anything myself. What I'm asking for is a function to take a line, create MText for the lines length, round up or down (.5 up and .4 down) and add a foot symbol,  then align the m text based off of the lines rotation. Thanks for any help you all are able to provide. Also, showing me how to set it to a certain text style and color would be appreciated as well but not necessary. 

Posted

Did you google "label lines plines" lots of examples already done, once you find google "rounding numbers lisp" lots also out there.

Posted (edited)

You're never too inexperienced to start coding. If you can use autocad, you can code, as in it's basic form, it is a list of command statements grouped together and executed in a linear sequence. It will work or fail depending on the logic of the sequence.

 

Why MText and not Text? Does the "Text" have to be readable ? Where is the text placed in relation to the line? How is the drawing set up (units etc) and are they consistent ? A simple sample drawing showing what is required always helps, saved in autocad 2007 format so (almost) everyone can open it. :thumbsup:

 

Text Style and color involve changing properties.

 

Without delving into visual lisp you can use the following code to find the properties of entities

 

(defun c:dumpit ( / ent)
  (dumpallproperties (car (entsel "\nSelect Entity to Dump : ")) 1)
);end_defun


You can cut and paste to the command line to load it, then type dumpit to run

I suggest you read the reference HERE as it links to getting/setting etc

Edited by dlanorh
Posted

Almost forgot look into Mtext, Fields does what you want and its all Autocad no coding.

Posted
11 hours ago, dlanorh said:

You're never too inexperienced to start coding. If you can use autocad, you can code, as in it's basic form, it is a list of command statements grouped together and executed in a linear sequence. It will work or fail depending on the logic of the sequence.

 

Why MText and not Text? Does the "Text" have to be readable ? Where is the text placed in relation to the line? How is the drawing set up (units etc) and are they consistent ? A simple sample drawing showing what is required always helps, saved in autocad 2007 format so (almost) everyone can open it. :thumbsup:

 

Text Style and color involve changing properties.

 

Without delving into visual lisp you can use the following code to find the properties of entities

 


(defun c:dumpit ( / ent)
  (dumpallproperties (car (entsel "\nSelect Entity to Dump : ")) 1)
);end_defun


You can cut and paste to the command line to load it, then type dumpit to run

I suggest you read the reference HERE as it links to getting/setting etc

 

I guess text or MText makes no difference. It does have to be readable, I need it to actually show up in the drawings. The text placement doesn't really matter too much, I can just manipulate it after it generates, but centered above the line and at the same rotation would work. Here is an oversimplified example of what I would like. Thank you for the lisp example, that seems like it should help a lot. 

line length example.dwg

Posted
11 hours ago, BIGAL said:

Almost forgot look into Mtext, Fields does what you want and its all Autocad no coding.

 

I googled some stuff and I found a pretty similar lisp file to what I want it to do, but it isn't perfect. I can post it here. Using fields is something I didn't think of, I'll have to look into that.

 

(defun c:LINELENGTH ( / CONST_STYLE CONST_TXT-HT CONT COUNT DIST ENT IP LAYER ROT SS)
(if (setq ss (ssget))
(progn
(setq count 0)
(repeat (sslength ss)
(setq cont (entget (ssname ss count)))
(if (= "LINE" (cdr (assoc 0 cont)))
(progn
(setq CONST_txt-ht 4
CONST_style "Prop"
layer (cdr (assoc 8 cont))
ip (cdr (assoc 10 cont))
rot (angle (cdr (assoc 10 cont)) (cdr (assoc 11 cont)))
dist (distance (cdr (assoc 10 cont)) (cdr (assoc 11 cont)))
ent (list
(cons 0 "MTEXT")
(cons 100 "AcDbEntity")
(cons 8 layer)
(cons 100 "AcDbMText")
(cons 10 ip)
(cons 40 CONST_txt-ht)
(cons 41 dist)
(cons 71 7)
(cons 72 5)
(cons 1 (rtos dist 2))
(cons 7 CONST_style)
(list 210 0.0 0.0 1.0)
(cons 11 (cdr (assoc 11 cont)))
(cons 50 rot)
(cons 73 1)
(cons 44 1.0)
)
)
(entmake ent)
)
)
(setq count (1+ count))
)
)
)
)
 

 

Posted

Try this, minimally tested.

 

(defun c:LINELENGTH ( / ss cnt ent len ang str m_pt t_pt n_ent)
  (setq ss (ssget  '((0 . "LINE"))))
  (repeat (setq cnt (sslength ss))
    (setq ent (ssname ss (setq cnt (1- cnt)))
          len (getpropertyvalue ent "length")
          ang (getpropertyvalue ent "angle")
          str (strcat (rtos len 2 0) "\'")
    )
    (if (< (/ pi 2) ang (* (/ pi 2) 3)) (setq ang (+ ang pi)))
    (setq m_pt (mapcar '(lambda (x y) (/ (+ x y) 2.0)) (vlax-curve-getstartpoint ent) (vlax-curve-getendpoint ent))
          t_pt (polar m_pt (+ ang (/ pi 2)) 1.0)
    )
    (setq n_ent (entmakex (list
                            (cons 0 "MTEXT")
                            (cons 100 "AcDbEntity")
                            (cons 8 "NOTES")
                            (cons 100 "AcDbMText")
                            (cons 10 t_pt)
                            (cons 40 4.0)
                            (cons 41 len)
                            (cons 71 8)
                            (cons 72 5)
                            (cons 1 str)
                            (cons 7 "PROP")
                            ;(cons 11 t_pt)
                            (cons 50 ang)
                            (cons 73 1)
                            (cons 44 1.0)
                          )
                )
    )
  )
  (princ)
)

 

  • Thanks 1
Posted
1 hour ago, dlanorh said:

Try this, minimally tested.

 


(defun c:LINELENGTH ( / ss cnt ent len ang str m_pt t_pt n_ent)
  (setq ss (ssget  '((0 . "LINE"))))
  (repeat (setq cnt (sslength ss))
    (setq ent (ssname ss (setq cnt (1- cnt)))
          len (getpropertyvalue ent "length")
          ang (getpropertyvalue ent "angle")
          str (strcat (rtos len 2 0) "\'")
    )
    (if (< (/ pi 2) ang (* (/ pi 2) 3)) (setq ang (+ ang pi)))
    (setq m_pt (mapcar '(lambda (x y) (/ (+ x y) 2.0)) (vlax-curve-getstartpoint ent) (vlax-curve-getendpoint ent))
          t_pt (polar m_pt (+ ang (/ pi 2)) 1.0)
    )
    (setq n_ent (entmakex (list
                            (cons 0 "MTEXT")
                            (cons 100 "AcDbEntity")
                            (cons 8 "NOTES")
                            (cons 100 "AcDbMText")
                            (cons 10 t_pt)
                            (cons 40 4.0)
                            (cons 41 len)
                            (cons 71 8)
                            (cons 72 5)
                            (cons 1 str)
                            (cons 7 "PROP")
                            ;(cons 11 t_pt)
                            (cons 50 ang)
                            (cons 73 1)
                            (cons 44 1.0)
                          )
                )
    )
  )
  (princ)
)

 

That seemed to do the trick! Thank you very much

Posted
44 minutes ago, nj1105nj said:

That seemed to do the trick! Thank you very much

 

                            ;(cons 11 t_pt)
                            (cons 73 1)
                            (cons 44 1.0)

Not a problem. You can remove the above lines from the code. code 11 is not needed, and codes 73 and 44 are to do with line spacing, and are optional for single line MText

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