AutoLisp入门基础教程(4)

这个程序的目的是在平面上画出矩形区域,并将这些矩形的位置和大小写在文件里。

(defun c:tt()
    ;
    (setq all_data '())
    ; Define the original point
    (setq pt0 nil)
    (while (not pt0)
        (setq pt0 (getpoint "\n Define the original point"))
    )
    ; Define the two corners of rectangle
    (setq pt1 (getpoint "\nThe first corner of rectangle"))
    (if (not pt1)
        ; when pt1 is not available
        (setQ pt2 nil)      
        ; when pt1 is available
        (setq pt2 (getcorner pt1 "\nThe other corner of rectangle"))
    )
    (setq num 0)
    ; save the osmode
    (setq oldsanp (getvar "osmode"))
    (while (and pt1 pt2)
        ;set osmode = 0 for accurate plot
        (setvar "osmode" 0)
        ; Plot rectangle
        (command "rectang" pt1 pt2)
        (setq pc (polar pt1 (angle pt1 pt2) (/ (distance pt1 pt2) 2)))
        ; Plot circle 
        (command "circle" pc 800)
        ; Marked with NO.
        (setq num (1+ num))
        (command "text" "m" pc 800 0 (itoa num))
        ; reset the osmode
        (setvar "osmode" oldsanp)
        ; Define coordinate of the rectangle center: pcx, pcy
        (setq pcx (- (/ (+ (car pt1) (car pt2)) 2) (car pt0))
              pcy (- (/ (+ (cadr pt1) (cadr pt2)) 2) (cadr pt0))
        )
        ; Define the X or Y ranges of the rectangle: dx, dy
        (setq dx (abs (- (car pt1) (car pt2)))
              dy (abs (- (cadr pt1) (cadr pt2)))
        )
        (prin1)
        ; Store information in all_data
        (setq all_data (cons (list pcx pcy dx dy) all_data))
        ; Define the two corners of rectangle
        (setq pt1 (getpoint "\nThe first corner of rectangle"))
        (if (not pt1)
            ; when pt1 is not available
            (setq pt2 nil)      
            ; when pt1 is available
            (setq pt2 (getcorner pt1 "\nThe other corner of rectangle"))
        )
        (prin1)
    )
    ; Reverse the list
    (setq all_data (reverse all_data))
    ; write file 
    (setq dat_file (getfiled "Save file as" "C:\\tempfile" "csv" 1))
    (setq fo (open dat_file "w"))
    (write-line "NO., pcx, pcy, dx, dy" fo)
    ; element index start from 0
    (setq n (length all_data)
          i 0
    )
    (repeat n
        (setq data (nth i all_data))
        (write-line (strcat (itoa (1+ i)) ", "
                       (rtos (/ (nth 0 data) 1000) 2 3) ", "
                       (rtos (/ (nth 1 data) 1000) 2 3) ", "
                       (rtos (/ (nth 2 data) 1000) 2 3) ", "
                       (rtos (/ (nth 3 data) 1000) 2 3)
                )
                fo
         )
        (setq i (1+ i))
    )
    (close fo)
    (princ (strcat "\nWrite file:" dat_file))
    (prin1)
)
(princ "Shortcut: TT")
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,323评论 19 139
  • 是你的一句呐喊 震撼了无数寂寞的魂灵 是你的超前思维 让无数驿动的心得以安放 也是你轻轻一弹 从此朝圣者有了膜拜的...
    浮光_掠影阅读 4,077评论 15 29
  • 最近迷上了一部纪录片——《了不起的匠人》,看多了励志成功学鸡汤,这部纪录片简直就是一股清流、匠人们用心打磨作品,为...
    清野拾梦阅读 4,160评论 0 2
  • 是不是寒风乍起 你就意兴阑珊只想蜷缩 是不是春风十里 你就开始假装热爱生活 是不是寻常日子 你总是循规蹈矩选择沉默...
    小五爱我阅读 3,130评论 1 1
  • 笔记 最近任务: 1.数组定义及其作用 2.数据库查询,嵌套查询,多表查询
    jmyang1518阅读 877评论 0 1