insert block by Text

insert block by Text

Anonymous
Not applicable
1,212 Views
10 Replies
Message 1 of 11

insert block by Text

Anonymous
Not applicable

Hi, is there any information on the following subject:

 

I'ld like to insert one of 3 blocks.

 

First I type "SB"

then a letter: "A" or "B" or "C"

 

Then it should insert a block  "SB_A" or "SB_B" or "SB_C"

it should ask for a point and a rotation.

 

 

 

I already have a similar lisp but it only takes numbers not letters (I know it's cause it asks for integer, but I don't know the alternative code"

0 Likes
1,213 Views
10 Replies
Replies (10)
Message 2 of 11

m_badran
Advocate
Advocate

Can you provide a sample?

0 Likes
Message 3 of 11

m_badran
Advocate
Advocate

Can you provide DWG sample?

0 Likes
Message 4 of 11

ВeekeeCZ
Consultant
Consultant

Try this one...

 

(defun c:InsertSB ( / suf name)
  (while (/= (setq suf (getstring "\nSB suffix: ")) "")
    (if (tblsearch "BLOCK" (setq name (strcat "SB_" (strcase suf))))
      (progn
        (command "_.INSERT" name "_Scale" 1)
        (while (> (getvar 'CMDACTIVE) 0) (command PAUSE)))))
  (princ)
)

 

While you keeping entering some letter, it you insert the block. Enter for finish.

If you don't like a multiple behaviour, just replace the first while with if.

0 Likes
Message 5 of 11

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... 

I already have a similar lisp but it only takes numbers not letters (I know it's cause it asks for integer, but I don't know the alternative code"


It probably has a (getint) function in it that should be replaced by a (getstring) function, or maybe better by (initget) and (getkword) to restrict the possible input.  If you can post the code, it sounds like an easy adjustment.

Kent Cooper, AIA
0 Likes
Message 6 of 11

Anonymous
Not applicable

But where do I put the getkword (or similar)?

 

here's the code:

 

(defun C:tw	(
			/
			wandbreedte w_naam
		)

(if (= lastwandbreedte nil) 
	(setq lastwandbreedte 240)
)	

(princ "\rGeef wandbreedte [U72/24/30/60/72/90/120/240]")
(princ lastwandbreedte)
(princ "> :")

(initget 6 "U72 24 30 60 72 90 120 240")
(setq wandbreedte (getint))

(if(= wandbreedte nil)
	
	(setq wandbreedte lastwandbreedte)
)

(setq w_naam (strcat "TW" (itoa wandbreedte)))

(command "insert" w_naam pause 1 1 pause)

(setq lastwandbreedte wandbreedte)

(princ)

)

 

The "U72" in this code does not work. Because of the "U" 

0 Likes
Message 7 of 11

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

But where do I put the getkword (or similar)?

.... 

The "U72" in this code does not work. Because of the "U" 


Try this [untested]:

(defun C:tw	(
			/
			wandbreedte w_naam
		)

(if (= lastwandbreedte nil) 
	(setq lastwandbreedte "240")
;; make it a string, not an integer, to relate to (initget)/(getkword)/
;; (strcat) functions, and to make "U72" work ) ;;Put prompt within (getkword) function instead of here ;;(princ "\rGeef wandbreedte [U72/24/30/60/72/90/120/240]") ;;(princ lastwandbreedte) ;;(princ "> :") (initget "U72 24 30 60 72 90 120 240");; removed the 6
;; which would be relevant for (getint) but not (getkword) ;;(setq wandbreedte (getint));; replaced by: (setq wandbreedte (getkword (strcat "\rGeef wandbreedte [U72/24/30/60/72/90/120/240] <" lastwandbreedte ">: " ); strcat ); getkword ); setq (if (not wandbreedte);; slightly simpler than (= wandbreedte nil) (setq wandbreedte lastwandbreedte) ) ;;(setq w_naam (strcat "TW" (itoa wandbreedte)));; use text string version instead: (setq w_naam (strcat "TW" wandbreedte)) (command "insert" w_naam pause 1 1 pause) (setq lastwandbreedte wandbreedte) (princ) )
Kent Cooper, AIA
0 Likes
Message 8 of 11

ВeekeeCZ
Consultant
Consultant

Or you can make a command for each block name. Then you don't need to type SB <enter> A <enter>... but just SBA <enter> (or SB_A...) 

 

(defun c:SB_A nil (InsertSB "SB_A"))
(defun c:SB_B nil (InsertSB "SB_B"))
(defun c:SB_C nil (InsertSB "SB_C"))

(defun InsertSB (name / )
  (if (tblsearch "BLOCK" name)
    (progn
      (command "_.INSERT" name "_Scale" 1)
      (while (> (getvar 'CMDACTIVE) 0) (command PAUSE))))
  (princ)
)

 

0 Likes
Message 9 of 11

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:

Or you can make a command for each block name. .... 


I thought of that, too, and it sounds like a good idea for a small number of choices as in the original question.  But it looks from Post 6 as though that was a simplified example.  You could still do the same for 8 choices [or as many as you like], but that would require the User to know what all the choices are for the separate command names.  I can imagine [the OP would have to decide] that a version that shows you the choices within one command may be preferable, and more preferable as the number increases.  It would be necessary to add any further choices to both the (initget) string argument and the (getkword) prompt, but not to add any further command definitions.

Kent Cooper, AIA
0 Likes
Message 10 of 11

scot-65
Advisor
Advisor
My first thought is to employ a dialog (DCL) with radio_buttons
where each one has an allow_accept and no set_tile initialization.
(defun c:SB...
🙂

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 11 of 11

dbroad
Mentor
Mentor

Working on the idea of just creating more commands instead of prompting, this approach will allow you to build as many commands as you want from a simple list.

 

Spoiler
;;core subroutine that varies only by suffix.
(defun insb (suf)
(command "insert" (strcat "sb_" suf) pause 1 1 pause)
)

;;build your command functions from a list
(mapcar '(lambda (suf)
(set (read (strcat "c:SB_" suf))
(list nil ;no arguments/no local vars
(list insb suf) ;call to sub function
(list princ) ;exit quietly
)
)
) ;list of suffixes.
(list "U72" "24" "30" "60" "72" "90" "120" "240")
)
Architect, Registered NC, VA, SC, & GA.
0 Likes