Whenever attempting to understand a snippet of code, it is usually helpful to step through the evaluation from the inside-out, reviewing the values returned by each expression evaluated.
In this case, let us assume that our drawing contains a single red circle on layer
"TEST-N".
Evaluating the
ssget expression at the
Visual LISP IDE Console yields a selection set containing such circle:
_$
(setq s
(ssget "_X" '
((8 .
"*?`-N") (62 .
1))))
We can then verify the number of times that the
repeat loop will be evaluated and the value of the index variable
i by evaluating the
sslength expression:
The next
setq expression assigns three values to three symbols:
The variable
i is decremented to
0 (since selection set indexes are zero-based, i.e. they start at zero):
The DXF data for the entity residing at index 0 is obtained:
((0 . "CIRCLE") (100 . "AcDbEntity") ... (8 . "TEST-N") (62 . 1) (100 . "AcDbCircle") (10 0.0 0.0 0.0) (40 . 1.0))
And the dotted pair for DXF group 8 (the layer) is obtained from this DXF data:
Moving on to the innermost expressions within the
entmod expression: first, the dotted pair corresponding to DXF group 62 (the colour) is substituted for a dotted pair with value 256 (ByLayer):
_$
(subst '
(62 .
256) '
(62 .
1) x
)((0 . "CIRCLE") (100 . "AcDbEntity") ... (8 . "TEST-N") (62 . 256) (100 . "AcDbCircle") (10 0.0 0.0 0.0) (40 . 1.0))
This list is then supplied as an argument to the surrounding
subst expression which substitutes the new layer name.
Breaking down the construction of the dotted pair for the new DXF group 8 entry, we have the following:
When supplied with a dotted pair,
cdr will return the value associated with the key - in this case, the layer name:
The
strlen function provides the number of characters comprising this layer name:
2 characters are then subtracted from this total:
And this figure is then supplied to the
substr function to indicate the number of characters to be returned following the given starting character:
We can insert the known results of the previous evaluated expressions into this expression for clarity:
In words, this expression is saying:
"Return 4 characters starting from the 1st character in the string 'TEST-N'"The
cons function is then used to
construct the dotted pair:
And hence the second
subst expression has the following arguments:
(subst '
(8 .
"TEST") '
(8 .
"TEST-N") <DXF data whose colour has already been substituted
>)
This will therefore return a list of DXF data in which both DXF groups 8 & 62 have been substituted with new values, corresponding to a change to the layer & colour respectively.
But the entity has not been modified at this point! Up until now, we have only been working with a list of data and have not committed the change to the drawing database...
This is where the
entmod function is introduced, to
modify the
entity:
(entmod <DXF data whose layer
& colour has been substituted
>)
This final expression updates the DXF data as it appears in the drawing database, causing the objects in the drawing to be modified.