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

How to mimic enter key for next line of text?

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
pete592
7153 Views, 5 Replies

How to mimic enter key for next line of text?

I am writing a program that allows the user to select hatched areas in a drawing and return the quantity, area, and the weight for the equivalent areas covered by tread plate.

 

My goal is to insert the data with text at a point of the user's choosing, so the end result is a text object in model space that looks like this:

 

Total Objects: 2

Total Tread Plate Area: 20.83 sq ft

.125 AL Tread Plate Weight: 40 lbs

.125 Steel Tread Plate Weight: 128.33 lbs

 

What I can't figure out how to do is to have the text appear on multiple lines, instead of all across one line.

I'm hoping that there is a way to mimic the enter key to enter the next line of text, just like when you use the text function in AutoCAD, instead of using multiple text commands and having to position each one using code.

 

(defun c:tread (/ cnt tot ss p sqft alwt stwt)
(vl-load-com)
  (setq    cnt 0
    tot 0
  )
  (print "Select Hatches (or Enter for ALL): ")
  (if (or (ssget '((0 . "HATCH"))) (ssget "_X" '((0 . "HATCH"))))
    (progn
      (vlax-for    h (setq
            ss (vla-get-ActiveselectionSet
             (vla-get-ActiveDocument (vlax-get-acad-object))
               )
          )
    (setq cnt (1+ cnt)
          tot (+ tot (vla-get-Area h))
    )
      )
      (vla-delete ss)
    )
  )
  (setq p (getpoint "Select Weight Data Insertion Point:"))
  (setq sqft (/ tot 144))
  ; calc aluminum tread plate weight
  (setq alwt (* sqft 1.92))
  ; calc steel tread plate weight
  (setq stwt (* sqft 6.16))
  ; insert data text at insertion point
  (command "text" "style" "standard" "justify" "tl" p 3.0
       "0.0" (strcat "Total Objects: " (rtos cnt)
             "Total Tread Plate Area: " (rtos sqft) " sq ft"
             " .125 AL Tread Plate Weight: " (rtos alwt) " lbs"
             " .125 Steel Tread Plate Weight: " (rtos stwt) " lbs"))
  (princ)
)

 

Thanks in advance for any assistance.

 

Pete

 

 

5 REPLIES 5
Message 2 of 6
Kent1Cooper
in reply to: pete592


@pete592 wrote:

.... 

My goal is .... text at a point of the user's choosing, so .... have the text appear on multiple lines, instead of all across one line.

I'm hoping that there is a way to mimic the enter key to enter the next line of text, just like when you use the text function in AutoCAD, instead of using multiple text commands and having to position each one using code.

 

....

  ; insert data text at insertion point
  (command "text" "style" "standard" "justify" "tl" p 3.0
       "0.0" (strcat "Total Objects: " (rtos cnt)
             "Total Tread Plate Area: " (rtos sqft) " sq ft"
             " .125 AL Tread Plate Weight: " (rtos alwt) " lbs"
             " .125 Steel Tread Plate Weight: " (rtos stwt) " lbs"))
  (princ)
)

....


Text entities can't be multiple-lined, but Mtext entities can [that's what the M is for -- Multi-line text].  If you type T as a default shortcut, you're actually getting the Mtext command, not Text, and it's only in Mtext that you can go to the next line with Enter [doing so in Text simply finishes it].

 

The character code \P is the line feed within a single Mtext entity's text content [try some and look at its entity data] -- that appears as two backslashes and a P in the content in entity data or VLA properties, because of how backslashes "work" in text strings in AutoCAD.  You should be able to do it by using the Mtext command instead of Text, and putting "\\P" into the text content string with (strcat), though I'm having trouble getting it to work right in a (command) function in quickie experiments.  Search the Discussion Group and you'll probably find something, or use (entmake) instead of (command "_.mtext" ...) [you can find lots of examples of that with a Search].

 

You can also have them be separate Text entities, automatically positioned under each other, by just restarting the Text command and giving Enter for the start point, which will move it down a line from the previous Text:
 

(command "_.text" "style" "standard" "justify" "tl" p 3.0 0 (strcat "Total Objects: " (rtos cnt)))

(command "_.text" "" (strcat "Total Tread Plate Area: " (rtos sqft) " sq ft"))

(command "_.text" "" (strcat " .125 AL Tread Plate Weight: " (rtos alwt) " lbs"))

(command "_.text" "" (strcat " .125 Steel Tread Plate Weight: " (rtos stwt) " lbs"))

Kent Cooper, AIA
Message 3 of 6
pete592
in reply to: pete592

Thanks Ken!

 

The four lines with separate text commands works great.

 

This isn't the first time you've helped me.  I really apprecate it!

 

Pete

Message 4 of 6
Kent1Cooper
in reply to: pete592


@pete592 wrote:

Thanks Ken!

 

The four lines with separate text commands works great.

 

This isn't the first time you've helped me.  I really apprecate it!

 

Pete


You're welcome.  And to carry through what I often suggest to others:

 

You can combine multiple (command) functions into one, and save a little code:

 

(command

  "_.text" "style" "standard" "justify" "tl" p 3.0 0 (strcat "Total Objects: " (rtos cnt))

  "_.text" "" (strcat "Total Tread Plate Area: " (rtos sqft) " sq ft")

  "_.text" "" (strcat " .125 AL Tread Plate Weight: " (rtos alwt) " lbs")

  "_.text" "" (strcat " .125 Steel Tread Plate Weight: " (rtos stwt) " lbs")

); command

 

Unfortunately, you can't use Enter to recall the previous command in place of the "_.text" after the first one, because that won't recall a command from an AutoLISP (command) function, but will recall whatever command you last used at the Command: prompt.  So you do still need to spell out the command name for each line.

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


@Kent1Cooper wrote:

 

....  You should be able to do it by using the Mtext command instead of Text, and putting "\\P" into the text content string with (strcat), though I'm having trouble getting it to work right in a (command) function in quickie experiments.  ....


I figured it out.  It was partly getting the thing ended correctly, and partly the fact that, as it turns out but I hadn't expected, the backslash-P thing for line feed is case-sensitive -- it only works with an upper-case P.

 

You should be able to do this [it works for me, substituting some things for your variable values]:

 

  (command "_.mtext" p "style" "standard" "justify" "tl" "height" 3.0 "_width" 0

    ;; note that sequence is different -- insertion point comes first, other options can

    ;; come in any order except width has to come last -- and width option replaces

    ;; picking other corner in on-screen Mtext [making it 0 means word wrap will be

    ;; determined only by line-feed entries built into the text content, not by length

    ;; of content surpassing the Mtext window width]
    (strcat

      "Total Objects: " (itoa cnt) \\P

        ;; the end of the line above should be two backslashes and a P within double-quotes;

        ;; the "system" takes the quotes away and underlines it, for some reason, when it's in

        ;; isolation, unlike the ones below which it leaves alone....
      "Total Tread Plate Area: " (rtos sqft) " sq ft\\P"
      " .125 AL Tread Plate Weight: " (rtos alwt) " lbs\\P"
      " .125 Steel Tread Plate Weight: " (rtos stwt) " lbs"

    ); strcat

    ""

  ); command

 

That results in its being all in one entity, with the various advantages that entails.

 

EDIT:  changed total-objects (rtos) function to (itoa), since cnt variable will always be an integer.

Kent Cooper, AIA
Message 6 of 6
pete592
in reply to: Kent1Cooper

Sweet!  Thanks again!

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

Post to forums  

Autodesk Design & Make Report

”Boost