Regarding the order structure, AutoCAD keeps entities on the order of their creation, but also identifies each with a unique handle (dxf code 5) which is in hexadecimal format.
From my experience, I have never trusted any (ssget) method to create a selection set in their order of creation, so it's sort of like Alex Haley's story Roots where to get the history in order, you have to start at the beginning.
You can do that by using (entnext ...).
(entnext) returns the first graphical entity in the drawing database.
(entnext e) returns the next entity after e in the drawing database.
So, you could create a list of the entities in order using a function like this:
(defun @elist ( / e elist)
(while (setq e (if e (entnext e)(entnext)))
(setq elist (cons e elist))
)
(reverse elist)
)
But, (entnext) returns subentities too, like attributes and heavy or 3D polyline vertices, as in...
Line
Text
Circle
3DPoly, vertex, vertex, vertex, Seqend
Insert, Attrib, Attrib, Attrib, Seqend
etc.
So, if you have thousands of entities in a drawing, using @entnext will probably take a long while.
**** BTW, I don't know how to get out of this code formatting. Oh, well.
Now then, you might think of ordering entities by their handle.
There are hex->integer functions around here somewhere (I think @dbroad's contribution
is one of the best). But watch your @$$ there because if you copy entities from another
drawing, AutoCAD tries to preserve their handles and there is no guarantee that they will
result in a numerical order of creation.
So, it seems (even though someone much smarter than myself will correct me) that
@@elist might be your best bet.
BUT, rather than creating a list, it may be faster to create a selection set, as in:
(defun @ssall ( / e ss)
(setq ss (ssadd))
(while (setq e (if e (entnext e)(entnext)))
(ssadd e ss)
)
)
And I think that subentities are excluded individually because they are children of their parents.
BUT, I haven't tested any of this, and I'm holding up dinner. :/