Convert negative vector to positive vector

Convert negative vector to positive vector

carlos_m_gil_p
Advocate Advocate
883 Views
9 Replies
Message 1 of 10

Convert negative vector to positive vector

carlos_m_gil_p
Advocate
Advocate

Hello how are you.


Does anyone know of a function that identifies if a vector is negative?
And if it is negative, change it to positive.

 

I need to select a circle, get its vector and check if it is negative.
If your vector is negative, change it to positive, but leave it in the same position.

 

I started programming, but I couldn't identify if it is negative.

 

(defun c:xxx (/) 
  ;
  (setq obj (vlax-ename->vla-object (car (entsel "Select Circle"))))
  (setq normal (vlax-safearray->list (vlax-variant-value (vla-get-normal obj))))
  ;
)

 

I attach a dwg showing a circle with the positive vector and another with the negative vector.

 

Thank you.

 


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Accepted solutions (1)
884 Views
9 Replies
Replies (9)
Message 2 of 10

hosneyalaa
Advisor
Advisor

 

 

Hi @carlos_m_gil_p 

See this and try 

https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-EA1EE244-2D63-4491-8133-1ADAF9531BB6

 

;; Change the normal for the circle
    (setq newNormal (vlax-3d-point 1 1 -1))
    (vla-put-Normal circleObj newNormal)
    (vla-Update circleObj)
    (setq newNormal (vlax-variant-value newNormal))

 

Message 3 of 10

CADaSchtroumpf
Advisor
Advisor

Like this ?

(defun c:xxx ( / ss circleObj Normal newNormal)
  (princ "\nSelect circle")
  (while (setq ss (ssget "_+.:E:S" '((0 . "CIRCLE"))))
    (setq
      circleObj (vlax-ename->vla-object (ssname ss 0))
      Normal (vlax-get circleObj 'Normal)
    )
    (cond
      ((< (caddr Normal) 0.0)
        (setq
          newNormal (vlax-3d-point (list (car Normal) (cadr Normal) (abs (caddr Normal))))
        )
        (vla-put-Normal circleObj newNormal)
        (vla-Update circleObj)
        (princ "\nVector Z is now positive")
      )
      (T
        (princ "\nNo change")
      )
    )
    (princ "\nSelect circle")
  )
  (prin1)
)
Message 4 of 10

leeminardi
Mentor
Mentor

I took a different approach.  Rather than flipping the normal the code flips the circle with rotate3d about an axis perpendicular to the normal and the z axis. 

 

(defun c:flipn (/ en obj normal p vecPostive axis p2)
; flips circle whose normal is pointing in the negative Z direction
  (vl-load-com)
  (setq en (car (entsel "Select Circle")))
  (setq obj (vlax-ename->vla-object en))
  (setq	normal (vlax-safearray->list
		 (vlax-variant-value (vla-get-normal obj))
	       )
  )
  (setq	p (vlax-safearray->list
	    (vlax-variant-value (vla-get-center obj))
	  )
  )
  (setq vecPositive '(0 0 1))  ; defines the positive direction
  (if (< (dot normal vecPositive))
    (progn
      (setq axis (cross normal vecPositive)
	    p2	 (mapcar '+ p axis)
      )
      (command "_rotate3d" en "" "2" p p2 180)
    )
  )
  (princ)
)
;;; Compute the dot product of 2 vectors a and b
(defun dot (a b / dd)
  (setq dd (mapcar '* a b))
  (setq dd (+ (nth 0 dd) (nth 1 dd) (nth 2 dd)))
)					;end of dot

;;; Compute the cross product of 2 vectors a and b
(defun cross (a b / crs)
  (setq	crs (list
	      (- (* (nth 1 a) (nth 2 b))
		 (* (nth 1 b) (nth 2 a))
	      )
	      (- (* (nth 0 b) (nth 2 a))
		 (* (nth 0 a) (nth 2 b))
	      )
	      (- (* (nth 0 a) (nth 1 b))
		 (* (nth 0 b) (nth 1 a))
	      )
	    )				;end list
  )					;end setq c
)					;end cross

 

 

lee.minardi
Message 5 of 10

carlos_m_gil_p
Advocate
Advocate

Hello @hosneyalaa  thank you for your response.
But making the vector change, I know how to do it.
What I don't know is identifying when the vector is negative or positive.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Message 6 of 10

carlos_m_gil_p
Advocate
Advocate

Hello @CADaSchtroumpf  thank you for your response.
This option seems good to me to identify when the vector is negative or positive. (< (Normal caddr) 0.0)
I don't know if it's the best option but it has worked.
Something like that was what I needed.

 


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Message 7 of 10

leeminardi
Mentor
Mentor
Accepted solution

@carlos_m_gil_p   It appears that you are assuming that the meaning of a "positive vector" is one that has a world z component that is positive. A simple test.  In general, you can determine if two vectors are pointing in the same general diection by taking the dot product of the two vectors.  If the result is positive the projection of one of the vectors onto the other will be pointing in the same direction.  If negative, they are in opposite diections.

 

In my program I added a varirable for defining the meaning of "positive" and defined it as 0,0,1.  The z axis.

lee.minardi
Message 8 of 10

carlos_m_gil_p
Advocate
Advocate

Hello @leeminardi  thank you for your response.
Interesting option to rotate the circle, but it seems like more unnecessary code to me, but it still works.
The (< (dot normal vecPositive)) option also seems good to me to identify when the vector is negative or positive.
Which was what I was trying to look for at first, how to identify the negative and the positive.
I don't know if it's the best option but it has worked too.
Thank you.

 


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Message 9 of 10

carlos_m_gil_p
Advocate
Advocate

Hello @leeminardi  you respond very quickly, I hadn't gotten to your answer. Hahahahaha.
I did notice the dot product thing.
If so, I'm going to go with the dot product option, thanks for your explanation.
Sorry for not expressing myself well.

 


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Message 10 of 10

leeminardi
Mentor
Mentor

@carlos_m_gil_p   I decided to rotate the circle since I had a problem getting vla-put-Normal to work.

If your definition will always be z axis is defined as positve you can either go with (< (caddr nomral ) 0) or dot product.  I used the dot product because it provides the option of redifing the meaning of a positive vector. (and I prefer to work with vectors 😉).

lee.minardi