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

Nil Value

12 REPLIES 12
Reply
Message 1 of 13
Jolo5309
1969 Views, 12 Replies

Nil Value

I have a piece of code that updates a block to another name, then refreshes it. What I am failing is at is that it fails when there are no files there.

 

I tried this:

(setq blkname(ssget "X" (list (cons 2 "FB006_1"))))
(if (> (sslength blkname) 0 ) (here is the issue,when blkname is Nil it fails)

 

I don't understand what the correct procedure is if blkname is nil

 

Thanks

John

12 REPLIES 12
Message 2 of 13
kdub_nz
in reply to: Jolo5309

Perhaps ;

      (if (setq ss (ssget "X" (list (cons 2 "BlockName"))))
        (progn
         ;; do mojo with selection set ss
        )
        ;;
        ;; else
        (progn (alert "It Happened\nRoutine will exit now") (exit))
      )

 


// Called Kerry in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 3 of 13
Kent1Cooper
in reply to: Jolo5309


@Jolo5309 wrote:

....

I tried this:

(setq blkname(ssget "X" (list (cons 2 "FB006_1"))))
(if (> (sslength blkname) 0 ) (here is the issue,when blkname is Nil it fails)

....


When there aren't any, (ssget) doesn't return an empty selection set, but rather returns nil.  If there are any, the selection set will necessarily be more than zero, so you don't need to check its length.  Since (if) considers its test to be satisfied if the answer is anything other than nil, you can simply do this:

 

(setq blkname (ssget "X" (list (cons 2 "FB006_1"))))
(if blkname

....

 

Or, if you're really after a specific Block name, and don't need variable capability, you can shorten the filter part:

 

(setq blkname (ssget "X" '((2 . "FB006_1"))))
(if blkname

....

 

Or nest that within the (if) function as KerryBrown suggested:

 

(if (setq blkname (ssget "X" '((2 . "FB006_1"))))
....

Kent Cooper, AIA
Message 4 of 13
Jolo5309
in reply to: Kent1Cooper

Great, that worked, thanks!

Message 5 of 13
kdub_nz
in reply to: Jolo5309

 

Just a tip though ;

Make your variable names meaningful.

Using a variableName like blockname suggests the value held will be the name of a Block

 .... It's actually a SelectionSet 


// Called Kerry in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 6 of 13
scottbolton
in reply to: kdub_nz

True, but in some cases I find it easier to use just one variable name rather than many - a simple example:

 

(if (setq line (ssget "X" '((0 . "LINE"))))
  (progn
    (setq line (ssname line 0))
    (setq line (entget line))
    )
  )

 

vs

 

(if (setq line_ss (ssget "X" '((0 . "LINE"))))
  (progn
    (setq line_ent (ssname line_ss 0)
          line_cont (entget line_ent))
    )
  )


 S

Message 7 of 13
kdub_nz
in reply to: scottbolton

Perhaps it is easier for you now, when the code is fresh, but you'll be scratching your head when you come back to the code later.

Also, more importantly it is more difficult than necessary for anyone else who is reading your code ;

Also, as you migrate to languages that are strongly typed it's not possible to use one variable to identify differing types of values.

 

Take that advise or leave it , it's really no skin off my nose ... but when I see code written like that my eyes glaze over and I lose interest fairly quckly ; and I don't believe I'd be the only one.

Stay well.

 

 


@scottbolton wrote:

True, but in some cases I find it easier to use just one variable name rather than many - a simple example:

 

(if (setq line (ssget "X" '((0 . "LINE"))))
  (progn
    (setq line (ssname line 0))
    (setq line (entget line))
    )
  )

 

vs

 

(if (setq line_ss (ssget "X" '((0 . "LINE"))))
  (progn
    (setq line_ent (ssname line_ss 0)
          line_cont (entget line_ent))
    )
  )


 S

 


 


// Called Kerry in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 8 of 13
Jolo5309
in reply to: kdub_nz

I used blkname because it helps me remember later on in the code what the selection set is for. Realistically I should have used ssBlkName because that would have made more sense.

Message 9 of 13
SomeBuddy
in reply to: scottbolton

I totally agree with Kerry, it is not good practice to use the same variable name for different data types. For simple things like your example and as Kerry says, while the code is still fresh in your memory, it might seem more convenient to use the same variable name, but for complex things, it's a big mistake, especially if you edit your code after some time, or even worse, if somebody else has to make corrections/modifications in your code, if it ever happens for whatever reason that you are not working anymore for that company.

 

Variable names should have names as suggestive and self explanatory as possible, names which speak for themselves and I would say that for big and complex programs it's a good thing also if variables names are preceded by some prefix indicating the data type, like int for integers, dbl for reals, str for strings, lst for lists, ent for entity name, sst for selection set, obj for object and so on. And then, when you return to your code after one year or more, you can read and understand easily what's all about.

 

 

Message 10 of 13
Kent1Cooper
in reply to: scottbolton


@scottbolton wrote:

... in some cases I find it easier to use just one variable name rather than many - a simple example:

 

(if (setq line (ssget "X" '((0 . "LINE"))))
  (progn
    (setq line (ssname line 0))
    (setq line (entget line))
    )
  )

....


[In addition to what KerryBrown and SomeBuddy have already said, I would point out that this particular simple example is going to find every Line in the drawing, and get entity data for only the first one [how will you know which one that is?], and thereafter not have access to the rest of the Lines it found, to do anything with them, because the variable containing the selection set that contained the Lines will have been overwritten by an entity name and then again by an entity data list.  That could only produce predictable results if there's only one Line in the drawing.  But maybe you really meant to write:

 

(if (setq line (ssget ":S" '((0 . "LINE"))))

 

to make the User select just the one they want.]

Kent Cooper, AIA
Message 11 of 13
scottbolton
in reply to: Kent1Cooper

Key words:

 

"in some cases" "a simple example"

 

 

Message 12 of 13
SomeBuddy
in reply to: Kent1Cooper

I guess one can't do good by force!

Message 13 of 13
Kent1Cooper
in reply to: scottbolton


@scottbolton wrote:

Key words:

 

"in some cases" "a simple example"


Well, I would have liked to assume that, simple or not, it was an actual example of code from an actual case done that way, but obviously it wasn't.  One that is actual would at least make a better example, especially if you have one that illustrates a circumstance where re-purposing a variable name may not risk the possible confusion others have mentioned.

Kent Cooper, AIA

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

Post to forums  

Autodesk Design & Make Report

”Boost