CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 2023 开发者帮助

绘制行

2024-5-18 18:54| 发布者: admin| 查看: 127| 评论: 0|原作者: admin|来自: AutoCAD

绘制行

看看你是否能理解以下函数。将其与伪代码进行比较,并尝试捕获刚才描述的几何计算。您可能不熟悉一些 AutoLISP 函数,如果您需要有关这些函数的帮助,请参阅 AutoLISP 文档。现在,只需阅读代码;不要写任何东西。

(defun gp:Calculate-and-Draw-Tiles (BoundaryData / PathLength
                                    TileSpace TileRadius SpaceFilled SpaceToFill
                                    RowSpacing offsetFromCenter
                                    rowStartPoint pathWidth pathAngle
                                    ObjectCreationStyle TileList)
  (setq PathLength          (cdr (assoc 41 BoundaryData))
        TileSpace           (cdr (assoc 43 BoundaryData))
        TileRadius          (cdr (assoc 42 BoundaryData))
        SpaceToFill         (- PathLength TileRadius)
        RowSpacing          (* (+ TileSpace (* TileRadius 2.0))
                              (sin (Degrees->Radians 60))
                            ) ;_ end of *
        SpaceFilled         RowSpacing
        offsetFromCenter    0.0
        offsetDistance      (/ (+ (* TileRadius 2.0) TileSpace) 2.0)
        rowStartPoint       (cdr (assoc 10 BoundaryData))
        pathWidth           (cdr (assoc 40 BoundaryData))
        pathAngle           (cdr (assoc 50 BoundaryData))
        ObjectCreationStyle (strcase (cdr (assoc 3 BoundaryData)))
  ) ;_ end of setq

  ;; Compensate for the first call to gp:calculate-Draw-tile Row  
  ;; in the loop below.
  (setq rowStartPoint
        (polar rowStartPoint
               (+ pathAngle pi)
               (/ TileRadius 2.0)
        ) ;_ end of polar
  ) ;_ end of setq
  ;; Draw each row of tiles.
  (while (<= SpaceFilled SpaceToFill)
    ;; Get the list of tiles created, adding them to our list.
    (setq tileList
          (append tileList
                  (gp:calculate-Draw-TileRow
                    (setq rowStartPoint
                          (polar rowStartPoint
                                 pathAngle
                                 RowSpacing
                          ) ;_ end of polar
                    ) ;_ end of setq
                    TileRadius
                    TileSpace
                    pathWidth
                    pathAngle
                    offsetFromCenter
                    ObjectCreationStyle
                  ) ;_ end of gp:calculate-Draw-TileRow
          ) ;_ end of append
          ;; Calculate the distance along the path for the next row.
          SpaceFilled
          (+ SpaceFilled RowSpacing)
          ;; Alternate between a zero and a positive offset
          ;; (causes alternate rows to be indented).
          offsetFromCenter
          (if (= offsetFromCenter 0.0)
            offsetDistance
            0.0
          ) ;_ end of if
    ) ;_ end of setq
  ) ;_ end of while
  ;; Return the list of tiles created.
  tileList
) ;_ end of defun

代码中的几个部分可能需要一些额外的解释。

在循环开始之前,将出现以下代码片段:while

;; Compensate for the very first start point!!
(setq rowStartPoint(polar rowStartPoint 
(+ pathAngle pi)(/ TileRadius 2.0)))

弄清楚这个算法背后的逻辑有三个部分:

  • 变量通过分配用户选择的点作为路径的起点来开始其在函数中的生命。rowStartPointgp:Calculate-and-Draw-Tiles
  • 传递给函数的第一个参数执行以下操作:gp:calculate-Draw-TileRow

    (setq rowStartPoint(polar rowStartPoint pathAngle RowSpacing))

    另一种说法是:在调用函数时,变量被设置为超出电流的一个距离。gp:calculate-Draw-TileRowrowStartPointRowSpacingrowStartPoint

  • 该参数用作行中圆心的起点。rowStartPointgp:calculate-Draw-TileRow

为了补偿绘制第一行(即通过循环的第一个循环)期间的初始前移,您需要向相反方向稍微移动。目的是避免在路径边界和第一行之间出现大面积的空白空间。一半是足以移动点的量。这可以通过沿矢量方向投影来实现,该矢量方向与 .如果您考虑一下,这会将点暂时置于路径边界之外。rowStartPointwhilerowStartPointTileRadiuspolarrowStartPointPathAngle

下一个片段(为便于阅读而修改)可能有点令人费解:

(setq tileList
  (append tileList
          (gp:calculate-Draw-TileRow
            (setq rowStartPoint
                  (polar rowStartPoint pathAngle RowSpacing)
            ) ;_ end of setq
            TileRadius
            TileSpace
            pathWidth
            pathAngle
            offsetFromCenter
            ObjectCreationStyle
          )
  )
)

从本质上讲,存在着一个缠绕着的调用。setqappendgp:calculate-Draw-TileRow

该函数将返回绘制的每个图块的对象 ID。(对象 ID 指向图形中的切片对象。您正在逐行绘制图块,因此该函数一次返回一行的对象 ID。该函数将新的对象 ID 添加到存储在 中的任何现有对象 ID 中。gp:calculate-Draw-TileRowappendtileList

; Near the end of the function, you can find the following code fragment:
(setq offsetFromCenter
  (if (= offsetFromCenter 0.0)
    offsetDistance
    0.0
  )
)

这是偏移切换开关,它确定正在绘制的行是应以路径为中心的圆开始,还是应从路径偏移开始。此算法的伪代码如下:

; Set the offset amount to the following:
; If the offset is currently zero, set it to the offset distance;
; Otherwise, set it back to zero.

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1   苏公网安备32011402011833)

GMT+8, 2025-3-14 08:41

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部