Error while making block if block name already exists

Error while making block if block name already exists

Anonymous
Not applicable
1,739 Views
4 Replies
Message 1 of 5

Error while making block if block name already exists

Anonymous
Not applicable

Hi everybody,

I have made a lisp for creating blocks with certain parameters. If the block name already exists the function gets canceled. Can I somehow provide a option to stop or continue redefining the block

(defun c:blkmake ()
  (command "undo" "be")
  (princ "\nSelect the objects |||| Cannot redefine blocks")
  (setq obj (ssget))
  (initget 1 " YES NO ")
  (setq	keywordclr
	 (getkword "\nChange Block color to BYBLOCK? [ YES / NO ]: <YES>")
  )
  (if (eq keywordclr "YES")
    (change_byblock)
  )
  (initget 1 " YES NO ")
  (setq	keyword
	 (getkword "\nBlock has Center line? [ YES / NO ]: <YES>")
  )
  (if (eq keyword "YES")
    (change_color)
  )
  (princ "\nSpecify insertion base point: ")
  (setq pnt (getpoint))
  (princ "\nEnter block name: ")
  (setq name (getstring t))
  (command "_.-block" name pnt obj "")
  (command "insert" name pnt "1" "1" "0" "")
  (initget 1 " YES NO ")
  (setq	ukeyword
	 (getkword "\nScale BLock Uniformly? [ YES / NO ]: <YES>")
  )
  (if (eq ukeyword "YES")
    (scale_uniformly)
  )
  (command "undo" "end")
  (command "undo" "end")
)

(defun change_byblock ()
  (command "CHPROP" obj "" "LAYER" "0" "")
  (command "CHPROP" obj "" "COLOR" "BYBLOCK" "")
  (command "CHPROP" obj "" "LTYPE" "BYBLOCK" "")
  (command "CHPROP" obj "" "LWEIGHT" "BYBLOCK" "")
)


(defun change_color ()
  (princ "\nEnter color code")
  (setq ccolor (getint))
  (setq cobj (ssget))
  (command "CHPROP" cobj "" "COLOR" ccolor "")
  (command "CHPROP" cobj "" "LTYPE" "CENTER" "")
)

(defun scale_uniformly ()
  (command "-bedit" name "scale_uniformly")
)

 

0 Likes
Accepted solutions (1)
1,740 Views
4 Replies
Replies (4)
Message 2 of 5

Kent1Cooper
Consultant
Consultant

….

(setq name (getstring t))

(if (tblsearch "block" name)

  (progn

    (initget "Yes No")

    (setq redef (getkword "\nBlock name exists -- redefine? [Yes/No]: "))

    (if (= redef "Yes")

      (command "_.block" …include Yes answer to redefinition

    ); if

  ); progn

); if

(command "_.insert" ….

….

Kent Cooper, AIA
0 Likes
Message 3 of 5

Anonymous
Not applicable

Thank you

It works but, the following command doesn't execute & how do i make the code if block name doesnot exists

(command "insert" name pnt "1" "1" "0" "")

 

0 Likes
Message 4 of 5

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

.... if block name doesnot exists .... 


 

I didn't quite think it all the way through.  Try this:

….

(setq name (getstring t))

(if (tblsearch "block" name)

  (progn ; then

    (initget "Yes No")

    (setq redef (getkword "\nBlock name exists -- redefine? [Yes/No]: "))

    (if (= redef "Yes")

      (command "_.block" …include Yes answer to redefinition

    ); if

  ); progn

  (command "_.block" ; else …without that answer 

); if

(command "_.insert" ….

Kent Cooper, AIA
0 Likes
Message 5 of 5

Anonymous
Not applicable

Thank you that worked perfectly!!!

0 Likes