yeahyeah 发表于 2013-8-5 20:16:06

矩形面区域内的均匀布置

(defun c:fb ()                                ;本程序的设计目的是:在某矩形面域内按照均匀分布粘贴某一图元
(setq pa (getpoint "指定第一点:"))
(setq pb (getpoint "\n 指定对角点:"))
(setq pax (car pa))
(setq pay (cadr pa))
(setq pbx (car pb))
(setq pby (cadr pb))
(setq a (getdist "\n 横向布置点数:"))        ;取均匀布置排布数
(setq b (getdist "\n 纵向布置点数:"))
(setq m 1)                                ;设置循环变量
(setq n 1)
(while (<= m a)                        ;while1
    (setq mx (+ pax (* (/ m (+ a 1)) (- pbx pax))))
    (while (<= n b)                        ;while2
      (setq my (+ pay (* (/ n (+ b 1)) (- pby pay))))
      (setq n (+ n 1))
      (setq newp (list mx my 0))
      (command "pasteclip" newp)
    )                                        ;end while2
    (setq m (+ m 1) n 1)
)                                        ;end while1
)       


注:
1、执行这个程序之前,需要带基点复制一图元;
2、(setq m (+ m 1) n 1):原来这句我写的是 (setq m (+ m 1) ),检查测试多次也没成功,无奈之下到群求助,感谢【pzweng】路人的修正和帮助!



yeahyeah 发表于 2013-8-5 20:23:07

我刚学autolisp七八天,可以说是最新人了,还不知道怎么样设置默认值,要是能把a、b默认设置为1,那就好了。

wowan1314 发表于 2013-8-5 20:33:43

yeahyeah 发表于 2013-8-5 20:23 static/image/common/back.gif
我刚学autolisp七八天,可以说是最新人了,还不知道怎么样设置默认值,要是能把a、b默认设置为1,那就好了。 ...

七八天,这个水平很高了。。我三年才有你的水平。呵呵。

另用getint函数来得到整数。 要使其有默认值 看看initget函数。

yeahyeah 发表于 2013-8-5 20:44:49

1、感谢群里的桔子哥提供思路!
(or (setq a (getdist"\n 横向布置点数[默认值1]:")) (setq a 1.0))
(or (setq b (getdist"\n 纵向布置点数[默认值1]:")) (setq b 1.0))

2、感谢群里的小帅提供思路!
(if (null a)(setq a 1)(setq a (getdist"\n 横向布置点数[默认值1]:"))
(if (null b)(setq b 1)(setq b (getdist"\n 横向布置点数[默认值1]:"))

谢谢大家的帮助!

yeahyeah 发表于 2013-8-5 21:43:14

好奇怪啊!怎么回事啊?
桔子哥那个成功啦,

但是小帅那个改成这样
(setq a (getdist "\n 横向布置点数[默认值1]:"))
(if (null a)(setq a 1))
(setq b (getdist "\n 横向布置点数[默认值1]:"))
(if (null b)(setq b 1))
怎么也不成功啊?为什么????????????

yeahyeah 发表于 2013-8-5 21:45:20

wowan1314 发表于 2013-8-5 20:33 static/image/common/back.gif
七八天,这个水平很高了。。我三年才有你的水平。呵呵。

另用getint函数来得到整数。 要使其有默认值 ...

哦,谢谢啊!我会看的。
另,你懂小帅那个为什么没成功么?

yeahyeah 发表于 2013-8-5 21:54:49

yeahyeah 发表于 2013-8-5 21:45 static/image/common/back.gif
哦,谢谢啊!我会看的。
另,你懂小帅那个为什么没成功么?

5#?那是什么?

yeahyeah 发表于 2013-8-5 22:05:25

5楼那个好奇怪啊,最后都粘贴到第一个点上去了

yeahyeah 发表于 2013-8-5 22:54:09

5楼失败的原因在于:
(/ m (+ a 1)和(/ n (+ b 1),若a,b都是整数型,则计算所得值为0.
此事在以后遇到相除计算时一定要注意!
所以程序可改为:
(setq a (getdist "\n 横向布置点数[默认值1]:"))
(if (null a)(setq a 1.0))
(setq b (getdist "\n 横向布置点数[默认值1]:"))
(if (null b)(setq b 1.0))

还是这个原因:
(setq a (getdist "\n 横向布置点数:"))      ;取均匀布置排布数
(setq b (getdist "\n 纵向布置点数:"))
这两句中的getdist,本来是表示距离,按理说对于该程序而言,使用函数不当。但是,若被getint代替,则程序失败,还是上面的同理原因。用getreal代替可以。

yeahyeah 发表于 2013-8-5 23:00:32

;**************整个程序设计修改整理如下************************

(defun c:fb (/ a b)                              ;本程序的设计目的是:在某矩形面域内按照均匀分布粘贴某一图元
(setq pa (getpoint "指定第一点:"))
(setq pb (getpoint "\n 指定对角点:"))
(setq pax (car pa))
(setq pay (cadr pa))
(setq pbx (car pb))
(setq pby (cadr pb))

(or (setq a (getreal"\n 横向布置点数[默认值1]:")) (setq a 1.0))
(or (setq b (getreal"\n 纵向布置点数[默认值1]:")) (setq b 1.0))
;或者
;;;; (setq a (getreal "\n 横向布置点数[默认值1]:"))
;;;(if (null a)(setq a 1.0))
;;;(setq b (getreal "\n 横向布置点数[默认值1]:"))
;;;(if (null b)(setq b 1.0))

(setq m 1)                              ;设置循环变量
(setq n 1)
(while (<= m a)                        ;while1
    (setq mx (+ pax (* (/ m (+ a 1)) (- pbx pax))))
    (while (<= n b)                        ;while2
      (setq my (+ pay (* (/ n (+ b 1)) (- pby pay))))
      (setq n (+ n 1))
      (setq newp (list mx my 0))
      (command "pasteclip" newp)
    )                                        ;end while2
    (setq m (+ m 1) n 1)
)                                        ;end while1
)      
页: [1] 2
查看完整版本: 矩形面区域内的均匀布置