Jump to content

Hatch Between two polylines


Anis

Recommended Posts

hello all;
I'm using the code bellow, but i need to click on almost 2000 road cross sections one by one, indeed that is time consuming.
it would be more general if the code can select all green lines at ones and then the red lines. mean that, if there be 2000 road cross sections for example; the code can select all cross sections and report a cut and fill file separately for each section (the same as AreaLabelV1-9.lsp of Mr.Lee Mac does)
appreciate if anyone can help me
tnx
 
  1. ;;; Cut & Fill      by ymg                                                    ;
  2. ;;;                                                                           ;
  3.  
  4.  
  5.  
  6. (defun c:cf (/ ** *acdoc* a are b bnd c cutcol d dir dl1 dl2 e fillcol hcol
  7.                intl len1 len2 p p0 p1 p2 pm pol1 pol2 sp1 sp2 spe ss1
  8.                ss2 totcut totfill txt txtlayer varl)
  9.                
  10.    (vl-load-com)
  11.  
  12.    (defun *error* (msg)
  13.         (mapcar 'eval varl)
  14.         (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")))
  15.            (princ (strcat "\nError: " msg))
  16.         )
  17.         (and *acdoc* (vla-endundomark *acdoc*))
  18.         (princ)
  19.    )
  20.  
  21.    (setq varl '("OSMODE" "CMDECHO" "DIMZIN" "PEDITACCEPT")
  22.          varl (mapcar (function (lambda (a) (list 'setvar a (getvar a)))) varl)
  23.    )
  24.  
  25.    (or *acdoc* (setq *acdoc* (vla-get-activedocument (vlax-get-acad-object))))
  26.  
  27.    (vla-startundomark *acdoc*)
  28.  
  29.    (setvar 'CMDECHO 0)
  30.    (setvar 'DIMZIN  0)
  31.    (setvar 'OSMODE  0)
  32.  
  33.  
  34.    (setq cutcol 1  fillcol 3  ; Cut is Red, Fill is Green                     ;
  35.          totcut 0  totfill 0  ; Total Cut and Total Fill                      ;
  36.            txtlayer "Text"    ; Name of Layer for Cut and Fill Values         ;
  37.  
  38.    )
  39.    (while (not (setq **  (princ "\nSelect Reference Polyline:")
  40.                      ss1 (ssget "_+.:L:S" '((0 . "LWPOLYLINE")))
  41.                )
  42.           )
  43.         (princ "\nYou Must Select a Polyline:")
  44.    )                  
  45.    (while (not (setq **  (princ "\nSelect Proposed Polyline:")
  46.                      ss2 (ssget "_+.:L:S" '((0 . "LWPOLYLINE")))
  47.                )
  48.           )
  49.         (princ "\nYou Must Select a Polyline:")
  50.    )
  51.  
  52.      
  53.    (setq pol1 (ssname ss1 0)
  54.          len1 (vlax-curve-getDistAtParam pol1 (vlax-curve-getEndParam pol1))
  55.          pol2 (ssname ss2 0)
  56.          len2 (vlax-curve-getDistAtParam pol2 (vlax-curve-getEndParam pol2))
  57.          sp1  (vlax-curve-getstartpoint pol1)
  58.          spe  (vlax-curve-getendpoint pol1)
  59.          sp2  (if (vlax-curve-isClosed pol2)
  60.                  (setq lst2 (listpol pol2)
  61.                        disl (mapcar '(lambda (a) (distance sp1 a)) lst2)
  62.                        **   (plineorg pol2 (nth (vl-position (apply 'min disl) disl) lst2))
  63.                  )
  64.                  (vlax-curve-getstartpoint pol2)
  65.                )  
  66.          dir  (if (< (/ pi 2) (angle sp1 spe) (/ (* 3 pi) 2)) -1 1)
  67.    )      
  68.    
  69.  
  70.    ; Getting all the intersections between poly.                              ;
  71.  
  72.    (setq intl (intersections pol1 pol2))
  73.  
  74.    (if (> (length intl) 1)
  75.       (progn
  76.    
  77.    ; Computing distance of intersections on each polyline                     ;
  78.    
  79.          (setq dl1  (mapcar '(lambda (a) (getdistoncurve pol1 a)) intl)
  80.                dl2  (mapcar '(lambda (a) (getdistoncurve pol2 a)) intl)
  81.          )
  82.    
  83.    ; If both polyline are closed add first Intersection to end of list        ;
  84.    ; We also add a distance to each distances list                            ;
  85.  
  86.          (if (and (vlax-curve-isClosed pol1) (vlax-curve-isClosed pol2))
  87.             (setq dl1  (append dl1 (list (+ (car dl1) len1)))
  88.                   dl2  (append dl2 (list (+ (car dl2) len2)))
  89.                   intl (append intl (list (car intl)))
  90.                   dir  (if (iscw_p (listpol pol1)) -1 1)    
  91.             )      
  92.          )
  93.    
  94.  
  95.    ; Finding points at mid-distance between intersections on each polyline    ;
  96.    ; Calculating midpoint between mid-distance points to get an internal point;
  97.    ; Creating a list of all these points plus the intersection points         ;
  98.    
  99.          (setq pm
  100.             (mapcar
  101.                 '(lambda (a b c d e)
  102.                     (list (midpoint
  103.                               (setq p1 (getptoncurve pol1 (rem (* (+ a b) 0.5) len1)))
  104.                               (setq p2 (getptoncurve pol2 (rem (* (+ c d) 0.5) len2)))
  105.                            )
  106.                            p1 p2 e            
  107.                      )
  108.                   )
  109.                   dl1 (cdr dl1) dl2 (cdr dl2) intl
  110.              )
  111.          )      
  112.  
  113.    
  114.    
  115.          (foreach i pm
  116.             (setq  p (car    i)  ; Midpoint between p1 p2                           ;
  117.                   p0 (cadddr i)  ; Intersection Point                               ;
  118.                   p1 (cadr   i)  ; Midpoint of Intersections on Reference Polyline  ;
  119.                   p2 (caddr  i)  ; Midpoint of Intersections on Proposed Polyline   ;
  120.             )
  121.             (if (> (abs (onside p2 p0 p1)) 1e-3) ; Not Colinear                     ;
  122.                (progn
  123.                   (vl-cmdf "._-BOUNDARY" p "")
  124.                   (setq are (vla-get-area (vlax-ename->vla-object (entlast)))
  125.                         bnd (entlast)
  126.                   )
  127.            
  128.                   (if (minusp (* (onside p2 p0 p1) dir))              
  129.                      (setq totfill (+ totfill are) hcol fillcol)
  130.                      (setq totcut  (+ totcut  are) hcol  cutcol)
  131.                   )
  132.                
  133.                   (vl-cmdf "._-HATCH" "_CO" hcol "." "_P" "SOLID" "_S" bnd "" "")
  134.                   (entdel bnd)
  135.                )
  136.             )
  137.          )
  138.          (setq   p (cadr (grread nil 13 0))
  139.                txt (strcat "{\\C3;Fill: " (rtos totfill 2 2) " m2\\P\\C1;Cut: " (rtos totcut  2 2) " m2}")
  140.          )       
  141.          (entmakex (list
  142.                       (cons 0 "MTEXT")
  143.                       (cons 100 "AcDbEntity")
  144.                       (cons 8 txtlayer)
  145.                       (cons 100 "AcDbMText")
  146.                       (cons 10 p)                
  147.                       (cons 40 3.0)
  148.                       (cons 1 txt)
  149.                     )
  150.          )           
  151.  
  152.          (command "_MOVE" (entlast) "" p pause)
  153.       )
  154.       (Alert "Not Enough Intersections To Process !")
  155.   )
  156.  
  157.   (*error* nil)
  158.  
  159. )
  160.  
  161. (princ "\nCalculates Cut & Fill Between Two Intersecting Polylines")
  162. (princ "\nCF to start...")
  163.  
  164.  
  165.  
  166. (defun midpoint (p1 p2)
  167.    (mapcar '(lambda (a b) (* (+ a b) 0.5)) p1 p2)
  168. )
  169.  
  170. ; onside        by ymg                                                        ;
  171. ; Negative return, point is on left of v1->v2                                 ;
  172. ; Positive return, point is on right of v1->v2                                ;
  173. ;        0 return, point is smack on the vector.                              ;
  174. ;                                                                             ;
  175.  
  176. (defun onside (p v1 v2 / x y)
  177.     (setq x (car p) y (cadr p))
  178.     (- (* (- (cadr v1) y) (-  (car v2) x)) (* (- (car  v1) x) (- (cadr v2) y)))
  179. )
  180.  
  181. ;                                                                             ;
  182. ; Is Polyline Clockwise                      by LeeMac                        ;
  183. ;                                                                             ;
  184. ; Argument:   l,  Point List                                                  ;
  185. ; Returns:    t, Polyline is ClockWise                                        ;
  186. ;           nil, Polyline is CounterClockWise                                 ;
  187. ;                                                                             ;
  188.  
  189. (defun iscw_p (l)
  190.     (if (equal (car l) (last l) 1e-8) (setq l (cdr l)))
  191.     (minusp
  192.         (apply '+
  193.             (mapcar
  194.                 (function
  195.                   (lambda (a b) (- (* (car b) (cadr a)) (* (car a) (cadr b))))
  196.                 )
  197.                 l (cons (last l) l)
  198.             )
  199.         )
  200.     )
  201. )
  202.  
  203. ;;                                                                            ;
  204. ;; Return list of intersection(s) between two VLA-Object or two ENAME         ;
  205. ;; obj1 - first VLA-Object                                                    ;
  206. ;; obj2 - second VLA-Object                                                   ;
  207. ;; mode - intersection mode (acExtendNone acExtendThisEntity                  ;
  208. ;;                                acExtendOtherEntity acExtendBoth)           ;
  209. ;; Requires triplet                                                           ;
  210. ;;                                                                            ;
  211.  
  212. (defun Intersections (obj1 obj2)
  213.    (or (= (type obj1) 'VLA-OBJECT) (setq obj1 (vlax-ename->vla-object obj1)))
  214.    (or (= (type obj2) 'VLA-OBJECT) (setq obj2 (vlax-ename->vla-object obj2)))
  215.            
  216.    (triplet (vlax-invoke obj1 'intersectwith obj2 acExtendNone))
  217. )
  218.  
  219. ;;                                                                            ;
  220. ;; triplet, Separates a list into triplets of items.                          ;
  221. ;;                                                                            ;
  222.  
  223. (defun triplet (l)
  224.    (if l (cons (list (car l) (cadr l) (caddr l))(triplet (cdddr l))))
  225. )
  226.  
  227.  
  228. (defun getdistoncurve (e p)
  229.              (vlax-curve-getclosestpointto e p)
  230.         )    
  231.    )         
  232. )
  233.  
  234. (defun getptoncurve (e d)
  235. )
  236.  
  237. ;;                                                                            ;
  238. ;; listpol     by ymg    (Simplified a Routine by Gile Chanteau               ;
  239. ;;                                                                            ;
  240. ;; Parameter:  en,  Entity Name or Object Name of Any Type of Polyline        ;
  241. ;;                                                                            ;
  242. ;; Returns:    List of Points in Current UCS                                  ;
  243. ;;                                                                            ;
  244. ;; Notes:      On Closed Polyline the Last Vertex is Same as First)           ;
  245. ;;                                                                            ;
  246.  
  247. (defun listpol (en / i l)
  248.    (repeat (setq i (fix (1+ (vlax-curve-getEndParam en))))
  249.       (setq l (cons (trans (vlax-curve-getPointAtParam en (setq i (1- i))) 0 1) l))
  250.    )
  251. )
  252.  
  253.  
  254. ;; plineorg   by (gile) (Modified into a function by ymg)                     ;
  255. ;;  https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/          ;
  256. ;;            change-polyline-start-point/td-p/2154331                        ;
  257. ;;                                                                            ;
  258. ;; Function to modify origin of a closed polyline                             ;
  259. ;;                                                                            ;
  260. ;; Arguments:                                                                 ;
  261. ;;   en : Ename or VLA-Object of a Closed Polyline.                           ;
  262. ;;   pt : Point                                                               ;
  263. ;;                                                                            ;
  264. ;; Returns: Point of Origin if successful, else nil.                          ;
  265. ;;                                                                            ;
  266.  
  267. (defun plineorg (en pt / blst d1 d2 d3 n norm obj pa plst)
  268.    (if (= (type en) 'ENAME)
  269.       (setq obj (vlax-ename->vla-object  en))
  270.       (setq obj en   en (vlax-vla-object->ename obj))
  271.    )
  272.    
  273.     ;; bulgratio   by (gile)                                 ;
  274.     ;; Returns a bulge which is proportional to a reference  ;
  275.     ;; Arguments :                                           ;
  276.     ;; b : the reference bulge                               ;
  277.     ;; k : the ratio (between angles or arcs length)         ;
  278.  
  279.    (defun bulgratio (b k / a)
  280.       (setq a (atan b))
  281.       (/ (sin (* k a)) (cos (* k a)))
  282.    )
  283.  
  284.     ;; Sublist  by (gile)                                    ;
  285.     ;; Returns a sublist similar to substr function.         ;
  286.     ;; lst : List from which sublist is to be extracted      ;
  287.     ;; idx : Index of Item at Start of sublist               ;
  288.     ;; len : Length of sublist or nil to return all items.   ;
  289.  
  290.    (defun sublist (lst n len / rtn)
  291.       (if (or (not len) (< (- (length lst) n) len))
  292.          (setq len (- (length lst) n))
  293.       )
  294.       (setq n (+ n len))
  295.       (repeat len
  296.          (setq rtn (cons (nth (setq n (1- n)) lst) rtn))
  297.       )
  298.    )
  299.  
  300.    (if (and (= (vla-get-closed obj) :vlax-true)
  301.             (= (vla-get-objectname obj) "AcDbPolyline")
  302.        )    
  303.       (progn
  304.          (setq plst (vlax-get obj 'coordinates)
  305.                norm (vlax-get obj 'normal)
  306.                pt   (vlax-curve-getClosestPointTo en (trans pt 1 0))
  307.                pa   (vlax-curve-getparamatpoint obj pt)
  308.                n    (/ (length plst) 2)        
  309.          )
  310.          (repeat n
  311.             (setq blst (cons (vla-getbulge obj (setq n (1- n))) blst))
  312.          )
  313.          (if (= pa (fix pa))
  314.             (setq n    (fix pa)
  315.                   plst (append (sublist plst (* 2 n) nil)
  316.                                (sublist plst 0 (* 2 n))
  317.                        )
  318.                   blst (append (sublist blst n nil) (sublist blst 0 n))
  319.             )
  320.             (setq n    (1+ (fix pa))
  321.                   d3   (vlax-curve-getdistatparam en n)
  322.                   d2   (- d3 (vlax-curve-getdistatpoint en pt))
  323.                   d3   (- d3 (vlax-curve-getdistatparam en (1- n)))
  324.                   d1   (- d3 d2)
  325.                   pt   (trans pt 0 (vlax-get obj 'normal))
  326.                   plst (append (list (car pt) (cadr pt))
  327.                                (sublist plst (* 2 n) nil)
  328.                                (sublist plst 0 (* 2 n))
  329.                        )
  330.                   blst (append (list (bulgratio (nth (1- n) blst) (/ d2 d3)))
  331.                                (sublist blst n nil)
  332.                                (sublist blst 0 (1- n))
  333.                                (list (bulgratio (nth (1- n) blst) (/ d1 d3)))
  334.                        )
  335.             )
  336.          )
  337.          (vlax-put obj 'coordinates plst)
  338.          (repeat (setq n (length blst))
  339.             (vla-setbulge obj (setq n (1- n)) (nth n blst))
  340.          )
  341.          (trans pt 0 1)
  342.       )
  343.       nil
  344.    )
  345. )
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...