Jump to content

How to use lisp to enter the angle of inclination of the line in degrees


Nikon

Recommended Posts

Hello to all readers.
In AutoCAD, to enter the slope of the line, you need to use the angle bracket "<".
To get an angle of 30°,  enter <30 in the command string.
Is it possible to write in lisp as well?
Lisp has such a function "<". 
Lisp will not perceive "<" as a function?
My attempt to create the code:

1. Specify 1 point

2. Enter the angle <30

3. Specify the 2nd point
I have no experience in writing codes, so I ask for help from experts.

(defun c:line_ang1 ( )
 (vl-load-com)
(setq p1 (getpoint "\nSpecify the base point: "))
(setq pnd (getreal "\nSpecify the angle:  < "))
(setq xpb (car pb)) 
(setq ypb (cadr pb))
(setq p2   ???
(command "_line" p1 pb p2 "")
 (princ)
 )

The code is certainly incorrect...
thanks for any help...

Edited by Nikon
Link to comment
Share on other sites

So to get point 2, convert the angle number to radians (search deg to rad LISPs)

Look at polar (polar pt ang dist) though you will also need to know a distance to calculate the point.

 

Link to comment
Share on other sites

Is it possible to do without radians?

If AutoCAD has a function for setting the angle in degrees, then this can be done via lisp.
But how to write it down correctly?
The 2nd point can simply be indicated on the screen when the line appears at a given angle...

 

Edited by Nikon
Link to comment
Share on other sites

My take on your problem. Just ask if this is what you want.

 

Pick a point then

image.png.45c97792a08186c74ae28442d6d5728d.png

 

The No code way is to use SNAPANG 30 & press F8, Ortho on, this will set your angle as you draw a line. Just remember must do Snapang 0 to go to normal angle.

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

Probably, you need to put 60 in the code instead of the angle bracket, so that you can then specify the angle value in degrees...
 (ascii "<")  60

Link to comment
Share on other sites

My version. Don't scold too much.

(defun c:11 (/ p0 ang dist)
 (vl-load-com)
 (setvar "DYNMODE" 1)
 (setvar "ORTHOMODE" 0)
 (setvar "SNAPMODE" 0)
 (setvar "OSMODE" 0)
 (setq p0   (getpoint "SPick the first point >")
       ang  (angtof (rtos (getreal "Enter angle >")) 0)
       dist (distance p0 (getpoint p0 "Pick a second point >"))
 )
 (entmakex (list (cons 0 "LINE") (cons 10 p0) (cons 11 (polar p0 ang dist))))
)

 

  • Like 1
Link to comment
Share on other sites

Think OP wants to set the angle by text and set the distance on the screen.

 

LISP maybe:

get angle, set snapang to this, draw line, reset snapang to previous. Add error processing to return snapang to original in case of escape for example.

  • Like 1
Link to comment
Share on other sites

"get angle, set snapang to this, draw line, reset snapang to previous...

I'm trying to write a macro, but it doesn't work

^C^C_snapang;\_LINE;\_snapang;_0

_snapang It is not returned  to  "0"

                ? ? ? 

Edited by Nikon
Link to comment
Share on other sites

(defun c:11 (/ p0 ang dist)
 (vl-load-com)
 (setq p0  (getpoint "Pick the first point >")
       ang (angtof (rtos (getreal "Enter angle >")) 0)
 )
 (setvar "SNAPANG" ang)
 (setq dist (distance p0 (getpoint p0 "Pick a second point >")))
 (entmakex (list (cons 0 "LINE") (cons 10 p0) (cons 11 (polar p0 ang dist))))
 (setvar "SNAPANG" 0)
)

 

  • Like 2
Link to comment
Share on other sites

2 hours ago, 1958 said:
(defun c:11 (/ p0 ang dist)
 (vl-load-com)
 (setq p0  (getpoint "Pick the first point >")
       ang (angtof (rtos (getreal "Enter angle >")) 0)
 )
 (setvar "SNAPANG" ang)
 (setq dist (distance p0 (getpoint p0 "Pick a second point >")))
 (entmakex (list (cons 0 "LINE") (cons 10 p0) (cons 11 (polar p0 ang dist))))
 (setvar "SNAPANG" 0)
)

 

 

Small change I'd make, 

(defun c:11 (/ p0 ang dist)
  (defun 11error ( errormsg / )
    (setvar "SNAPANG" old_snapang)  ; reset variables
    (setq *error* temperr)          ; reset *error*
    (prompt "\nFunction Canelled.")
  )

 (vl-load-com)
 (setq p0  (getpoint "Pick the first point >")
       ang (angtof (rtos (getreal "Enter angle >")) 0)
 )

(setq Old_snapang (getvar "SNAPANG") ) ; to record what it was, might not be 0
(setq temperr *error*)				;store *error* ; can wait till here to set error function, after recording old variables
(setq *error* 11error)

 (setvar "SNAPANG" ang)
 (setq dist (distance p0 (getpoint p0 "Pick a second point >")))

(setvar "SNAPANG" Old_Snapang) ; moved this here, seto to old value (no assumption what it was)

 (entmakex (list (cons 0 "LINE") (cons 10 p0) (cons 11 (polar p0 ang dist))))

(setq *error* temperr) ; reset error

)

 

Edited by Steven P
  • Like 1
  • Agree 1
Link to comment
Share on other sites

Yes, that is OK. The command line is telling you the last thing the LISP recorded, so in my example it is telling you about the error function that is reset, in the code above the report is '0' (the last variable that was set - SnapAng), the first example by 1958 should be the entity name of the line created  <entity name abcdef>. Mine has "Error" in that text since the error function was the last thing recorded, nothing to worry about.

 

You can hide these by exiting the LISP quietly with the last line being (princ), write blank to the command line, and noting that if your last line is for example (princ "Lisp Ended") and no more then the "Lisp Ended" text, being the last thing recorded by the LISP will be repeated.

Link to comment
Share on other sites

3 hours ago, Nikon said:

@BIGAL  It is unclear what this is and where to?

(defun c:30 .........
(defun c:45 ........
(defun c:60 ..........

 

 

 

Have a few LISPs, 30, 45, 60 one for each of the most frequent angles that you use, the angle is set in the LISP instead of by the user and is quicker

Link to comment
Share on other sites

1 hour ago, Steven P said:
(defun c: 30 .........
(defun c: 45 ........
(defun c: 60 ..........
(defun c: 35 .........
(defun c: 50 ........
(defun c: 68 ..........

You can continue the list of angles

 

I have over 100 lisps in automatic download.
Opening files in AutoCAD takes a little longer, 
that's why I'm trying to make simple codes using macros...

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