Author Topic: My arrow not same as cursor position  (Read 2378 times)

0 Members and 1 Guest are viewing this topic.

Adesu

  • Guest
My arrow not same as cursor position
« on: December 04, 2006, 09:51:52 PM »
Hi Alls,
I just create a program lisp to rotate an object,that object is an Arrow,if user move his cursor,the arrow should be follow cursor location,but my code still problem.
as a sample if cursor move to top drawing area,the arrow not to top position,maybe left or righ side,I want the arrow follow it the cursor moving,here that code.
Code: [Select]
; arbc is stand for Automatic Rotate By Cursor
;        Design by  : Adesu <Ade Suharna>
;        Email      : mteybid@yuasabattery.co.id
;        Homepage   : http://www.yuasa-battery.co.id
;        Create     : 05 December 2006
;        Program no.: 0480/12/2006
;        Edit by    :

(defun massoc (key alist / x nlist)          ; Jaysen Long
  (foreach x alist
    (if
      (eq key (car x))
      (setq nlist (cons (cdr x) nlist))
      )
    )
  (reverse nlist)
  )

(defun c:arbc (/ ang_arrow ang_cursor cen el gr head
       loc lst p1 p2 p3 pt sse)
  (setq loc '(0 0 0))
  (setq p1 (polar loc 0 5))
  (setq p2 (polar loc (* pi 0.75) 5))
  (setq p3 (polar loc (* pi 1.25) 5))
  (command "_pline" p1 p2 loc p3 "c" "")
  (setq el (entlast))
  (while
    (setq gr (grread t 7))
    (setq pt (cadr gr))
    (setq ang_cursor (angle pt loc))
    (setq sse (entget el))
    (setq lst (massoc 10 sse))
    (setq head (nth 0 lst))   
    (setq cen (nth 2 lst))     
    (setq ang_arrow (angle cen head))
    (if
      (not (eq ang_cursor ang_arrow))
      (command "_rotate" el "" loc "r" 0 ang_cursor "")
      )                                         ; if
    )                                           ; while
  (princ)
  )                                             ; defun

Derwin

  • Guest
Re: My arrow not same as cursor position
« Reply #1 on: December 04, 2006, 11:32:59 PM »
your problem was for some reason an incrementing rotate, you wanted an absolute. heres mine:

Quote
(defun polaradd( ANG1 ANG2)
  (rem (+ 2PI ANG1 ANG2) 2PI)
)
(defun drawarrow( loc dir / p1 p2 p3)
  (setq p1 (polar loc (polaradd dir 0) 5))
  (setq p2 (polar loc (polaradd dir (* pi 0.75)) 5))
  (setq p3 (polar loc (polaradd dir (* pi 1.25)) 5))
  (grdraw p1 p2 -1)
  (grdraw p2 loc -1)
  (grdraw loc p3 -1)
  (grdraw p3 p1 -1)
)
(defun c:arbc (/ loc ang_arrow ang_cursor )
  (setq loc '(0 0 0))
  (setq ang_arrow (angle loc (cadr (grread t 7))))
  (drawarrow loc ang_arrow) ;draw first
  (while
    (setq ang_cursor (angle loc (cadr (grread t 7))))
    (if (not (eq ang_cursor ang_arrow))
      (progn
        (drawarrow loc ang_arrow) ;erase last
        (drawarrow loc ang_cursor) ;draw new
        (setq ang_arrow ang_cursor)
      );endPROGN
    );endIF
  );endWHILE 
  (princ)
)


Adesu

  • Guest
Re: My arrow not same as cursor position
« Reply #2 on: December 05, 2006, 01:17:48 AM »
Hi Derwin,
Your code still
Code: [Select]
_$
POLARADD
DRAWARROW
_$ (setq loc '(0 0 0))
(0 0 0)
_$ (setq ang_arrow (angle loc (cadr (grread t 7))))
5.09273
_$
_$ (drawarrow loc ang_arrow)
; error: bad argument type: numberp: nil
_$

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: My arrow not same as cursor position
« Reply #3 on: December 05, 2006, 01:42:25 AM »
Adesu,
you'd need something like this ;

(setq 2PI ( * 2 PI) )
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Adesu

  • Guest
Re: My arrow not same as cursor position
« Reply #4 on: December 05, 2006, 01:59:51 AM »
Hi Kerry,
it's great,thanks you.

Adesu

  • Guest
Re: My arrow not same as cursor position
« Reply #5 on: December 05, 2006, 03:34:24 AM »
Hi,
I'm got headache to analysis what function of
Code: [Select]
(defun polaradd( ANG1 ANG2)
(setq 2PI ( * 2 PI) )
  (rem (+ 2PI ANG1 ANG2) 2PI)
)

And then I remove it,and change with this in order to more simple
Code: [Select]
(defun drawarrow (loc ang_arrow / p1 p2 p3)       
  (setq ang 0)
  (setq p1 (polar loc (+ ang_arrow ang) 5))
  (setq p2 (polar loc (+ ang_arrow (* pi 0.75)) 5))
  (setq p3 (polar loc (+ ang_arrow (* pi 1.25)) 5))
  (grdraw p1 p2 -1)
  (grdraw p2 loc -1)
  (grdraw loc p3 -1)
  (grdraw p3 p1 -1)
  )

(defun c:arbc (/ ang_arrow ang_cursor gr loc)
  (setq loc '(0 0 0))                             
  (setq gr (cadr (grread t 7)))                   
  (setq ang_arrow (angle loc gr))                 
  (drawarrow loc ang_arrow)                       
  (while
    (setq ang_cursor (angle loc (cadr (grread t 7))))   
    (if
      (not (eq ang_cursor ang_arrow))                     
      (progn
(drawarrow loc ang_arrow)               
(drawarrow loc ang_cursor)               
(setq ang_arrow ang_cursor)             
)                                        ; progn
      )                                          ; if
    )                                            ; while
  (princ)
  )

And thanks again to "Derwin",you just give me input for that code.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: My arrow not same as cursor position
« Reply #6 on: December 05, 2006, 08:31:39 AM »
Not sure exactly what you are trying to do, but try this.

Code: [Select]
(defun c:test (/ gr org pt ang_arrow)
  (defun arrow (p0 arw_ang col / p1 p2 p3)
    (setq p1 (polar p0 (+ arw_ang 0) 5))
    (setq p2 (polar p0 (+ arw_ang (* pi 0.75)) 5))
    (setq p3 (polar p0 (+ arw_ang (* pi 1.25)) 5))
    (grvecs (list col p1 p2 p2 p0 p0 p3 p3 p1))
  )

  (setq org '(0 0 0))
  (while (= (car (setq gr (grread nil 7))) 5)
    (setq pt        (cadr gr)
          ang_arrow (angle org pt)
    )
    (redraw)
    (arrow pt ang_arrow -1)
  )
  (princ)
)


Edit: code clean up
« Last Edit: December 05, 2006, 07:33:53 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Adesu

  • Guest
Re: My arrow not same as cursor position
« Reply #7 on: December 05, 2006, 06:41:18 PM »
Hi CAB,
Good alternative,thanks for your sharing.

Derwin

  • Guest
Re: My arrow not same as cursor position
« Reply #8 on: December 05, 2006, 06:45:52 PM »
sorry i left the 2PI out, id added that one to my startup lisp from day one. as to why 'polaradd':
1. during my second day of lisping, i thought i saw angles > 2PI drawing funny. this is the 'rem A 2PI'. i now suspect i was mistaken.
2. the extra addition of 2PI within 'polaradd' was so i could pass a negative angle and get a 'polarsub'.
3. that it is a seperate function is because some days when i get generative creative, modifying polaradd is a common tactic, by adding another term, modifying a running total, scaling etc..

CABs code is obviously smaller and neater, and so id use that,
can i ask what this function is for?