明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 13920|回复: 15

[函数] [求助]任意两条线的交点坐标

  [复制链接]
发表于 2010-1-5 10:23:00 | 显示全部楼层 |阅读模式
任意两条线的交点坐标,包括PL线与PL线,pl线与LINE,PL线与圆弧,pl线与圆等等,包括它们之间的任意组合方式,得到交点坐标表。请大侠帮助。谢谢
发表于 2024-5-19 22:53:38 | 显示全部楼层
yoyoho 发表于 2010-1-6 22:19
(defun m_IntersectWith (m_ent1 m_ent2 / m_obj1 m_obj2 m_objcopy1 m_objcopy2 m_jdtab m_jdtab1 i)&nbsp ...

yoyoho大侠好!我一条线与N条等高线外观交点粘贴cass高程属性块20240519圆和闭合多段线不能实现,自己修改过的文件上传到这里了!感觉大侠还能继续优化→能否实现框选cass舒心高程块→输出剖面线并粘贴高程值http://bbs.mjtd.com/forum.php?mo ... mp;page=6#pid980652
发表于 2024-6-2 16:30:58 | 显示全部楼层
李龙山 发表于 2010-1-5 22:24
测试通过了。(defun c:get_int0( / ent1 ent2 ps)  (setq ent1 (car (entsel "选择第一实体:")))&nbsp ...

谢谢 才开始学习 也在找这个
发表于 2018-3-5 14:37:35 | 显示全部楼层
任意空间的线如果Z坐标不一致的,还是求不到交点啊,好难啊
明经网友  发表于 2010-1-5 10:58:00
你去看一看置顶帖,里面就有。
回复 支持 反对

使用道具

发表于 2010-1-5 12:11:00 | 显示全部楼层

网内搜一下 vla-IntersectWith

 楼主| 发表于 2010-1-5 21:22:00 | 显示全部楼层

(defun m_IntersectWith(m_ent1 m_ent2 / m_obj1 m_obj2 m_objcopy1 m_objcopy2 m_jdtab m_jdtab1 i)
  ;;适用对象: Line、Polyline、LWPolyline、Circle、Arc、Ellipse、3dPolyline、Spline
  ;;支持求空间虚交点,但Z坐标始终为0.0,要求Z坐标,请用(vlax-curve-getClosestPointToProjection)函数
  (setq m_obj1 (vlax-ename->vla-object m_ent1))
  (setq m_obj2 (vlax-ename->vla-object m_ent2))
 
  (setq m_objcopy1 (vla-copy m_obj1));;复制第一条曲线实体
  (setq m_objcopy2 (vla-copy m_obj2));;复制第二条曲线实体
 
  (setq m_objcopy1 (m_ShadowToXY m_objcopy1));;得到投影实体
  (setq m_objcopy2 (m_ShadowToXY m_objcopy2));;得到投影实体

 
  (setq m_jdtab1 (vla-intersectwith m_objcopy1 m_objcopy2 acExtendnone));;得到交点集
 
  (if (> (vlax-safearray-get-u-bound (vlax-variant-value m_jdtab1) 1) 1);;判断有无交点
      (progn
(setq m_jdtab1 (vlax-safearray->list (vlax-variant-value m_jdtab1)));;safearray数组转换为list表
(setq i 0)
(repeat (/ (length m_jdtab1) 3)
   (setq m_jd (list (nth i m_jdtab1) (nth (+ 1 i) m_jdtab1) (nth (+ 2 i) m_jdtab1)));;取得一个交点
   (setq m_jdtab (cons m_jd m_jdtab));;构造交点表((第一个交点) (第二个交点)。。。)
   (setq i (+ 3 i))
)
      )
      (princ"\n两曲线无交点!")
  )
 
  (vla-delete m_objcopy1);;删除复制的第一条曲线实体
  (vla-delete m_objcopy2);;删除复制的第二条曲线实体
 
  (setq m_jdtab m_jdtab);;返回交点表,无交点返回nil
)

(defun m_ShadowToXY(m_obj / m_objname m_pts m_pts1 i)
  ;;对曲线实体m_obj创建一个投影至xy平面的曲线实体,即对曲线实体上每个控制点的z坐标值置为0.0
  ;;输入曲线实体(vla对象)
  ;;返回投影实体(vla对象)
  (setq m_objname (vla-get-objectname m_obj));;取得实体的类型名称
  (m_princ "\nObjectName:" m_objname)
  (cond
    ((= "AcDbSpline" m_objname);;样条曲线(Spline)
     (setq i 0)
     (setq m_pts (vlax-variant-value (vla-get-fitpoints m_obj)));;取得样条曲线的拟合点
     (setq m_pts1 (vlax-variant-value (vla-get-controlpoints m_obj)));;取得样条曲线的控制点
     (repeat (vla-get-numberoffitpoints m_obj);;循环
       (vlax-safearray-put-element m_pts (+ i 2) 0.0);;改变每个拟合点的z值为0.0
       (setq i (+ i 3))
     )
     (vla-put-fitpoints m_obj m_pts);;更改曲线拟合点属性

     (setq i 0)
    
     (repeat (vla-get-numberofcontrolpoints m_obj);;循环
       (vlax-safearray-put-element m_pts1 (+ i 2) 0.0);;改变每个控制点的z值为0.0
       (setq i (+ i 3))
     )
     (vla-put-controlpoints m_obj m_pts1);;更改曲线控制点属性
    )
   
    ((= "AcDb3dPolyline" m_objname);;三维多段线(3dpolyline)
     (setq i 0)
     (setq m_pts (vlax-variant-value (vla-get-coordinates m_obj)));;取得3维多段线的控制点
     (repeat (/ (length (vlax-safearray->list m_pts)) 3)
       (vlax-safearray-put-element m_pts (+ i 2) 0.0)
       (setq i (+ i 3))
     )
     (vla-put-coordinates m_obj m_pts)
    )
   
    ((= "AcDbLine" m_objname);;直线(line)
     (setq i 0)
     (setq m_pts (vlax-variant-value (vla-get-startpoint m_obj)));;取得直线的起点座标
     (setq m_pts1 (vlax-variant-value (vla-get-endpoint m_obj)));;取得直线的端点座标
     (vlax-safearray-put-element m_pts 2 0.0);;改变起点座标z值为0.0
     (vlax-safearray-put-element m_pts1 2 0.0)
     (vla-put-startpoint m_obj m_pts)
     (vla-put-endpoint m_obj m_pts1)
    )

    ((or (= "AcDbCircle" m_objname);;园(circle)
  (= "AcDbArc" m_objname);;圆弧(arc)
  (= "AcDbEllipse" m_objname);;椭圆及椭圆弧(ellipse)
     )
     (setq m_pts (vlax-variant-value (vla-get-center m_obj)));;取得中心点座标
     (vlax-safearray-put-element m_pts 2 0.0);;改变中心点座标z值为0.0
     (vla-put-center m_obj m_pts)
    )
   
    ((or (= "AcDbPolyline" m_objname);;多段线(polyline、lwpolyline)
  (= "AcDb2dPolyline" m_objname);;拟合的2维多段线(polyline、lwpolyline)
     )
     (vla-put-elevation m_obj 0.0);;改变标高值为0.0
    )
  )
  (setq m_obj m_obj)
)


(defun C:test(/ m_ent1 m_ent2 m_jdtab)
 (vl-load-com)
 
 (setq m_ent1 (car (entsel "\n请选择第一条曲线: ")))
  (setq m_ent2 (car (entsel "\n请选择第二条曲线: ")))
  (setq ZL-GETINTERS ( m_ent1 m_ent2))
  (princ"\n交点表: ")(princ ZL-GETINTERS)
  (princ)
)

Andyhon大侠

帮我看看,测试后也不返回交点列表呀,也不知道哪里出错

 楼主| 发表于 2010-1-5 22:24:00 | 显示全部楼层

测试通过了。

(defun c:get_int0( / ent1 ent2 ps)
  (setq ent1 (car (entsel "选择第一实体:")))
  (setq ent2 (car (entsel "选择第二实体:")))
  (print)
  (setq ps (obj_int ent1 ent2))
)
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;功能:返回两个对象的所有交点
;参数: ent1、ent2 均为ename对象
(defun obj_int (ent1 ent2 / ax_ent_1 ax_ent_2 intpoints points i)
   (setq ax_ent_1 (vlax-ename->vla-object ent1)
         ax_ent_2 (vlax-ename->vla-object ent2)
   )
   (setq intpoints (vla-intersectwith ax_ent_1 ax_ent_2 acextendnone))
   (setq intpoints (vlax-variant-value intpoints))
   (setq i 0)
   (if (> (vlax-safearray-get-u-bound intpoints 1) 0)
     (repeat (/ (+ 1
               (- (vlax-safearray-get-u-bound intpoints 1)
                  (vlax-safearray-get-l-bound intpoints 1)
               )
            )
            3
         )
       (setq points (append points (list (list
         (vlax-safearray-get-element intpoints i)
         (vlax-safearray-get-element intpoints (+ i 1))
         (vlax-safearray-get-element intpoints (+ i 2))
       )))
       )
       (setq i (+ 3 i))
     )
   )
   points
)

发表于 2010-1-6 09:16:00 | 显示全部楼层
提供另一种调用做比较
http://www.menziengineering.ch/Downloads/Download.htm
VxGetInters - Returns all intersection points between two objects
 楼主| 发表于 2010-1-6 19:59:00 | 显示全部楼层
谢谢Andyhon
发表于 2010-1-6 22:19:00 | 显示全部楼层

(defun m_IntersectWith (m_ent1 m_ent2 / m_obj1 m_obj2 m_objcopy1 m_objcopy2 m_jdtab m_jdtab1 i)
  ;;适用对像: Line、Polyline、LWPolyline、Circle、Arc、Ellipse、3dPolyline、Spline
  ;;支持求空间虚交点,但Z坐标始终为0.0,要求Z坐标,请用(vlax-curve-getClosestPointToProjection)函数
  (setq m_obj1 (vlax-ename->vla-object m_ent1))
  (setq m_obj2 (vlax-ename->vla-object m_ent2))

  (setq m_objcopy1 (vla-copy m_obj1));;复制第一条曲线实体
  (setq m_objcopy2 (vla-copy m_obj2));;复制第二条曲线实体

  (setq m_objcopy1 (m_ShadowToXY m_objcopy1));;得到投影实体
  (setq m_objcopy2 (m_ShadowToXY m_objcopy2));;得到投影实体


  (setq m_jdtab1 (vla-intersectwith m_objcopy1 m_objcopy2 acExtendnone));;得到交点集

  (if (> (vlax-safearray-get-u-bound (vlax-variant-value m_jdtab1) 1) 1);;判断有无交点
      (progn
(setq m_jdtab1 (vlax-safearray->list (vlax-variant-value m_jdtab1)));;safearray数组转换为list表
(setq i 0)
(repeat (/ (length m_jdtab1) 3)
   (setq m_jd (list (nth i m_jdtab1) (nth (+ 1 i) m_jdtab1) (nth (+ 2 i) m_jdtab1)));;取得一个交点
   (setq m_jdtab (cons m_jd m_jdtab));;构造交点表((第一个交点) (第二个交点)。。。)
   (setq i (+ 3 i))
)
      )
      (princ"\n两曲线无交点!")
  )

  (vla-delete m_objcopy1);;删除复制的第一条曲线实体
  (vla-delete m_objcopy2);;删除复制的第二条曲线实体

  (setq m_jdtab m_jdtab);;返回交点表,无交点返回nil
)

(defun m_ShadowToXY(m_obj / m_objname m_pts m_pts1 i)
  ;;对曲线实体m_obj创建一个投影至xy平面的曲线实体,即对曲线实体上每个控制点的z坐标值置为0.0
  ;;输入曲线实体(vla对像)
  ;;返回投影实体(vla对像)
  (setq m_objname (vla-get-objectname m_obj));;取得实体的类型名称
;  (m_princ "\nObjectName:" m_objname)

  (cond
    ((= "AcDbSpline" m_objname);;样条曲线(Spline)
     (setq i 0)
     (setq m_pts (vlax-variant-value (vla-get-fitpoints m_obj)));;取得样条曲线的拟合点
     (setq m_pts1 (vlax-variant-value (vla-get-controlpoints m_obj)));;取得样条曲线的控制点
     (repeat (vla-get-numberoffitpoints m_obj);;循环
       (vlax-safearray-put-element m_pts (+ i 2) 0.0);;改变每个拟合点的z值为0.0
       (setq i (+ i 3))
     )
     (vla-put-fitpoints m_obj m_pts);;更改曲线拟合点属性

     (setq i 0)

     (repeat (vla-get-numberofcontrolpoints m_obj);;循环
       (vlax-safearray-put-element m_pts1 (+ i 2) 0.0);;改变每个控制点的z值为0.0
       (setq i (+ i 3))
     )
     (vla-put-controlpoints m_obj m_pts1);;更改曲线控制点属性
    )

    ((= "AcDb3dPolyline" m_objname);;三维多段线(3dpolyline)
     (setq i 0)
     (setq m_pts (vlax-variant-value (vla-get-coordinates m_obj)));;取得3维多段线的控制点
     (repeat (/ (length (vlax-safearray->list m_pts)) 3)
       (vlax-safearray-put-element m_pts (+ i 2) 0.0)
       (setq i (+ i 3))
     )
     (vla-put-coordinates m_obj m_pts)
    )

    ((= "AcDbLine" m_objname);;直线(line)
     (setq i 0)
     (setq m_pts (vlax-variant-value (vla-get-startpoint m_obj)));;取得直线的起点座标
     (setq m_pts1 (vlax-variant-value (vla-get-endpoint m_obj)));;取得直线的端点座标
     (vlax-safearray-put-element m_pts 2 0.0);;改变起点座标z值为0.0
     (vlax-safearray-put-element m_pts1 2 0.0)
     (vla-put-startpoint m_obj m_pts)
     (vla-put-endpoint m_obj m_pts1)
    )

    ((or (= "AcDbCircle" m_objname);;园(circle)
  (= "AcDbArc" m_objname);;圆弧(arc)
  (= "AcDbEllipse" m_objname);;椭圆及椭圆弧(ellipse)
     )
     (setq m_pts (vlax-variant-value (vla-get-center m_obj)));;取得中心点座标
     (vlax-safearray-put-element m_pts 2 0.0);;改变中心点座标z值为0.0
     (vla-put-center m_obj m_pts)
    )

    ((or (= "AcDbPolyline" m_objname);;多段线(polyline、lwpolyline)
  (= "AcDb2dPolyline" m_objname);;拟合的2维多段线(polyline、lwpolyline)
     )
     (vla-put-elevation m_obj 0.0);;改变标高值为0.0
    )
  )
  (setq m_obj m_obj)
)


(defun C:test (/ m_ent1 m_ent2 m_jdtab)
; (vl-load-com)

 (setq m_ent1 (car (entsel "\n请选择第一条曲线: ")))
  (setq m_ent2 (car (entsel "\n请选择第二条曲线: ")))
  (setq ZL-GETINTERS (m_IntersectWith m_ent1 m_ent2))
  (princ"\n交点表: ")(princ ZL-GETINTERS)
  (princ)
)

;Andyhon大侠

;帮我看看,测试后也不返回交点列表呀,也不知道哪里出错

;AUTOCAD 2008 測試O.K

发表于 2010-1-6 22:36:00 | 显示全部楼层
向原创作者求助可能要快些有回应
若能附上您调试失败的文件(*.Dwg)更利于厘清症结所在
发表于 2010-7-27 12:13:00 | 显示全部楼层
本帖最后由 作者 于 2010-7-27 13:00:21 编辑

请问VLX怎么可以实现不相交的两实体延伸所得到的交点?

 

呵呵,搞定了

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-12-20 16:12 , Processed in 0.187569 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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