嗨,迈克尔,
首先,这里是一个完整的评论版本的程序,以帮助您的分析:
- [color=GREEN];; Define function and declare local variables[/color]
- ([color=BLUE]defun[/color] c:aspacing ( [color=BLUE]/[/color] a b d )
- [color=GREEN];; Evaluate each of the following expressions until[/color]
- [color=GREEN];; an expression returns nil or no expressions remain[/color]
- ([color=BLUE]and[/color]
- [color=GREEN];; Prompt the user for the first point, assign result to local variable 'a'[/color]
- ([color=BLUE]setq[/color] a ([color=BLUE]getpoint[/color] [color=MAROON]"\n1st point: "[/color]))
- [color=GREEN];; Prompt the user for the second point (with rubber band to the first point)[/color]
- [color=GREEN];; Assign result to the local variable 'b'[/color]
- [color=GREEN];; Note that by virtue of the AND function being a Special Form, this expression[/color]
- [color=GREEN];; will not be evaluated in the event that the first point prompt returns nil.[/color]
- ([color=BLUE]setq[/color] b ([color=BLUE]getpoint[/color] [color=MAROON]"\n2nd point: "[/color] a))
- [color=GREEN];; Calculate linear distance between points a & b, assign result of calculation[/color]
- [color=GREEN];; to local variable 'd'[/color]
- ([color=BLUE]setq[/color] d ([color=BLUE]distance[/color] a b))
- [color=GREEN];; Append the following DXF data to the current drawing database[/color]
- ([color=BLUE]entmake[/color]
- [color=GREEN];; Construct a DXF list of dotted pairs to append to the database[/color]
- ([color=BLUE]list[/color]
- [color=GREEN];; DXF Group 0: Entity Type[/color]
- [color=GREEN];; Fixed data, so expressed as quoted literal dotted pair -[/color]
- [color=GREEN];; See http://bit.ly/18ftLyF for more information on this concept[/color]
- '(000 . [color=MAROON]"XLINE"[/color])
- [color=GREEN];; DXF Group 100: Subclass marker designating object type[/color]
- '(100 . [color=MAROON]"AcDbEntity"[/color])
- [color=GREEN];; DXF Group 100: Subclass marker designating entity type[/color]
- '(100 . [color=MAROON]"AcDbXline"[/color])
- [color=GREEN];; Construct a dotted pair from the following atoms:[/color]
- [color=GREEN];; See http://bit.ly/18ftLyF for more information on why this cannot be quoted as a literal[/color]
- ([color=BLUE]cons[/color]
- [color=GREEN];; DXF Group 10: WCS Base point for XLine[/color]
- 10
- [color=GREEN];; Translate the following point from UCS to WCS[/color]
- ([color=BLUE]trans[/color]
- [color=GREEN];; Calculate the position of a point relative to point 'a' based on given calculations[/color]
- ([color=BLUE]polar[/color] a ([color=BLUE]angle[/color] a b) ([color=BLUE]/[/color] d ([color=BLUE]fix[/color] ([color=BLUE]1+[/color] ([color=BLUE]/[/color] d 4200.0))) 2.0))
- 1 0
- ) [color=GREEN];; end trans[/color]
- ) [color=GREEN];; end cons[/color]
- [color=GREEN];; Construct a dotted pair from the following atoms:[/color]
- ([color=BLUE]cons[/color]
- [color=GREEN];; DXF Group 11: WCS vector defining XLine direction[/color]
- 11
- [color=GREEN];; Translate the following point from UCS to WCS, independent of UCS origin[/color]
- ([color=BLUE]trans[/color]
- [color=GREEN];; Calculate a UCS vector perpendicular to a->b[/color]
- ([color=BLUE]list[/color] ([color=BLUE]-[/color] ([color=BLUE]cadr[/color] a) ([color=BLUE]cadr[/color] b)) ([color=BLUE]-[/color] ([color=BLUE]car[/color] b) ([color=BLUE]car[/color] a)))
- 1 0 [color=BLUE]t[/color]
- ) [color=GREEN];; end trans[/color]
- ) [color=GREEN];; end cons[/color]
- ) [color=GREEN];; end list[/color]
- ) [color=GREEN];; end entmake[/color]
- ) [color=GREEN];; end and[/color]
- [color=GREEN];; Suppress the value returned by the last evaluated expression (AND returns t/nil)[/color]
- ([color=BLUE]princ[/color])
- ) [color=GREEN];; end defun[/color]
本例中使用AutoLISP和函数更方便生成简洁的代码,但可能以可读性为代价。该示例也可以使用简单的if语句编写,但是,还必须使用progn函数使多个表达式能够作为单个“then”参数表达式的一部分进行计算:
- [color=GREEN];; Define function and declare local variables[/color]
- ([color=BLUE]defun[/color] c:aspacing ( [color=BLUE]/[/color] a b d )
- [color=GREEN];; If the following test expression returns a non-nil value[/color]
- ([color=BLUE]if[/color]
- [color=GREEN];; Evaluate each of the following expressions until[/color]
- [color=GREEN];; an expression returns nil or no expressions remain[/color]
- ([color=BLUE]and[/color]
- [color=GREEN];; Prompt the user for the first point, assign result to local variable 'a'[/color]
- ([color=BLUE]setq[/color] a ([color=BLUE]getpoint[/color] [color=MAROON]"\n1st point: "[/color]))
- [color=GREEN];; Prompt the user for the second point (with rubber band to the first point)[/color]
- [color=GREEN];; Assign result to the local variable 'b'[/color]
- [color=GREEN];; Note that by virtue of the AND function being a Special Form, this expression[/color]
- [color=GREEN];; will not be evaluated in the event that the first point prompt returns nil.[/color]
- ([color=BLUE]setq[/color] b ([color=BLUE]getpoint[/color] [color=MAROON]"\n2nd point: "[/color] a))
- ) [color=GREEN];; end and[/color]
- [color=GREEN];; Evaluate the following expressions as a single expression[/color]
- [color=GREEN];; constituting the 'then' argument for the IF function[/color]
- ([color=BLUE]progn[/color]
- [color=GREEN];; Calculate linear distance between points a & b, assign result of calculation[/color]
- [color=GREEN];; to local variable 'd'[/color]
- ([color=BLUE]setq[/color] d ([color=BLUE]distance[/color] a b))
- [color=GREEN];; Append the following DXF data to the current drawing database[/color]
- ([color=BLUE]entmake[/color]
- [color=GREEN];; Construct a DXF list of dotted pairs to append to the database[/color]
- ([color=BLUE]list[/color]
- [color=GREEN];; DXF Group 0: Entity Type[/color]
- [color=GREEN];; Fixed data, so expressed as quoted literal dotted pair -[/color]
- [color=GREEN];; See http://bit.ly/18ftLyF for more information on this concept[/color]
- '(000 . [color=MAROON]"XLINE"[/color])
- [color=GREEN];; DXF Group 100: Subclass marker designating object type[/color]
- '(100 . [color=MAROON]"AcDbEntity"[/color])
- [color=GREEN];; DXF Group 100: Subclass marker designating entity type[/color]
- '(100 . [color=MAROON]"AcDbXline"[/color])
- [color=GREEN];; Construct a dotted pair from the following atoms:[/color]
- [color=GREEN];; See http://bit.ly/18ftLyF for more information on why this cannot be quoted as a literal[/color]
- ([color=BLUE]cons[/color]
- [color=GREEN];; DXF Group 10: WCS Base point for XLine[/color]
- 10
- [color=GREEN];; Translate the following point from UCS to WCS[/color]
- ([color=BLUE]trans[/color]
- [color=GREEN];; Calculate the position of a point relative to point 'a' based on given calculations[/color]
- ([color=BLUE]polar[/color] a ([color=BLUE]angle[/color] a b) ([color=BLUE]/[/color] d ([color=BLUE]fix[/color] ([color=BLUE]1+[/color] ([color=BLUE]/[/color] d 4200.0))) 2.0))
- 1 0
- ) [color=GREEN];; end trans[/color]
- ) [color=GREEN];; end cons[/color]
- [color=GREEN];; Construct a dotted pair from the following atoms:[/color]
- ([color=BLUE]cons[/color]
- [color=GREEN];; DXF Group 11: WCS vector defining XLine direction[/color]
- 11
- [color=GREEN];; Translate the following point from UCS to WCS, independent of UCS origin[/color]
- ([color=BLUE]trans[/color]
- [color=GREEN];; Calculate a UCS vector perpendicular to a->b[/color]
- ([color=BLUE]list[/color] ([color=BLUE]-[/color] ([color=BLUE]cadr[/color] a) ([color=BLUE]cadr[/color] b)) ([color=BLUE]-[/color] ([color=BLUE]car[/color] b) ([color=BLUE]car[/color] a)))
- 1 0 [color=BLUE]t[/color]
- ) [color=GREEN];; end trans[/color]
- ) [color=GREEN];; end cons[/color]
- ) [color=GREEN];; end list[/color]
- ) [color=GREEN];; end entmake[/color]
- ) [color=GREEN];; end progn[/color]
- ) [color=GREEN];; end if[/color]
- [color=GREEN];; Suppress the value returned by the last evaluated expression[/color]
- ([color=BLUE]princ[/color])
- ) [color=GREEN];; end defun[/color]
非常欢迎您-如果您对发布的代码还有其他问题,请随时提问。 |