TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Gliderider on September 17, 2008, 07:43:13 AM

Title: Entity name of last drawn entity type
Post by: Gliderider on September 17, 2008, 07:43:13 AM
How can I get the entity name of the last drawn entity type? e.g... The entity name of the last drawn polyline, even if there have been circles and arcs drawn since?
Title: Re: Entity name of last drawn entity type
Post by: kpblc on September 17, 2008, 08:05:54 AM
Do you mean (entlast)?
Title: Re: Entity name of last drawn entity type
Post by: Gliderider on September 17, 2008, 08:12:43 AM
Entlast returns the last created entity, I'm looking to find the last created entity of a certain type.
I tried (ssget "L" '((0 . "LWPOLYLINE"))) but that didn't work, returned nil
Title: Re: Entity name of last drawn entity type
Post by: Alan Cullen on September 17, 2008, 08:42:45 AM
Not to get too carried away here...but there are two types of polylines here...L and LW...depends on a variable setting...

Maybe......

(ssget "L" '((0 . "LWPOLYLINE" "POLYLINE")))

Just my two bobs worth.
Title: Re: Entity name of last drawn entity type
Post by: Gliderider on September 17, 2008, 08:57:10 AM
Thanks, but that doesn't seem to be a solution...
Title: Re: Entity name of last drawn entity type
Post by: VovKa on September 17, 2008, 10:16:24 AM
Code: [Select]
(ssname (ssget "_X" (list (cons 0 "LWPOLYLINE"))) 0)
Title: Re: Entity name of last drawn entity type
Post by: CAB on September 17, 2008, 10:27:07 AM
Very nice VovKa..

Mine is slower.
Code: [Select]
;;  CAB - get last entity of a type in datatbase
(defun GetLastType (typ / e1 result )
  (and
    (setq e1 (entnext))
    (while (setq e1 (entnext e1))
      (if (= typ (cdr (assoc 0 (entget e1))))
        (setq result e1)
      )
    )
  )
  result
)
Title: Re: Entity name of last drawn entity type
Post by: Gliderider on September 17, 2008, 10:37:51 AM
Thanks to both of you Vovka and Cab... there's more than one way to skin a cat.
Title: Re: Entity name of last drawn entity type
Post by: VovKa on September 17, 2008, 10:54:34 AM
thanx CAB.
it's a pity, lpseifert, but both our ways will sometimes fail when working in paperspaces :(
Title: Re: Entity name of last drawn entity type
Post by: MP on September 17, 2008, 10:55:36 AM
Thinking outside the cat box ...

Code: [Select]
(defun _Sort_SS_By_ObjectID ( ss )
    ;;  returns the selection set as a list
    ;;  of objects, ordered by objectid
    (vl-sort
        (if (eq 'pickset (type ss))
            (mapcar
               '(lambda ( x ) (vlax-ename->vla-object (cadr x)))
                (vl-remove-if '(lambda ( x ) (minusp (car x))) (ssnamex ss))
            )
        )
       '(lambda ( a b ) (< (vla-get-objectid a) (vla-get-objectid b)))
    )
)

Example:

Code: [Select]
(last (_Sort_SS_By_ObjectID (ssget "x" '((0 . "LwPolyline")))))
Once you get the desired object you could then turn it back into an ename if that's what you need.
Title: Re: Entity name of last drawn entity type
Post by: CAB on September 17, 2008, 11:02:49 AM
thanx CAB.
it's a pity, lpseifert, but both our ways will sometimes fail when working in paperspaces :(
Are you implying that the user may want only Current Space & then there would be a bad result?
Title: Re: Entity name of last drawn entity type
Post by: CAB on September 17, 2008, 11:03:40 AM
MP, never hindered by the box it would seem. 8-)
Title: Re: Entity name of last drawn entity type
Post by: Alan Cullen on September 17, 2008, 11:06:44 AM
Try this.....

(ssget "L" '((0 . "LWPOLYLINE,POLYLINE")))
Title: Re: Entity name of last drawn entity type
Post by: VovKa on September 17, 2008, 11:27:10 AM

Are you implying that the user may want only Current Space & then there would be a bad result?

create a layout "layout1"
draw a polyline there
create another layout "layout2"
and draw a polyline there
now activate "Model"
run your code and note the result
now activate "layout1" and then activate back "Model"
run your code and see that results are different (at least it is so in acad 2005)

this is all because of the specific bahavior of (entnext) which steps through model first and then through last active layout.

ssget does a like error

we should both add some kind of (= (getvar "CTAB") (cdr (assoc 410 ....
Title: Re: Entity name of last drawn entity type
Post by: CAB on September 17, 2008, 12:37:22 PM
In ACAD2000 all code worked as expected.
Must be a version specific error.