@Anonymous wrote:
That worked really well.
Thanks for that.
Ozitag
You're welcome. If you don't mind, I have a suggestion to simplify the routine a little, as follows:
(defun C:TLENG (/ ss tl ent)
(setq
ss (ssget '((0 . "LINE,ARC,CIRCLE,*POLYLINE,SPLINE,ELLIPSE")))
tl 0
); end setq
(repeat (sslength ss)
(setq
ent (ssname ss 0)
tl
(+
tl
(vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent))
); end + & tl
); end setq
(ssdel ent ss)
); end repeat
(alert (strcat "Total length of selected objects is " (rtos tl)))
(princ)
); end defun
[If you don't ever have any "risk" of a selection containing XLINEs, you could reduce that filter list to:
'((0 . "*LINE,ARC,CIRCLE,ELLIPSE"))
The *LINE part will find Lines, all kinds of Polylines, and Splines. But you wouldn't want to do it that way and let it also find Xlines, if you ever use them, because there will be a problem trying to determine their length.]
If you filter the selection set for the allowable entity types at the time of selection, then you know everything in the set will have a length that you want to add to the total. That means you don't need to use a variable for the entity type or check that against a list, nor do you need to use a variable for the length, if present, to add to the total [you can just go ahead and add it directly], nor do you need the fallback position of adding 0 if it's not one of the right entity types. So several variables and Lisp functions are eliminated.
The (repeat) and (ssdel) combination, in place of the (while) and increment-variable approach, is less of an "improvement" per se, but my personal preference. Someone else has made the argument that (repeat (sslength ss).... is really a better "description" of what you want to do in a case like this. But it also eliminates both the incrementing variable and the resetting of it, and the need for the routine to do any evaluation when it jumps back to the beginning of a (while) loop. I couldn't say whether there's any time savings, comparing the work of (ssdel) against the combined work of the (while (>= n 0) evaluation and the (setq n (1- n)) incrementing. But any such difference would never be noticeable unless you had a very large selection set.
SomeBuddy often uses an approach [worth taking a look at -- you can search the Discussion Group history], involving (ssnamex), that essentially converts the selection into a list of entity names, so it can use (foreach) on that list, instead of using either (while) or (repeat) on the selection set. It can save a little code, but I've wondered whether the overhead of doing that conversion, and the needed screening of the resulting list for only certain kinds of information, outweigh any savings -- I wouldn't know how to test that. But in any case, I find the (repeat) approach easier to grasp, looking at the code.
And of course, if you are going to need to use that selection set again for anything after you've added up the lengths, you wouldn't want to do it my way, because mine empties the selection set in the process of moving through it.
Kent Cooper, AIA