明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1765|回复: 8

求助——想通过循环来画很多个三角形,希望高手帮我看看

[复制链接]
发表于 2012-11-3 17:17:13 | 显示全部楼层 |阅读模式
我得到一组txt的点的坐标,数据量很大。现在希望每七个点画一个三角形,这样画出很多的三角形。txt中坐标的值已经按照一定的顺序排列好了,每一行的后三个数据是点的x,y,z坐标。现在不知道怎么通过循环来写这个代码。所以来求助。下面有我自己写的一些:
刚刚接触lisp,请大大多多赐教。先谢谢了~

(defun xiang
(setq file (getfiled "选择文件..." "" "" 8))
(setq f (open file "r"))
(setq n (getint "输入单元数:"))

(repeat n                                ;;大循环 这个n怎么确定?设定为人工输入


(repeat 7                                ;;小循环
(setq j 0)
(setq j (1+ j))
                                         ;;(set (read (strcat "x" (itoa i))) x) 把x的值赋值给形如xn的形式的字符串(就是xi的意思)
(setq str (read-line f))                  ;;读取一次f,读取一行数据
(setq x (substr str 15 17))                     ;;读取一行数据中的第3到6个数据,也就是我的第一点坐标                           
(setq x (atof x))                           ;;得到第一点x坐标   但是数据会全部在一起了,没有分成三个数据
(setq y (substr str 32 17))                    ;;读取一行数据中的第3到6个数据,也就是我的第一点坐标 但是
(setq y (atof y))                          ;;得到第一点y坐标   但是数据会全部在一起了,没有分成三个数据
(setq z (substr str 49 ))                    ;;读取一行数据中的第3到6个数据,也就是我的第一点坐标 但是   
(setq z (atof z))                        ;;得到第一点z坐标   但是数据会全部在一起了,没有分成三个数据
(setq p (list x y z))                          ;;得到第一个点
(set (read (strcat "p" (itoa j))) p)     ;;把p赋值给pi(read这个函数整个表示pi)这里得到很多p1  p2  。。。pi

(command "pline" p1 p2 p3 p4 p5 p6 p7 "")
)                                            ;;得到很多的点了    小循环结束,画出一个三角形

)                                        ;;大循环结束
(close f)

)

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2012-11-3 18:28:08 | 显示全部楼层
本帖最后由 Andyhon 于 2012-11-3 19:39 编辑


  1. (defun xiang_A (str)
  2.       ;; 小循環
  3.       ;; 讀取一行數據
  4.       ;; (setq str "     257     1       15.8750000         .0000000         .0000000")
  5.       (setq x (substr str 15 17))
  6.       ;;讀取一行數據中的第3到6個數據,也就是我的第一點坐標
  7.       (setq x (atof x))
  8.       ;;得到第一點x坐標   但是數據會全部在一起了,沒有分成三個數據
  9.       (setq y (substr str 32 17))
  10.       ;;讀取一行數據中的第3到6個數據,也就是我的第一點坐標 但是
  11.       (setq y (atof y))
  12.       ;;得到第一點y坐標   但是數據會全部在一起了,沒有分成三個數據
  13.       (setq z (substr str 49))
  14.       ;;讀取一行數據中的第3到6個數據,也就是我的第一點坐標 但是
  15.       (setq z (atof z))
  16.       ;;得到第一點z坐標   但是數據會全部在一起了,沒有分成三個數據
  17.       
  18.       (list x y z)
  19. )
  20. (defun xiang ()
  21.   (setq file (getfiled "選擇文件..." "" "" 8))
  22.   (setq f (open file "r")
  23.       pts nil
  24. i  0
  25.   )
  26.   ;; (setq n (getint "輸入單元數:"))
  27.   (setvar "CmdEcho" 0)
  28.   (setvar "OsMode" 0)
  29.   (while (setq str (read-line f))
  30.     (setq pts (cons (xiang_A str) pts)
  31.    i   (1+ i)
  32.     )
  33.     (cond
  34.       ;;得到很多的點了    小循環結束,畫出一個三角形
  35.       ((= i 7)
  36.        (Command "_PLINE")
  37.        (mapcar 'Command pts)
  38.        (Command "")
  39.        (setq pts nil
  40.       i  0
  41.        )
  42.       )
  43.     )
  44.   )
  45.   ;;大循環結束
  46.   (close f)
  47.   (setvar "OsMode" 39)
  48.   (setvar "CmdEcho" 0)
  49. )
 楼主| 发表于 2012-11-3 18:51:58 | 显示全部楼层
Andyhon 发表于 2012-11-3 18:28

谢谢,我先研究一下……
发表于 2012-11-3 19:50:06 | 显示全部楼层
(defun c:zz (/ fil file lst nu snap str)
  (vl-load-com)
  (setq snap (getvar "osmode"))
  (setvar "osmode" 0)
  (setvar "cmdecho" 0)
  (setq fil (getfiled "打开数据表" "" "txt" 0))
  (setq file (open fil "r")
        lst '()
        str (read-line file)
  )
  (while str
    (setq lst (cons (strtolst str) lst))
    (setq str (read-line file))
  )
  (setq lst (reverse lst))
  (close file)
  (repeat (fix (/ (length lst) 7))
    (command "pline")
    (repeat 7
      (setq nu (car lst))
      (command (list (nth 2 nu) (nth 3 nu) (nth 4 nu)))
      (setq lst (cdr lst))
    )
    (command "C")
  )
  (setvar "osmode" snap)
  (princ)
)
(defun strtolst (str / i ls lst st str1) ; 提取字符串中的数字转化为实数表
  (setq ls '("0" "1"
         "2" "3"
         "4" "5"
         "6" "7"
         "8" "9"
         "." "-"
        )
  )
  (setq i 1
        str1 ""
        lst '()
  )
  (repeat (strlen str)
    (if (member (setq st (substr str i 1))
                ls
        )
      (setq str1 (strcat str1 st))
      (if (/= str1 "")
        (setq lst (cons (atof str1) lst)
              str1 ""
        )
      )
    )
    (setq i (1+ i))
  )
  (if (member st ls)
    (setq lst (cons (atof str1) lst))
  )
  (reverse lst)
)
发表于 2012-11-3 19:51:49 | 显示全部楼层

  1. ;;; 读取一行数据
  2. (defun xiang_A (str)
  3.       (setq x (substr str 15 17)
  4.             y (substr str 32 17)
  5.             z (substr str 49)
  6.       )
  7.       (Mapcar 'atof  (list x y z))
  8. )
发表于 2012-11-3 20:05:13 | 显示全部楼层

  1. (defun xiang_A (str)
  2.    (Mapcar 'atof
  3.      (list
  4.        (substr str 15)
  5.        (substr str 32)
  6.        (substr str 49)
  7.    ) )
  8. )
发表于 2012-11-3 21:48:29 | 显示全部楼层
Andyhon 大侠,能转到我的那个http://bbs.mjtd.com/thread-98992-1-1.html里看看吗。谢谢。。
 楼主| 发表于 2012-11-3 22:18:04 | 显示全部楼层
langjs 发表于 2012-11-3 19:50
(defun c:zz (/ fil file lst nu snap str)
  (vl-load-com)
  (setq snap (getvar "osmode"))

谢谢,我研究一下~刚刚开始,一个程序要看好久。。太谢谢了。。
 楼主| 发表于 2012-11-3 22:19:15 | 显示全部楼层
Andyhon 发表于 2012-11-3 20:05

这几种都可以导出点的坐标吧,学习了。感谢~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-9-26 03:51 , Processed in 0.195358 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表