Author Topic: Make solid hatch from given plines  (Read 5018 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: Make solid hatch from given plines
« Reply #15 on: July 06, 2016, 02:43:48 AM »
This is how would I do it, but I am still not sure why all this... You can simply get REGIONS with BPOLY command and with original SPLINE entities and then do whatever you like after... Also if you have open SPLINES like in your case, you may use CAB's BreakObjects.lsp posted here at www.theswamp.org at forum show your stuff, to break SPLINES after which you can simply use REGION command to extract REGIONS... Of course you remove biggest one, but that's actually all you have to do - the effect is the same as bbpoly by Lee Mac if not even better (SPLINES don't degrade when in REGIONS)... Anyway here is my version and it don't have bugs...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:extshortlwsaddv ( / *error* bbucs intersobj1obj2 add_vtx *adoc* ucsf ss i ent fuzz ll ur bbl ent1 ent2 ss-ent1 k intpts )
  2.  
  3.  
  4.   (defun *error* ( m )
  5.     (if ucsf
  6.       (command "_.UCS" "_P")
  7.     )
  8.     (vla-endundomark *adoc*)
  9.     (if m
  10.       (prompt m)
  11.     )
  12.     (princ)
  13.   )
  14.  
  15.   (defun bbucs ( ss / UCS2WCSMatrix WCS2UCSMatrix n ent minpt maxpt minptlst maxptlst minptbbx minptbby minptbbz minptbb maxptbbx maxptbby maxptbbz maxptbb )
  16.  
  17.     (vl-load-com)
  18.  
  19.     ;; Doug C. Broad, Jr.
  20.     ;; can be used with vla-transformby to
  21.     ;; transform objects from the UCS to the WCS
  22.     (defun UCS2WCSMatrix ()
  23.       (vlax-tmatrix
  24.         (append
  25.           (mapcar
  26.            '(lambda (vector origin)
  27.             (append (trans vector 1 0 t) (list origin))
  28.           )
  29.           (list '(1 0 0) '(0 1 0) '(0 0 1))
  30.           (trans '(0 0 0) 0 1)
  31.           )
  32.           (list '(0 0 0 1))
  33.         )
  34.       )
  35.     )
  36.     ;; transform objects from the WCS to the UCS
  37.     (defun WCS2UCSMatrix ()
  38.       (vlax-tmatrix
  39.         (append
  40.           (mapcar
  41.            '(lambda (vector origin)
  42.             (append (trans vector 0 1 t) (list origin))
  43.           )
  44.           (list '(1 0 0) '(0 1 0) '(0 0 1))
  45.           (trans '(0 0 0) 1 0)
  46.           )
  47.           (list '(0 0 0 1))
  48.         )
  49.       )
  50.     )
  51.  
  52.     (if ss
  53.       (progn
  54.         (repeat (setq n (sslength ss))
  55.           (setq ent (ssname ss (setq n (1- n))))
  56.           (vla-TransformBy (vlax-ename->vla-object ent) (UCS2WCSMatrix))
  57.           (vla-getboundingbox (vlax-ename->vla-object ent) 'minpoint 'maxpoint)
  58.           (vla-TransformBy (vlax-ename->vla-object ent) (WCS2UCSMatrix))
  59.           (setq minpt (vlax-safearray->list minpoint))
  60.           (setq maxpt (vlax-safearray->list maxpoint))
  61.           (setq minptlst (cons minpt minptlst))
  62.           (setq maxptlst (cons maxpt maxptlst))
  63.         )
  64.         (setq minptbbx (caar (vl-sort minptlst '(lambda (a b) (< (car a) (car b))))))
  65.         (setq minptbby (cadar (vl-sort minptlst '(lambda (a b) (< (cadr a) (cadr b))))))
  66.         (setq minptbbz (caddar (vl-sort minptlst '(lambda (a b) (< (caddr a) (caddr b))))))
  67.         (setq maxptbbx (caar (vl-sort maxptlst '(lambda (a b) (> (car a) (car b))))))
  68.         (setq maxptbby (cadar (vl-sort maxptlst '(lambda (a b) (> (cadr a) (cadr b))))))
  69.         (setq maxptbbz (caddar (vl-sort maxptlst '(lambda (a b) (> (caddr a) (caddr b))))))
  70.         (setq minptbb (list minptbbx minptbby minptbbz))
  71.         (setq maxptbb (list maxptbbx maxptbby maxptbbz))
  72.       )
  73.     )
  74.     (list minptbb maxptbb)
  75.   )
  76.  
  77.   (defun intersobj1obj2 ( obj1 obj2 / coords pt ptlst )
  78.     (if (eq (type obj1) 'ENAME) (setq obj1 (vlax-ename->vla-object obj1)))
  79.     (if (eq (type obj2) 'ENAME) (setq obj2 (vlax-ename->vla-object obj2)))
  80.     (setq coords (vl-catch-all-apply 'vlax-safearray->list (list (vl-catch-all-apply 'vlax-variant-value (list (vla-intersectwith obj1 obj2 AcExtendBoth))))))
  81.     (if (vl-catch-all-error-p coords)
  82.       (setq ptlst nil)
  83.       (repeat (/ (length coords) 3)
  84.         (setq pt (list (car coords) (cadr coords) (caddr coords)))
  85.         (setq ptlst (cons pt ptlst))
  86.         (setq coords (cdddr coords))
  87.       )
  88.     )
  89.     ptlst
  90.   )
  91.  
  92.   (defun add_vtx ( obj add_pt ent_name / bulg sw ew )
  93.       (vla-GetWidth obj (fix add_pt) 'sw 'ew)
  94.       (vla-addVertex
  95.           obj
  96.           (1+ (fix add_pt))
  97.           (vlax-make-variant
  98.               (vlax-safearray-fill
  99.                   (vlax-make-safearray vlax-vbdouble (cons 0 1))
  100.                       (list
  101.                           (car (trans (vlax-curve-getpointatparam obj add_pt) 0 ent_name))
  102.                           (cadr (trans (vlax-curve-getpointatparam obj add_pt) 0 ent_name))
  103.                       )
  104.               )
  105.           )
  106.       )
  107.       (setq bulg (vla-GetBulge obj (fix add_pt)))
  108.       (vla-SetBulge obj
  109.           (fix add_pt)
  110.           (/
  111.               (sin (/ (* 4 (atan bulg) (- add_pt (fix add_pt))) 4))
  112.               (cos (/ (* 4 (atan bulg) (- add_pt (fix add_pt))) 4))
  113.           )
  114.       )
  115.       (vla-SetBulge obj
  116.           (1+ (fix add_pt))
  117.           (/
  118.               (sin (/ (* 4 (atan bulg) (- (1+ (fix add_pt)) add_pt)) 4))
  119.               (cos (/ (* 4 (atan bulg) (- (1+ (fix add_pt)) add_pt)) 4))
  120.           )
  121.       )
  122.       (vla-SetWidth obj (fix add_pt) sw (+ sw (* (- ew sw) (- add_pt (fix add_pt)))))
  123.       (vla-SetWidth obj (1+ (fix add_pt)) (+ sw (* (- ew sw) (- add_pt (fix add_pt)))) ew)
  124.       (vla-update obj)
  125.   )
  126.  
  127.   (if (= (getvar 'worlducs) 0)
  128.     (progn
  129.       (command "_.UCS" "_W")
  130.       (setq ucsf t)
  131.     )
  132.   )
  133.   (setq ss (ssget "_:L" '((0 . "LWPOLYLINE"))))
  134.   (repeat (setq i (sslength ss))
  135.     (setq ent (ssname ss (setq i (1- i))))
  136.     (if (not (and (vlax-curve-isplanar ent) (equal (caddr (vlax-curve-getstartpoint ent)) 0.0 1e-6)))
  137.       (ssdel ent ss)
  138.     )
  139.   )
  140.   (setq fuzz 0.51)
  141.   (while (> fuzz 0.5)
  142.     (initget 6)
  143.     (setq fuzz (getdist "\nPick or specify fuzz distance <1e-2> - should not be greater than 0.5 : "))
  144.     (if (null fuzz)
  145.       (setq fuzz 1e-2)
  146.     )
  147.   )
  148.   (setq ll (car (bbucs ss)) ur (cadr (bbucs ss)))
  149.   (setq bbl (list ll (list (car ur) (cadr ll)) ur (list (car ll) (cadr ur))))
  150.   (repeat (setq i (sslength ss))
  151.     (setq ent1 (ssname ss (setq i (1- i))))
  152.     (vla-getboundingbox (vlax-ename->vla-object ent1) 'll 'ur)
  153.     (mapcar 'set '(ll ur) (mapcar 'vlax-safearray->list (list ll ur)))
  154.     (setq ss-ent1 (ssget "_CP" bbl '((0 . "LWPOLYLINE"))))
  155.     (ssdel ent1 ss-ent1)
  156.     (repeat (setq k (sslength ss-ent1))
  157.       (setq ent2 (ssname ss-ent1 (setq k (1- k))))
  158.       (setq intpts (intersobj1obj2 ent1 ent2))
  159.       (if intpts
  160.         (foreach pt intpts
  161.           (if
  162.             (or
  163.               (and
  164.                 (vlax-curve-getparamatpoint ent1 pt)
  165.                 (not (vlax-curve-getparamatpoint ent2 pt))
  166.               )
  167.               (and
  168.                 (not (vlax-curve-getparamatpoint ent1 pt))
  169.                 (vlax-curve-getparamatpoint ent2 pt)
  170.               )
  171.             )
  172.             (cond
  173.               ( (and (vlax-curve-getparamatpoint ent1 pt) (not (equal (vlax-curve-getparamatpoint ent1 pt) (vlax-curve-getstartparam ent1) 1e-5)) (not (equal (vlax-curve-getparamatpoint ent1 pt) (vlax-curve-getendparam ent1) 1e-5)) (or (<= (distance (vlax-curve-getstartpoint ent2) pt) fuzz) (<= (distance (vlax-curve-getendpoint ent2) pt) fuzz)))
  174.                 (add_vtx (vlax-ename->vla-object ent1) (vlax-curve-getparamatpoint ent1 pt) ent1)
  175.                 (cond
  176.                   ( (<= (distance (vlax-curve-getstartpoint ent2) pt) fuzz)
  177.                     (entupd (cdr (assoc -1 (entmod (subst (cons 10 (list (car pt) (cadr pt))) (assoc 10 (entget ent2)) (entget ent2))))))
  178.                   )
  179.                   ( (<= (distance (vlax-curve-getendpoint ent2) pt) fuzz)
  180.                     (entupd (cdr (assoc -1 (entmod (subst (cons 10 (list (car pt) (cadr pt))) (assoc 10 (reverse (entget ent2))) (entget ent2))))))
  181.                   )
  182.                 )
  183.               )
  184.               ( (and (vlax-curve-getparamatpoint ent2 pt) (not (equal (vlax-curve-getparamatpoint ent2 pt) (vlax-curve-getstartparam ent2) 1e-5)) (not (equal (vlax-curve-getparamatpoint ent2 pt) (vlax-curve-getendparam ent2) 1e-5)) (or (<= (distance (vlax-curve-getstartpoint ent1) pt) fuzz) (<= (distance (vlax-curve-getendpoint ent1) pt) fuzz)))
  185.                 (add_vtx (vlax-ename->vla-object ent2) (vlax-curve-getparamatpoint ent2 pt) ent2)
  186.                 (cond
  187.                   ( (<= (distance (vlax-curve-getstartpoint ent1) pt) fuzz)
  188.                     (entupd (cdr (assoc -1 (entmod (subst (cons 10 (list (car pt) (cadr pt))) (assoc 10 (entget ent1)) (entget ent1))))))
  189.                   )
  190.                   ( (<= (distance (vlax-curve-getendpoint ent1) pt) fuzz)
  191.                     (entupd (cdr (assoc -1 (entmod (subst (cons 10 (list (car pt) (cadr pt))) (assoc 10 (reverse (entget ent1))) (entget ent1))))))
  192.                   )
  193.                 )
  194.               )
  195.             )
  196.           )
  197.         )
  198.       )
  199.     )
  200.   )
  201.   (*error* nil)
  202. )
  203.  

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ziele_o2k

  • Newt
  • Posts: 49
Re: Make solid hatch from given plines
« Reply #16 on: July 06, 2016, 06:08:29 AM »
Hatching with splines boundary has big problems with displaying hatches (maybe not with SOLID but for exapmle with ANSI38 ) so I don't want to use _bpoly.
Problems occurs in most of cad programs (AutoCad; GstarCad; ZWCad).
Below my final version to extend/trim plines.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:tee4 nil (PZ:FindPlinesToModify (ssget '((0 . "LWPOLYLINE")))))
  2. (defun PZ:FindPlinesToModify (ss / *error* PZ:ExtendPline EntLst tmpLst)
  3.   (defun *error* (msg / so)
  4.     (cond
  5.       ((not msg))
  6.       ((member msg '("Function cancelled" "quit / exit abort")))
  7.       (
  8.         (princ (strcat "\n  <!>  Error : " msg "  <!> "))
  9.         (cond (DebugMode (vl-bt)))
  10.       )
  11.     )  
  12.     (princ)
  13.   )
  14.   (defun PZ:ExtendPline ( lst / vla_lst pl_m vla_pl_s vla_pl_e tmpLine_s tmpLine_e pts eds pte ede )
  15.     (setq
  16.       vla_lst (mapcar 'vlax-ename->vla-object lst)
  17.       pl_m (car lst)
  18.       vla_pl_s (cadr vla_lst)
  19.       vla_pl_e (caddr vla_lst)
  20.     )
  21.     (setq
  22.       tmpLine_s
  23.       (cd:ACX_AddLine
  24.         (cd:ACX_ASpace)
  25.         nil
  26.       )
  27.       tmpLine_e
  28.       (cd:ACX_AddLine
  29.         (cd:ACX_ASpace)
  30.         nil
  31.       )
  32.     )
  33.     (cond
  34.       (
  35.         (eq (logand(cdr (assoc 70 (entget pl_m))) 1) 1);(eq (logand(cdr (assoc 70 (entget (car(entsel))))) 1) 1)
  36.         nil
  37.       );skip closed plines
  38.       (
  39.         (setq pts (car(LM:intersections tmpLine_s vla_pl_s acextendnone)))
  40.         (setq eds (subst (cons 10 (list (car pts) (cadr pts))) (assoc 10 (entget pl_m)) (entget pl_m)))
  41.       )
  42.       (
  43.         (setq pts
  44.           (caar
  45.             (vl-sort
  46.               (mapcar
  47.                 '(lambda (_1)
  48.                   (list _1 (distance _1 (vlax-curve-getPointAtParam pl_m (vlax-curve-getStartParam pl_m))))
  49.                 )
  50.                 (LM:intersections tmpLine_s vla_pl_s acextendthisentity)
  51.               )
  52.               (function (lambda (e1 e2) (< (cadr e1) (cadr e2))))
  53.             )
  54.           )
  55.         )
  56.         (setq eds (subst (cons 10 (list (car pts) (cadr pts))) (assoc 10 (entget pl_m)) (entget pl_m)))
  57.       )
  58.     )
  59.     (entmod eds)
  60.     (cond
  61.       (
  62.         (eq (logand(cdr (assoc 70 (entget pl_m))) 1) 1)
  63.         nil
  64.       );skip closed plines
  65.       (
  66.         (setq pte (car(LM:intersections tmpLine_e vla_pl_e acextendnone)))
  67.         (setq ede (subst (cons 10 (list (car pte) (cadr pte))) (assoc 10 (reverse(entget pl_m))) (entget pl_m)))
  68.       )
  69.       (
  70.         (setq pte
  71.           (caar
  72.             (vl-sort
  73.               (mapcar
  74.                 '(lambda (_1)
  75.                   (list _1 (distance _1 (vlax-curve-getPointAtParam pl_m (vlax-curve-getEndParam pl_m))))
  76.                 )
  77.                 (LM:intersections tmpLine_e vla_pl_e acextendthisentity)
  78.               )
  79.               (function (lambda (e1 e2) (< (cadr e1) (cadr e2))))
  80.             )
  81.           )
  82.         )
  83.         (setq ede (subst (cons 10 (list (car pte) (cadr pte))) (assoc 10 (reverse(entget pl_m))) (entget pl_m)))
  84.       )
  85.     )
  86.     (entmod ede)
  87.     (vla-Delete tmpLine_s)
  88.     (vla-Delete tmpLine_e)
  89.   )
  90.   (setq EntLst (LM:ss->ent ss))
  91.   (setq finalLst
  92.     (mapcar
  93.       '(lambda (_1)
  94.         (setq tmpLst
  95.           (list _1
  96.             (caar
  97.               (vl-sort
  98.                 (mapcar
  99.                   '(lambda (_2)
  100.                     (list
  101.                       ;_1
  102.                       _2
  103.                       (distance (vlax-curve-getClosestPointTo (vlax-ename->vla-object _2) (vlax-curve-getStartPoint _1)) (vlax-curve-getStartPoint _1))
  104.                     )
  105.                   )
  106.                   (LM:ListDifference EntLst (list _1))
  107.                 )
  108.                 (function (lambda (e1 e2) (< (cadr e1) (cadr e2))))
  109.               )
  110.             )
  111.             (caar
  112.               (vl-sort
  113.                 (mapcar
  114.                   '(lambda (_2)
  115.                     (list
  116.                       ;_1
  117.                       _2
  118.                       (distance (vlax-curve-getClosestPointTo (vlax-ename->vla-object _2) (vlax-curve-getEndPoint _1)) (vlax-curve-getEndPoint _1))
  119.                     )
  120.                   )
  121.                   (LM:ListDifference EntLst (list _1))
  122.                 )
  123.                 (function (lambda (e1 e2) (< (cadr e1) (cadr e2))))
  124.               )
  125.             )
  126.           )
  127.         )
  128.       )
  129.       EntLst
  130.     )
  131.   )
  132.   (foreach _n finalLst (PZ:ExtendPline _n))
  133.   (princ)
  134. )
  135. ;;-------------------=={ List Difference }==------------------;;
  136. ;;                                                            ;;
  137. ;;  Returns items appearing exclusively in one list but not   ;;
  138. ;;  another, i.e. the relative complement: l1 \ l2            ;;
  139. ;;------------------------------------------------------------;;
  140. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  141. ;;------------------------------------------------------------;;
  142. ;;  Arguments:                                                ;;
  143. ;;  l1,l2 - lists for which to return the difference          ;;
  144. ;;------------------------------------------------------------;;
  145. ;;  Returns:  List of items appearing exclusively in list l1  ;;
  146. ;;------------------------------------------------------------;;
  147. (defun LM:ListDifference ( l1 l2 )
  148.   (vl-remove-if '(lambda ( x ) (member x l2)) l1)
  149. )
  150. ;;--------------=={ SelectionSet -> Entities }==--------------;;
  151. ;;                                                            ;;
  152. ;;  Converts a SelectionSet to a list of Entities             ;;
  153. ;;------------------------------------------------------------;;
  154. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  155. ;;------------------------------------------------------------;;
  156. ;;  Arguments:                                                ;;
  157. ;;  ss - Valid SelectionSet (Pickset)                         ;;
  158. ;;------------------------------------------------------------;;
  159. ;;  Returns:  List of Entity names, else nil                  ;;
  160. ;;------------------------------------------------------------;;
  161. (defun LM:ss->ent ( ss / i l )
  162.     (if ss
  163.         (repeat (setq i (sslength ss))
  164.             (setq l (cons (ssname ss (setq i (1- i))) l))
  165.         )
  166.     )
  167. )
To run, please load CadPack functions from this post.

Thank you ribarm for help!