Jump to content

How to start a new LISP routine.


j2lstaples

Recommended Posts

Hello everyone, 

I know the lurkers are here waiting for a new topic and I want to keep it relevant to AutoLISP with a discussion and your opinions on this matter.

I have been creating scripts for our design process that help a lot and I want to do more cool stuff. For example, I have a routine that inserts rail joints in between rail blocks in the drawing so that I don't have to insert them manually every time. There's also another routine which labels our annotation bubbles with the correct number. I also created a routine that checks for the attributes of a block and scans the drawing in its vicinity and check if it the attribute configuration for that block is valid or correct. 

 

My question is, when you guys start a project, what's your process when creating a new tool/script. Do you write it down on paper? Do you create a functional block diagram? What other tools do you use to start a new script. Do you write pseudocode first? I don't have a senior member who also uses LISP to learn from and I have you guys as my teachers.

 

Currently, I have a project that I thought of where a script will create a polyline using blocks as the outline. First it will use getpoint for a starting point, and then the script scans the around around the starting point for the block, then using Lee Mac's chain select, it proceeds to process how the polyline gets handled (i.e. calculating vertex points, change to arcs or straight, etc.). It sounds complicated but I think it's doable.

 

Please share your process. I'd love to know.

Link to comment
Share on other sites

Starting out I would create file and in that file I would list what I wanted the lisp to do. Then I would break down that list into steps Autocad would need to take to complete the task. having small tasks is easier to code (and debug). After everything is running without errors then I would go back and add in error checking or limitations to eliminate the code breaking. it might be if nothing was selected end code. or if the wrong thing was selected prompt the user to try again. adding QoL to lisp rather than waiting on  an error code an trying again.

 

Then after awhile this become second nature and you won't really need to do this anymore. unless your working on something taking multiple steps.

 

Find the largest radius of selected entities

 

User selects entities
compair the radius of each entity
display the largest one

 

;;----------------------------------------------------------------------------;;
;; Display largest raidus of selected arcs or circles
(defun C:LRAD ()
  (setq ss (ssget)) ;User selects entities  
  (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))) 
    ;compair the radius of each entity
  )
  ;Display the largest one
)

 

if this code was run it would show "Largest Radius: 0.000" if nothing was selected.
;;----------------------------------------------------------------------------;;
;; Display largest raidus of selected arcs or circles
(defun C:LRAD (/ ss r x)
  (setq ss (ssget '((0 . "ARC,CIRCLE")))) ;limit selection to Arc's and circles aka stuff with a raidus
  (setq r 0) ;set r at 0 to keep track of largest radius
  (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
    (if (> (setq x (cdr (assoc 40 (entget ent)))) r) ;compair the raidus to the variable r 
      (setq r x) ;if r is smaller set r to the new value
    )
  )
  (prompt (strcat "\nLargest Radius: " (rtos r 2 3))) ;once all entities have been processed display the value of r
  (princ)
)

 

this code would show "Nothing was Selected"
;;----------------------------------------------------------------------------;;
;; Display largest raidus of selected arcs or circles
(defun C:LRAD (/ ss r x)
  (if (setq ss (ssget '((0 . "ARC,CIRCLE")))) ;limit selection to Arc's and circles aka stuff with a raidus
    (progn
      (setq r 0) ;set r at 0 to keep track of largest radius
      (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
      (if (> (setq x (cdr (assoc 40 (entget ent)))) r) ;compair the raidus to the variable r if its bigger set r to the new value
        (setq r x)
      )
      (prompt (strcat "\nLargest Radius: " (rtos r 2 3))) ;once all entities have been processed display the value of r
    )
    (prompt "\nNothing was Selected")
  )
  (princ)
)

 

  • Like 2
Link to comment
Share on other sites

For some code where you are repeating the same code over and over then look at a defun for that task, reduces size of code.

 

The other thing I do as hinted by mhupp I make a defun to complete 1 task this way I can test just that section of code adding more steps as I go. Often that defun gets used in other code.

 

Lastly I tend to copy and paste to command line rather than trying to write 1 big bit of code, it may be like 1 line entsel, then 20 lines doing something with entity selected get each step working before throwing into a repeat 50 times. 

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