MichaelAllfire 发表于 2022-7-5 17:04:49

Lisp routin的故障排除

大家好,
 
我的lisp例程代码有一些问题。目标是拾取两个点,并生成一条垂直于点之间角度的连接线,该连接线基于函数内的一些计算进行定位。然而,我得到了一些不稳定的角度,没有真正的一致性。有什么想法吗?
 
(defun c:aspacing () ;define function
(setq a (getpoint "\nFirst Point ")) ;first point
(setq b (getpoint "\nSecond Point ")) ;second point
(setq x (/ (distance a b) 4200)) ;divide distance from a to b by 4200
(setq y (fix (1+ x))) ;round up to next whole number
(setq z1 (/ (distance a b) y))        ;value of S - divide distance a b by y
(setq z2 (/ (/ (distance a b) y) 2))        ;value of half S
(setq ang (* (angle a b) 57.2958))        ;set angle of distance
(setq a1 (polar a ang z2)) ;starting point for xline
(command "xline" a1 (polar a1 (+ ang 90) 100) "") ;xline at 90 degrees to a b angle
) ;end function
(princ) ;clean loading
 
非常感谢!

BIGAL 发表于 2022-7-5 17:12:20

对此不确定,angle*57.2958
 
(+ang 90)Autocad将以弧度为单位工作角度,因此(+ang(/pi 2.0))请注意2.0
 
(setq z2(/(/(距离a b)y)2.0))以确保真正的非整数答案只是良好的内务管理。

Lee Mac 发表于 2022-7-5 17:19:16

我不知道为什么要乘以角度,但以下几点应该会让你走上正确的道路:
(defun c:aspacing ( / a b d )
   (and (setq a (getpoint "\n1st point: "))
      (setq b (getpoint "\n2nd point: " a))
      (setq d (distance a b))
      (entmake
            (list
               '(000 . "XLINE")
               '(100 . "AcDbEntity")
               '(100 . "AcDbXline")
                (cons 10 (trans (polar a (angle a b) (/ d (fix (1+ (/ d 4200.0))) 2.0)) 1 0))
                (cons 11 (trans (list (- (cadr a) (cadr b)) (- (car b) (car a))) 1 0 t))
            )
      )
   )
   (princ)
)

MichaelAllfire 发表于 2022-7-5 17:23:45

 
嘿,比格尔,谢谢你的回复!
 
*57.2958是将弧度转换为度,但是现在,如果我理解正确的话,我发现我不需要这样做,因为代码的其他部分也处理弧度。(+ang(/pi 2.0))是否会使角度增加90度?
 
2.0 for real是一个很好的提示,谢谢。刚开始接触这种编程,学习良好的实践是很有帮助的。谢谢你!

MichaelAllfire 发表于 2022-7-5 17:26:18

 
嗨,李,
 
我在理解一些代码时有点困难。有没有可能对不同的代码行发表一些评论?
 
我还不太明白和部分是如何工作的。我的理解是,如果所有参数都为真,则返回true。正在测试哪些参数?我也不理解列表或后续代码行。
 
一旦代码变得更通用,我就会迷失方向,因为我仍在努力学习并思考它。
 
非常感谢你迄今为止的帮助!

Lee Mac 发表于 2022-7-5 17:31:04

嗨,迈克尔,
 
首先,这里是一个完整的评论版本的程序,以帮助您的分析:

