Jump to content

Recommended Posts

Posted
1 hour ago, PGia said:

I was wrong.

c:CPL of @GP_ is also inconsistent in some cases.

Img1.thumb.png.ac20ced626693072ffca0efe8ddaf6cc.pngImg2.thumb.png.b502288dcda7534e49847945f58ea227.png

CPL.dwg 48.18 kB · 1 download

 

I already stated that in a few posts. All of them fail in certain situations.

 

That's why people are still taking a shot at solving the issue.

 

I have also already mentioned Civil/GIS programs can do this pretty well. QGIS (free/donation) has a plug-in to do this.

Posted (edited)

Here is my first attempt at this problem.

It creates offset lines and checks the intersections. The precision is dictated by the offsetdistance (on line 7).

The lower it is, the longer it takes to make the polyline and the more points it will have. But it will be more accurate.

 

Afterwards I run this function on the resulting polyline to clean it up. 

 

;|
; Center line - dexus
; https://www.cadtutor.net/forum/topic/98778-hybrid-parallel
|;
(defun c:testcl (/ ent1 lst offset offsetdistance pts r s1 s2 ss start te1 te2)

  (setq offsetdistance 0.03)

  (defun _polyline (pts)
    (entmakex
      (append
        (list
          (cons 0 "LWPOLYLINE")
          (cons 100 "AcDbEntity")
          (cons 100 "AcDbPolyline")
          (cons 90 (length pts))
          (cons 8 (getvar 'clayer))
          (cons 70 0)
        )
        (mapcar (function (lambda (x) (cons 10 x))) pts)
      )
    )
  )

  (defun _side (pline pnt / cpt end target der) ; https://www.theswamp.org/index.php?topic=55685.msg610429#msg610429
    (setq cpt (vlax-curve-getClosestPointTo pline pnt)
          end (vlax-curve-getEndParam pline)
          target (vlax-curve-getParamAtPoint pline cpt)
          der (if
                (and
                  (equal target (fix target) 1e-8)
                  (or
                    (vlax-curve-isClosed pline)
                    (and (not (equal (vlax-curve-getStartParam pline) target 1e-8)) (not (equal end target 1e-8)))
                  )
                )
                (mapcar '-
                  (polar cpt (angle '(0 0) (vlax-curve-getFirstDeriv pline (rem (+ target 1e-3) end))) 1.0)
                  (polar cpt (angle (vlax-curve-getFirstDeriv pline (rem (+ (- target 1e-3) end) end)) '(0 0)) 1.0)
                )
                (vlax-curve-getFirstDeriv pline target)
              )
            )
    (minusp (sin (- (angle cpt pnt) (angle '(0.0 0.0) der))))
  )

  ;; Intersections - Lee Mac
  ;; mod - [int] acextendoption enum of intersectwith method
  (defun LM:intersections ( ob1 ob2 mod / lst rtn )
    (if
      (and
        (vlax-method-applicable-p ob1 'intersectwith)
        (vlax-method-applicable-p ob2 'intersectwith)
        (setq lst (vlax-invoke ob1 'intersectwith ob2 mod))
      )
      (repeat (/ (length lst) 3)
        (setq rtn (cons (list (car lst) (cadr lst) (caddr lst)) rtn) lst (cdddr lst))
      )
    )
    (reverse rtn)
  )

  (defun getLength (ent)
    (-
      (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent))
      (vlax-curve-getDistAtParam ent (vlax-curve-getStartParam ent))
    )
  )

  (if
    (not 
      (while
        (cond
          ((not (setq ss (ssget '((0 . "LWPOLYLINE")))))
            (princ "\nNothing selected. Try again...\n")
          )
          ((/= (sslength ss) 2)
            (princ "\nSelect 2 polylines! Try again...\n")
          )
          ((and
              (setq ent1 (ssname ss 0))
              (setq ent2 (ssname ss 1))
              (setq ent1 (vlax-ename->vla-object ent1))
              (setq ent2 (vlax-ename->vla-object ent2))
            )
            nil ; Stop loop
          )
        )
      )
    )
    (progn
      (setq s1 (_side ent1 (vlax-curve-getStartPoint ent2)))
      (setq s2 (_side ent2 (vlax-curve-getStartPoint ent1)))
      (setq maxlen (max (getLength ent1) (getLength ent2)))
      (setq offset 0.0)
      (while
        (progn
          (setq offset (+ offset offsetdistance))
          (setq te1 nil)
          (setq te2 nil)
          (setq r
            (cond
              ( (or ; Make offset
                  (vl-catch-all-error-p (setq te1 (vl-catch-all-apply 'vlax-invoke (list ent1 'Offset (if s1 offset (- offset))))))
                  (vl-catch-all-error-p (setq te2 (vl-catch-all-apply 'vlax-invoke (list ent2 'Offset (if s2 offset (- offset))))))
                )
                (princ "\nOffset failed. ")
                nil
              )
              ((setq lst (LM:intersections (car te1) (car te2) acExtendNone))
                (setq len (getLength (car te1)))
                (setq lst (mapcar (function (lambda (pt) (list (/ (vlax-curve-getDistAtPoint (car te1) pt) len) pt))) lst))
                (setq start t)
                (setq pts (append lst pts))
              )
              ((not start) t)
              ((> offset maxlen) nil)
              (start nil)
            )
          )
          (if te1 (mapcar 'vla-delete te1))
          (if te2 (mapcar 'vla-delete te2))
          r
        )
      )
      (_polyline (mapcar 'cadr (vl-sort pts (function (lambda (a b) (< (car a) (car b)))))))
    )
  )
)

 

 

Edited by dexus
Posted (edited)
2 hours ago, PGia said:

I was wrong.

c:CPL of @GP_ is also inconsistent in some cases.

 

 

Could you post the dwg?

 

Edited by GP_

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