Jump to content

Draw a middle polyline in closed/open polygon of polyline


Saxlle

Recommended Posts

Hello everyone,

 

I'm trying to make a .lsp file which will going to draw a middle polyline inside closed/open polygon of polyline. The main problem is when the number of vertex of the one side of polyline are not equal to the number of vertex on the other side of polyline. Also, the problem are happening when the "starting point" (which present the positon of starting to create a polygon of polyline) are not "natural" for start a creating of polyline. 

 

In an attachment there is a Test example.dwg and DrawMidPolyline.lsp which i started to create/write, but i have obstacles (which are explained above and inside Test example.dwg) which i dont know how to handle it.

 

**Maybe there need to compare angles between points on the polyline to the decide where is the "first" starting positon of the middle polyline which present the begining, and then comparing the other angles to generate a middle points all over to the "end" position, and then create a middle polyline.

 

Thanks in advance everyone for help.

Test example.dwg DrawMidPolyline.lsp

Link to comment
Share on other sites

Hi,

I have a somewhat similar procedure, but it works with two polylines.
Two solutions are drawn up in the end, you just have to keep one of your choice!
I'm attaching your example drawing modified to work with my procedure.
Does this sound interesting to you?

 

(vl-load-com)
(defun add_vtx (obj add_pt ent_name / bulg)
  (vla-addVertex
    obj
    (1+ (fix add_pt))
    (vlax-make-variant
      (vlax-safearray-fill
        (vlax-make-safearray vlax-vbdouble (cons 0 1))
          (list
            (car (trans (vlax-curve-getpointatparam obj add_pt) 0 ent_name))
            (cadr (trans (vlax-curve-getpointatparam obj add_pt) 0 ent_name))
          )
      )
    )
  )
  (setq bulg (vla-GetBulge obj (fix add_pt)))
  (vla-SetBulge obj
    (fix add_pt)
    (/
      (sin (/ (* 4 (atan bulg) (- add_pt (fix add_pt))) 4))
      (cos (/ (* 4 (atan bulg) (- add_pt (fix add_pt))) 4))
    )
  )
  (vla-SetBulge obj
    (1+ (fix add_pt))
    (/
      (sin (/ (* 4 (atan bulg) (- (1+ (fix add_pt)) add_pt)) 4))
      (cos (/ (* 4 (atan bulg) (- (1+ (fix add_pt)) add_pt)) 4))
    )
  )
  (vla-update obj)
)
(defun c:vertex_median ( / fst_ob snd_obj vrt_pt pt lst_pt fst_dxf snd_dxf lst1 lst2 lstx ptx)
  (princ "\nSelect first polyline: ")
  (while (not (setq fst_obj (ssget "_+.:E:S" '((0 . "LWPOLYLINE"))))))
  (princ "\nSelect second polyline: ")
  (while (not (setq snd_obj (ssget "_+.:E:S" '((0 . "LWPOLYLINE"))))))
  (setq
    fst_obj (ssname fst_obj 0)
    snd_obj (ssname snd_obj 0)
    vrt_pt (vlax-variant-value (vla-IntersectWith (vlax-ename->vla-object fst_obj) (vlax-ename->vla-object snd_obj) 0))
  )
  (if (>= (vlax-safearray-get-u-bound vrt_pt 1) 0)
    (progn
      (setq pt (vlax-safearray->list vrt_pt))
      (if pt
        (if (> (length pt) 3)
          (repeat (/ (length pt) 3)
            (setq lst_pt (cons (list (car pt) (cadr pt) (caddr pt)) lst_pt) pt (cdddr pt))
          )
          (setq lst_pt (cons pt lst_pt))
        )
      )
    )
  )
  (setq
    fst_dxf (entget fst_obj)
    snd_dxf (entget snd_obj)
    lst1 (mapcar 'cdr (vl-remove-if '(lambda (x) (/= (car x) 10)) fst_dxf))
    lst2 (mapcar 'cdr (vl-remove-if '(lambda (x) (/= (car x) 10)) snd_dxf))
    lstx nil
  )
  (if (and lst_pt (listp lst_pt))
    (progn
      (setq obj (vlax-ename->vla-object fst_obj))
      (foreach el lst_pt
        (if (not (member T (mapcar '(lambda (x) (equal (list (car el) (cadr el)) x 1E-8)) lst1)))
          (add_vtx obj (vlax-curve-getparamatpoint obj (vlax-curve-getClosestPointTo obj el)) fst_obj)
        )
      )
      (setq obj (vlax-ename->vla-object snd_obj))
      (foreach el lst_pt
        (if (not (member T (mapcar '(lambda (x) (equal (list (car el) (cadr el)) x 1E-8)) lst2)))
          (add_vtx obj (vlax-curve-getparamatpoint obj (vlax-curve-getClosestPointTo obj el)) snd_obj)
        )
      )
    )
  )
  (setq
    fst_dxf (entget fst_obj)
    snd_dxf (entget snd_obj)
    lst1 (mapcar 'cdr (vl-remove-if '(lambda (x) (/= (car x) 10)) fst_dxf))
    lst2 (mapcar 'cdr (vl-remove-if '(lambda (x) (/= (car x) 10)) snd_dxf))
    lstx nil
  )
  (foreach n lst1
    (setq
      pt (nth (vl-position (apply 'min (setq lst_d (mapcar '(lambda (x) (distance n x)) lst2))) lst_d) lst2)
      ptx (list (* 0.5 (+ (car n) (car pt))) (* 0.5 (+ (cadr n) (cadr pt))))
      lstx (cons ptx lstx)
    )
  )
  (entmake
    (append
      (list
        (cons 0 "LWPOLYLINE")
        (cons 100 "AcDbEntity")
        (assoc 67 fst_dxf)
        (assoc 410 fst_dxf)
        (assoc 8 fst_dxf)
        (if (assoc 62 fst_dxf) (assoc 62 fst_dxf) (cons 62 256))
        (if (assoc 6 fst_dxf) (assoc 6 fst_dxf) (cons 6 "BYLAYER"))
        (if (assoc 370 fst_dxf) (assoc 370 fst_dxf) (cons 370 -1))
        (cons 100 "AcDbPolyline")
        (cons 90 (length lstx))
        (assoc 70 fst_dxf)
        (if (assoc 38 fst_dxf) (assoc 38 fst_dxf) (cons 38 0.0))
        (if (assoc 39 fst_dxf) (assoc 39 fst_dxf) (cons 39 0.0))
      )
      (apply 'append
        (mapcar
          '(lambda (x10)
            (list
              (cons 10 x10)
              '(40 . 0.0)
              '(41 . 0.0)
              '(42 . 0.0)
              '(91 . 0)
            )
          )
          lstx
        )
      )
      (list (assoc 210 fst_dxf))
    )
  )
  (setq lstx nil)
  (foreach n lst2
    (setq
      pt (nth (vl-position (apply 'min (setq lst_d (mapcar '(lambda (x) (distance n x)) lst1))) lst_d) lst1)
      ptx (list (* 0.5 (+ (car n) (car pt))) (* 0.5 (+ (cadr n) (cadr pt))))
      lstx (cons ptx lstx)
    )
  )
  (entmake
    (append
      (list
        (cons 0 "LWPOLYLINE")
        (cons 100 "AcDbEntity")
        (assoc 67 snd_dxf)
        (assoc 410 snd_dxf)
        (assoc 8 snd_dxf)
        (if (assoc 62 snd_dxf) (assoc 62 snd_dxf) (cons 62 256))
        (if (assoc 6 snd_dxf) (assoc 6 snd_dxf) (cons 6 "BYLAYER"))
        (if (assoc 370 snd_dxf) (assoc 370 snd_dxf) (cons 370 -1))
        (cons 100 "AcDbPolyline")
        (cons 90 (length lstx))
        (assoc 70 snd_dxf)
        (if (assoc 38 snd_dxf) (assoc 38 snd_dxf) (cons 38 0.0))
        (if (assoc 39 snd_dxf) (assoc 39 snd_dxf) (cons 39 0.0))
      )
      (apply 'append
        (mapcar
          '(lambda (x10)
            (list
              (cons 10 x10)
              '(40 . 0.0)
              '(41 . 0.0)
              '(42 . 0.0)
              '(91 . 0)
            )
          )
          lstx
        )
      )
      (list (assoc 210 snd_dxf))
    )
  )
  (sssetfirst nil (ssadd snd_obj (ssadd fst_obj (ssadd))))
  (prin1)
)

 

Test example.dwg

  • Thanks 1
Link to comment
Share on other sites

Hi Tsuky,

Your code in post is great! 👍. Unfortunately, i can not implement that into my code, because i need to create a middle polyline inside closed or opened polygon made by polyline, and then to export it in X,Y coordinates of middle polyline with other datas in .csv format. Thank you for reply to this topic and trying to help me in solution.

 

Best regards!

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...