TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: tcdan on August 01, 2005, 02:00:07 AM

Title: (challenge) A Little Math Question
Post by: tcdan on August 01, 2005, 02:00:07 AM
Newbies will be able to do this as well as advanced LISPers.

Write a line of code (or more) that will set the NEArest object snap without modifying any of the other object snap settings.

It's a fun little math excercise :)
Title: (challenge) A Little Math Question
Post by: Crank on August 01, 2005, 02:40:12 AM
(SETVAR "OSMODE" (BOOLE 6 (GETVAR "OSMODE") 512)); on/off

(SETVAR "OSMODE" (- (GETVAR "OSMODE") -512 (LOGAND (GETVAR "OSMODE") 512))); on

or:

(SETVAR "OSMODE" (+ (LOGAND (GETVAR "OSMODE") 31231) 512)); on
Title: (challenge) A Little Math Question
Post by: MP on August 01, 2005, 07:27:52 AM
Quote from: tcdan
... Write a line of code that will set the NEArest object snap without modifying any of the other object snap settings ...

Code: [Select]
(setvar "osmode"
    (logior 512
        (getvar "osmode")
    )
)
Title: (challenge) A Little Math Question
Post by: tcdan on August 01, 2005, 11:11:36 AM
Wow that's great!  I knew you guys could come up with a faster way than this:
Code: [Select]
    (setq snaps (getvar "osmode"))
  (if ( = (rem (/ snaps 512) 2) 0) (setvar "osmode" (+ 512 snaps)))  ;; set 'nearest' snap
Title: (challenge) A Little Math Question
Post by: daron on August 01, 2005, 12:19:18 PM
Crank's first one's my personal favorite. It sets and unsets it. Boole is Coole.
Title: (challenge) A Little Math Question
Post by: Keith™ on August 01, 2005, 01:27:39 PM
Here is a neat little trick that doesn't require setting osnaps ... you can override it in lisp to the one you require.
Code: [Select]

(setq pt (osnap (getpoint "\nSelect point") "_nearest"))
Title: (challenge) A Little Math Question
Post by: Crank on August 04, 2005, 08:29:27 AM
Quote from: Keith
Here is a neat little trick that doesn't require setting osnaps ... you can override it in lisp to the one you require.
Code: [Select]

(setq pt (osnap (getpoint "\nSelect point") "_nearest"))

That's probably the best solution. You don't have to restore the osnap when you finish the function.

But if you do want to change the OSMODE then it's just an OR-operation, so this should also work:
Code: [Select]
(SETVAR "OSMODE" (BOOLE 7 (GETVAR "OSMODE") 512)); on
Title: (challenge) A Little Math Question
Post by: Jürg Menzi on August 04, 2005, 08:39:52 AM
(osnap pnt "_nea") fails on some objects. Therefore I use this solution:
Code: [Select]
(defun MeEntSel ( / CurEnt CurObj NeaPnt)
 (if (setq CurEnt (entsel "\nSelect object: "))
  (progn
   (setq CurObj (vlax-ename->vla-object (car CurEnt))
         NeaPnt (if (vl-position
                     (vla-get-ObjectName CurObj)
                    '("AcDbArc" "AcDbCircle" "AcDb2dPolyline")
                    )
                 (vlax-curve-getClosestPointToProjection
                  CurObj
                  (trans (cadr CurEnt) 1 0)
                  (trans (getvar "VIEWDIR") 1 0 1)
                 )
                 (osnap (cadr CurEnt) "_NEA")
                )
   )
   (list (car CurEnt) (trans NeaPnt 0 1))
  )
 )
)