Anyone know?

Discussion in 'AutoCAD' started by Rudy Tovar, Oct 21, 2004.

  1. Rudy Tovar

    Rudy Tovar Guest

    Can't remember where I saw the post regarding point within a polyline or
    boundary...

    Does anyone remember or have a snip of code willing to share?
     
    Rudy Tovar, Oct 21, 2004
    #1
  2. Rudy Tovar

    T.Willey Guest

    T.Willey, Oct 21, 2004
    #2
  3. Rudy Tovar

    MP Guest

    ;;D. C. Broad 7/17/03

    ;;Will determine with high degree of certainty whether
    ;;a point is inside a closed polyline object-NOT INFALLIBLE
    ;;Limitations: The current ucs must be aligned with the
    ;;polyline verticies for the ray to be properly constructed
    (defun test()
    (ptinsidepoly(getpoint)(objectize(car(entsel"\nPick closed polyline"))))
    )
    (defun ptinsidepoly (pt-wcs poly / ray
    result closest varpt ray
    intersections intersections2
    verbose)
    (setq verbose t) ;;uncomment this out to understand branching
    (setq closest (vlax-curve-getclosestpointto poly pt-wcs))
    (setq result
    (cond
    ;;Test if pt on polyline
    ((equal closest pt-wcs 1e-10)
    (if verbose
    (princ "Point is on polyline (not inside). "))
    nil)
    ;;Test if polyline is not closed
    ((equal :vlax-false (vla-get-closed poly))
    (if verbose
    (princ "Polyline is not closed. Can't be inside. "))
    nil)
    ;;Use a ray and the intersectwith method to determine the
    ;;number of intersections between the ray and the point
    ;;An even number of intersections is highly likely make the
    ;;point outside (it is possible that the ray might pass through
    ;;a segment and one vertex and still be
    ((progn
    (setq varpt (vlax-3d-point pt-wcs)
    ray (vla-addray
    (vla-objectidtoobject
    (vla-get-document poly)
    (vla-get-ownerid poly))
    varpt
    (vlax-3d-point (trans (polar pt-wcs 0.05 1) 1 0)))
    intersections (vlax-invoke ray "intersectwith" poly 0))
    (zerop (rem (length intersections) 6))
    )
    ;;not inside
    (if verbose
    (princ "Even or zero number of intersections(outside). "))
    nil)
    ;;if odd and more than one intersection then it is certainly inside
    ((> (length intersections) 3)
    ;;is inside
    (if verbose
    (princ
    "Odd number of intersections greater than 1 (inside). "))
    t)
    ;;at this point, if the ray has intersected just once, then the point
    ;;may still not be inside the polyline. That is, the ray may intersect
    ;;tangent with the polyline or extending through a single vertex.
    ((progn
    (vla-rotate ray varpt (* 1.334 pi))
    ;;an odd rotation angle
    (setq intersections2
    (vlax-invoke ray "intersectwith" poly 0))
    (zerop (rem (length intersections2) 6)))
    ;;is not inside
    (if verbose
    (princ
    "Even number of intersections with rotated ray (not inside). "))
    nil)
    (t
    (if verbose
    (princ
    "All other conditions exhausted. Highly likely inside. "))
    ;;99.95% chance certainty. There is a minute chance however that
    t)
    ;;the ray has just struck through 2 separate verticies without
    ;;starting from inside the polyline
    )
    ) ;;end of result setq
    (if ray
    (vla-delete ray))
    result)


    hth
    Mark
     
    MP, Oct 21, 2004
    #3
  4. Rudy Tovar

    Rudy Tovar Guest

    I got this error...

    Pick closed polyline; error: no function definition: OBJECTIZE
     
    Rudy Tovar, Oct 21, 2004
    #4
  5. Rudy Tovar

    Rudy Tovar Guest

    Am I to assume that the object1 is an actual object as well as object2?

    How does it work?
     
    Rudy Tovar, Oct 21, 2004
    #5
  6. Rudy Tovar

    MP Guest

    oh that was just my little test
    sorry
    the ptinsidepoly was dougs.
    do your own test
    or
    (defun objectize(ent)
    (vlax-ename->vla-object ent)
    )


    also here's a version from luis

    ;;; 9/4/03 by Luis Esquivel
    ;;; http://www.draftteam.com

    (defun thisDwg ()
    (vla-get-activeDocument (vlax-get-acad-object)))

    (or :model
    (setq :model
    (vla-get-modelSpace (thisDwg))))

    (defun pSpace () (vla-get-paperSpace (thisDwg)))

    (defun get-activeSpace ()
    (if (= acModelSpace (vla-get-activeSpace (thisDwg)))
    :model
    (if (= (vla-get-mSpace (thisDwg)) :vlax-true)
    :model
    (pSpace))))

    (defun addline (sp ep)
    (vla-addline
    (get-activeSpace)
    (vlax-3d-point sp)
    (vlax-3d-point ep)))

    (defun point-inside-region-p
    (vla_poly pt aid-pt / vla_line return_obj param result)
    (setq param (vlax-curve-getparamatpoint vla_poly pt))
    (setq
    result (if (not (vl-position (list (car pt) (cadr pt)) pts))
    (progn
    (setq vla_line (addline pt aid-pt))
    (if (not
    (vl-catch-all-error-p
    (setq return_obj
    (vl-catch-all-apply
    'vlax-safearray->list
    (list (vlax-variant-value
    (vla-intersectwith
    vla_poly
    vla_line
    acextendnone)))))))
    (progn
    (vla-delete vla_line)
    (fix (/ (length return_obj) 3.0)))
    (vla-delete vla_line)))))
    (if (and param (= (type (- param (fix param))) 'real))
    (setq result nil))
    result)

    ;;; return T or nil
    (defun C:TEST (/ ent elst pts pt d vla_poly aid-pt lc uc flag)
    (if
    (and (setq ent (car (entsel "\nSelect a polyline: ")))
    (eq (cdadr (setq elst (entget ent))) "LWPOLYLINE")
    (setq pt (getpoint "\nTest point: ")))
    (progn
    (foreach item elst
    (if (eq (car item) 10)
    (setq pts (cons (cdr item) pts))))
    (vla-getboundingbox
    (setq vla_poly (vlax-ename->vla-object ent))
    'lc
    'uc)
    ;; diagonal
    (setq d (distance (vlax-safearray->list lc)
    (vlax-safearray->list uc)))
    (setq aid-pt (polar pt (/ pi 2.0) d))
    (if (setq flag (point-inside-region-p vla_poly pt aid-pt))
    (not (zerop (rem flag 2)))))))

    (princ)
     
    MP, Oct 21, 2004
    #6
  7. Rudy Tovar

    T.Willey Guest

    T.Willey, Oct 21, 2004
    #7
  8. Rudy Tovar

    Rudy Tovar Guest

    Thanks, it worked great...although I'll still test out the one prior...
     
    Rudy Tovar, Oct 21, 2004
    #8
  9. Rudy Tovar

    Rudy Tovar Guest

    Thanks Luis.

    Is the code condensed even more?

    Faster the better...
     
    Rudy Tovar, Oct 21, 2004
    #9
  10. Rudy Tovar

    John Uhden Guest

    Yep. That's the last one I can find.
     
    John Uhden, Oct 22, 2004
    #10
  11. Rudy Tovar

    Rudy Tovar Guest

    Looking at the code, I'm seeing that you took into consideration the point
    of a pline which include the points of a bulge where the arch intersects the
    point of the segment.
     
    Rudy Tovar, Oct 22, 2004
    #11
  12. Rudy Tovar

    John Uhden Guest

    Pretty much most things, I think. I trust the included remarks were helpful.
     
    John Uhden, Oct 23, 2004
    #12
  13. Rudy,
    I have found the non object methods less dependable and slower than ones that use a point object then erase.
    I have not tested every routine out there of course so am open to correction on that.
    The problems seem to happen on complicated plines with lots of arcs.

    Curiuos how dependably the non-object code posted works

    I guess I am trying to use the speed of acad's selection engine verses geometric calculations.
    Let me know if you want to pursue that...

    "Rudy Tovar" <>
    |>Can't remember where I saw the post regarding point within a polyline or
    |>boundary...
    |>
    |>Does anyone remember or have a snip of code willing to share?
    |>

    James Maeding
    jmaeding at hunsaker dot com
    Civil Engineer/Programmer
     
    James Maeding, Oct 25, 2004
    #13
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.