@Kent1Cooper wrote:
This can actually be done with the (ssnamex) function, if you want that capability. If you want it to work as described above, as you appear to have confirmed in Post 20, the current accepted-solution code doesn't do that, ....
But it could be made to work that way, because (ssnamex) will return a list that can distinguish in what way a given object was selected. So it can know whether you picked on one, and find the nearer end, or whether it was selected with a fence, and find the nearer end to where the fence crossed it, or whether it was just part of a Window/Crossing selection, and find both ends. So it could put one on only the end selected by pick or fence on a Polyline selected in those ways, even if the other end is also "free".
....
Here's what I mean. Lightly tested, but this works for me in your sample drawing [subject to the Note below]. It uses (ssnamex) on the selection, and looks at each entry in the resulting list. It knows how you selected each Polyline, even if you mix different selection methods in the same running of the command. When it finds one that was selected by either individual pick or with a Fence, it realizes there's a "closer end" on the Polylines, figures out which end the pick point or the Fence crossing point is closer to, and Inserts a Block on only that end. If it finds one selected by W / C / WP / CP, for which there is no "closer end," it puts Blocks on both ends. [I haven't experimented with selecting the same Polyline more than once by different methods in the same running of the command.]
[There are additional kinds of entries in the (ssnamex) returned list, about things like the definitions of polygons used in W/C/WP/CP selections, but they start with different numbers than the ones that identify the two categories of types of selection, so it just ignores them.]
NOTE: I adjusted one thing in your sample drawing to get it to work right. The Block itself is defined as Unitless, but the drawing had its insertion scale ["Units to scale inserted content:" in the Insertion scale area of the UNITS dialog box] set to meters, which forced the A-tapa Blocks to be tiny, even with the code specifying a scale of 5 as in the example. I could select them and their scale was 0.127 in the Properties box, and I could change them collectively to 5 there, and they would be right. But I changed that setting in Units to also be Unitless, and it works as expected. I'll let you deal further with that issue, since it will vary depending on Units settings in individual drawings. You may want to change the units designation in the Block definition, rather than the setting in a given drawing. Or you might increase the specified scale in the code to compensate.
(defun C:ATapaEnds (/ ss ent pt start end dist inspt rotpt)
(if (setq ss (ssget '((0 . "LWPOLYLINE"))))
(foreach item (ssnamex ss)
(cond
( (member (car item) '(1 4)); selected by pick or Fence
(setq
ent (cadr item); the Polyline
pt (osnap (cadr (nth 3 item)) "_nea")
; pick point or Fence-crossing point [forced to precisely on ent]
start (vlax-curve-getStartPoint ent)
end (vlax-curve-getEndPoint ent)
dist (vlax-curve-getDistAtPoint ent pt); where along it
); setq
(if (> (- (vlax-curve-getDistAtPoint ent (vlax-curve-getEndPoint ent)) dist) dist)
; pick or Fence-crossing point closer to start than end
(setq ; then
inspt (vlax-curve-getStartPoint ent)
rotpt (vlax-curve-getPointAtParam ent 1)
); setq
(setq ; else [closer to end]
inspt (vlax-curve-getEndPoint ent)
rotpt (vlax-curve-getPointAtParam ent (1- (vlax-curve-getEndParam ent)))
); setq
); if
(command "_.insert" "A-tapa" "_scale" 5 "_none" inspt rotpt)
); pick / Fence selection condition
( (member (car item) '(2 3)); selected by W, C, WP, CP
(setq ent (cadr item)); the Polyline
(command
"_.insert" "A-tapa" "_scale" 5
"_none" (vlax-curve-getStartPoint ent)
"_none" (vlax-curve-getPointAtParam ent 1)
"_.insert" "A-tapa" "_scale" 5
"_none" (vlax-curve-getEndPoint ent)
"_none" (vlax-curve-getPointAtParam ent (1- (vlax-curve-getEndParam ent)))
); command
); W/C/WP/CP condition
); cond
); foreach
); if
); defun
(vl-load-com)
No error handling, or the usual other bells and whistles, yet.
Kent Cooper, AIA