Lee Mac, thanks so much for the revised function, which does exactly what I want, and especially for the remarks included with it.
Excellent - you're welcome
I tried to adapt it to make an equivalent Mirror command, but this always terminates with nothing selected. What did I do wrong?
Note that the
MIRROR command has an additional prompt
(Erase source objects? [Yes/No] <N>:), hence an additional pause or automated input is required:
(command "_.mirror" sel
"" "\\" "\\" "")
And I hate to be greedy, but there are a couple of things I still don't understand:
Don't worry - ask away! Sharing knowledge is what this place is all about
1.....if the entity returned by (entlast) is a complex entity (such as an attributed block or 3D polyline), the subsequent calls to (entnext) will return the subentities which follow the parent entity (i.e. the attribute references or 3D polyline vertices respectively). For this reason, I suggested the lastent function that I posted above, which will return the last entity in the drawing database for which a call to (entnext) will return nil.
Does that mean that (lastent) returns the last non-complex entity? If so, what if there's a newer entity that is complex? What keeps that newer complex entity from being included in the final selection set? I see where you skip over the sub-entities, but where do you skip over a complex entity that's newer than the last non-complex entity?
Not quite -
(entlast) will return the last visible primary entity in the database, however, in the case of a complex entity such as an attributed block reference or 3D polyline, there will be subentities (attribute references / vertices) which follow the primary entity in the database.
Therefore, if the last object added to the database was complex and we were to step through the database starting from the entity returned by
(entlast), we would encounter the subentities which belong to that complex entity before encountering the new objects created by the command(s) invoked following the call to
(entlast).
Whereas, my
(lastent) function will obtain the last visible primary entity using a call to
(entlast) and will then continue to iterate over the drawing database from this entity until there are no further subentities (i.e. when
(entnext) returns
nil), at which point the function will return the entity occupying the very last index in the database. Of course, if the initial call to
(entlast) returns a non-complex entity (such as a
LINE,
POINT etc.), the call to
(entnext) will immediately return
nil (as there are no subentities which follow), and the primary entity will be returned by the function as if
(entlast) had been used.
2. What's that rtn all by itself at the end of the (lastent) function) It's not part of any expression other than the (defun) function, so I'm surprised it's even allowed in (defun sym ([arguments] [/ variables ...]) expr ...). And what does it do?
When evaluated, a function will return the last evaluated expression within the
defun expression; in this case, the last evaluated expression is the symbol '
rtn' which is evaluated to yield the value to which it points - that is, the last entity in the database.
Consider that if the isolated '
rtn' symbol were
not present at the end of the function, the function would instead return the value returned by the
while expression; and since
while returns the value of the last evaluated expression following the test expression, the
lastent function would return
nil if the entity returned by the initial
(entlast) call was non-complex (as the test expression for the
while loop would immediately return
nil).