Jump to content

AutoLISP code does/does not work depending on zoom level


Sjotroll

Recommended Posts

Hi, I've written this code which draws triangles on the inside or outside of a polyline (with multiple vertexes), but it gives increasingly better/worse results depending on the zoom level (better results are obtained with higher zoom). The zoom levels I'm talking about are such that when the polyline occupies the whole screen it does work with some errors, if you reduce the zoom then it does not work, and when you increase the zoom it works perfectly. It probably depends on the relative size of the triangles (which are user inputs) to the zoom level. Is there a way to correct this behavior?

(defun c:vert (/ obj1 oft wid spa ip obj2 len l1 l2 sp pt1 pt2 ptm slopeP slopeM ptt1 ptt2 pttR pttL ptt pss it)

(vl-load-com)

(setq 
obj1 (ssget C)
oft (float (getreal "Triangle height: "))
wid (float (getreal "Triangle width: "))
spa (float (getreal "Distance between triangles: "))
)

(setq ip (strcase (getstring "R(ight) or L(eft): ")))

(setq
obj2 (vlax-ename->vla-object (ssname obj1 0))
len (vlax-get-property obj2 "length")
l1 (float 0)
l2 (float wid)
)

(while (< l1 len)
(setq
sp (vlax-curve-getPointAtDist obj2 0)
pt1 (vlax-curve-getPointAtDist obj2 l1)
pt2 (vlax-curve-getPointAtDist obj2 l2)
ptm (vlax-curve-getPointAtDist obj2 (/ (+ l1 l2) 2))
slopeP (+ (if (= (car pt2) (car pt1)) (/ pi 2) (atan (/ (- (nth 1 pt2) (nth 1 pt1)) (- (nth 0 pt2) (nth 0 pt1))))) (/ pi 2))
slopeM (- (if (= (car pt2) (car pt1)) (/ pi 2) (atan (/ (- (nth 1 pt2) (nth 1 pt1)) (- (nth 0 pt2) (nth 0 pt1))))) (/ pi 2))
ptt1 (list (+ (nth 0 ptm) (* oft (cos slopeP))) (+ (nth 1 ptm) (* oft (sin slopeP))))
ptt2 (list (+ (nth 0 ptm) (* oft (cos slopeM))) (+ (nth 1 ptm) (* oft (sin slopeM))))
pttR (if (< (- (* (- (nth 0 pt2) (nth 0 pt1)) (- (nth 1 ptt1) (nth 1 pt1))) (* (- (nth 0 ptt1) (nth 0 pt1)) (- (nth 1 pt2) (nth 1 pt1)))) 0) ptt1 ptt2)
pttL (if (> (- (* (- (nth 0 pt2) (nth 0 pt1)) (- (nth 1 ptt1) (nth 1 pt1))) (* (- (nth 0 ptt1) (nth 0 pt1)) (- (nth 1 pt2) (nth 1 pt1)))) 0) ptt1 ptt2)
ptt (if (= ip "R") pttR pttL)
)

(command "_.pline" pt1 ptt pt2 "")

(setq
l1 (float (+ l1 wid spa))
l2 (float (+ l2 spa wid))
)
)
)

 

Link to comment
Share on other sites

So will this work if instead of a triangle you insert say a letter, point or a single line?


Also just wondering where it works perfectly until? If you can give a hint then that is a bit less thinking on the other end of the internet to you. i'd perhaps start with checking the pt2 gives the result you want and work backwards and forwards from there

 

 

Edited by Steven P
Link to comment
Share on other sites

Seems like a classic OSMODE problem.

Try setting OSMODE to zero or use:

(command "_.pline" "_non" pt1 "_non" ptt "_non" pt2 "")

 

  • Like 1
  • Agree 1
Link to comment
Share on other sites

10 hours ago, Roy_043 said:

Seems like a classic OSMODE problem.

Try setting OSMODE to zero or use:

(command "_.pline" "_non" pt1 "_non" ptt "_non" pt2 "")

 

 

That was it, thank you! For some reason I thought that by not writing e.g. "near" no object snap would occur, but I forgot I have OSNAP on by default.

  • Like 1
Link to comment
Share on other sites

Glad you got the solution you needed. I've done some lengthy VLISP coding and I always add a routine which stashes the current values for 'OSMODE,  'PICKBOX, etc... into variables for safe keeping. Then when your VLISP code is ready to exit, send it back to the routine to reset all the original values for these environment settings. Without setting OSMODE and PICKBOX to 0, you'll end up picking up objects you don't want. Do a search on LEE-MAC's website, I'm sure you'll find good code examples there for this.

Edited by Bill Tillman
Link to comment
Share on other sites

39 minutes ago, Bill Tillman said:

Without setting OSMODE and PICKBOX to 0, you'll end up picking up objects you don't want.

 

I consider it a bug with (command if you input points and yes you can have osmode is set to 0 it will still snap to things if your zoomed out enough.

 

--edit

 

How to store/set multiple variables

(setq lst (list 'CMDECHO 'OSMODE 'PICKBOX 'CLAYER) ;built a list of what you want
      val (mapcar 'getvar lst) ;saves current var of above list
)
(mapcar 'setvar lst '(0 5 0 "dims")) ;sets the list of system variables
....
(mapcar 'setvar lst val) ;restore origonal values

 

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

like mhupp and Bill osmode 16384 is all off, I use osmode 0 for 99.999% of time and have no problems.

(setq oldsnap (getvar 'osmode))

(setvar 'osmode 0)

....

(setvar 'osmode oldsnap)

 

Link to comment
Share on other sites

Forgot to mention that there is a bug to do with Zoom levels and lisp that occurs occasionally people like me 40+ years know about it. Usually osmode fixes, sometimes need to do "ZOOM C pt scale" to get around it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...