Author Topic: how to calculate the correct nor?  (Read 1315 times)

0 Members and 1 Guest are viewing this topic.

ssdd

  • Newt
  • Posts: 35
how to calculate the correct nor?
« on: December 03, 2016, 07:34:18 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:tt()
  2.         Space (if (= 1 (getvar "CVPORT"))(vla-get-PaperSpace AcDoc)(vla-get-ModelSpace AcDoc)))
  3.    (setq ss (ssget  (List (cons 0 "spline,line,arc,LWPOLYLINE"))) n -1)
  4.    (while (< n (- (sslength ss) 1))            
  5.     (setq e (ssname ss (setq n (1+ n))))
  6.     (setq en (vlax-ename->vla-object e))
  7.     (setq sta (vlax-curve-getStartPoint  en))
  8.     (setq end (vlax-curve-getEndPoint    en))
  9.     (setq nor (vlax-curve-getFirstDeriv en (vlax-curve-getStartParam en)));(Error, how to calculate the correct nor?)
  10.     (setq en1 (VL-AddLWPolyline Space (list(list 5 20 0)(list 5 -20 0)(list -5 -20 0)(list -5 20 0)):vlax-true))
  11.     (vla-put-Normal en1 (vlax-3d-Point nor))
  12.      (vla-Move  en1 (vlax-3d-Point  0 0 0) (vlax-3d-Point  sta))
  13. )
  14.   (vla-EndUndoMark AcDoc)
  15.   (princ))
  16.  
  17.  
  18.  
  19. (defun VL-AddLWPolyline (Space Pts Closed / obj)
  20.   (setq Pts
  21.     (apply
  22.       (quote append)
  23.       (mapcar
  24.         (function
  25.           (lambda (%)
  26.             (list (car %) (cadr %))
  27.           )
  28.         )
  29.         (mapcar
  30.           (function
  31.             (lambda (%)
  32.               (trans % 1 (trans '(0 0 1) 1 0 T))
  33.             )
  34.           )
  35.           Pts
  36.         )
  37.       )
  38.     )
  39.   )
  40.   (setq obj
  41.       (vlax-make-variant
  42.         (vlax-safearray-fill
  43.           (vlax-make-safearray vlax-vbdouble (cons 0 (1- (length Pts))))
  44.           Pts
  45.         )
  46.       )
  47.     )
  48.   )
  49.   (if Closed (vla-put-closed obj Closed))
  50.   obj
  51. )


Edit (John) : Added code tags.
« Last Edit: December 03, 2016, 07:58:56 AM by John Kaul (Se7en) »

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: how to calculate the correct nor?
« Reply #1 on: December 03, 2016, 03:17:36 PM »
Try this (untested) :

Code: [Select]
(setq nor
  (mapcar '/
    (vlax-curve-getFirstDeriv en (vlax-curve-getStartParam en))
    (list
      (distance '(0 0 0) (vlax-curve-getFirstDeriv en (vlax-curve-getStartParam en)))
      (distance '(0 0 0) (vlax-curve-getFirstDeriv en (vlax-curve-getStartParam en)))
      (distance '(0 0 0) (vlax-curve-getFirstDeriv en (vlax-curve-getStartParam en)))
    )
  )
)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: how to calculate the correct nor?
« Reply #2 on: December 03, 2016, 04:35:39 PM »
The problem is related to the Arbitrary Axis Algorithm. The best approach IMO is to create the new polyline in the WCS and then use a transformation matrix:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:tt ( / acDoc n norNew norObj obj ss)
  2.   (vla-endundomark acDoc)
  3.   (if (setq ss (ssget '((0 . "SPLINE,LINE,ARC,LWPOLYLINE"))))
  4.     (repeat (setq n (sslength ss))
  5.       (setq obj (vlax-ename->vla-object (ssname ss (setq n (1- n)))))
  6.       (setq norObj (vlax-get obj 'normal))
  7.       (setq norNew (KGA_Geom_VectorUnit (vlax-curve-getfirstderiv obj (vlax-curve-getstartparam obj))))
  8.       (vla-transformby
  9.         (VL-AddLWPolyline
  10.           (vlax-get acDoc (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace))
  11.           '((5.0 20.0 0.0) (5.0 -20.0 0.0) (-5.0 -20.0 0.0) (-5.0 20.0 0.0))
  12.           :vlax-true
  13.         )
  14.         (vlax-tmatrix
  15.           (KGA_Geom_MatrixMake
  16.             (KGA_Geom_VectorCrossProduct norNew norObj) ; X vector.
  17.             norObj                                      ; Y vector.
  18.             norNew                                      ; Z vector.
  19.             (vlax-curve-getstartpoint obj)              ; Origin.
  20.           )
  21.         )
  22.       )
  23.     )
  24.   )
  25.   (vla-endundomark acDoc)
  26.   (princ)
  27. )
  28.  
  29. (defun VL-AddLWPolyline (Space Pts Closed / obj)
  30.   (setq obj
  31.       Space
  32.       (vlax-make-variant
  33.         (vlax-safearray-fill
  34.           (vlax-make-safearray vlax-vbdouble (cons 0 (1- (* 2 (length Pts)))))
  35.           (apply
  36.             (quote append)
  37.             (mapcar
  38.               (function
  39.                 (lambda (%) (list (car %) (cadr %)))
  40.               )
  41.               Pts
  42.             )
  43.           )
  44.         )
  45.       )
  46.     )
  47.   )
  48.   (vla-put-normal obj (vlax-3d-point '(0.0 0.0 1.0))) ; Normal of the WCS.
  49.   (if Closed (vla-put-closed obj Closed))
  50.   obj
  51. )
  52.  
  53. (defun KGA_Geom_MatrixMake (xVec yVec zVec org)
  54.   (append
  55.     (mapcar 'list xVec yVec zVec org)
  56.     '((0.0 0.0 0.0 1.0))
  57.   )
  58. )
  59.  
  60. ; Yes, I know! There are nicer looking cross product functions out there... But are they faster?
  61. (defun KGA_Geom_VectorCrossProduct (vec1 vec2)
  62.   (list
  63.     (float (- (* (cadr vec1) (caddr vec2)) (* (caddr vec1) (cadr vec2))))
  64.     (float (- (* (caddr vec1) (car vec2)) (* (car vec1) (caddr vec2))))
  65.     (float (- (* (car vec1) (cadr vec2)) (* (cadr vec1) (car vec2))))
  66.   )
  67. )
  68.  
  69. (defun KGA_Geom_VectorScale (vec scl)
  70.   (mapcar '(lambda (a) (* a (float scl))) vec)
  71. )
  72.  
  73. (defun KGA_Geom_VectorUnit (vec / mag)
  74.   (if (/= 0.0 (setq mag (distance '(0.0 0.0 0.0) vec)))
  75.     (KGA_Geom_VectorScale vec (/ 1.0 mag))
  76.   )
  77. )

ssdd

  • Newt
  • Posts: 35
Re: how to calculate the correct nor?
« Reply #3 on: December 03, 2016, 07:13:03 PM »
thank  Marko !
thank  roy_043!