Jump to content

Use Autolisp to draw a pline.


Wwx95

Recommended Posts

Hi, can anyone help to chech what's wrong with my code,I can not get the figure.

 

(defun c:huatu()
 (setq n (getint "Input number of points: "))
 (while(> n 0)
   (setq n (- n 1))
   (progn
     (setq p (getpoint "Input x and y: "))
     (command "pline" p "")
     ) 
   )
 )

 

I am trying to draw a 6 points pline in AUTOCAD. Thanks.

Edited by Wwx95
Link to comment
Share on other sites

This?

 

(defun c:test  (/ n a b i l)
 (if (and (setq n (getint "\nInput number of points: "))
          (setq a (getpoint "\nSpecify first point: "))
          (setq l (cons a l))
          )
   (progn
     (while (and (/= n 0)
                 (setq b (getpoint "\nNext point: " a))
                 )
       (setq l (cons b l)
             i (car l)
             n (1- n)
             )
       (mapcar '(lambda (p) (grdraw i p 1 7) (setq i p)) (cdr l))
       (setq a b)
       )
     (if l (progn
             (command "_.pline")
             (foreach x l
                (command "_non" x)
               )
             (command "")
             (redraw)
             )
       )
     )
   )
 (princ)
 )

Link to comment
Share on other sites

What is the initial value of “l” in your code? And what it used as?

 

The variable 'l' is holding the coordinate points to be used by the function grdraw to draw a vector between the points that represents the polyline before it is drawn by the command: pline

Link to comment
Share on other sites

Heres an simple example, with some comments:

(defun C:test ( / lst p1 p2 )
(setq lst (list)) ; construct a empty list to store the points
(initget (+ 1 2 4)) ; disallow in the prompt: pressing enter, zero or negative value
(setq n (getint "\nInput number of points"))
(repeat n 
	(setq p1 (getpoint "\nSpecify point"))
	(setq lst (cons p1 lst)) ; add the point to the list
); end of repeat, all points are stored in the list
(command "_.pline" 
	(foreach pt lst (command "_non" pt)) ; draw each point from the list
) ; draw the polyline
(princ) ; exit silently
);defun

EDIT1: Heres a bit more complex (so the routine would be more user-friendly):

(defun C:test ( / lst p1 )
(setq lst (list)) ; construct a empty list to store the points
(initget (+ 1 2 4)) ; disallow in the prompt: pressing enter, zero or negative value
(setq n (getint "\nInput number of points"))
(repeat n 
	(if (not p1) ; check if there p1 does not exist
		(setq p1 (getpoint "\nSpecify first point")) ; ask for p1 if it does not exist 
		(setq p1 (getpoint "\nSpecify next point" (last (reverse lst)))) ; to display the vector from previous point
	)
	(setq lst (cons p1 lst)) ; add the point to the list
); end of repeat, all points are stored in the list
(command "_.pline" 
	(foreach pt lst (command "_non" pt)) ; draw each point from the list
) ; draw the polyline
(princ) ; exit silently
);defun

EDIT2: Heres even more complex, based on what I've learned from Tharwat's code:

(defun C:test ( / oldcmd lst p1 p2 col )
(setq oldcmd (getvar 'cmdecho)) ; store this variable
(setq lst (list)) ; construct a empty list to store the points
(initget (+ 1 2 4)) ; disallow in the prompt: pressing enter, zero or negative value
(if
	(setq n (getint "\nInput number of points: ")) ; ask for the number of points
	(progn ; if we get the number of points, do all the stuff within the progn function
		(setvar 'cmdecho 0) ; set this variable to 0, to avoid any useless prompts
		(repeat n 
			(if (not p1) ; check if there p1 does not exist
				(setq p1 (getpoint "\nSpecify first point")) ; ask for p1 if it does not exist 
				(progn
					(setq p1 (getpoint "\nSpecify next point" (setq p2 (last (reverse lst))))) ; to display the vector from previous point
					(grdraw p1 p2 (if (not col) (setq col 1) (setq col (+ col 1))) 0) ; visualize the segments, with some colors
				)
			)
			(setq lst (cons p1 lst)) ; add the point to the list
		); end of repeat, all points are stored in the list
		(command "_.pline" 
			(foreach pt lst (command "_non" pt)) ; draw each point from the list
		) ; draw the polyline
		(redraw) ; this is used as "regen" for the grdraw function
		(setvar 'cmdecho oldcmd) ; reset the variable
	); end of progn
);if
(princ) ; exit silently
);defun

Its my first time using grdraw function, so I find his code a good example for its usage.

Edited by Grrr
Link to comment
Share on other sites

Heres an simple example, with some comments:

(defun C:test ( / lst p1 p2 )
(setq lst (list)) ; construct a empty list to store the points
(initget (+ 1 2 4)) ; disallow in the prompt: pressing enter, zero or negative value
(setq n (getint "\nInput number of points"))
(repeat n 
	(setq p1 (getpoint "\nSpecify point"))
	(setq lst (cons p1 lst)) ; add the point to the list
); end of repeat, all points are stored in the list
(command "_.pline" 
	(foreach pt lst (command "_non" pt)) ; draw each point from the list
) ; draw the polyline
(princ) ; exit silently
);defun

EDIT1: Heres a bit more complex (so the routine would be more user-friendly):

(defun C:test ( / lst p1 )
(setq lst (list)) ; construct a empty list to store the points
(initget (+ 1 2 4)) ; disallow in the prompt: pressing enter, zero or negative value
(setq n (getint "\nInput number of points"))
(repeat n 
	(if (not p1) ; check if there p1 does not exist
		(setq p1 (getpoint "\nSpecify first point")) ; ask for p1 if it does not exist 
		(setq p1 (getpoint "\nSpecify next point" (last (reverse lst)))) ; to display the vector from previous point
	)
	(setq lst (cons p1 lst)) ; add the point to the list
); end of repeat, all points are stored in the list
(command "_.pline" 
	(foreach pt lst (command "_non" pt)) ; draw each point from the list
) ; draw the polyline
(princ) ; exit silently
);defun

EDIT2: Heres even more complex, based on what I've learned from Tharwat's code:

(defun C:test ( / oldcmd lst p1 p2 col )
(setq oldcmd (getvar 'cmdecho)) ; store this variable
(setq lst (list)) ; construct a empty list to store the points
(initget (+ 1 2 4)) ; disallow in the prompt: pressing enter, zero or negative value
(if
	(setq n (getint "\nInput number of points: ")) ; ask for the number of points
	(progn ; if we get the number of points, do all the stuff within the progn function
		(setvar 'cmdecho 0) ; set this variable to 0, to avoid any useless prompts
		(repeat n 
			(if (not p1) ; check if there p1 does not exist
				(setq p1 (getpoint "\nSpecify first point")) ; ask for p1 if it does not exist 
				(progn
					(setq p1 (getpoint "\nSpecify next point" (setq p2 (last (reverse lst))))) ; to display the vector from previous point
					(grdraw p1 p2 (if (not col) (setq col 1) (setq col (+ col 1))) 0) ; visualize the segments, with some colors
				)
			)
			(setq lst (cons p1 lst)) ; add the point to the list
		); end of repeat, all points are stored in the list
		(command "_.pline" 
			(foreach pt lst (command "_non" pt)) ; draw each point from the list
		) ; draw the polyline
		(redraw) ; this is used as "regen" for the grdraw function
		(setvar 'cmdecho oldcmd) ; reset the variable
	); end of progn
);if
(princ) ; exit silently
);defun

Its my first time using grdraw function, so I find his code a good example for its usage.

 

Thanks, lol, I think I'd better learn from your first code, after I understand every thing is this code,then I will check your edit1 and edit2. Thank you so much!!

Link to comment
Share on other sites

Thanks, lol, I think I'd better learn from your first code, after I understand every thing is this code,then I will check your edit1 and edit2. Thank you so much!!

 

Well, that was my intention ! ;)

Link to comment
Share on other sites

The variable 'l' is holding the coordinate points to be used by the function grdraw to draw a vector between the points that represents the polyline before it is drawn by the command: pline

 

So what is the initial value of l??

setq l (cons a l)

And the "and" in here is used to guarantee that "n" "a" and "l" are valid value?

Link to comment
Share on other sites

  • 2 weeks later...
Well, that was my intention ! ;)

 

Hi,I got another problem,so in your code 1 and code 2,

(foreach pt lst (command "_non" pt)) ; draw each point from the list

"_non" in here means what?I know this step is designed to draw every point,right?

Link to comment
Share on other sites

Hi,I got another problem,so in your code 1 and code 2,
(foreach pt lst (command "_non" pt)) ; draw each point from the list

"_non" in here means what?I know this step is designed to draw every point,right?

 

Quote from LM's website:

non (or 'none')

This is an Object Snap modifier (similar to end or mid) which instructs the command to ignore any active Object Snap modes for the next point input. The modifier keyword is prefixed with an underscore to account for non-English versions of AutoCAD, as described above.

From here: http://lee-mac.com/scriptwriting.html

I wish he described more stuff like "\\" "@" "_pause" in there.

Link to comment
Share on other sites

I know this thread is getting old, but I was practicing/playing with these lately:

; Grrr
; Another attempt for emaking pline (loop with cond behaviour):

(defun C:test ( / oldcmd ptx pt ptlst cnt )

(setq oldcmd (getvar 'cmdecho))
(while T
	(setvar 'cmdecho 0)
	(initget 1 "D Done")
	(while
		(cond
			(
				(not 
					(if ptx
						(setq pt (getpoint "\nSpecify point or [Done]: " ptx))
						(setq pt (getpoint "\nSpecify point or [Done]: " ))
					)
				)
				(princ "\nYou must specify a point!")
			)
			( (and (= (type pt) 'LIST) (not (or (= (type pt) 'STR) (= pt "") )) )
				(setq ptx pt)
				(setq ptlst (cons pt ptlst))
				(if (>= (length ptlst) 2)
					(progn
						(setq cnt 0)
						(redraw)
						(repeat (length ptlst)
							(if (nth (+ cnt 1) ptlst)
								(progn
									(grdraw (nth cnt ptlst) (nth (+ cnt 1) ptlst) 2 3)
									(setq cnt (+ cnt 1))
								)
							)
						)
					)
				)
			)
			(	
				(or
					(and (wcmatch pt "DONE") (>= (length ptlst) 2) )
					(and (wcmatch pt "D") (>= (length ptlst) 2) )
				)
				(redraw)
				(if
					(and
						(entmake '( (0 . "POLYLINE") (66 . 1)))
						(setq cnt 0)
						(repeat (length ptlst)
							(entmake (list (cons 0 "VERTEX") (cons 10 (nth cnt ptlst))))
							(setq cnt (+ cnt 1))
						)
						(entmake '((0 . "SEQEND")))
					)
					(progn
						(setq ptx nil)
						(setq pt nil)
						(setq ptlst nil)
						(setq cnt nil)
					)
				)
			)
		);cond
	)
	(setvar 'cmdecho oldcmd)
)
(princ)
)

And due my grread practice:

; Grrr
; Draws a freehand poly, accidental result during grread practice:

(defun C:test ( / pt1 oldcmd lst LoopFlag UserIn ReturnChar pt2 )

(while T
	(and
		(setq pt1 (getpoint "\nSpecify starting point: "))
	)
	(progn
		(setq oldcmd (getvar 'CMDECHO))
		(setvar 'CMDECHO 0)
		(setq lst (list)) ; create an empty list
		(setq LoopFlag T)
		(princ "\nMove mouse to draw the poly, press LMB to end.")
		(while LoopFlag
			(setq UserIn (grread T))
			(setq ReturnChar (cadr UserIn))
			(cond
				((= (car UserIn) 5) ; cursor is moved
					(setq pt2 (cadr UserIn))
					(grdraw pt1 pt2 1 3) ; preview the poly
					(setq lst (cons pt2 lst)) ; construct the vertices list
					(setq pt1 pt2)
				)
				((= (car UserIn) 3) ; LMB is pressed
					(command "_.pline" 
						(foreach pt lst (command "_non" pt)) ; draw each point from the list
					) ; draw the polyline
					(setq LoopFlag nil)
					(redraw) ; erase the preview
				)
				(T 
					nil
				)
			);cond			
		);while LoopFlag
		(setvar 'CMDECHO oldcmd)
	);progn
)
(princ)
)

I've seen similar LISP before, but I had this accidental result when I tried to analyse cursor movement and pressing LMB, using grread.

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...