Author Topic: (entprev)  (Read 56530 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3266
  • Marko Ribar, architect
Re: (entprev)
« Reply #15 on: November 22, 2013, 10:02:34 AM »
Yes, there is still something wrong...

Code: [Select]
Command: !e
<Entity name: 7ffffb05c60>
Command: (entnext e)
<Entity name: 7ffffb05c80>
Command: (entprev e)
<Entity name: 7ffffb05c50>
Command: (entprev (entnext e))
nil
Command: (entnext (entprev e))
<Entity name: 7ffffb05c60>

Why is this :
Code: [Select]
Command: (entprev (entnext e))
nil

M.R. (It seems that you're right Antonio something's wrong...)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3266
  • Marko Ribar, architect
Re: (entprev)
« Reply #16 on: November 22, 2013, 10:09:27 AM »
It seems that only my version works correct with this trick :

Code: [Select]
Command: (entprev (entnext e))
<Entity name: 7ffffb05c60>
Command: (entnext (entprev e))
<Entity name: 7ffffb05c60>

But its the slowest variant... :cry:
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

LE3

  • Guest
Re: (entprev)
« Reply #17 on: November 22, 2013, 10:14:23 AM »
maybe this:
Code: [Select]
(defun lastEnt (/ a b) (if (setq a (entlast)) (while (setq b (entnext a)) (setq a b))) a)
(defun previousEnt (/ le pe) (setq le (lastEnt)) (entdel le) (setq pe (lastEnt)) (entdel le) pe)

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: (entprev)
« Reply #18 on: November 22, 2013, 10:19:59 AM »
Nice idea Luis  :-)

ribarm

  • Gator
  • Posts: 3266
  • Marko Ribar, architect
Re: (entprev)
« Reply #19 on: November 22, 2013, 10:27:29 AM »
I don't see where Luis is supplying entity from witch are result to be retrieved...

And IMO Antonio's code should work, but something's wrong, I just don't see it...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3266
  • Marko Ribar, architect
Re: (entprev)
« Reply #20 on: November 22, 2013, 10:47:07 AM »
I figured why Antonio's code isn't working as it should...

When (entprev (entnext e))
(entnext e) is actually nested entity from entity e, so when supplied this kind of entity, this condition (eq EntNam (ssname SelSet Countr)) will never be true, so (while) loop is continuing to search until all entities from SelSet witch is actually (ssget "_X") are processed, as a result of while loop not finding match, counter is incrementing all the way, and this line (setq EntOut (ssname SelSet (1+ Countr))  EntNxt EntOut) will assign EntOut variable nil, because (ssname SelSet (sslength SelSet)) is always nil - ssname can find entities in range from [0 - (1- (sslength SelSet))]... So EntOut = nil => (entprev (entnext e)) => nil
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

LE3

  • Guest
Re: (entprev)
« Reply #21 on: November 22, 2013, 11:13:37 AM »
I don't see where Luis is supplying entity from witch are result to be retrieved...
no problem, was just trying to add something on the plate - eventually if I get a chance will try to write the function with the from entity argument.... but my lisp skills are dead. have fun

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: (entprev)
« Reply #22 on: November 22, 2013, 11:15:37 AM »
@ Lee Mac:
Test 1.
Create a dwg with 2 entities:
(LM_entprev (entlast)) => nil

Test 2.
Create a dwg with 3 entities and delete the 2nd entity:
(LM_entprev (entlast)) => <Entity name: #>
(entget (LM_entprev (entlast))) => nil

Took me a while to understand your (hex--). Interesting approach.
But why not?:
Code: [Select]
(defun alt_hex-- ( hex / foo)
  (defun foo (lst)
    (cond
       ((null lst) nil)
       ((= 65 (car lst)) (cons 57 (cdr lst)))
       ((= 48 (car lst)) (cons 70 (foo (cdr lst))))
       ((cons (1- (car lst)) (cdr lst)))
    )
  )
  (vl-list->string (reverse (foo (reverse (vl-string->list hex)))))
)


Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: (entprev)
« Reply #23 on: November 22, 2013, 11:35:27 AM »
@ Lee Mac:
Test 1.
Create a dwg with 2 entities:
(LM_entprev (entlast)) => nil

Test 2.
Create a dwg with 3 entities and delete the 2nd entity:
(LM_entprev (entlast)) => <Entity name: #>
(entget (LM_entprev (entlast))) => nil

I don't have time right now, but I shall investigate your findings - thanks roy  :-)

Took me a while to understand your (hex--). Interesting approach.
But why not?:
Code: [Select]
(defun alt_hex-- ( hex / foo)
  (defun foo (lst)
  ...
)

Thanks - Both methods are equivalent of course, I just like to experiment with different methods in order to discover & demonstrate new ways of doing things.  :-)

