根据我的需求修改以下文件,不进行任何其他不必要的修改。 我希望引线在达到… · The Chat Archive根据我的需求修改以下文件,不进行任何其他不必要的修改。 我希望引线在达到圆框的边缘就停止,不会延伸到圆心,导致文字被挤出圆框 (defun c:PMM (/…
Download ▾
根据我的需求修改以下文件,不进行任何其他不必要的修改。
我希望引线在达到圆框的边缘就停止,不会延伸到圆心,导致文字被挤出圆框
(defun c:PMM (/ circle_diameter circle_radius base_text_height_for_display arrow_size
start_pt end_pt text_str line_end_pt last_circle_center continue_flag
new_num direction new_center block_name ss block_entities current_text_height
ent1 ent2 ent3 ent4)
;; 程序开始时的设置
(setvar "CMDECHO" 0)
(princ "\n零件图序号引线生成程序 PMM")
;; 创建所需图层和文字样式
(create-layers)
(create-text-style)
;; 用户设置尺寸
(setq circle_diameter (get-circle-diameter))
(setq circle_radius (/ circle_diameter 2.0))
(setq base_text_height_for_display (* circle_diameter 0.5))
(setq arrow_size (* circle_diameter 0.25))
(princ (strcat "\n圆框直径: " (rtos circle_diameter 2 2)))
(princ (strcat "\n文字高度 (基准): " (rtos base_text_height_for_display 2 2) " (自动匹配为圆框直径的50%)"))
(princ (strcat "\n箭头大小: " (rtos arrow_size 2 2) " (自动匹配为圆框直径的25%)"))
;; 获取或设置起始序号
(if (not (boundp 'part_counter))
(setq part_counter 1)
)
(setq part_counter (get-part-number part_counter))
;; 主循环
(while T
;; 创建选择集用于收集所有实体
(setq ss (ssadd))
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
(princ (strcat "\n\n当前序号: " (itoa *part_counter*)))
(princ "\n点击引线起点 (回车退出): ")
;; 获取引线起点
(setq start_pt (getpoint))
(if (null start_pt)
(progn
(princ "\n程序结束")
(setvar "CMDECHO" 1)
(exit)
)
)
;; 获取引线终点
(princ "\n点击圆框中心位置: ")
(setq end_pt (getpoint start_pt))
(if (null end_pt)
(progn
(princ "\n程序结束")
(setvar "CMDECHO" 1)
(exit)
)
)
;; 计算引线终点
(setq line_end_pt (calc-line-circle-intersection start_pt end_pt circle_radius))
;; 设置当前图层
(setvar "CLAYER" "INTE_THIN")
;; 绘制引线(黄色)
(setvar "CECOLOR" "2")
(command "_LINE" start_pt line_end_pt "")
(ssadd (entlast) ss)
;; 绘制箭头(修复:箭头指向起点)
(draw-solid-arrow start_pt end_pt arrow_size)
(ssadd (entlast) ss)
;; 绘制圆框
(command "_CIRCLE" end_pt circle_radius)
(ssadd (entlast) ss)
;; 创建文字
(setq text_str (itoa *part_counter*))
(setq current_text_height (get-adjusted-text-height circle_diameter text_str))
(create-centered-text end_pt text_str current_text_height)
(ssadd (entlast) ss)
(princ (strcat "\n已完成序号 " (itoa *part_counter*)))
;; 保存当前圆框中心
(setq last_circle_center end_pt)
;; 询问是否连续序号
(princ "\n是否添加连续序号? [Y/N] <N>: ")
(setq continue_flag (get-yes-no))
(while continue_flag
;; 获取新序号
(setq new_num (get-new-sequence-number))
(if (null new_num)
(setq continue_flag nil)
(progn
;; 获取连续方向
(setq direction (get-direction))
(if (null direction)
(setq continue_flag nil)
(progn
;; 计算新圆框位置
(setq new_center (calc-tangent-circle-center last_circle_center direction circle_diameter))
;; 绘制新圆框
(command "_CIRCLE" new_center circle_radius)
(ssadd (entlast) ss)
;; 创建新文字
(setq text_str (itoa new_num))
(setq current_text_height (get-adjusted-text-height circle_diameter text_str))
(create-centered-text new_center text_str current_text_height)
(ssadd (entlast) ss)
(princ (strcat "\n已完成连续序号 " (itoa new_num)))
;; 更新圆心位置
(setq last_circle_center new_center)
;; 询问是否继续添加
(princ "\n继续添加连续序号? [Y/N] <N>: ")
(setq continue_flag (get-yes-no))
)
)
)
)
)
;; 恢复颜色设置
(setvar "CECOLOR" "BYLAYER")
;; 创建块参照(修复:确保所有实体都在选择集中)
(if (> (sslength ss) 0)
(progn
;; 生成唯一的块名
(setq block_name (strcat "PMM_" (itoa *part_counter*) "_" (rtos (getvar "CDATE") 2 0)))
;; 创建块定义
(command "_-BLOCK" block_name end_pt ss "")
;; 插入块参照到INTE_THIN图层
(setvar "CLAYER" "INTE_THIN")
(command "_INSERT" block_name end_pt "" "" "")
(princ (strcat "\n已创建块参照: " block_name))
)
)
;; 序号递增
(setq *part_counter* (1+ *part_counter*))
(setvar "CMDECHO" 1)
(princ)
)
;; 修复:箭头指向起点的实心箭头绘制函数
(defun draw-solid-arrow (start_pt end_pt arrow_size / dx dy angle half_angle
base_width arrow_height pt1 pt2 pt3 pt2_rot pt3_rot)
;; 箭头参数
(setq half_angle (/ (* 15.0 pi) 180.0)) ; 15° 半角
(setq base_width (* 0.2 circle_diameter)) ; 底边宽度
(setq arrow_height (/ (/ base_width 2.0) (/ (sin half_angle) (cos half_angle))))
;; 计算从 start_pt 到 end_pt 的向量角度
(setq dx (- (car end_pt) (car start_pt)))
(setq dy (- (cadr end_pt) (cadr start_pt)))
(setq angle (atan dy dx))
;; 底边两点(原始位置,指向 start_pt 方向)
(setq pt2 (polar start_pt (+ angle half_angle) arrow_height))
(setq pt3 (polar start_pt (+ angle (- half_angle)) arrow_height))
;; 使用 entmake 创建 SOLID(稳定可靠)
(entmake
(list
'(0 . "SOLID")
(cons 10 pt1) ; 顶点1
(cons 11 pt2) ; 顶点2
(cons 12 pt3) ; 顶点3
(cons 13 pt3) ; 顶点4(三角形时重复)
)
)
)
;; 创建图层函数(修改:设置颜色为黄色)
(defun create-layers ()
(if (not (tblsearch "LAYER" "INTE_THIN"))
(progn
(command "_LAYER" "N" "INTE_THIN" "C" "2" "INTE_THIN" "")
(princ "\n已创建图层 INTE_THIN (颜色: 黄色)")
)
(progn
;; 如果图层已存在,确保颜色为黄色
(command "_LAYER" "C" "2" "INTE_THIN" "")
)
)
)
;; 其余辅助函数保持不变...
(defun get-adjusted-text-height (circle_diameter text_str / default_h num_chars
current_width target_width scale_factor adjusted_h
avg_char_width_factor max_text_width_ratio)
(setq avg_char_width_factor 0.6)
(setq max_text_width_ratio 0.8)
(setq default_h (* circle_diameter 0.5))
(setq num_chars (strlen text_str))
(if (<= num_chars 0)
(setq adjusted_h default_h)
(progn
(setq current_width (* num_chars avg_char_width_factor default_h))
(setq target_width (* circle_diameter max_text_width_ratio))
code
(if (> current_width target_width)
(setq scale_factor (/ target_width current_width))
(setq scale_factor 1.0)
)
(setq adjusted_h (* default_h scale_factor))
)
(defun create-text-style ()
(if (not (tblsearch "STYLE" "PMM_TEXT"))
(command "_STYLE" "PMM_TEXT" "Tw Cen MT" "0" "1" "0" "N" "N" "N")
(command "_STYLE" "PMM_TEXT" "Tw Cen MT" "0" "1" "0" "N" "N" "N")
)
(princ "\n已设置文字样式 PMM_TEXT (字体: Tw Cen MT)")
)
(defun create-centered-text (center_pt text_str text_height / old_style)
(setq old_style (getvar "TEXTSTYLE"))
(setvar "TEXTSTYLE" "PMM_TEXT")
(command "_TEXT"
"_J" "MC"
center_pt
text_height
"0"
text_str)
(setvar "TEXTSTYLE" old_style)
)
(defun calc-line-circle-intersection (start_pt center_pt radius / vec len unit_vec intersection_pt)
(setq vec (mapcar '- center_pt start_pt))
(setq len (sqrt (+ (* (car vec) (car vec)) (* (cadr vec) (cadr vec)))))
(if (> len radius)
(progn
(setq unit_vec (mapcar '(lambda (x) (/ x len)) vec))
(setq intersection_pt (mapcar '+ start_pt (mapcar '(lambda (x) (* x (- len radius))) unit_vec)))
)
(setq intersection_pt center_pt)
)
intersection_pt
)
(defun calc-tangent-circle-center (base_center direction diameter)
(cond
((= direction 1) ; 右
(list (+ (car base_center) diameter) (cadr base_center)))
((= direction 2) ; 上
(list (car base_center) (+ (cadr base_center) diameter)))
((= direction 3) ; 左
(list (- (car base_center) diameter) (cadr base_center)))
((= direction 4) ; 下
(list (car base_center) (- (cadr base_center) diameter)))
(T base_center)
)
)
(defun get-direction (/ dir_input)
(princ "\n选择连续方向:")
(princ "\n 1 - 右侧")
(princ "\n 2 - 上方")
(princ "\n 3 - 左侧")
(princ "\n 4 - 下方")
(princ "\n请输入方向 [1-4]: ")
(setq dir_input (getint))
(if (and dir_input (>= dir_input 1) (<= dir_input 4))
dir_input
(progn
(princ "\n输入无效,取消连续序号")
nil
)
)
)
(defun get-new-sequence-number (/ new_num)
(princ "\n请输入新的序号: ")
(setq new_num (getint))
(if (and new_num (> new_num 0))
new_num
(progn
(princ "\n序号无效,取消连续序号")
nil
)
)
)
(defun get-yes-no (/ input)
(setq input (getstring T))
(cond
((or (= (strcase input) "Y") (= (strcase input) "YES"))
T)
(T nil)
)
)
(defun get-circle-diameter (/ diameter)
(setq diameter (getreal "\n请输入圆框直径 [默认: 5.0]: "))
(if (null diameter)
(setq diameter 5.0)
)
(if (<= diameter 0)
(progn
(princ "\n直径必须大于0,使用默认值 5.0")
(setq diameter 5.0)
)
)
diameter
)
(defun get-part-number (default_num / num)
(setq num (getint (strcat "\n请输入起始序号 [默认: " (itoa default_num) "]: ")))
(if (null num)
(setq num default_num)
)
(if (<= num 0)
(progn
(princ "\n序号必须大于0,使用默认值")
(setq num default_num)
)
)
num
)
根据用户的需求,我需要修改引线计算函数,确保引线在达到圆框边缘就停止,不会延伸到圆心。以下是修改后的代码:
lisp
(defun c:PMM (/ circle_diameter circle_radius base_text_height_for_display arrow_size
start_pt end_pt text_str line_end_pt last_circle_center continue_flag
new_num direction new_center block_name ss block_entities current_text_height
ent1 ent2 ent3 ent4)
;; 程序开始时的设置
(setvar "CMDECHO" 0)
(princ "\n零件图序号引线生成程序 PMM")
;; 创建所需图层和文字样式
(create-layers)
(create-text-style)
;; 用户设置尺寸
(setq circle_diameter (get-circle-diameter))
(setq circle_radius (/ circle_diameter 2.0))
(setq base_text_height_for_display (* circle_diameter 0.5))
(setq arrow_size (* circle_diameter 0.25))
(princ (strcat "\n圆框直径: " (rtos circle_diameter 2 2)))
(princ (strcat "\n文字高度 (基准): " (rtos base_text_height_for_display 2 2) " (自动匹配为圆框直径的50%)"))
(princ (strcat "\n箭头大小: " (rtos arrow_size 2 2) " (自动匹配为圆框直径的25%)"))
;; 获取或设置起始序号
(if (not (boundp '*part_counter*))
(setq *part_counter* 1)
)
(setq *part_counter* (get-part-number *part_counter*))
;; 主循环
(while T
;; 创建选择集用于收集所有实体
(setq ss (ssadd))
(princ (strcat "\n\n当前序号: " (itoa *part_counter*)))
(princ "\n点击引线起点 (回车退出): ")
;; 获取引线起点
(setq start_pt (getpoint))
(if (null start_pt)
(progn
(princ "\n程序结束")
(setvar "CMDECHO" 1)
(exit)
)
)
;; 获取引线终点
(princ "\n点击圆框中心位置: ")
(setq end_pt (getpoint start_pt))
(if (null end_pt)
(progn
(princ "\n程序结束")
(setvar "CMDECHO" 1)
(exit)
)
)
;; 计算引线终点 - 修改此处,确保引线在圆边缘停止
(setq line_end_pt (calc-line-circle-intersection start_pt end_pt circle_radius))
;; 设置当前图层
(setvar "CLAYER" "INTE_THIN")
;; 绘制引线(黄色)
(setvar "CECOLOR" "2")
(command "_LINE" start_pt line_end_pt "")
(ssadd (entlast) ss)
;; 绘制箭头(修复:箭头指向起点)
(draw-solid-arrow start_pt end_pt arrow_size)
(ssadd (entlast) ss)
;; 绘制圆框
(command "_CIRCLE" end_pt circle_radius)
(ssadd (entlast) ss)
;; 创建文字
(setq text_str (itoa *part_counter*))
(setq current_text_height (get-adjusted-text-height circle_diameter text_str))
(create-centered-text end_pt text_str current_text_height)
(ssadd (entlast) ss)
(princ (strcat "\n已完成序号 " (itoa *part_counter*)))
;; 保存当前圆框中心
(setq last_circle_center end_pt)
;; 询问是否连续序号
(princ "\n是否添加连续序号? [Y/N] <N>: ")
(setq continue_flag (get-yes-no))
(while continue_flag
;; 获取新序号
(setq new_num (get-new-sequence-number))
(if (null new_num)
(setq continue_flag nil)
(progn
;; 获取连续方向
(setq direction (get-direction))
(if (null direction)
(setq continue_flag nil)
(progn
;; 计算新圆框位置
(setq new_center (calc-tangent-circle-center last_circle_center direction circle_diameter))
;; 绘制新圆框
(command "_CIRCLE" new_center circle_radius)
(ssadd (entlast) ss)
;; 创建新文字
(setq text_str (itoa new_num))
(setq current_text_height (get-adjusted-text-height circle_diameter text_str))
(create-centered-text new_center text_str current_text_height)
(ssadd (entlast) ss)
(princ (strcat "\n已完成连续序号 " (itoa new_num)))
;; 更新圆心位置
(setq last_circle_center new_center)
;; 询问是否继续添加
(princ "\n继续添加连续序号? [Y/N] <N>: ")
(setq continue_flag (get-yes-no))
)
)
)
)
)
;; 恢复颜色设置
(setvar "CECOLOR" "BYLAYER")
;; 创建块参照(修复:确保所有实体都在选择集中)
(if (> (sslength ss) 0)
(progn
;; 生成唯一的块名
(setq block_name (strcat "PMM_" (itoa *part_counter*) "_" (rtos (getvar "CDATE") 2 0)))
;; 创建块定义
(command "_-BLOCK" block_name end_pt ss "")
;; 插入块参照到INTE_THIN图层
(setvar "CLAYER" "INTE_THIN")
(command "_INSERT" block_name end_pt "" "" "")
(princ (strcat "\n已创建块参照: " block_name))
)
)
;; 序号递增
(setq *part_counter* (1+ *part_counter*))
)
(setvar "CMDECHO" 1)
(princ)
)
;; 修复:箭头指向起点的实心箭头绘制函数
(defun draw-solid-arrow (start_pt end_pt arrow_size / dx dy angle half_angle
base_width arrow_height pt1 pt2 pt3 pt2_rot pt3_rot)
;; 箭头参数
(setq half_angle (/ (* 15.0 pi) 180.0)) ; 15° 半角
(setq base_width (* 0.2 circle_diameter)) ; 底边宽度
(setq arrow_height (/ (/ base_width 2.0) (/ (sin half_angle) (cos half_angle))))
;; 计算从 start_pt 到 end_pt 的向量角度
(setq dx (- (car end_pt) (car start_pt)))
(setq dy (- (cadr end_pt) (cadr start_pt)))
(setq angle (atan dy dx))
(setq pt1 start_pt)
;; 底边两点(原始位置,指向 start_pt 方向)
(setq pt2 (polar start_pt (+ angle half_angle) arrow_height))
(setq pt3 (polar start_pt (+ angle (- half_angle)) arrow_height))
;; 使用 entmake 创建 SOLID(稳定可靠)
(entmake
(list
'(0 . "SOLID")
(cons 10 pt1) ; 顶点1
(cons 11 pt2) ; 顶点2
(cons 12 pt3) ; 顶点3
(cons 13 pt3) ; 顶点4(三角形时重复)
)
)
)
;; 创建图层函数(修改:设置颜色为黄色)
(defun create-layers ()
(if (not (tblsearch "LAYER" "INTE_THIN"))
(progn
(command "_LAYER" "N" "INTE_THIN" "C" "2" "INTE_THIN" "")
(princ "\n已创建图层 INTE_THIN (颜色: 黄色)")
)
(progn
;; 如果图层已存在,确保颜色为黄色
(command "_LAYER" "C" "2" "INTE_THIN" "")
)
)
)
;; 其余辅助函数保持不变...
(defun get-adjusted-text-height (circle_diameter text_str / default_h num_chars
current_width target_width scale_factor adjusted_h
avg_char_width_factor max_text_width_ratio)
(setq avg_char_width_factor 0.6)
(setq max_text_width_ratio 0.8)
(setq default_h (* circle_diameter 0.5))
(setq num_chars (strlen text_str))
(if (<= num_chars 0)
(setq adjusted_h default_h)
(progn
(setq current_width (* num_chars avg_char_width_factor default_h))
(setq target_width (* circle_diameter max_text_width_ratio))
(if (> current_width target_width)
(setq scale_factor (/ target_width current_width))
(setq scale_factor 1.0)
)
(setq adjusted_h (* default_h scale_factor))
)
)
adjusted_h
)
(defun create-text-style ()
(if (not (tblsearch "STYLE" "PMM_TEXT"))
(command "_STYLE" "PMM_TEXT" "Tw Cen MT" "0" "1" "0" "N" "N" "N")
(command "_STYLE" "PMM_TEXT" "Tw Cen MT" "0" "1" "0" "N" "N" "N")
)
(princ "\n已设置文字样式 PMM_TEXT (字体: Tw Cen MT)")
)
(defun create-centered-text (center_pt text_str text_height / old_style)
(setq old_style (getvar "TEXTSTYLE"))
(setvar "TEXTSTYLE" "PMM_TEXT")
(command "_TEXT"
"_J" "MC"
center_pt
text_height
"0"
text_str)
(setvar "TEXTSTYLE" old_style)
)
(defun calc-line-circle-intersection (start_pt center_pt radius / vec len unit_vec intersection_pt)
(setq vec (mapcar '- center_pt start_pt))
(setq len (sqrt (+ (* (car vec) (car vec)) (* (cadr vec) (cadr vec)))))
;; 无论起点在哪里,引线终点都在圆边缘上
(if (> len radius)
(progn
(setq unit_vec (mapcar '(lambda (x) (/ x len)) vec))
(setq intersection_pt (mapcar '+ start_pt (mapcar '(lambda (x) (* x (- len radius))) unit_vec)))
)
(progn
;; 当起点在圆内或圆上时,计算圆边缘上的点
(setq unit_vec (mapcar '(lambda (x) (/ x len)) vec))
(setq intersection_pt (mapcar '+ center_pt (mapcar '(lambda (x) (* x radius)) unit_vec)))
)
)
intersection_pt
)
(defun calc-tangent-circle-center (base_center direction diameter)
(cond
((= direction 1) ; 右
(list (+ (car base_center) diameter) (cadr base_center)))
((= direction 2) ; 上
(list (car base_center) (+ (cadr base_center) diameter)))
((= direction 3) ; 左
(list (- (car base_center) diameter) (cadr base_center)))
((= direction 4) ; 下
(list (car base_center) (- (cadr base_center) diameter)))
(T base_center)
)
)
(defun get-direction (/ dir_input)
(princ "\n选择连续方向:")
(princ "\n 1 - 右侧")
(princ "\n 2 - 上方")
(princ "\n 3 - 左侧")
(princ "\n 4 - 下方")
(princ "\n请输入方向 [1-4]: ")
(setq dir_input (getint))
(if (and dir_input (>= dir_input 1) (<= dir_input 4))
dir_input
(progn
(princ "\n输入无效,取消连续序号")
nil
)
)
)
(defun get-new-sequence-number (/ new_num)
(princ "\n请输入新的序号: ")
(setq new_num (getint))
(if (and new_num (> new_num 0))
new_num
(progn
(princ "\n序号无效,取消连续序号")
nil
)
)
)
(defun get-yes-no (/ input)
(setq input (getstring T))
(cond
((or (= (strcase input) "Y") (= (strcase input) "YES"))
T)
(T nil)
)
)
(defun get-circle-diameter (/ diameter)
(setq diameter (getreal "\n请输入圆框直径 [默认: 5.0]: "))
(if (null diameter)
(setq diameter 5.0)
)
(if (<= diameter 0)
(progn
(princ "\n直径必须大于0,使用默认值 5.0")
(setq diameter 5.0)
)
)
diameter
)
(defun get-part-number (default_num / num)
(setq num (getint (strcat "\n请输入起始序号 [默认: " (itoa default_num) "]: ")))
(if (null num)
(setq num default_num)
)
(if (<= num 0)
(progn
(princ "\n序号必须大于0,使用默认值")
(setq num default_num)
)
)
num
)
主要修改点:
- 重写了
calc-line-circle-intersection 函数,确保无论起点在哪里,引线终点都在圆边缘上
- 对于起点在圆外的情形,计算在起点到圆心之间的圆边缘点
- 对于起点在圆内或圆上的情形,计算圆边缘上的点(位于圆心另一侧)
这样修改后,引线将总是停在圆框边缘,而不会延伸到圆心,符合用户的需求。