AutoLISP equivalent of CAL - VEC1 function?

AutoLISP equivalent of CAL - VEC1 function?

JBerns
Advisor Advisor
917 Views
8 Replies
Message 1 of 9

AutoLISP equivalent of CAL - VEC1 function?

JBerns
Advisor
Advisor

Community,

 

The AutoCAD CAL command offers the VEC1 function to calculate a unit vector.
https://help.autodesk.com/view/ACDLT/2025/ENU/?guid=GUID-28103F9A-424C-402B-9B10-A45147D07B96 

 

For example, (1.0 0.0 0.0) or (0.0 1.0 0.0) or (0.707107 0.707107 0)

 

Is there a more efficient way to calculate the unit vector in AutoLISP or Visual LISP than my method below?

 

(defun C:TEST ()
  (graphscr)
  (if (setq pt1 (getpoint "\nFirst point: "))
    (progn
      (if (setq pt2 (getpoint "\nSecond point: "))
        (progn
          ;; vector
          (setq	dx (- (car pt2) (car pt1))
                dy (- (cadr pt2) (cadr pt1))
                dz (- (caddr pt2) (caddr pt1))
          );_setq
          ;; unit vector
          (setq vx (atof (rtos (cos (angle pt1 pt2)) 2 8))
                vy (atof (rtos (sin (angle pt1 pt2)) 2 8))
                vz nil ;;???
          );_setq
          ;; report
          (princ "\nAutoLISP Vec: ") (princ (list dx dy dz))
          (princ "\nAutoLISP Vec1: ")(princ (list vx vy vz))
          (textscr)
        );_progn
      );_if
    );_progn
  );_if
  (princ)
);_C:TEST

 

I have searched references and online, but have not found an equivalent VEC1 function in AutoLISP or Visual LISP?

 

I am aware of (mapcar '- pt1 pt2) method to get a vector, but I need a unit vector. This will be used for an XLine.

 

BTW - I used ATOF and RTOS functions to round of values such as (6.12323e-17 1.0 nil).

 

Also, struggling to calculate the Z vector. Most times points will be 2D, but wanted option in case of 3D points.

Likely must calculate the point's incline from a plane, (presume XY of WCS). Assistance would be appreciated.

 

Thank you for your time and attention. I look forward to your replies.

 


Regards,
Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Accepted solutions (4)
918 Views
8 Replies
Replies (8)
Message 2 of 9

MunteanStefan
Contributor
Contributor
Accepted solution

Hi

 

You don't need the unit vector for XLINE. This one will work just fine:

(if
  (and
    (setq p1 (getpoint "\nSpecify first point: "))
    (setq p2 (getpoint "\nSpecify second point: "))
  )
  (progn
    (setq p1 (trans p1 1 0)
          p2 (trans p2 1 0)
    )
    (entmakex
      (list
       '(0 . "XLINE")
       '(100 . "AcDbEntity")
       '(100 . "AcDbXline")
        (cons 10 p1)
        (cons 11 (mapcar '- p2 p1))
      )
    )
  )
)

 

Anyway, if you want the unit vector, divide each coordinate of the vector by it's length.

(setq v (mapcar '- p2 p1))
(setq d (distance '(0 0 0) v))
(setq u (mapcar '(lambda (a) (/ a d)) v))

 

 

0 Likes
Message 3 of 9

Kent1Cooper
Consultant
Consultant
Accepted solution

I agree that you don't need the unit vector if the purpose is to draw an XLINE [it may store the directional element as a unit vector, but it doesn't need that to draw it].  But if you want the unit vector anyway or for some other purpose, this is a great application for a couple of uses of the (mapcar) function, with which you don't need to deal with XYZ separately:

(defun C:TEST ()
  (graphscr)
  (if (setq pt1 (getpoint "\nFirst point: "))
    (if (setq pt2 (getpoint "\nSecond point: "))
      (progn
        (setq
          dist (distance pt1 pt2)
          ;; vector:
          delta (mapcar '- pt2 pt1)
          unitvector (mapcar '/ delta (list dist dist dist))
        );_setq
        ;; report
        (princ "\nAutoLISP Vec: ") (princ delta)
        (princ "\nAutoLISP Vec1: ") (princ unitvector)
        (textscr)
      );_progn
    );_if
  );_if
  (princ)
)

 

Kent Cooper, AIA
0 Likes
Message 4 of 9

komondormrex
Mentor
Mentor
Accepted solution

try this one, and use 'trans' as per your needs.

 

(defun vec1 (pt1 pt2 / vec)
  (mapcar '(lambda (coordinate) ( / coordinate (apply 'max (mapcar 'abs vec)))) (setq vec (mapcar '- pt2 pt1)))
)

 

 

 

updated

0 Likes
Message 5 of 9

JBerns
Advisor
Advisor

@MunteanStefan / @Kent1Cooper  / @komondormrex ,

 

All great solutions! Thanks everyone.

I had looked at the entity data of an XLine and thought the unit vector was required.

Good to know otherwise.

I thought there would be mapcar and lambda functions involved. Not my strong skill.

 

Thanks again all.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
Message 6 of 9

JBerns
Advisor
Advisor

@komondormrex,

 

I discovered a "divide by zero" error when providing points such as vertical points picked in a negative (down) direction or horizontal points picked in a negative (right-left) direction.

 

Can you confirm the same please?

 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 7 of 9

komondormrex
Mentor
Mentor
Accepted solution

yep, already updated in #4.

0 Likes
Message 8 of 9

JBerns
Advisor
Advisor

Thanks, @komondormrex!

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 9 of 9

JBerns
Advisor
Advisor

@MunteanStefan / @Kent1Cooper / @komondormrex,

 

Demo of TL (Temp Lines) command:

 

Thanks again to all that assisted with the XLine creation for this utility.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional