Using Autolisp, get a string modify font,size then show text in the autocad.

Using Autolisp, get a string modify font,size then show text in the autocad.

Anonymous
Not applicable
2,510 Views
12 Replies
Message 1 of 13

Using Autolisp, get a string modify font,size then show text in the autocad.

Anonymous
Not applicable

Hello, As I'm new to autocad using lisp.

I'm trying to make the following program.

Ask a user to make a put a text.

Ask about font and size.

then Whenever he click on the autocad the string he entered in pasted there.

Could you please help?

0 Likes
Accepted solutions (1)
2,511 Views
12 Replies
Replies (12)
Message 2 of 13

ВeekeeCZ
Consultant
Consultant

Well, you're saying that you are "trying" - let us see where you've got so far. Post the code.

0 Likes
Message 3 of 13

Anonymous
Not applicable

Forgive me for the rude request but as I'm new my code is so bad

0 Likes
Message 4 of 13

ВeekeeCZ
Consultant
Consultant

@Anonymous wrote:

... my code is so bad


 

Seriously man! Seeing the file looks like you're also a very bad liar.

 

 

; Automatic coordinate labeling
; Edwin Prakoso
; http://cad-notes.com
;
; Limitation
; ----------
; Will use current leader style and current units setting

0 Likes
Message 5 of 13

Anonymous
Not applicable

I'm abit familiar with C, all of this is done in two days "heavily searching".

I don't guess it's a compliment, I'm actually embarrassed to show this code.

I was trying to solve this step by step.

First is to get the text which succeeded.

then make a loop to infinite just like in C which succeeded

then try to show it in autocad drawing and it failed.

whenever I've succeeded in this, I will try to modify it and move so on into my actual goal

Help would be appreciated.

Thank you.

0 Likes
Message 6 of 13

ВeekeeCZ
Consultant
Consultant
Accepted solution

No need to be a shame of being a newbie. But don't like liers!! 

 

Here is some code. Font and size are defined by Text Style... as you should know...

 

