Stretch a line equally in both directions

Stretch a line equally in both directions

Anonymous
Not applicable
1,807 Views
4 Replies
Message 1 of 5

Stretch a line equally in both directions

Anonymous
Not applicable

Would it be possible to stretch a line in both directions by 100 along its length, which is along the Y axis, by autolisp.

0 Likes
Accepted solutions (1)
1,808 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant

It's more LENGTHEN DELTA than STRETCH what you want.

 

(defun c:LengthenDeltaBoth ( / ss en)
  (if (and (setq ss (ssget "_+.:E:S" '((0 . "LINE"))))
	   (setq en (ssname ss 0)))
    (command "_.LENGTHEN"
	     "_DE"
	     100
	     (list en (trans (cdr (assoc 10 (entget en))) 0 1))
	     (list en (trans (cdr (assoc 11 (entget en))) 0 1))
	     ""
	     ))
  (princ)
)

 

0 Likes
Message 3 of 5

Anonymous
Not applicable

Thanks again, colud this be done for all lines from layer bolt_axis in the whole drawing?

0 Likes
Message 4 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

Ok, then probably this way would be better.

 

(defun c:LengthenDeltaBoth ( / ss ed i p1 p2)
  (if (setq ss (ssget "_X" '((0 . "LINE") (8 . "bolt_axis"))))
    (repeat (setq i (sslength ss))
      (setq ed (entget (ssname ss (setq i (1- i))))
	    p1 (cdr (assoc 10 ed))
	    p2 (cdr (assoc 11 ed))
	    ed (subst (cons 10 (polar p1 (angle p2 p1) 100.))
		      (assoc 10 ed)
		      ed)
	    ed (subst (cons 11 (polar p2 (angle p1 p2) 100.))
		      (assoc 11 ed)
		      ed))
      (entmod ed))
    (princ "\nNo lines on 'bolt_axis' layer in the drawing."))
  (princ)
)
Message 5 of 5

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:

.... 

....
      (setq ....
....
ed (subst (cons 10 (polar p1 (angle p2 p1) 100.)) (assoc 10 ed) ed) ed (subst (cons 11 (polar p2 (angle p1 p2) 100.)) (assoc 11 ed) ed)) (entmod ed)....

Here's an interesting factoid I picked up somewhere a while back:  Instead of (subst)ituting 10- and 11-code entries into the entity data in place of the current ones, you can simply tack them onto the end, leaving the older ones in the list, and the later/newer ones will override the earlier/older ones!  So the above-quoted parts, which revise the entity data list twice to make the two substitutions and then (entmod) the result, can be replaced by this:

 

 

      (entmod
        (append
          ed
          (list
            (cons 10 (polar p1 (angle p2 p1) 100))
            (cons 11 (polar p2 (angle p1 p2) 100))
          ); list
        ); append
      ); entmod

[By the way, the decimal points can be used on the 100's if you want, but are not needed -- they accomplish nothing in this situation.]

 

Another thing that can be done [made easier by (ssget)'s "_X" mode -- it's slightly more complicated if User selection is involved], is to convert the selection set to a list of entity names, and use (foreach) on that list, eliminating the need for an incrementing variable in a (repeat) function.  And it also occurred to me to try doing it by SCALE-ing each Line about its midpoint, rather than changing endpoint locations, which can accomplish it with a little less code:

 

(defun C:LBE100 (/ ss edata p1 p2 len); = Lengthen Both Ends by 100 units
  (if (setq ss (ssget "_X" '((0 . "LINE") (8 . "bolt_axis"))))
    (foreach lin (mapcar 'cadr (ssnamex ss)); then
      (setq
        edata (entget lin)
        p1 (cdr (assoc 10 edata))
        p2 (cdr (assoc 11 edata))
        len (distance p1 p2)
      ); setq
      (command "_.scale" lin "" "_mtp" p1 p2 (/ (+ len 200) len)); by ratio of new length to old
    ); foreach
    (prompt "\nNo lines on 'bolt_axis' layer in the drawing."); else
  ); if
  (princ)
); defun

However, that won't work on any Lines that are not in the current space, so if multiple spaces are possible, the (entmod) approach should be used.

 

Those with old-enough versions that they don't have the MTP/M2P Osnap mode can replace this part:

  "_mtp" p1 p2

with this to find the Line's midpoint:

  "_none" (mapcar '/ (mapcar '+ p1 p2) '(2 2 2))

Kent Cooper, AIA
0 Likes