Author Topic: Help with grvecs  (Read 1820 times)

0 Members and 1 Guest are viewing this topic.

Logan

  • Newt
  • Posts: 41
Help with grvecs
« on: July 18, 2017, 12:41:24 AM »
Hello guys.
I'm having trouble finalizing this code.
Maybe the answer is very simple, but I can not see it.
I am trying to draw a vector by limiting the angle to every 30 °.
It's working fine, but the last few angles do not draw perfectly.  :embarrassed2:
Can you help me?

Code: [Select]
(defun c:Every30 (/ ang mid pt1 pt2 lst gr)
  (setq pt1 (getpoint "\nFirst point: "))
  (while (= (car (setq gr (grread T 1))) 5)
    (if
      (setq lst
     (vl-some
       '(lambda (firstAng secondAng)
  (if
    (and
      (setq pt2 (cadr gr)
    ang (angle pt1 pt2)
      ) ;_ >setq
      (< ang firstAng)
      (> ang secondAng)
    ) ;_ >and
     (progn
       (setq mid (/ firstAng 2))
       (if
(> ang mid)
     (list 7 pt1 (polar pt1 firstAng (distance pt1 pt2)))
     (list 7 pt1 (polar pt1 secondAng (distance pt1 pt2)))
       ) ;_ >if
     ) ;_ >progn
  ) ;_ >if
) ;_ >lambda
       '(0.5236 1.0472 1.5708 2.0944 2.6180 3.1416 3.6652 4.1888 4.7124 5.2360 5.7596 0.0)
       '(0.0 0.5236 1.0472 1.5708 2.0944 2.6180 3.1416 3.6652 4.1888 4.7124 5.2360 5.7596)
     ) ;_ >vl-some
      ) ;_ >setq
       (progn
(redraw)
(grvecs lst)
       ) ;_ >progn
    ) ;_ >if
  ) ;_ >while
) ;_ >defun
;|«Visual LISP© Format Options»
(72 2 40 2 T ">" 60 9 0 0 nil T T T T)
;*** DO NOT add text below the comment! ***|;


Best regards, Luís Augusto
« Last Edit: July 18, 2017, 12:56:18 AM by Logan »

ronjonp

  • Needs a day job
  • Posts: 7524
Re: Help with grvecs
« Reply #1 on: July 18, 2017, 09:37:53 AM »
The code you posted works for me .. here's another way to do it:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:every30 (/ ang gr pt1 pt2)
  2.   (setq pt1 (getpoint "\nFirst point: "))
  3.   (while (= (car (setq gr (grread t 1))) 5)
  4.     (redraw)
  5.     (setq pt2 (cadr gr)
  6.           ang (angle pt1 pt2)
  7.     )
  8.     (grdraw pt1 (polar pt1 (- ang (rem ang (angtof "30"))) (distance pt1 pt2)) 1)
  9.   )
  10. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Logan

  • Newt
  • Posts: 41
Re: Help with grvecs
« Reply #2 on: July 18, 2017, 04:20:45 PM »
Thank you very much for sharing your knowledge Ron.

I'm feeling embarrassed, man.
Before I see your code, I tried to do it another way.
I've come to the conclusion that I need to study hard to get to their level.

