Author Topic: Select Entity at End Problem  (Read 2671 times)

0 Members and 1 Guest are viewing this topic.

Rabbit

  • Guest
Select Entity at End Problem
« on: October 28, 2011, 09:41:51 AM »
I need to use Lisp to select an entity (arc, line, leader, lwpolyline or spline) and then select the endpoint of that entity near the selection point.  I've use ENTSEL to get the entity information and the click point, but I can't figure out a way to get the enpoint nearest to that click to insert a donut entity, which obtains the layer properties of the selected entity.

What pointers do you guys have?

Sincerely,
Rabbit

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Select Entity at End Problem
« Reply #1 on: October 28, 2011, 09:47:16 AM »

for the line

distance from pick point to dxf 10
distance from pick point to dxf 11

if to dxf10 is the shorter distance, dxf10 is nearest.


the other entitys will use a similar methodology.

Regards,
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Rabbit

  • Guest
Re: Select Entity at End Problem
« Reply #2 on: October 28, 2011, 10:19:46 AM »
Codes 10 and 11 don't work for arcs.  I'm going to have to search for a bit of code for getting the endpoints of arcs.

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Select Entity at End Problem
« Reply #3 on: October 28, 2011, 10:34:30 AM »
For the most part, each entity type will require it's own calculation.  3DFACEs SOLIDs and TRACEs could share a function.  Some entity types would be impossible to calculate ( Circle ).  Some would be very difficult.  -David
R12 Dos - A2K

jaydee

  • Guest
Re: Select Entity at End Problem
« Reply #4 on: October 28, 2011, 10:37:03 AM »
I use this

(setq npt (car (cdr (entsel))))
(setq endpt (osnap npt "end"))
(command "_.insert" donutblk endpt "" "" "")

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Select Entity at End Problem
« Reply #5 on: October 28, 2011, 10:41:45 AM »
I would be inclined to use vlax-curve-getstartpoint / vlax-curve-getendpoint since these will work for all curve entities.

Then compare the distances between each point and your selection point to determine which to use.
« Last Edit: October 28, 2011, 10:54:59 AM by Lee Mac »

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Select Entity at End Problem
« Reply #6 on: October 28, 2011, 10:44:01 AM »
I would be incline to use vlax-curve-getstartpoint / vlax-curve-getendpoint since these will work for all curve entities.

Then compare the distances between each point and your selection point to determine which to use.

X2

Here's a quick example:


Code: [Select]
(defun c:epsp (/ e p)
  (and (setq e (entsel))
       (setq p (cadr e))
       (setq e (car e))
       (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-curve-getendpoint (list e))))
       (setq p (caar (vl-sort (mapcar '(lambda (x) (list x (distance p x)))
      (list (vlax-curve-getendpoint e) (vlax-curve-getstartpoint e))
      )
      '(lambda (a b) (< (cadr a) (cadr b)))
     )
       )
       )
       (entmakex (list (cons 0 "CIRCLE") (cons 10 p) (cons 40 0.5)))
  )
  (princ)
)
« Last Edit: October 28, 2011, 10:56:27 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Rabbit

  • Guest
Re: Select Entity at End Problem
« Reply #7 on: October 28, 2011, 11:01:13 AM »
Sometimes the simplest answers are the best

(setq *DOT*Wire (entsel "\nSelect wire near end for insertion point of dot: "))
  (setq *DOT*WireEntity (car *DOT*Wire))
  (setq *DOT*WireEndPoint (osnap (car (cdr *DOT*Wire)) "end"))
  (setq *DOT*WireLayer (cdr (assoc 8 (entget *DOT*WireEntity))))
  (command "-layer" "s" *DOT*WireLayer "")
  (command "donut" 0.0 (/ Dimscl 27) *DOT*WireEndPoint "")

Works perfectly.
Thanks jaydee

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Select Entity at End Problem
« Reply #8 on: October 28, 2011, 11:25:02 AM »
Another...

Code: [Select]
(defun c:test ( / ent pnt rad sel )
    (while
        (progn (setvar 'ERRNO 0) (setq sel (entsel))
            (cond
                (   (= 7 (getvar 'ERRNO))
                    (princ "\nMissed, try again.")
                )
                (   (null sel)
                    nil
                )
                (   (listp sel)
                    (if (vl-catch-all-error-p
                            (vl-catch-all-apply 'vlax-curve-getendpoint (list (car sel)))
                        )
                        (princ "\nInvalid Object.")
                        (progn
                            (setq ent (car  sel)
                                  pnt (cadr sel)
                                  rad 0.5
                            )
                            (setq pnt
                                (if (<  (distance pnt (vlax-curve-getstartpoint ent))
                                        (distance pnt (vlax-curve-getendpoint   ent))
                                    )
                                    (vlax-curve-getstartpoint ent)
                                    (vlax-curve-getendpoint   ent)
                                )
                            )
                            (entmakex
                                (list
                                   '(0 . "LWPOLYLINE")
                                   '(100 . "AcDbEntity")
                                   '(100 . "AcDbPolyline")
                                   '(90 . 2)
                                   '(70 . 1)
                                    (cons 43 rad)
                                    (cons 10 (polar pnt pi (/ rad 2.0)))
                                   '(42 . 1)
                                    (cons 10 (polar pnt 0. (/ rad 2.0)))
                                   '(42 . 1)
                                )
                            )
                        )
                    )
                )
            )
        )
    )
    (princ)
)

jaydee

  • Guest
Re: Select Entity at End Problem
« Reply #9 on: October 28, 2011, 11:17:17 PM »
Sometimes the simplest answers are the best

Works perfectly.
Thanks jaydee

Hi rabit.
Not sure it the best, if it works for you that what matters.
One thing i found the OSNAP method is not very reliable in some instance where there is a lot of entities closed to the one you pick, then its possible the osnap snap end point to the wrong entity.

Since then i have take up advice from this forum and used/replace osnap options with
vlax-curve-getstartpoint
vlax-curve-getendpoint

It might be difficult to follow initially, but its worth it.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Select Entity at End Problem
« Reply #10 on: October 29, 2011, 07:56:10 AM »
Or something like this to account for oddly shaped curves where the opposite endpoint is closer to the selection...

Code: [Select]
(defun _ClosestEndPoint (ent pnt / dst)
  (setq dst (vlax-curve-getDistAtPoint
              ent
              (vlax-curve-getClosestPointToProjection ent (trans pnt 1 ent) '(0. 0. 1.))
            )
  )
  (caar
    (vl-sort (list (list (vlax-curve-getStartPoint ent) 0.)
                   (list (vlax-curve-getEndPoint ent)
                         (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent))
                   )
             )
             '(lambda (a b) (< (abs (- dst (cadr a))) (abs (- dst (cadr b)))))
    )
  )
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Select Entity at End Problem
« Reply #11 on: November 07, 2011, 04:52:07 AM »
@ alanjt:
This 'projection' stuff is not exactly my strong point.
But shouldn't this line:
  (vlax-curve-getClosestPointToProjection ent (trans pnt 1 ent) '(0. 0. 1.))
Be changed to:
  (vlax-curve-getClosestPointToProjection ent (trans pnt 1 0) '(0. 0. 1.))
?
And shouldn't the viewdir somehow play a part in this?

BTW: Good to know that the 'curve' functions will also swallow enames!