;; Define function and declare local variables
(defun c:aspacing ( / a b d )
   ;; Evaluate each of the following expressions until
   ;; an expression returns nil or no expressions remain
   (and
      ;; Prompt the user for the first point, assign result to local variable 'a'
      (setq a (getpoint "\n1st point: "))
      ;; Prompt the user for the second point (with rubber band to the first point)
      ;; Assign result to the local variable 'b'
      ;; Note that by virtue of the AND function being a Special Form, this expression
      ;; will not be evaluated in the event that the first point prompt returns nil.
      (setq b (getpoint "\n2nd point: " a))
      ;; Calculate linear distance between points a & b, assign result of calculation
      ;; to local variable 'd'
      (setq d (distance a b))
      ;; Append the following DXF data to the current drawing database
      (entmake
            ;; Construct a DXF list of dotted pairs to append to the database
            (list
                ;; DXF Group 0: Entity Type
                ;; Fixed data, so expressed as quoted literal dotted pair -
                ;; See http://bit.ly/18ftLyF for more information on this concept
               '(000 . "XLINE")
                ;; DXF Group 100: Subclass marker designating object type
               '(100 . "AcDbEntity")
                ;; DXF Group 100: Subclass marker designating entity type
               '(100 . "AcDbXline")
                ;; Construct a dotted pair from the following atoms:
                ;; See http://bit.ly/18ftLyF for more information on why this cannot be quoted as a literal
                (cons
                  ;; DXF Group 10: WCS Base point for XLine
                  10
                  ;; Translate the following point from UCS to WCS
                  (trans
                        ;; Calculate the position of a point relative to point 'a' based on given calculations
                        (polar a (angle a b) (/ d (fix (1+ (/ d 4200.0))) 2.0))
                        1 0
                  ) ;; end trans
                ) ;; end cons
                ;; Construct a dotted pair from the following atoms:
                (cons
                  ;; DXF Group 11: WCS vector defining XLine direction
                  11
                  ;; Translate the following point from UCS to WCS, independent of UCS origin
                  (trans
                        ;; Calculate a UCS vector perpendicular to a->b
                        (list (- (cadr a) (cadr b)) (- (car b) (car a)))
                        1 0 t
                  ) ;; end trans
                ) ;; end cons
            ) ;; end list
      ) ;; end entmake
   ) ;; end and
   ;; Suppress the value returned by the last evaluated expression (AND returns t/nil)
   (princ)
) ;; end defun

 
本例中使用AutoLISP和函数更方便生成简洁的代码,但可能以可读性为代价。该示例也可以使用简单的if语句编写,但是,还必须使用progn函数使多个表达式能够作为单个“then”参数表达式的一部分进行计算:

;; Define function and declare local variables
(defun c:aspacing ( / a b d )
   ;; If the following test expression returns a non-nil value
   (if
       ;; Evaluate each of the following expressions until
       ;; an expression returns nil or no expressions remain
       (and
         ;; Prompt the user for the first point, assign result to local variable 'a'
         (setq a (getpoint "\n1st point: "))
         ;; Prompt the user for the second point (with rubber band to the first point)
         ;; Assign result to the local variable 'b'
         ;; Note that by virtue of the AND function being a Special Form, this expression
         ;; will not be evaluated in the event that the first point prompt returns nil.
         (setq b (getpoint "\n2nd point: " a))
       ) ;; end and
       ;; Evaluate the following expressions as a single expression
       ;; constituting the 'then' argument for the IF function
       (progn
         ;; Calculate linear distance between points a & b, assign result of calculation
         ;; to local variable 'd'
         (setq d (distance a b))
         ;; Append the following DXF data to the current drawing database
         (entmake
               ;; Construct a DXF list of dotted pairs to append to the database
               (list
                   ;; DXF Group 0: Entity Type
                   ;; Fixed data, so expressed as quoted literal dotted pair -
                   ;; See http://bit.ly/18ftLyF for more information on this concept
                  '(000 . "XLINE")
                   ;; DXF Group 100: Subclass marker designating object type
                  '(100 . "AcDbEntity")
                   ;; DXF Group 100: Subclass marker designating entity type
                  '(100 . "AcDbXline")
                   ;; Construct a dotted pair from the following atoms:
                   ;; See http://bit.ly/18ftLyF for more information on why this cannot be quoted as a literal
                   (cons
                     ;; DXF Group 10: WCS Base point for XLine
                     10
                     ;; Translate the following point from UCS to WCS
                     (trans
                           ;; Calculate the position of a point relative to point 'a' based on given calculations
                           (polar a (angle a b) (/ d (fix (1+ (/ d 4200.0))) 2.0))
                           1 0
                     ) ;; end trans
                   ) ;; end cons
                   ;; Construct a dotted pair from the following atoms:
                   (cons
                     ;; DXF Group 11: WCS vector defining XLine direction
                     11
                     ;; Translate the following point from UCS to WCS, independent of UCS origin
                     (trans
                           ;; Calculate a UCS vector perpendicular to a->b
                           (list (- (cadr a) (cadr b)) (- (car b) (car a)))
                           1 0 t
                     ) ;; end trans
                   ) ;; end cons
               ) ;; end list
         ) ;; end entmake
      ) ;; end progn
   ) ;; end if
   ;; Suppress the value returned by the last evaluated expression
   (princ)
) ;; end defun

 
非常欢迎您-如果您对发布的代码还有其他问题,请随时提问。

