I would give that a B+ but there are a few minor things that need to be clarified. I will point them out and you will then understand the things you questioned before.
-Define the function as mlayer and the following arguements..
ex. (mlayer "Andre" 4 6 1 0)
- if the layer doesn't exist then go and copy layer 0 as the background for the new layer to create on, and in the following line change the name of the layer you copied to "Andre".
The last three arguments of this function should only be 0 or 1
The code first checks if the specified layer exists if it does not, then it will grab the contents of layer 0 as a starting point, a template if you will, for the new layer - in this instance "Andre"
check and see if you set the layer to be either frozen or thawed.. if set to 0 then it will stay thawed and 1 will set it to frozen
To expand upon this a bit, the variable
new_bit will hold all of the bitwise values for the layer as defined by DXF code 70, you will need to see the DXF code explanation in the help files for the bit codes. Suffice to say that 1 = freeze
check to see if it will be locked or unlocked.. if set to 0 it will stay unlocked and will return NIL if set to 1 it will lock the layer
Question: why (+ new_bit 4)??
If the layer is designated to be locked, the bitwise value for a locked layer will be added to
new_bit. As before the value of Freeze/Thaw Lock/Unlock as well as a few others we will not be concerned with here are bitwise. The bit for locking happens to be 4. So to lock AND freeze the value would be 4 + 1 or 5 that is why we are adding the 4 to
new_bit.
check and see if it will be on or off, if set to 0 it will be OFF if set to 1 then it will be turned on.
Question: why (- 0 aci_layer_color)
As defined by the program 1 = on and 0 = off, this has nothing to do with the layer actually being on or off, only how we tell what the user wants it to be. The reason for (- 0 aci_layer_color) is that the way AutoCAD determines if a layer is on or off is that the layer color is either a positive number OR a negative number. If the layer color index number is negative the layer will be turned off, to do this, if the user elected to turn off the layer, (on_off = 0) then we subtract the layer index number from 0 effectively giving a negative value and turning the layer off.
O.k the tricky part for me..
Now it's time to take all the variables and put them together..
take Layer name and add it to the layer_list ("Andre")
take Layer color and add it to the layer_list (cyan or 4)
modify all the new bits ( freeze/thaw , On/Off, Lock/Unlock)
question: Confused on the last one (setq layer_list (subst (cons 70 new_bit)(assoc 70 layer_list) layer_list)) ??
Ok, first we subst
itute the new layer name
layer_name for the old layer name
(assoc 2 layer_list) in the layer entity list
layer_list and save it back to itself. Then we use the same scenario to subst
itute the new color for this layer, finally we set the bitwise setting for the frz/thw lck/ulk layer state using the same scenario. Remember that DXF 70 holds the setting for frz/thw and lck/ulk and that is why we modify that setting.
If layer is TRUE and EXIST at the beginning then it will become true and will only be modified (entmod layer_list)
(entmake_list) will make the layer if it's FALSE from the beginning..
The reason we need this is because if layer "Andre" were to exist in the beginning, then we could not (entmake ...) it, but we could (entmod ...) it, and at the same time if the layer did not exist in the beginning, we could not (entmod ...) it but would have to create it with (entmake ...).
Hopefully this explained it a little better and you have a better understanding of lisp.