Jump to content

A *simple* corner mark function


SLiedtke

Recommended Posts

Hey everyone, long time lurker on these, forums, and I figure it's about time I just jump in.

This isn't my first lisp routine, but its the first one I've put together with the intention of doing it more or less the right way. 

The idea of it is simple, it draws a little mark on the "dims" layer everyone at my workplace uses. It just tells our fabricators a specific corner has been changed by us drafters. The current problems I'm having are:

-The polyline is not drawn completely (the mark should be a small 90° mark made up of two lines, like the letter L)

-Its a miracle the error handling works, considering I don't totally understand how. Any changes I make tend to break it.

Here's the code:

;SETUP
  (defun us() ;user setup
    (setq uce (getvar "CMDECHO")) ;User Command Echo
    (setq uosm (getvar "osmode")) ;User Object Snap Mode
    (setq upb (getvar "pickbox")) ;User Pickbox
    (setq ul (getvar "clayer"));user layer
  )
  (defun cs() ;Computer Setup
      (setvar "CMDECHO" 0) ;Computer Command Echo
      (setvar "osmode" 0) ;Computer Object Snap Mode
      (setvar "pickbox" 0) ;Computer Pickbox
  )
  (defun rus() ;Return User Setup
      (setvar "osmode" uosm) ;User Object Snap Mode
      (setvar "pickbox" upb) ;User Pickbox
      ;(setvar "CMDECHO" uce) ;User Command Echo
      (command "layer" "set" ul "") ;User Layer
  )
;complete
  (defun complete()
    (setvar "CMDECHO" uce) ;User Command Echo
    (command "undo" "e")
    (princ "C")
  )

(defun c:crmrk ( /  *error* cnr pt1 pt2 pt3)
  (defun *error* (msg)
      (setvar "osmode" uosm) ;User Object Snap Mode
      (setvar "pickbox" upb) ;User Pickbox
      (setvar "CMDECHO" uce) ;User Command Echo
      (command-s "layer" "set" ul "") ;User Layer
      (command-s "undo" "e")
      (command-s "u")
      (princ msg)
  )
;initial setup
  (us)
  (command "undo" "be") ;undo start
  (cs)
  (command "_.-osnap" "end,center") ;picking cursor
  (command "layer" "set" "dims" "")
;mark geometry
  (setq cnr (getpoint "\n Pick Corner"))
  (setq pt1 (polar cnr 3.3969326535897932384626433832795 2.0616))
  (setq pt2 (polar pt1 -1.5707963267948966192313216916398 1.5))
  (setq pt3 (polar pt2 0 1.5))
  (command "pline" pt3 pt2 pt1 "")
  ;(getpoint "\n pause")
  (rus);return user setup to complete command
  (command "rotate" (entlast) "" cnr "ref" 225 pause)
  (complete)
)

Also, please don't be afraid to tear me a new one if there's other stuff that I'm doing strangely, I'd like to be doing things right.

Link to comment
Share on other sites

Welcome.

 

us cs rus complete shouldn't be broken out but run inside the same lisp. this allows you to control/define local variables. when you don't define local variables it makes them global variables and if your are running multiple lisp that could be a potential to get unexpected results.

 

I always try to avoid using (command when i can it has the highest potential to mess up. vs setting a variable. plus even tho you have cmdecho set to 0 their could be output to the command line anyway.

these do the same thing.
(command "layer" "set" "dims" "")
(setvar 'clayer "dims")

 

Added a neat little trick with mapcar to set or get multiple variables at once.

(defun c:crmrk ( /  *error* lst val cnr pt1 pt2 pt3)
  (setq Drawing (vla-get-activedocument (vlax-get-acad-object))) ;needed for start and end marks
  (defun *error* (msg)
      (mapcar 'setvar lst val)
      (vla-endundomark Drawing)
      (princ msg)
  )
  (setq lst (list 'CMDECHO 'OSMODE 'PICKBOX 'CLAYER)
        val (mapcar 'getvar lst) ;saves current var of above list
  )
  (vla-startundomark Drawing) ;wont output to command line or affect selections
  (mapcar 'setvar lst '(0 5 0 "dims")) ;sets the list of system variables
  ;cmdecho = 0
  ;osmode = 5 ;replaces (command "_.-osnap" "end,center") ;picking cursor
  ;pickbox = 0 ;i usually keep it at 5
  ;clayer = "dims"  ;replaces (command "layer" "set" "dims" "")
  (setq cnr (getpoint "\nPick Corner: "))
  (setq pt1 (polar cnr pi 2.0))
  (setq pt2 (polar pt1 4.71239 1.5))
  (setq pt3 (polar pt2 0 1.5))
  (command "_.Pline" "_non" pt3 "_non" pt2 "_non" pt1 "")
  (command "_.Rotate" (entlast) "" cnr "ref" 225 pause)
  (vla-endundomark Drawing)
  (mapcar 'setvar lst val) ;returns system variables back to what they where before command
  (princ)
)

 

The error handling is if you exit out of this lisp or it errors (you don't pick point cnr) it will set the system variables back to what they where before the command was run.

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

Thanks a ton! I thought there might be a statement like "mapcar", but I couldn't for the life of me figure out how to google it.

I see what you mean about not breaking the user settings routines outside the main command. To be honest, I'm not really sure why I was doing it that way to begin with. Thanks for showing me the non "command" methods to do a lot of these operations, I have a feeling that's where the problems were coming from.

Your tips are gonna streamline my code quite a bit, I should've joined this forum months ago.

  • Like 2
Link to comment
Share on other sites

for got to comment on this but always use "_non" in front of points in command.

(command "_.Pline" "_non" pt3 "_non" pt2 "_non" pt1 "")

 

just know if any of those points where close to geometry even if osnaps are off  (f3 toggle) it could still snap to things that where close. the "_non" tells the command pline snap to this point only nothing else

  • Like 4
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...