(defun c:txt ( / txt pnt)

  (initdia)
  (command "_.STYLE")
  
  (if (= 0. (cdr (assoc 40 (tblsearch "STYLE" (getvar 'TEXTSTYLE)))))  ; set size if text height is 0
    (command-s "_.TEXTSIZE"))
  
  (setq txt (getstring T "\nType content: "))

  (while (setq pnt (getpoint (strcat "\rPlace text '" txt "': ")))
    (command "_.TEXT" "_non" pnt)
    (if (= 0. (cdr (assoc 40 (tblsearch "STYLE" (getvar 'TEXTSTYLE)))))
      (command (getvar 'TEXTSIZE)))
    (command 0. txt))

  (princ)
  )
Message 7 of 13

Anonymous
Not applicable

Thank you very much, I've modified the code to run it as following but some how it gives me error and it doesn't loop can you help me again?

I'm trying to make it as follows:

if a user enter 1 it type the "text"1,2

else if he doesn't it types text1

and loops again. while looping the counter doesn't reset.

Is there a function that I can use to make instead of typing anything else rather than 1, just if I click the mouse it runs as it isn't 1 and type text1 and if I type 1 it type text1,2?

Sorry for bothering you.

I've attached what I've done so far all thanks to you.

 

0 Likes
Message 8 of 13

ВeekeeCZ
Consultant
Consultant

Two steps further...

 

(defun c:Counter ( / *error* eko txt pnt)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if eko (setvar 'CMDECHO eko))
    (princ))
  
  ; ----------------------------------------------------------------------------------------------------
  
  (initdia)
  (command "_.STYLE")
  
  (if (= 0. (cdr (assoc 40 (tblsearch "STYLE" (getvar 'TEXTSTYLE)))))  ; set size if text height is 0
    (command-s "_.TEXTSIZE"))
 
  (setq eko (getvar 'CMDECHO))
  (setvar 'CMDECHO 0)

  
  (or *counter-txt*
      (setq *counter-txt* ""))
  (if (/= "" (setq txt (getstring T (strcat "\nType content <" *counter-txt* ">: "))))
    (setq *counter-txt* txt))
  
  
  (or *counter-inc*
      (setq *counter-inc* 1))
  (setq *counter-inc* (cond ((getint (strcat "\nStart with number <" (itoa *counter-inc*) ">: ")))
                        (*counter-inc*))
        *counter-inc* (1- *counter-inc*))
 
  
  (while (progn
           (initget "Done")
           (setq pnt (cond ((getpoint (strcat "\rPlace text '" (setq txt (strcat *counter-txt* (itoa (setq *counter-inc* (1+ *counter-inc*)))))
                                              "' [Done] <dual>: ")))
                           ((initget 1))
                           ((getpoint (strcat "\rPlace text '" (setq txt (strcat *counter-txt*
                                                                                 (itoa *counter-inc*)
                                                                                 ","
                                                                                 (itoa (setq *counter-inc* (1+ *counter-inc*)))))
                                              "': ")))))
           (/= pnt "Done"))
    
    (progn
      (command "_.TEXT" "_non" pnt)
      (if (= 0. (cdr (assoc 40 (tblsearch "STYLE" (getvar 'TEXTSTYLE)))))
        (command (getvar 'TEXTSIZE)))
      (command 0. txt)))
  
  (*error* "end")
  ) ;defun
Message 9 of 13

ВeekeeCZ
Consultant
Consultant

Also your code fixed from error. You need to add (progn) sometimes...

 

(defun c:txt ( / txt pnt)
  
  (initdia)
  (command "_.STYLE")
  
  (if (= 0. (cdr (assoc 40 (tblsearch "STYLE" (getvar 'TEXTSTYLE)))))  ; set size if text height is 0
    (command-s "_.TEXTSIZE"))
  
  (setq txt (getstring T "\nType content: "))
  (setq i 0)
  (while (setq d (getint "Dual enter 1"))
    (if (= d 1) ;if its true
      (progn
        (setq pntt (strcat txt (itoa i) "," (itoa (+ i 1))))
        (setq pnt (getpoint (strcat "\rPlace text '" pntt "': ")))
        (setq i (+ i 2))
        (command "_.TEXT" "_non" pnt)
        (if (= 0. (cdr (assoc 40 (tblsearch "STYLE" (getvar 'TEXTSTYLE)))))
          (command (getvar 'TEXTSIZE)))
        (command 0. pntt)
        )
      
      (progn ;else
        (setq pntt (strcat txt (itoa i))) (setq pnt (getpoint (strcat "\rPlace text '" pntt "': ")))
        (setq i (+ i 1))
        (command "_.TEXT" "_non" pnt)
        (if (= 0. (cdr (assoc 40 (tblsearch "STYLE" (getvar 'TEXTSTYLE)))))
          (command (getvar 'TEXTSIZE)))
        (command 0. pntt))
      ) ;if
    ) ;while
  (princ)
  ) ;defun

 

0 Likes
Message 10 of 13

Anonymous
Not applicable

Thank you very much.

Is this code modifiable?

I've noticed if I type enter or "Space bar" It change to "text"1,2.

Can It be modified on the number of enters or space bars the users do so it can write "text1,2,3" and "text1,2,3,4"

whenever more space bars are introduced?

0 Likes
Message 11 of 13

ВeekeeCZ
Consultant
Consultant

Sure thing that the code is editable! Feel free to do any adjustments required to fit the code to your needs.

Pretty sure that you'll learn some new tricks while doing so!

Good luck!

0 Likes
Message 12 of 13

Anonymous
Not applicable

Hello again, sorry for bothering you a lot,

As for this code I'm trying to modify it, but I can't seem to see the condition part.

As far as I know cond command in autolisp is as follows (cond((test)(result))….) and apply the first condition that it meets, but it isn't as shown so I'm getting confused.

The first cond in your code and the second, it doesn't tell what is the difference, is it initget that makes the difference?

I've attached what I thought of doing, maybe the error function doesn't allow more spaces or enteries other than 1.

Help would be appreciated, thank you sir.

0 Likes
Message 13 of 13

ВeekeeCZ
Consultant
Consultant

Good, good try!

 

Well kinda don't like as the HELP represents the cond functions. I think it rather should be:

(cond ((test-expr) [then-expr])) ...)

as then expression is optional. If it's omitted, then the result of test-expr is also the result of the condition. The cond function can have as many conditions as needed.

 

Important to remember:

- in LISP is every other value than NIL considered as True!!! Try: (and "Hello" True) or (and 1 True) even (and "" True) Minor exception is (and '() True).

- in LISP every function returns some value: (getpoint ...) (cond ...) (setq ...) (intiget)... all of them. Some functions return always nil, other always True. You need to see the HELP for that. 

 

So back to our code. First, let's strip the code from all unnecessary stuff. Then see the commentaries and try/trace yourself:

 

(cond ((getpoint "Pick a point <dual>: "))
      ; if you pick a point, (getpoint) returns a list of coordinates, which is T for the condition and it's passed as result of the cond funtion
      ; if you right-click, (getpoint) return nil, which is result of the condition. Since the cond funtion does not have T yes, it goes for next condition

      ((initget 1))
      ; initget is processed, returns always nil - see HELP - so the condition is nil, cond contionues to next condition

      ((getpoint "Pick a point for dual: "))
      ; try right-click. Becouse of previous (initget) it's not allowed!!
      ; only option is pick a point, which will be passet as T for the condition as the result of the cond function.

      )

See HERE how to TRACE the code, one expression after another. If you have any variables in the code (I haven't), click on it and hit the "Add watch button"...

 

Hope now you can your code get fixed.