Code: [Select]
(defun c:Every30 (/ ang mid pt1 pt2 lst gr)

  ;converts radians to degrees
  (defun RtD (r) (* 180.0 (/ r pi)))

  ;converts degrees to radians
  (defun DtR (d) (* pi (/ d 180.0)))

  (setq pt1 (getpoint "\nFirst point: "))

  (while (= (car (setq gr (grread T 1))) 5)

    (setq pt2 (cadr gr)
  ang (RtD (angle pt1 pt2))
    ) ;_ >setq

    (cond
      (
       (and (>= ang 0) (<= ang 30))
       (redraw)
       (if
(> ang 15)
  (grvecs (list 7 pt1 (polar pt1 (DtR 30) (distance pt1 pt2)))
  ) ;_ >grvecs
  (grvecs (list 7 pt1 (polar pt1 0.0 (distance pt1 pt2))))
       ) ;_ >if
      )
      (
       (and (>= ang 30) (<= ang 60))
       (redraw)
       (if
(> ang 45)
  (grvecs (list 7 pt1 (polar pt1 (DtR 60) (distance pt1 pt2)))
  ) ;_ >grvecs
  (grvecs (list 7 pt1 (polar pt1 (DtR 30) (distance pt1 pt2)))
  ) ;_ >grvecs
       ) ;_ >if
      )
      (
       (and (>= ang 60) (<= ang 90))
       (redraw)
       (if
(> ang 75)
  (grvecs (list 7 pt1 (polar pt1 (DtR 90) (distance pt1 pt2)))
  ) ;_ >grvecs
  (grvecs (list 7 pt1 (polar pt1 (DtR 60) (distance pt1 pt2)))
  ) ;_ >grvecs
       ) ;_ >if
      )
      (
       (and (>= ang 90) (<= ang 120))
       (redraw)
       (if
(> ang 105)
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 120) (distance pt1 pt2)))
  ) ;_ >grvecs
  (grvecs (list 7 pt1 (polar pt1 (DtR 90) (distance pt1 pt2)))
  ) ;_ >grvecs
       ) ;_ >if
      )
      (
       (and (>= ang 120) (<= ang 150))
       (redraw)
       (if
(> ang 135)
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 150) (distance pt1 pt2)))
  ) ;_ >grvecs
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 120) (distance pt1 pt2)))
  ) ;_ >grvecs
       ) ;_ >if
      )
      (
       (and (>= ang 150) (<= ang 180))
       (redraw)
       (if
(> ang 165)
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 180) (distance pt1 pt2)))
  ) ;_ >grvecs
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 150) (distance pt1 pt2)))
  ) ;_ >grvecs
       ) ;_ >if
      )
      (
       (and (>= ang 180) (<= ang 210))
       (redraw)
       (if
(> ang 195)
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 210) (distance pt1 pt2)))
  ) ;_ >grvecs
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 180) (distance pt1 pt2)))
  ) ;_ >grvecs
       ) ;_ >if
      )
      (
       (and (>= ang 210) (<= ang 240))
       (redraw)
       (if
(> ang 225)
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 240) (distance pt1 pt2)))
  ) ;_ >grvecs
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 210) (distance pt1 pt2)))
  ) ;_ >grvecs
       ) ;_ >if
      )
      (
       (and (>= ang 240) (<= ang 270))
       (redraw)
       (if
(> ang 255)
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 270) (distance pt1 pt2)))
  ) ;_ >grvecs
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 240) (distance pt1 pt2)))
  ) ;_ >grvecs
       ) ;_ >if
      )
      (
       (and (>= ang 270) (<= ang 300))
       (redraw)
       (if
(> ang 285)
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 300) (distance pt1 pt2)))
  ) ;_ >grvecs
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 270) (distance pt1 pt2)))
  ) ;_ >grvecs
       ) ;_ >if
      )
      (
       (and (>= ang 300) (<= ang 330))
       (redraw)
       (if
(> ang 315)
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 330) (distance pt1 pt2)))
  ) ;_ >grvecs
  (grvecs
    (list 7 pt1 (polar pt1 (DtR 300) (distance pt1 pt2)))
  ) ;_ >grvecs
       ) ;_ >if
      )
      (
       (and (>= ang 330) (<= ang 360))
       (redraw)
       (if (> ang 345)
(grvecs
   (list 7 pt1 (polar pt1 (DtR 360) (distance pt1 pt2)))
) ;_ >grvecs
(grvecs
   (list 7 pt1 (polar pt1 (DtR 330) (distance pt1 pt2)))
) ;_ >grvecs
       ) ;_ >if
      )
    ) ;_ >cond

  ) ;_ >while

) ;_ >defun

