明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 624|回复: 7

[讨论] 关于曲线两侧固定长度切线

[复制链接]
发表于 2021-2-28 11:04 | 显示全部楼层 |阅读模式
本帖最后由 20060510412 于 2021-2-28 11:05 编辑

前段时间有个需求,就是在曲线两侧求固定长度垂线,这个在论坛里面下载了源代码,自己有稍微修改一些,已经符合自己需求。
现在想进一步,求曲线两侧固定长度切线,原来以为很简单,直接使用GetFirstDerrive函数,结果发现切线的方向不是自己想要的,请问高手应该如何改进。

这是固定长度垂线的源代码(论坛里面下载修改的,感谢原作者):
;画垂线cxx
(setq saved_real 50)
(defun c:CCX(/ e p tmp_real)

        (setq tmp_real (getreal (strcat "垂线长度 <" (rtos saved_real 2 1) ">: ")))
        (if tmp_real ; 判断输入
    (setq saved_real tmp_real) ; 有输入,保存前输入的值
    (setq tmp_real saved_real) ; 没输入,将原保存的值赋值给 tmp_real
    )
        
  (while(setq e(car(entsel "\n拾取基准线")))
    (while(setq p(getpoint "\n垂足位置"))
      (entmakex ( list'(0 . "line")(cons 10(setq p(vlax-curve-getclosestpointto e p)))
                 (cons 11(polar p(+(angle p(mapcar'+(vlax-curve-getfirstDeriv e(vlax-curve-getParamAtPoint e p))p))(* pi 0.5))tmp_real))  ))
          (entmakex ( list'(0 . "line")(cons 10(setq p(vlax-curve-getclosestpointto e p)))
                 (cons 11(polar p(-(angle p(mapcar'+(vlax-curve-getfirstDeriv e(vlax-curve-getParamAtPoint e p)) p))(* pi 0.5))tmp_real))  ))                 
    )

  )
  (princ)
)



这是我自己修改的固定长度切线的源代码(切线方向无法控制,需改进):
(setq saved_real 50)
(defun c:CCX1(/ e p tmp_real)
        
        (setq tmp_real (getreal (strcat "垂线长度 <" (rtos saved_real 2 1) ">: ")))
        (if tmp_real ; 判断输入
    (setq saved_real tmp_real) ; 有输入,保存前输入的值
    (setq tmp_real saved_real) ; 没输入,将原保存的值赋值给 tmp_real
        )
        
  (while(setq e(car(entsel "\n拾取基准线")))
    (while(setq p(getpoint "\n垂足位置"))
      (entmakex ( list'(0 . "line")
                                                                        (cons 10(setq p(vlax-curve-getclosestpointto e p)))
                                                                        (cons 11(polar p(angle p(mapcar'+(vlax-curve-getfirstDeriv e(vlax-curve-getParamAtPoint e p))p)) tmp_real))  
                                                                        
                                                                )
                                
                        )                     
    )
               
  )
  (princ)
)


"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2021-2-28 13:17 | 显示全部楼层
(defun c:TT (/ E D P X)
  (if (and (setq E (entsel))
           (setq D (getdist "\n指定长度: "))
      )
    (progn
      (setq P (trans (cadr E) 1 0)
            E (car E)
            P (vlax-curve-getClosestPointTo E P)
            X (vlax-curve-getFirstDeriv E (vlax-curve-getParamAtPoint E P))
            X (angle '(0 0 0) X)
            X (polar P X D)
      )
      (entmake (list '(0 . "LINE") (cons 10 P) (cons 11 X)))
    )
  )
  (princ)
)
 楼主| 发表于 2021-2-28 14:25 | 显示全部楼层
本帖最后由 20060510412 于 2021-2-28 14:33 编辑
caoyin 发表于 2021-2-28 13:17
(defun c:TT (/ E D P X)
  (if (and (setq E (entsel))
           (setq D (getdist "\n指定长度: "))
...

您好,这个代码同样存在切线方向的问题。例如对于一段圆弧,求两侧端点的切线,效果应该都是朝向远离圆弧的方向。
但是实际情况是一个远离圆弧方向,一个朝向圆弧方向。

现在论坛不知道为什么,好像无法上传图片附件了。
发表于 2021-2-28 14:50 | 显示全部楼层
本帖最后由 caoyin 于 2021-2-28 15:23 编辑

(defun c:TT (/ E D P1 P1 P2 A1 A2)
  (if (and (setq E (entsel))
           (not (vlax-curve-isClosed (setq E (car E))))
           (setq D (getdist "\n指定长度: "))
      )
    (progn
      (setq P1 (vlax-curve-getStartPoint E)
            P2 (vlax-curve-getEndPoint E)
            A1 (angle (vlax-curve-getFirstDeriv E (vlax-curve-getStartParam E)) '(0 0 0))
            A2 (angle '(0 0 0) (vlax-curve-getFirstDeriv E (vlax-curve-getEndParam E)))
      )
      (entmake (list '(0 . "LINE")
                     (cons 10 P1)
                     (cons 11 (polar P1 A1 D))
               )
      )
      (entmake (list '(0 . "LINE")
                     (cons 10 P2)
                     (cons 11 (polar P2 A2 D))
               )
      )
    )
  )
  (princ)
)
 楼主| 发表于 2021-2-28 15:36 | 显示全部楼层
caoyin 发表于 2021-2-28 14:50
(defun c:TT (/ E D P1 P1 P2 A1 A2)
  (if (and (setq E (entsel))
           (not (vlax-curve-isClos ...

谢谢您,正是这个效果,受益匪浅。
 楼主| 发表于 2021-2-28 15:46 | 显示全部楼层
caoyin 发表于 2021-2-28 14:50
(defun c:TT (/ E D P1 P1 P2 A1 A2)
  (if (and (setq E (entsel))
           (not (vlax-curve-isClos ...

您好,我看您的代码是同时画出起点和终点的切线。
但我的使用场景是用户手动选择端点,然后才画出切线。此时还需要判断该点为startpoint还是endpoint,这个应该如何实现呢?
发表于 2021-2-28 17:08 | 显示全部楼层
(defun c:TT (/ E P D PP1 PP2 A)
  (if (and (setq E (entsel "\n选择靠近端点的位置: "))
           (progn
             (setq P (trans (cadr E) 1 0)
                   E (car E)
             )
             (not (vlax-curve-isClosed E))
           )
           (setq D (getdist "\n指定长度: "))
      )
    (progn
      (setq P   (vlax-curve-getClosestPointTo E P)
            PP  (vlax-curve-getParamAtPoint E P)
            PP1 (vlax-curve-getStartParam E)
            PP2 (vlax-curve-getEndParam E)
      )
      (if (< (- PP PP1) (- PP2 PP))
        (setq P (vlax-curve-getPointAtParam E PP1)
              A (angle (vlax-curve-getFirstDeriv E PP1) '(0 0 0))
        )
        (setq P (vlax-curve-getPointAtParam E PP2)
              A (angle '(0 0 0) (vlax-curve-getFirstDeriv E PP2))
        )
      )
      (entmake (list '(0 . "LINE")
                     (cons 10 P)
                     (cons 11 (polar P A D))
               )
      )
    )
  )
  (princ)
)
 楼主| 发表于 2021-2-28 21:36 | 显示全部楼层
caoyin 发表于 2021-2-28 17:08
(defun c:TT (/ E P D PP1 PP2 A)
  (if (and (setq E (entsel "\n选择靠近端点的位置: "))
           ( ...

大神辛苦了,正是我想象中的效果
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-24 23:41 , Processed in 0.511859 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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