MichaelAllfire 发表于 2022-7-5 17:35:04

一旦它进入了代码的entmake和list部分,它就会越过我的大脑。我想我对Lisp编程的这些部分没有足够的理解。我必须去做更多的研究!我会在你的网站上查看教程,还有其他你可以推荐的吗?谢谢你的帮助!
 
继续从代码的实际功能开始-我想向这个命令添加一些东西,但不知道如何进行。我希望第一条xline偏移多次,这样xline的总数=d/4200的四舍五入值。连接线之间的距离必须相等,但端点和所选两点之间的距离必须为该距离的一半。这有意义吗?
 
这可以通过一个简单的偏移命令(同样,我不知道很多基础知识,这是我唯一真正的想法)来实现吗,即从第一个点到第二个点的距离的2倍?
 
我还有更多的事情要补充,但在这个阶段还需要一小步。谢谢你!

BIGAL 发表于 2022-7-5 17:39:56

如果你在与entmake作斗争,并且了解dxf代码可能会让人望而却步,那么现在就回到老式。
 

(defun c:aspacing ( / a b d )
   (and (setq a (getpoint "\n1st point: "))
      (setq b (getpoint "\n2nd point: " a))
      (setq d (distance a b))
      (setq pt1 (trans (polar a (angle a b) (/ d (fix (1+ (/ d 4200.0))) 2.0)) 1 0))
      (setq pt2 (trans (list (- (cadr a) (cadr b)) (- (car b) (car a))) 1 0 t))
      (command "XLINE" pt1 pt2 "")
   )
   (princ)
)

 
请张贴您想要的图片或图纸,使其清晰。

MichaelAllfire 发表于 2022-7-5 17:47:32

嗨,比格尔,
 
你的代码结构似乎与我最初的帖子相似。这似乎更直接(至少对我来说)遵循一种又一种方法。至少在我看来是这样的。谢谢你的回复!
 
我对这个司令部有一个很大的总体规划。下面附上的是我试图实现这一部分命令的图像
 
xline(图像中应该是黄色的,不是很黄)是我希望命令绘制的部分,我不需要任何尺寸、文本、圆或灰线,只需要xline。
 
这对之前的帖子有意义吗?XLine的数量需要
如果这有意义的话。(距离/4200的四舍五入值)
 
谢谢你的帮助!

MichaelAllfire 发表于 2022-7-5 17:50:02

也许我应该补充一下我对命令其余部分的计划。
 
我希望命令在绘制xline后第二次重复,但方向不同。
 
在第二次运行时,它需要-
获取第二个方向的两点之间的距离(值0)
获取两条现有连接线之间的距离。(值1)
将12000除以值1。(值2)
将值2乘以1000(值3)
将值0除以值3(值4)
将值4四舍五入到最接近的整数(值5)
使用值5作为在第二个方向上间隔的连接线数,在末端保留半个x。
 
基本上,如果我可以在第二个方向重复这个命令,它将允许我创建一个如下图所示的网格,其中网格矩形不超过12平方米。
 

 
我希望这有意义。
页: [1] 2
查看完整版本: Lisp routin的故障排除