明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1657|回复: 4

怎么简化代码

[复制链接]
发表于 2004-4-29 21:21:00 | 显示全部楼层 |阅读模式
这是我写的一个画一些水平方向平行的线,我觉得太罗嗦了,怎么样可以简化一些吗? ;------------这个文件主要是画一些部件的主视图------------------ ;画一段机架返回该机架的起点和终点到左边的距离
(defun C:DrawQ/TY25D1XX-jj (PBase1 PBase2 LDAll P0 W ViewSel TypeChain
/ p1 p2 HalfWidth
List-Cen-Date List-DN-Date list-0-Date Num)
Base1 本段机架主视图的基点.
Base2 本段机架俯视图的基点.
;LDAll 本段机架的长度.
0 图纸的基点.
;HalfWidth 机架内面的半宽.
;ViewSel 视图选择 = 1 只画主视图 = 2 只画俯视图 = 3 都画.
;TypeChain 链条位置类型,(内外链类型). (setq LDAll (float LDAll)
W (float W)
) (if (or (= ViewSel 1) (= ViewSel 3))
;-------------画主视图机架-------------
;画3条中心线.
(progn
(command "layer" "s" "cen" "")
(setq p1 (mapcar '+ PBase1 (list 0 14.5)))
(setq p2 (mapcar '+ PBase1 (list LDAll 14.5)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 127.5)))
(setq p2 (mapcar '+ PBase1 (list LDAll 127.5)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 159.82)))
(setq p2 (mapcar '+ PBase1 (list LDAll 159.82)))
(command "line" P1 P2 "")
;画DA层的线3条
(command "layer" "s" "DA" "")
(setq p1 (mapcar '+ PBase1 (list 0 3)))
(setq p2 (mapcar '+ PBase1 (list LDAll 3)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 136)))
(setq p2 (mapcar '+ PBase1 (list LDAll 136)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 139)))
(setq p2 (mapcar '+ PBase1 (list LDAll 139)))
(command "line" P1 P2 "")
;画0层的线11条
(command "layer" "s" "0" "")
(setq p1 (mapcar '+ PBase1 (list 0 0)))
(setq p2 (mapcar '+ PBase1 (list LDAll 0)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 10)))
(setq p2 (mapcar '+ PBase1 (list LDAll 10)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 19)))
(setq p2 (mapcar '+ PBase1 (list LDAll 19)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 29.09)))
(setq p2 (mapcar '+ PBase1 (list LDAll 29.09)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 35.1)))
(setq p2 (mapcar '+ PBase1 (list LDAll 35.1)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 104.4)))
(setq p2 (mapcar '+ PBase1 (list LDAll 104.4)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 112.91)))
(setq p2 (mapcar '+ PBase1 (list LDAll 112.91)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 123)))
(setq p2 (mapcar '+ PBase1 (list LDAll 123)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 132)))
(setq p2 (mapcar '+ PBase1 (list LDAll 132)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 142)))
(setq p2 (mapcar '+ PBase1 (list LDAll 142)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 147)))
(setq p2 (mapcar '+ PBase1 (list LDAll 147)))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase1 (list 0 154)))
(setq p2 (mapcar '+ PBase1 (list LDAll 154)))
(command "line" P1 P2 "")
)
) ;----------------------画俯视图的机架-----------------
(if (= TypeChain "外")
(setq List-Cen-Date '(57.5)
List-DN-Date '(3 81 84)
list-0-Date '(0 37 52 63 78 99.5)
)
(setq List-Cen-Date '(26.5)
List-DN-Date '(3 81 84)
list-0-Date '(0 6 21 32 47 99.5)
)
)

(setq Num (length List-Cen-Date)
n 0
)
(repeat Num
(setq Y-Value (+ HalfWidth (Nth n List-Cen-Date)))
(setq p1 (mapcar '+ PBase2 (list 0 Y-Value)))
(setq p2 (mapcar '+ PBase2 (list LDAll Y-Value)))
(command "line" P1 P2 "")
(setq n (+ n 1))
)
(if (or (= ViewSel 2) (= ViewSel 3))
(progn
;1 画后面的机架.
(setq HalfWidth (/ (- W 199) 2)) (if (= TypeChain "外" )
(Progn
;画1条中心线.
(command "layer" "s" "cen" "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 57.5)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 57.5)))))
(command "line" P1 P2 "") (setq bbb (cdr p1))
;画3条虚线.
(command "layer" "s" "DA" "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 3)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 3)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 81)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 81)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 84)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 84)))))
(command "line" P1 P2 "")
;画6条实线.
(command "layer" "s" "0" "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 0)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 0)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 37)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 37)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 52)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 52)))))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 63)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 63)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 78)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 78)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 99.5)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 99.5)))))
(command "line" P1 P2 "")
;2 画前面的机架.
;画1条中心线.
(command "layer" "s" "cen" "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 57.5)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 57.5)))))
(command "line" P1 P2 "")
;画3条虚线.
(command "layer" "s" "DA" "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 3)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 3)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 81)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 81)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 84)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 84)))))
(command "line" P1 P2 "")
;画6条实线.
(command "layer" "s" "0" "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 0)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 0)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 37)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 37)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 52)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 52)))))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 63)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 63)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 78)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 78)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 99.5)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 99.5)))))
(command "line" P1 P2 "")
)
(Progn
;画1条中心线.
(command "layer" "s" "cen" "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 26.5)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 26.5)))))
(command "line" P1 P2 "") (setq bbb (cdr p1))
;画3条虚线.
(command "layer" "s" "DA" "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 3)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 3)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 81)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 81)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 84)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 84)))))
(command "line" P1 P2 "")
;画6条实线.
(command "layer" "s" "0" "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 0)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 0)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 6)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 6)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 21)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 21)))))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 32)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 32)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 47)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 47)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (+ 0 (+ HalfWidth 99.5)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (+ 0 (+ HalfWidth 99.5)))))
(command "line" P1 P2 "")
;2 画前面的机架.
;画1条中心线.
(command "layer" "s" "cen" "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 26.5)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 26.5)))))
(command "line" P1 P2 "")
;画3条虚线.
(command "layer" "s" "DA" "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 3)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 3)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 81)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 81)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 84)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 84)))))
(command "line" P1 P2 "")
;画6条实线.
(command "layer" "s" "0" "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 0)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 0)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 6)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 6)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 21)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 21)))))
(command "line" P1 P2 "") (setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 32)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 32)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 47)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 47)))))
(command "line" P1 P2 "")
(setq p1 (mapcar '+ PBase2 (list 0 (- 0 (+ HalfWidth 99.5)))))
(setq p2 (mapcar '+ PBase2 (list LDAll (- 0 (+ HalfWidth 99.5)))))
(command "line" P1 P2 "")
)
)
)
)
;返回的值.
(list
(cons "Start" (- (car p1) (car p0)))
(cons "End" (- (car p2) (car p0)) )
)
)
发表于 2004-4-30 20:21:00 | 显示全部楼层
其实能管用就行~~一味追求精简代码没有必要。


看到你用了很多Command函数,也许用Apply函数可能会精简一点。先创建一个列表,随便举个例子,比如 (setq        plist (list pt1 pt2 pt3 pt4...))....


创建表的时候当然可以用循环什么的。。。。看程序啦


最后用一句


(apply 'command (cons "pline" plist))


(command "")


这样也许会简单一点~~仅供参考。
发表于 2004-5-1 04:40:00 | 显示全部楼层
2楼的真是个好方法
发表于 2004-5-1 17:45:00 | 显示全部楼层
数据是否能做表


这样程序写起来可能会简单些
 楼主| 发表于 2004-5-1 17:53:00 | 显示全部楼层
4楼的方法很启发,我现在就是把数据(线的图层和起点的Y坐标放到一个偏移)放到表里,得到了很多简化,只用了2条command line 命令。谢谢。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-28 03:31 , Processed in 0.182108 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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