Jump to content

Problem in IF function


mostafa badran

Recommended Posts

Hi all,can anyone clarify what's the problem in this code and how can I fix it.

  (setq lnam (getstring"\nEnter lay name:"))
 (if ( = lnam t)
   (setvar "clayer" lnam))
 (if ( = lnam nil)
   (setvar "clayer" "0"))

Link to comment
Share on other sites

The lnam variable is expected to hold a string, so cannot compare it with T; may help also to validate existance of layer inputted by user:

(if (and (setq lnam (getstring"\nEnter lay name:"))
        (tblsearch "LAYER" lnam))
(setvar "CLAYER" lnam)
(setvar "CLAYER" "0")
)

Link to comment
Share on other sites

Since getstring will return an empty string on the user pressing enter, you can use:

 

(if (tblsearch "layer" (setq l (getstring t "\nLayer name: ")))
   (setvar 'clayer l)
   (setvar 'clayer "0")
)

Link to comment
Share on other sites

The lnam variable is expected to hold a string, so cannot compare it with T; may help also to validate existance of layer inputted by user:

(if (and (setq lnam (getstring"\nEnter lay name:"))
        (tblsearch "LAYER" lnam))
(setvar "CLAYER" lnam)
(setvar "CLAYER" "0")
)

 

Since getstring will return an empty string on the user pressing enter, you can use:

 

(if (tblsearch "layer" (setq l (getstring t "\nLayer name: ")))
   (setvar 'clayer l)
   (setvar 'clayer "0")
)

Thanks guys for respond,

it work fine . I need another thing I want to create layer with entmake and add it to the previous code I am try but it dos not work it create layer but not but it current layer how can i fix this.

thanks for help .

try this

(defun c:test (/)
(if (and (setq lnam (getstring"\nEnter lay name:"))
        (tblsearch "LAYER" lnam))
(setvar "CLAYER" lnam)
(setvar "CLAYER" "0")
)
(entmake
  (list
    (cons 0 "LAYER")
    (cons 100 "AcDbSymbolTableRecord")
    (cons 100 "AcDbLayerTableRecord")
    (cons 2 lnam)
    '(70 . 0)
    '(62 . 2)
    '(6 . "Continuous")))
)

or this

(defun c:test (/)
(if (tblsearch "layer" (setq l (getstring t "\nLayer name: ")))
   (setvar 'clayer l)
   (setvar 'clayer "0")
)
(entmake
  (list
    (cons 0 "LAYER")
    (cons 100 "AcDbSymbolTableRecord")
    (cons 100 "AcDbLayerTableRecord")
    (cons 2 lnam)
    '(70 . 0)
    '(62 . 2)
    '(6 . "Continuous")))
)

Link to comment
Share on other sites

Like this?

(if (not (tblsearch "layer" (setq lnam (getstring t "\nLayer name: "))))
(if (entmake (list (cons 0 "LAYER")
                  (cons 100 "AcDbSymbolTableRecord")
                  (cons 100 "AcDbLayerTableRecord")
                  (cons 2 lnam)
                  '(70 . 0)
                  '(62 . 2)
                  '(6 . "Continuous")))
 (setvar "CLAYER" lnam)
 (setvar "CLAYER" "0")
)
)

Edited by MSasu
Fixed code
Link to comment
Share on other sites

Please pay attention that you need to close only DEFUN statement, IF was aleady closed.

 

I am sorry I think is there something wrong It does not work with me .

Link to comment
Share on other sites

Hope this will help you understand the issue:

(defun c:test( / lnam )
(if (not (tblsearch "layer" (setq lnam (getstring t "\nLayer name: "))))
 (if (entmake (list (cons 0 "LAYER")
                   (cons 100 "AcDbSymbolTableRecord")
                   (cons 100 "AcDbLayerTableRecord")
                   (cons 2 lnam)
                   '(70 . 0)
                   '(62 . 2)
                   '(6 . "Continuous")))
  (setvar "CLAYER" lnam)
  (setvar "CLAYER" "0")
 )
)
(princ)
)

Edited by MSasu
Fixed code
Link to comment
Share on other sites

Hope this will help you understand the issue:

(defun c:test( / lnam )
(if (not (tblsearch "layer" (setq lnam (getstring t "\nLayer name: "))))
 (if (entmake (list (cons 0 "LAYER")
                   (cons 100 "AcDbSymbolTableRecord")
                   (cons 100 "AcDbLayerTableRecord")
                   (cons 2 lnam)
                   '(70 . 0)
                   '(62 . 2)
                   '(6 . "Continuous"))
  (setvar "CLAYER" lnam)
  (setvar "CLAYER" "0")
 )
)
(princ)
)

I am really appreciate your effort.

Did you test it ? not work again !!

Link to comment
Share on other sites

I edited the code here, so missed a parenthesis; I have fixed it now. Please try it again. Sorry for inconvenience!

Thank man Its work now.:)

Link to comment
Share on other sites

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