LE3

  • Guest
Re: (entprev)
« Reply #24 on: November 22, 2013, 01:02:03 PM »
I don't see where Luis is supplying entity from witch are result to be retrieved...

ok - played with on my lunch break.... and continued with a similar approach, let's see if works.
Code: [Select]
(defun lastEnt (/ a b) (if (setq a (entlast)) (while (setq b (entnext a)) (setq a b))) a)
(defun previousEnt (/ le pe) (setq le (lastEnt)) (entdel le) (setq pe (lastEnt)) (entdel le) pe)
(defun previousEntFromEnt  (e / a b l pe)
  (setq a e)
  (while (setq b (entnext a))
    (setq l (cons b l))
    (setq a b))
  (foreach i l (entdel i))
  (setq pe (previousEnt))
  (foreach i l (entdel i))
  pe)

test command:
Quote
(defun c:tst  (/ e p)
  (if (setq e (car (entsel)))
    (progn
      (if (setq p (previousEntFromEnt e))
   (redraw p 3)))))

owenwengerd

  • Bull Frog
  • Posts: 451
Re: (entprev)
« Reply #25 on: November 22, 2013, 01:06:24 PM »
I don't have time at the moment to dig out my old code, but I once wrote a version similar to Lee's that was better optimized for sparse handles (i.e. drawings with large handle gaps between consecutive entities) by using a look-ahead (or rather, look-behind in this case) algorithm in the case where the previous handle doesn't exist. The idea was to find an earlier entity without enumerating every single handle in a large gap. Once any previous entity is found, (entnext) could be used to find the "most" previous entity between the found entity and the base entity.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: (entprev)
« Reply #26 on: November 22, 2013, 03:00:15 PM »
My try  :D ... have to keep my hand in
Code - Auto/Visual Lisp: [Select]
  1. (defun hex->dec  (hex / fact)
  2.   (setq fact (expt 16 (strlen hex)))
  3.   (apply '+
  4.          (mapcar (function (lambda (h)
  5.                              (* (cond ((> h 57) (- h 55))
  6.                                       ((> h 47) (- h 48)))
  7.                                 (setq fact (/ fact 16)))))
  8.                  (vl-string->list hex))))
  9.  
  10.  
  11. (defun dec->hex  (dec / d lst)
  12.   (while (> dec 0)
  13.     (setq d   (rem dec 16)
  14.           lst (cons (cond ((> d 9) (+ d 55))
  15.                           (t (+ d 48)))
  16.                     lst)
  17.           dec (lsh dec -4)))
  18.   (vl-list->string lst))
  19.  
  20.  
  21. (defun entprev  (eName / h d en stop found)
  22.   (setq h    (hex->dec (cdr (assoc 5 (entget eName))))
  23.         stop (1- (hex->dec (cdr (assoc 5 (entget (entnext)))))))
  24.   (while (and (not found) (> (setq h (1- h)) stop))
  25.     (if (and (setq en (handent (dec->hex h))) (setq d (entget en)))
  26.       (progn (setq h (hex->dec (cdr (assoc 5 d))))
  27.              (if (eq (entnext en) eName)
  28.                (setq found en)))))
  29.   found)
A bit imperative, but it works.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: (entprev)
« Reply #27 on: November 22, 2013, 03:48:51 PM »
Code: [Select]
(defun entprev (e / e1 e2)
  (setq e1 (entnext))
  (while (and e1 (not (eq e (setq e2 (entnext e1)))))
    (setq e1 e2)
  )
  e1
)

be careful with decreasing hex, sometimes handles in custom generated dxfs are total mess

ribarm

  • Gator
  • Posts: 3266
  • Marko Ribar, architect
Re: (entprev)
« Reply #28 on: November 22, 2013, 03:49:32 PM »
Congratulations, Irne... It works, I've checked and it's fast...

(entnext (entprev e)) = e
and also
(entprev (entnext e)) = e, where (entnext e) is nested ename of e (POLYLINE entity)...

Thanks again, M.R. :-)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3266
  • Marko Ribar, architect
Re: (entprev)
« Reply #29 on: November 22, 2013, 03:54:19 PM »
Code: [Select]
(defun entprev (e / e1 e2)
  (setq e1 (entnext))
  (while (and e1 (not (eq e (setq e2 (entnext e1)))))
    (setq e1 e2)
  )
  e1
)

be careful with decreasing hex, sometimes handles in custom generated dxfs are total mess

Excellent logic, VovKa, congratulations, too...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube