Pedro
Pedro

Reputation: 49

Getting the measurements for stretching

I am creating an algorithm to help me expand boxes to the correct size, as such:

inteded result

I made a code that asks two points that should be the new depth and measures them. Then subtracts to the original depth of the block (0.51) and then asks the side to stretch.

(defun mystretchh ( dis / pt1 pt2 sel )
    (while
        (and
            (setq pt1 (getpoint "\nFirst point of selection window: "))
            (setq pt2 (getcorner pt1 "\nSecond point of selection window: "))
            (not (setq sel (ssget "_C" pt1 pt2)))
        )
        (princ "\nNo objects where found in the selection window.")
    )
    (if sel
        (progn
            (command "_.stretch" sel "" "_non" '(0 0) "_non" (list dis 0))
            t
        )
    )
)
(defun c:test (/ a x c p)
;ungroup
(command "pickstyle" 0)
;variables
(initget (+ 1 2 4))
(setq p (getpoint "\nSelect first point: "))
(setq c (getpoint "\nSelect second point: "))
(command "_.dist" p c)
(setq x (rtos (getvar 'distance) 2 3))


    ;calculate distance to stretch
    (setq a (- x 0.51))


    ;stretch
    (mystretchh a)

    ;regroup
    (command "pickstyle" 1)
    (print "Module modified.")
    (princ)
)

I have two problems:

  1. The stretch is working backwards, I tried using negative values to no avail.
  2. It reports a syntax error, but I cannot find it.

I haven't touched AutoLISP for half a year - maybe some of you can find the problems in a blink of an eye.

Upvotes: 0

Views: 347

Answers (1)

Lee Mac
Lee Mac

Reputation: 16015

You can calculate the distance between two points using the standard distance function, rather than calling the DIST command and then retrieving the value of the DISTANCE system variable.

Hence, this:

(command "_.dist" p c)
(setq x (rtos (getvar 'distance) 2 3))

Can become:

(setq x (rtos (distance p c) 2 3))

However, you are receiving a syntax error because you have converted the distance to a string using rtos, and then you are attempting to perform arithmetic on the string here:

(setq a (- x 0.51))

There is no need to convert the distance to a string, and so these expressions can become:

(setq a (- (distance p c) 0.51))

You may also want to check whether (distance p c) is greater than 0.51 before performing this subtraction to avoid unexpected results.

To determine the appropriate direction, since your current code can only stretch along the x-axis, you'll need to check whether or not the x-coordinate of point p is greater than that of point c.

Upvotes: 1

Related Questions