明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖
楼主: qevghdfcu

找大神编个一键分别计算道路断面面种的插件

[复制链接]
发表于 2024-4-23 22:17 | 显示全部楼层
https://www.theswamp.org/index.php?topic=45305.15
原帖第29楼,有ymg大神贴的源码可下载。
下面为转贴的源码,楼主可以依需求修改。

  1. ;;; Cut & Fill      by ymg                                                    ;
  2. ;;;                                                                           ;



  3. (defun c:cf (/ ** *acdoc* a are b bnd c cutcol d dir dl1 dl2 e fillcol hcol
  4.                intl len1 len2 p p0 p1 p2 pm pol1 pol2 sp1 sp2 spe ss1
  5.                ss2 totcut totfill txt txtlayer varl)
  6.                
  7.    (vl-load-com)

  8.    (defun *error* (msg)
  9.         (mapcar 'eval varl)
  10.         (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")))
  11.            (princ (strcat "\nError: " msg))
  12.         )
  13.         (and *acdoc* (vla-endundomark *acdoc*))
  14.         (princ)
  15.    )

  16.    (setq varl '("OSMODE" "CMDECHO" "DIMZIN" "PEDITACCEPT")
  17.          varl (mapcar (function (lambda (a) (list 'setvar a (getvar a)))) varl)
  18.    )

  19.    (or *acdoc* (setq *acdoc* (vla-get-activedocument (vlax-get-acad-object))))

  20.    (vla-startundomark *acdoc*)

  21.    (setvar 'CMDECHO 0)
  22.    (setvar 'DIMZIN  0)
  23.    (setvar 'OSMODE  0)


  24.    (setq cutcol 1  fillcol 3  ; Cut is Red, Fill is Green                     ;
  25.          totcut 0  totfill 0  ; Total Cut and Total Fill                      ;
  26.            txtlayer "Text"    ; Name of Layer for Cut and Fill Values         ;

  27.    )
  28.    (while (not (setq **  (princ "\nSelect Reference Polyline:")
  29.                      ss1 (ssget "_+.:L:S" '((0 . "LWPOLYLINE")))
  30.                )
  31.           )
  32.         (princ "\nYou Must Select a Polyline:")
  33.    )                  
  34.    (while (not (setq **  (princ "\nSelect Proposed Polyline:")
  35.                      ss2 (ssget "_+.:L:S" '((0 . "LWPOLYLINE")))
  36.                )
  37.           )
  38.         (princ "\nYou Must Select a Polyline:")
  39.    )

  40.      
  41.    (setq pol1 (ssname ss1 0)
  42.          len1 (vlax-curve-getDistAtParam pol1 (vlax-curve-getEndParam pol1))
  43.          pol2 (ssname ss2 0)
  44.          len2 (vlax-curve-getDistAtParam pol2 (vlax-curve-getEndParam pol2))
  45.          sp1  (vlax-curve-getstartpoint pol1)
  46.          spe  (vlax-curve-getendpoint pol1)
  47.          sp2  (if (vlax-curve-isClosed pol2)
  48.                  (setq lst2 (listpol pol2)
  49.                        disl (mapcar '(lambda (a) (distance sp1 a)) lst2)
  50.                        **   (plineorg pol2 (nth (vl-position (apply 'min disl) disl) lst2))
  51.                  )
  52.                  (vlax-curve-getstartpoint pol2)
  53.                )  
  54.          dir  (if (< (/ pi 2) (angle sp1 spe) (/ (* 3 pi) 2)) -1 1)
  55.    )      
  56.    

  57.    ; Getting all the intersections between poly.                              ;

  58.    (setq intl (intersections pol1 pol2))

  59.    (if (> (length intl) 1)
  60.       (progn
  61.    
  62.    ; Computing distance of intersections on each polyline                     ;
  63.    
  64.          (setq dl1  (mapcar '(lambda (a) (getdistoncurve pol1 a)) intl)
  65.                dl2  (mapcar '(lambda (a) (getdistoncurve pol2 a)) intl)
  66.          )
  67.    
  68.    ; If both polyline are closed add first Intersection to end of list        ;
  69.    ; We also add a distance to each distances list                            ;

  70.          (if (and (vlax-curve-isClosed pol1) (vlax-curve-isClosed pol2))
  71.             (setq dl1  (append dl1 (list (+ (car dl1) len1)))
  72.                   dl2  (append dl2 (list (+ (car dl2) len2)))
  73.                   intl (append intl (list (car intl)))
  74.                   dir  (if (iscw_p (listpol pol1)) -1 1)   
  75.             )      
  76.          )
  77.    

  78.    ; Finding points at mid-distance between intersections on each polyline    ;
  79.    ; Calculating midpoint between mid-distance points to get an internal point;
  80.    ; Creating a list of all these points plus the intersection points         ;
  81.    
  82.          (setq pm
  83.             (mapcar
  84.                 '(lambda (a b c d e)
  85.                     (list (midpoint
  86.                               (setq p1 (getptoncurve pol1 (rem (* (+ a b) 0.5) len1)))
  87.                               (setq p2 (getptoncurve pol2 (rem (* (+ c d) 0.5) len2)))
  88.                            )
  89.                            p1 p2 e            
  90.                      )
  91.                   )
  92.                   dl1 (cdr dl1) dl2 (cdr dl2) intl
  93.              )
  94.          )      

  95.    
  96.    
  97.          (foreach i pm
  98.             (setq  p (car    i)  ; Midpoint between p1 p2                           ;
  99.                   p0 (cadddr i)  ; Intersection Point                               ;
  100.                   p1 (cadr   i)  ; Midpoint of Intersections on Reference Polyline  ;
  101.                   p2 (caddr  i)  ; Midpoint of Intersections on Proposed Polyline   ;
  102.             )
  103.             (if (> (abs (onside p2 p0 p1)) 1e-3) ; Not Colinear                     ;
  104.                (progn
  105.                   (vl-cmdf "._-BOUNDARY" p "")
  106.                   (setq are (vla-get-area (vlax-ename->vla-object (entlast)))
  107.                         bnd (entlast)
  108.                   )
  109.             
  110.                   (if (minusp (* (onside p2 p0 p1) dir))               
  111.                      (setq totfill (+ totfill are) hcol fillcol)
  112.                      (setq totcut  (+ totcut  are) hcol  cutcol)
  113.                   )
  114.                
  115.                   (vl-cmdf "._-HATCH" "_CO" hcol "." "_P" "SOLID" "_S" bnd "" "")
  116.                   (entdel bnd)
  117.                )
  118.             )
  119.          )
  120.          (setq   p (cadr (grread nil 13 0))
  121.                txt (strcat "{\\C3;Fill: " (rtos totfill 2 2) " m2\\P\\C1;Cut: " (rtos totcut  2 2) " m2}")
  122.          )        
  123.          (entmakex (list
  124.                       (cons 0 "MTEXT")
  125.                       (cons 100 "AcDbEntity")
  126.                       (cons 8 txtlayer)
  127.                       (cons 100 "AcDbMText")
  128.                       (cons 10 p)               
  129.                       (cons 40 3.0)
  130.                       (cons 1 txt)
  131.                     )
  132.          )           

  133.          (command "_MOVE" (entlast) "" p pause)
  134.       )
  135.       (Alert "Not Enough Intersections To Process !")
  136.   )

  137.   (*error* nil)

  138. )

  139. (princ "\nCalculates Cut & Fill Between Two Intersecting Polylines")
  140. (princ "\nCF to start...")



  141. (defun midpoint (p1 p2)
  142.    (mapcar '(lambda (a b) (* (+ a b) 0.5)) p1 p2)
  143. )

  144. ; onside        by ymg                                                        ;
  145. ; Negative return, point is on left of v1->v2                                 ;
  146. ; Positive return, point is on right of v1->v2                                ;
  147. ;        0 return, point is smack on the vector.                              ;
  148. ;                                                                             ;

  149. (defun onside (p v1 v2 / x y)
  150.     (setq x (car p) y (cadr p))
  151.     (- (* (- (cadr v1) y) (-  (car v2) x)) (* (- (car  v1) x) (- (cadr v2) y)))
  152. )

  153. ;                                                                             ;
  154. ; Is Polyline Clockwise                      by LeeMac                        ;
  155. ;                                                                             ;
  156. ; Argument:   l,  Point List                                                  ;
  157. ; Returns:    t, Polyline is ClockWise                                        ;
  158. ;           nil, Polyline is CounterClockWise                                 ;
  159. ;                                                                             ;

  160. (defun iscw_p (l)
  161.     (if (equal (car l) (last l) 1e-8) (setq l (cdr l)))
  162.     (minusp
  163.         (apply '+
  164.             (mapcar
  165.                 (function
  166.                   (lambda (a b) (- (* (car b) (cadr a)) (* (car a) (cadr b))))
  167.                 )
  168.                 l (cons (last l) l)
  169.             )
  170.         )
  171.     )
  172. )

  173. ;;                                                                            ;
  174. ;; Return list of intersection(s) between two VLA-Object or two ENAME         ;
  175. ;; obj1 - first VLA-Object                                                    ;
  176. ;; obj2 - second VLA-Object                                                   ;
  177. ;; mode - intersection mode (acExtendNone acExtendThisEntity                  ;
  178. ;;                                acExtendOtherEntity acExtendBoth)           ;
  179. ;; Requires triplet                                                           ;
  180. ;;                                                                            ;

  181. (defun Intersections (obj1 obj2)
  182.    (or (= (type obj1) 'VLA-OBJECT) (setq obj1 (vlax-ename->vla-object obj1)))
  183.    (or (= (type obj2) 'VLA-OBJECT) (setq obj2 (vlax-ename->vla-object obj2)))
  184.            
  185.    (triplet (vlax-invoke obj1 'intersectwith obj2 acExtendNone))
  186. )

  187. ;;                                                                            ;
  188. ;; triplet, Separates a list into triplets of items.                          ;
  189. ;;                                                                            ;

  190. (defun triplet (l)
  191.    (if l (cons (list (car l) (cadr l) (caddr l))(triplet (cdddr l))))
  192. )


  193. (defun getdistoncurve (e p)
  194.    (vlax-curve-getDistatParam e
  195.         (vlax-curve-getparamatpoint e
  196.              (vlax-curve-getclosestpointto e p)
  197.         )     
  198.    )         
  199. )

  200. (defun getptoncurve (e d)
  201.    (vlax-curve-getpointatparam e (vlax-curve-getparamatdist e d))
  202. )

  203. ;;                                                                            ;
  204. ;; listpol     by ymg    (Simplified a Routine by Gile Chanteau               ;
  205. ;;                                                                            ;
  206. ;; Parameter:  en,  Entity Name or Object Name of Any Type of Polyline        ;
  207. ;;                                                                            ;
  208. ;; Returns:    List of Points in Current UCS                                  ;
  209. ;;                                                                            ;
  210. ;; Notes:      On Closed Polyline the Last Vertex is Same as First)           ;
  211. ;;                                                                            ;

  212. (defun listpol (en / i l)
  213.    (repeat (setq i (fix (1+ (vlax-curve-getEndParam en))))
  214.       (setq l (cons (trans (vlax-curve-getPointAtParam en (setq i (1- i))) 0 1) l))
  215.    )
  216. )


  217. ;; plineorg   by (gile) (Modified into a function by ymg)                     ;
  218. ;;  https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/          ;
  219. ;;            change-polyline-start-point/td-p/2154331                        ;
  220. ;;                                                                            ;
  221. ;; Function to modify origin of a closed polyline                             ;
  222. ;;                                                                            ;
  223. ;; Arguments:                                                                 ;
  224. ;;   en : Ename or VLA-Object of a Closed Polyline.                           ;
  225. ;;   pt : Point                                                               ;
  226. ;;                                                                            ;
  227. ;; Returns: Point of Origin if successful, else nil.                          ;
  228. ;;                                                                            ;

  229. (defun plineorg (en pt / blst d1 d2 d3 n norm obj pa plst)
  230.    (if (= (type en) 'ENAME)
  231.       (setq obj (vlax-ename->vla-object  en))
  232.       (setq obj en   en (vlax-vla-object->ename obj))
  233.    )
  234.    
  235.     ;; bulgratio   by (gile)                                 ;
  236.     ;; Returns a bulge which is proportional to a reference  ;
  237.     ;; Arguments :                                           ;
  238.     ;; b : the reference bulge                               ;
  239.     ;; k : the ratio (between angles or arcs length)         ;

  240.    (defun bulgratio (b k / a)
  241.       (setq a (atan b))
  242.       (/ (sin (* k a)) (cos (* k a)))
  243.    )

  244.     ;; Sublist  by (gile)                                    ;
  245.     ;; Returns a sublist similar to substr function.         ;
  246.     ;; lst : List from which sublist is to be extracted      ;
  247.     ;; idx : Index of Item at Start of sublist               ;
  248.     ;; len : Length of sublist or nil to return all items.   ;

  249.    (defun sublist (lst n len / rtn)
  250.       (if (or (not len) (< (- (length lst) n) len))
  251.          (setq len (- (length lst) n))
  252.       )
  253.       (setq n (+ n len))
  254.       (repeat len
  255.          (setq rtn (cons (nth (setq n (1- n)) lst) rtn))
  256.       )
  257.    )

  258.    (if (and (= (vla-get-closed obj) :vlax-true)
  259.             (= (vla-get-objectname obj) "AcDbPolyline")
  260.        )     
  261.       (progn
  262.          (setq plst (vlax-get obj 'coordinates)
  263.                norm (vlax-get obj 'normal)
  264.                pt   (vlax-curve-getClosestPointTo en (trans pt 1 0))
  265.                pa   (vlax-curve-getparamatpoint obj pt)
  266.                n    (/ (length plst) 2)         
  267.          )
  268.          (repeat n
  269.             (setq blst (cons (vla-getbulge obj (setq n (1- n))) blst))
  270.          )
  271.          (if (= pa (fix pa))
  272.             (setq n    (fix pa)
  273.                   plst (append (sublist plst (* 2 n) nil)
  274.                                (sublist plst 0 (* 2 n))
  275.                        )
  276.                   blst (append (sublist blst n nil) (sublist blst 0 n))
  277.             )
  278.             (setq n    (1+ (fix pa))
  279.                   d3   (vlax-curve-getdistatparam en n)
  280.                   d2   (- d3 (vlax-curve-getdistatpoint en pt))
  281.                   d3   (- d3 (vlax-curve-getdistatparam en (1- n)))
  282.                   d1   (- d3 d2)
  283.                   pt   (trans pt 0 (vlax-get obj 'normal))
  284.                   plst (append (list (car pt) (cadr pt))
  285.                                (sublist plst (* 2 n) nil)
  286.                                (sublist plst 0 (* 2 n))
  287.                        )
  288.                   blst (append (list (bulgratio (nth (1- n) blst) (/ d2 d3)))
  289.                                (sublist blst n nil)
  290.                                (sublist blst 0 (1- n))
  291.                                (list (bulgratio (nth (1- n) blst) (/ d1 d3)))
  292.                        )
  293.             )
  294.          )
  295.          (vlax-put obj 'coordinates plst)
  296.          (repeat (setq n (length blst))
  297.             (vla-setbulge obj (setq n (1- n)) (nth n blst))
  298.          )
  299.          (trans pt 0 1)
  300.       )
  301.       nil
  302.    )
  303. )

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

本版积分规则

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

GMT+8, 2024-5-4 23:56 , Processed in 0.212800 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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