Lisp erases snap settings- Please help!

Lisp erases snap settings- Please help!

Anonymous
Not applicable
1,229 Views
7 Replies
Message 1 of 8

Lisp erases snap settings- Please help!

Anonymous
Not applicable

Hi guys,

I have a beautifull lisp that I found that creates lines at specific devisions from the user , my snap settings are usually on ( I select all my object snap so I can pick every point as my cusor change ) but when I want to use my lisp again I need to select all the object snap again.
How can I keep my setting so it will not erase them?

0 Likes
Accepted solutions (1)
1,230 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
Accepted solution

May be

0 Likes
Message 3 of 8

ВeekeeCZ
Consultant
Consultant

Try... the routine should not end up with error on the regular basis. You should change the algorithm, not just add an error handling.

BTW It changes a current layer too - it should be returned as well.

 

(vl-load-com)

(defun c:somefunc  (/ *error* f curlayer curosmode ent exit a b spaces spaces1 testlst vlaobj1 vlaobj2 x y z)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (setvar 'clayer curlayer)
    (setvar 'osmode curosmode)
    (princ))
  
  
  (setq curlayer  (getvar 'clayer)
        curosmode (getvar 'osmode))
  
  (while (and (setq ent (car (entsel)))
              (setq vlaObj1   (vlax-ename->vla-object ent))
              (setq vlaObj2   (vlax-ename->vla-object (car (entsel))))
              (setq spaces1   (getreal "\nEnter number of spaces: ")))
    (cond ((or (minusp spaces1) (< spaces1 1)) "Spaces should be greater than 1")
          (T
           (setvar 'clayer (cdr (assoc 8 (entget ent))))
           (setvar 'osmode 16384)
           (setq spaces  (test spaces1 ())
                 testlst (vl-sort
                           (apply
                             'append
                             (mapcar
                               '(lambda (y)
                                  (mapcar '(lambda (x) (append (append (list (distance x y)) (list x)) (list y)))
                                          (list (vlax-curve-getStartPoint vlaObj1)
                                                (vlax-curve-getEndPoint vlaObj1))))
                               (list (vlax-curve-getStartPoint vlaObj2)
                                     (vlax-curve-getEndPoint vlaObj2))))
                           '(lambda (x y) (< (car x) (car y))))
                 a       (cdar testlst)
                 b       (cdr (apply 'append
                                     (car (mapcar '(lambda (y)
                                                     (mapcar '(lambda (x)
                                                                (if (not (or (equal (cadar testlst) (cadr x) y)
                                                                             (equal (caddar testlst) (caddr x) y)
                                                                             (equal (cadar testlst) (caddr x) y)
                                                                             (equal (caddar testlst) (cadr x) y)))
                                                                  x))
                                                             testlst))
                                                  (list 1e-15))))))
           (command "._undo" "_begin")
           (test2 a b spaces1 spaces)
           (command "._undo" "_end")
           (initget "Yes No")
           (setq f (cond ((getkword "\nFLip lines [Yes/No] <No>: "))
                         ("No")))
           (cond ((wcmatch f "Yes,Y")
                  (command "._undo" 1)
                  (setq z (last a))
                  (setq a (cons (car a) (cdr b)))
                  (setq b (cons (car b) (list z)))
                  (test2 a b spaces1 spaces))
                 (T ())))))
  (setvar 'clayer curlayer)
  (setvar 'osmode curosmode)
  (princ))

(defun test  (x y)
  (cond ((< x 1) y)
        (T (test (fix (- x 1)) (cons (fix x) y)))))

(defun test2  (a b c d)
  (mapcar '(lambda (x y) (command-s "._pline" x y ""))
          (mapcar '(lambda (x) (polar (car a) (angle (car a) (cadr a)) (* (/ (distance (car a) (cadr a)) c) x)))
                  (if (not (zerop (rem c (fix c))))
                    d
                    (cdr (reverse d))))
          (mapcar '(lambda (x) (polar (car b) (angle (car b) (cadr b)) (* (/ (distance (car b) (cadr b)) c) x)))
                  (if (not (zerop (rem c (fix c))))
                    d
                    (cdr (reverse d))))))

  

0 Likes
Message 4 of 8

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

... lisp ... that creates lines at specific devisions from the user ....


Try SplitBetween.lsp, with its SLB command, available here.  The main advantage is that it doesn't need to ask you whether you want to Flip the resulting Lines -- it figures out for itself whether the source Lines/Polylines are drawn in opposite directions, and draws the new Lines accordingly.  But it has other advantages.  You can just pick four points, if you don't already have existing Lines, or if you want to use one segment of a multi-segment-Polyline, or locations on Blocks, etc.  It has meaningful prompts, highlights selected Lines so you see you got them, and if you miss or pick the wrong kind of thing, it asks you again instead of failing.  On the same thread are versions that draw Polylines instead of Lines, and that draw on the current Layer regardless of the Layer of selected Lines.

Kent Cooper, AIA
0 Likes
Message 5 of 8

Kent1Cooper
Consultant
Consultant

Some commentary on the code in LINE_DIV.lsp, just because some things caught my attention:

 

This part:

 

    (cond ((or (minusp spaces1) (< spaces1 1)) "Spaces should be greater than 1")

 

allows 1 itself, which is meaningless, and since negative numbers are always less than any positive-number cut-off, it's testing for more things than it needs to.  Also, it spells out a message that it does not convey to the User in any way.  It should be something like:

 

    (cond ((< spaces1 2) (prompt "Spaces should be greater than 1"))

 

This line:

 

    (setvar 'osmode 16384)

 

is viable but curious -- most routines set it to 0.  At 16384, Osnap is off and no modes are operative, which is redundant.  At 0, Osnap is "on" but no modes are operative, so it's effectively off anyway.

 

This part:

 

           (initget "Yes No")
           (setq f (cond ((getkword "\nFLip lines [Yes/No] <No>: "))
                         ("No")))
           (cond ((wcmatch f "Yes,Y")

 

shows a misunderstanding of how (initget)/(getkword) work.  If the User types an acceptable option, the (getkword) function returns the word from (initget) exactly as it is written there, whether the User types just the capitalized letter(s), in either upper or lower case, or more of the word.  So the f variable can contain only either "Yes" [whether the User typed Y or y or Ye or yE or yeS or YEs or ....], or "No" [whether they typed N or n or nO or No or NO or no], or nil if they hit Enter, and if they type anything not conforming to the options, it will ask again.  It will never contain "Y".  So the last line there can be just:

 

           (cond ((= f "Yes")

 

This line:

 

                 (T ())))))

 

shows a misunderstanding of how (cond) works.  To create a "fallback" condition [the T] that does nothing is exactly the same as not creating that fallback condition at all.  If none of the conditions before this are satisfied, it will do nothing anyway, without being "told" to do nothing.  That line can be just:

 

                 ))))

Kent Cooper, AIA
Message 6 of 8

Ranjit_Singh
Advisor
Advisor

It feels nice when someone sees value in your work. Maybe in future give a little credit to the author Smiley Wink

0 Likes
Message 7 of 8

scot-65
Advisor
Advisor
It should be
(setvar 'OSMODE (+ (getvar 'OSMODE) 16384))

That way, if the program fails (or anything else including
poor program writing), the snap setting can be toggled
back on by the user by selecting the switch on the status line.

???

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 8 of 8

Kent1Cooper
Consultant
Consultant

@scot-65 wrote:
It should be
(setvar 'OSMODE (+ (getvar 'OSMODE) 16384))
....

.... only if running Osnap is currently on.  If there are modes set, but Osnap is already not running, the 16384 bit will already be part of the value, and the above will give an invalid [too large] value, which will be rejected.

 

If you don't use a good *error* handler to ensure re-setting the OSMODE value in the event of a problem, and want to take the approach of leaving the mode settings as they are but turning off running Osnap if it's currently on, what you need to do is to add 16384 to the value only if it isn't already part of it:

 

(setvar 'OSMODE (logior (getvar 'OSMODE) 16384))

Kent Cooper, AIA