Drawing arcs with autolisp

Drawing arcs with autolisp

Anonymous
Not applicable
5,567 Views
4 Replies
Message 1 of 5

Drawing arcs with autolisp

Anonymous
Not applicable

I think I might be forgetting to reset something.

I have created the following program to illustrate my issue:

 

(defun C:myTestProg()
      (setq flatLength 3)
      (setq arcHeight 5)
      (setq flatLeft (list (* -1 (/ flatLength 2)) 0))
      (setq flatRight (list (/ flatLength 2) 0))
      (setq arcTop (list 0 arcHeight))
      (command "line" flatLeft flatRight "")
      (command "arc" flatRight arcTop flatLeft)
      (princ)
)

If I run this in the commandline of autocad 2012 I get a line from (-1.5,0) to (1.5,0) and an arc between these points and through (0,5).

However, if I immediately repeat the program I get the same line, but an arc between those two points and through (0,2.4).

Repeating a third time or more gives the same line and arc through (0,2.4).

Am I not terminating the arc command correctly? I get an error if I add "" to the end.

 

Thanks

 

-Eric

0 Likes
Accepted solutions (1)
5,568 Views
4 Replies
Replies (4)
Message 2 of 5

hmsilva
Mentor
Mentor
Accepted solution

Hi Eric,

 

using command function, we need to ensure that there is no interference from object snap modes....

 

Try

(defun C:myTestProg()
  (setq flatLength 3)
  (setq arcHeight 5)
  (setq flatLeft (list (* -1 (/ flatLength 2)) 0))
  (setq flatRight (list (/ flatLength 2) 0))
  (setq arcTop (list 0 arcHeight))
  (command "line" "_NONE" flatLeft "_NONE" flatRight "")
  (command "arc" "_NONE" flatRight "_NONE" arcTop "_NONE" flatLeft)
  (princ)
)

;; or

(defun C:myTestProg()
  (setq osm (getvar 'OSMODE))
  (setvar 'OSMODE 0)
  (setq flatLength 3)
  (setq arcHeight 5)
  (setq flatLeft (list (* -1 (/ flatLength 2)) 0))
  (setq flatRight (list (/ flatLength 2) 0))
  (setq arcTop (list 0 arcHeight))
  (command "line" flatLeft flatRight "")
  (command "arc" flatRight arcTop flatLeft)
  (setvar 'OSMODE osm)
  (princ)
)

 

Hope this helps,
Henrique

EESignature

Message 3 of 5

Anonymous
Not applicable

Perfect, works like a charm!

Thanks Henrique

0 Likes
Message 4 of 5

hmsilva
Mentor
Mentor

@Anonymous wrote:

Perfect, works like a charm!

Thanks Henrique


You're welcome, Eric!
Glad I could help

Henrique

EESignature

0 Likes
Message 5 of 5

Lee_Mac
Advisor
Advisor

Be careful with division of integers, since:

 

_$ (/ 3 2)
1

 

Whereas, using reals (doubles) yields:

 

_$ (/ 3.0 2.0)
1.5

 

As an alternative to calling standard AutoCAD commands, you could also use entmake to add the entity DXF data directly to the drawing database and cut out the 'middle man' - consequently, this method is much faster and is not affected by Object Snap:

 

(defun c:mytestprog ( / flatlength archeight flatleft flatright lst )
    (setq flatlength 3.0
          archeight  5.0
          flatleft   (list (/ flatlength -2.0) 0)
          flatright  (list (/ flatlength  2.0) 0)
    )
    (entmake
        (list
           '(0 . "LINE")
            (cons 10 flatleft)
            (cons 11 flatright)
        )
    )
    (if (setq lst (LM:3PArc flatright (list 0 archeight) flatleft))
        (entmake
            (cons '(0 . "ARC")
                (mapcar 'cons '(10 50 51 40) lst)
            )
        )
    )
    (princ)
)

;; 3-Point Arc  -  Lee Mac
;; Returns the Center, Start/End Angle and Radius of the
;; Arc defined by three supplied points.

(defun LM:3PArc ( p1 p2 p3 / cn m1 m2 )
    (setq m1 (mid p1 p2)
          m2 (mid p2 p3)
    )
    (if
        (setq cn
            (inters
                m1 (polar m1 (+ (angle p1 p2) (/ pi 2.)) 1.0)
                m2 (polar m2 (+ (angle p2 p3) (/ pi 2.)) 1.0)
                nil
            )
        )
        (append (list cn)
            (if (LM:Clockwise-p p1 p2 p3)
                (list (angle cn p3) (angle cn p1))
                (list (angle cn p1) (angle cn p3))
            )
            (list (distance cn p1))
        )
    )
)

;; Midpoint  -  Lee Mac
;; Returns the midpoint of two points

(defun mid ( a b )
    (mapcar (function (lambda ( a b ) (/ (+ a b) 2.0))) a b)
)

;; Clockwise-p  -  Lee Mac
;; Returns T if p1,p2,p3 are clockwise oriented

(defun LM:Clockwise-p ( p1 p2 p3 )
    (< (* (- (car  p2) (car  p1)) (- (cadr p3) (cadr p1)))
       (* (- (cadr p2) (cadr p1)) (- (car  p3) (car  p1)))
    )
)

 

The above uses my 3-Point Arc function from here.

 

Lee

0 Likes