I'll try to modify your code to draw a polyline, of course, if you let it.

Best regards, Luís Augusto
« Last Edit: July 18, 2017, 04:26:57 PM by Logan »

ChrisCarlson

  • Guest
Re: Help with grvecs
« Reply #3 on: July 18, 2017, 04:26:25 PM »
If you are doing every 30°, why not simply enable Polar Tracking?

ronjonp

  • Needs a day job
  • Posts: 7524
Re: Help with grvecs
« Reply #4 on: July 18, 2017, 04:44:32 PM »
If you are doing every 30°, why not simply enable Polar Tracking?
This^^

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Logan

  • Newt
  • Posts: 41
Re: Help with grvecs
« Reply #5 on: July 18, 2017, 09:19:35 PM »
If you are doing every 30°, why not simply enable Polar Tracking?
Certainly yes Master_Shake!
Could it be that I was wrong to enter this forum?  :laugh:
The problem with Polar Tracking is that it allows you to create a vector between the angles that I intend to limit. I also did not want to change variables in the user environment. Then I saw in that case a challenge for me.

I'm sorry I was not clear with my intentions, my friend.
I figured the fact that I got here with an attempt at code, showed that my only intention was to train my skills as a programmer. (Not that I'm a programmer)
Proof of this was that I thanked him for the knowledge Ron gave me and not for his code.  :wink:
Thank you very much for sharing your knowledge Ron.
And just below I emphasized that I needed to study a little more ...
I've come to the conclusion that I need to study hard to get to their level.
I often write things that have no use for me at that moment, just to exercise.
When I find it difficult to find the solution I search insanely before asking for help, unlike those who arrive here in the Swamp empty-handed demanding a solution to their problems.
Thank you for expanding my vision.

Best regards, Luís Augusto.
« Last Edit: July 18, 2017, 09:44:12 PM by Logan »

ronjonp

  • Needs a day job
  • Posts: 7524
Re: Help with grvecs
« Reply #6 on: July 19, 2017, 09:38:39 AM »
Give this a try:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:every30 (/ _lwpolyline ang lwp p1 p2 tmp)
  2.   (defun _lwpolyline (points)
  3.     (entmakex (append (list (cons 0 "LWPOLYLINE")
  4.                             (cons 100 "AcDbEntity")
  5.                             (cons 100 "AcDbPolyline")
  6.                             (cons 90 (length points))
  7.                             (cons 70 0)
  8.                       )
  9.                       (mapcar (function (lambda (p) (cons 10 p))) points)
  10.               )
  11.     )
  12.   )
  13.   (while
  14.     (and (or p1 (setq p1 (getpoint "\nSpecify start point: ")))
  15.          (setq p2 (getpoint p1 "\nSpecify next point: "))
  16.          (setq p2 (polar p1 (- (setq ang (angle p1 p2)) (rem ang (angtof "30"))) (distance p1 p2)))
  17.     )
  18.      (if lwp
  19.        (setq tmp (subst (cons 90 (1+ (cdr (assoc 90 (entget lwp)))))
  20.                         (assoc 90 (entget lwp))
  21.                         (entget lwp)
  22.                  )
  23.              tmp (entmod (append tmp (list (cons 10 p2))))
  24.        )
  25.        (setq lwp (_lwpolyline (list p1 p2)))
  26.      )
  27.      (setq p1 p2)
  28.   )
  29.   (princ)
  30. )
« Last Edit: July 19, 2017, 09:44:02 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Logan

  • Newt
  • Posts: 41
Re: Help with grvecs
« Reply #7 on: July 20, 2017, 12:23:25 AM »
Many thanks for your sample Ron.
Your code is very inspiring.  :idea:
Soon I will post my attempt.
Thanks again for sharing your knowledge with us.  :wink: