Author Topic: (entprev)  (Read 56487 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
(entprev)
« on: November 21, 2013, 03:32:16 PM »
As opposed to (entnext), I wrote this little sub-function, but I don't know jet where can I use it but for checking purposes... :loco:
Maybe someone may find it useful...

Code: [Select]
(defun entprev ( e / ss en ssxelst )
  (if (eq (type e) 'ename)
    (if (setq ss (ssget "_X"))
      (progn
        (setq en (ssname ss (- (sslength ss) 1)))
        (setq ssxelst (cons en ssxelst))
        (while (setq en (entnext en))
          (setq ssxelst (cons en ssxelst))
        )
        (cadr (member e ssxelst))
      )
    )
    (progn (prompt "\n; error: bad argument type: lentityp ")(princ e)(princ))
  )
)

 :wink:
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: (entprev)
« Reply #1 on: November 22, 2013, 05:06:10 AM »
   Another approach:
Code: [Select]
; Version 1.00 - 22/11/2013
;
(defun ALE_EntPrevious (EntNam / SelSet Countr SsLngt)
  (if (eq (type EntNam) 'ename)
    (if (setq SelSet (ssget "_X"))
      (progn
        (setq Countr 0   SsLngt (sslength SelSet))
        (while (> SsLngt Countr)
          (if (eq EntNam (ssname SelSet Countr))
            (setq SsLngt 0)
            (setq Countr (1+ Countr))
          )
        )
        (ssname SelSet (1+ Countr))
      )
    )
    (progn (prompt "\n; error: bad argument type: lentityp ")(princ EntNam)(princ))
  )
)

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: (entprev)
« Reply #2 on: November 22, 2013, 05:26:33 AM »
Antonio, (ssname) function will return parent entityname instead of nested... My version uses (entnext) to build list of all entity + nested entity names and then it retrieves previous one from supplied entity , wich can be nested too...

Test this with 3d polyline entity or old heavy 2d polyline and see what I am talking about...

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: (entprev)
« Reply #3 on: November 22, 2013, 06:29:03 AM »
Here is just a slight improvement in that that now list of all entity + nested entity names is shorter - it is proccessed all from beginning till supplied entity is reached and therefore rest of entity names are unneccessary to build into this list... Previous entity name is returned just when our desired entity is reached...

Code: [Select]
(defun entprev ( e / ss en ssxelst )
  (if (eq (type e) 'ename)
    (if (setq ss (ssget "_X"))
      (progn
        (setq en (ssname ss (- (sslength ss) 1)))
        (setq ssxelst (cons en ssxelst))
        (while (and (/= (setq en (entnext en)) nil) (/= en e))
          (setq ssxelst (cons en ssxelst))
        )
        (setq ssxelst (cons en ssxelst))
        (cadr (member e ssxelst))
      )
    )
    (progn (prompt "\n; error: bad argument type: lentityp ")(princ e)(princ))
  )
)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: (entprev)
« Reply #4 on: November 22, 2013, 08:17:42 AM »
Try this:
Code: [Select]
; Version 1.00 - 22/11/2013
;
(defun ALE_NEntPrevious (EntNam / SelSet Countr SsLngt EntOut EntNxt)
  (if (eq (type EntNam) 'ename)
    (if (setq SelSet (ssget "_X"))
      (progn
        (setq Countr 0   SsLngt (sslength SelSet))
        (while (> SsLngt Countr)
          (if (eq EntNam (ssname SelSet Countr))
            (setq SsLngt 0   EntOut (ssname SelSet (1+ Countr))  EntNxt EntOut)
            (setq Countr (1+ Countr))
          )
        )
        (while (and EntNxt (setq EntNxt (entnext EntNxt)))
          (if (eq EntNxt EntNam) (setq EntNxt nil) (setq EntOut EntNxt))
        )
        EntOut
      )
    )
    (progn (prompt "\n; error: bad argument type: lentityp ")(princ EntNam)(princ))
  )
)

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: (entprev)
« Reply #5 on: November 22, 2013, 08:23:17 AM »
An alternative method perhaps:
Code - Auto/Visual Lisp: [Select]
  1. ;; entprev  -  Lee Mac 2013-11-22
  2. (defun entprev ( e1 / e2 h1 h2 )
  3.     (setq h1 (cdr (assoc 5 (entget (entnext))))
  4.           h2 (cdr (assoc 5 (entget e1)))
  5.     )
  6.     (if (/= h1 h2)
  7.         (while
  8.             (not
  9.                 (or (= h1 (setq h2 (hex-- h2)))
  10.                     (and (setq e2 (handent h2)) (entnext e2))
  11.                 )
  12.             )
  13.             (setq e2 nil)
  14.         )
  15.     )
  16.     e2
  17. )
  18.  
  19. ;; Decrement Hex  -  Lee Mac 2013-11-22
  20. (defun hex-- ( h )
  21.     (   (lambda ( f ) (vl-list->string (reverse (f (reverse (vl-string->list h))))))
  22.         (lambda ( l )
  23.             (cond
  24.                 (   (null l) nil)
  25.                 (   (= 65 (car l)) (cons 57 (cdr l)))
  26.                 (   (= 48 (car l)) (cons 70 (f (cdr l))))
  27.                 (   (cons (1- (car l)) (cdr l)))
  28.             )
  29.         )
  30.     )
  31. )

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: (entprev)
« Reply #6 on: November 22, 2013, 08:30:41 AM »
I think that now Antonio hit the target right into center... Although I am not familiar with Lee's suggestion, I would of course use the code that is better, but unfortunately I don't have benchmarking codes and haven't done this process - I think it will be nice to compare, but I don't know how and with what example...
 :embarrassed:
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: (entprev)
« Reply #7 on: November 22, 2013, 08:32:48 AM »
@Lee: Very alternative!  :kewl:

(small drawing)
Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved

Elapsed milliseconds / relative speed for 4096 iteration(s):

    (LM_ENTPREV KKKK).................1186 / 6.27 <fastest>
    (ALE_NENTPREVIOUS KKKK).....5116 / 1.45
    (ENTPREV KKKK)........................7441 / 1 <slowes

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: (entprev)
« Reply #8 on: November 22, 2013, 08:36:14 AM »
Thank you Marc  8-)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: (entprev)
« Reply #9 on: November 22, 2013, 09:01:44 AM »
@Lee I think there is something wrong:

Code: [Select]
Comando: (entget (ALE_nEntPrevious kkkk))
((-1 . <Nome entità: 7ffffb18880>) (0 . "SEQEND") (330 . <Nome entità: 7ffffb18870>) (5 . "5C10") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "$0__NORM") (6 . "Continuous") (-2 . <Nome entità: 7ffffb18870>))

Comando: (entget (entprev kkkk))
((-1 . <Nome entità: 7ffffb18880>) (0 . "SEQEND") (330 . <Nome entità: 7ffffb18870>) (5 . "5C10") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "$0__NORM") (6 . "Continuous") (-2 . <Nome entità: 7ffffb18870>))

Comando: (entget (lm_entprev kkkk))
((-1 . <Nome entità: 7ffffb18980>) (0 . "VERTEX") (330 . <Nome entità: 7ffffb18870>) (5 . "5C20") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "$0__NORM") (6 . "Continuous") (100 . "AcDbVertex") (100 . "AcDb2dVertex") (10 502.003 -324.114 0.0) (40 . 0.0) (41 . 0.0) (42 . 0.0713466) (91 . 0) (70 . 0) (50 . 5.68134))


Just a little improvement:

Code: [Select]
(defun ALE_NEntPrevious (EntNam / SelSet Countr SsLngt EntOut EntNxt)
  (if (eq (type EntNam) 'ename)
    (if (setq SelSet (ssget "_X"))
      (progn
        (setq Countr 0   SsLngt (sslength SelSet))
        (while (> SsLngt Countr)
          (if (eq EntNam (ssname SelSet Countr))
            (setq SsLngt 0)
            (setq Countr (1+ Countr))
          )
        )
        (setq EntOut (ssname SelSet (1+ Countr))  EntNxt EntOut)
        (while (and EntNxt (setq EntNxt (entnext EntNxt)))
          (if (eq EntNxt EntNam) (setq EntNxt nil) (setq EntOut EntNxt))
        )
        EntOut
      )
    )
    (progn (prompt "\n; error: bad argument type: lentityp ")(princ EntNam)(princ))
  )
)

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: (entprev)
« Reply #10 on: November 22, 2013, 09:15:01 AM »
Marc'Antonio, I did few more tests and Lee's code gave me correct results like my, or your code... Can you elaborate your results?... How did you get them?...

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: (entprev)
« Reply #11 on: November 22, 2013, 09:27:30 AM »
@Lee I think there is something wrong:

This should fix it:
Code - Auto/Visual Lisp: [Select]
  1. ;; entprev  -  Lee Mac 2013-11-22
  2. (defun entprev ( e1 / e2 h1 h2 )
  3.     (setq h1 (cdr (assoc 5 (entget (entnext))))
  4.           h2 (cdr (assoc 5 (entget e1)))
  5.     )
  6.     (if (/= h1 h2)
  7.         (while
  8.             (not
  9.                 (or (= h1 (setq h2 (hex-- h2)))
  10.                     (and (setq e2 (handent h2)) (entnext e2))
  11.                 )
  12.             )
  13.             (setq e2 nil)
  14.         )
  15.     )
  16.     (if (and e2 (not (eq e1 (entnext e2))))
  17.         (entnext e2)
  18.         e2
  19.     )
  20. )
  21.  
  22. ;; Decrement Hex  -  Lee Mac 2013-11-22
  23. (defun hex-- ( h )
  24.     (   (lambda ( f ) (vl-list->string (reverse (f (reverse (vl-string->list h))))))
  25.         (lambda ( l )
  26.             (cond
  27.                 (   (null l) nil)
  28.                 (   (= 65 (car l)) (cons 57 (cdr l)))
  29.                 (   (= 48 (car l)) (cons 70 (f (cdr l))))
  30.                 (   (cons (1- (car l)) (cdr l)))
  31.             )
  32.         )
  33.     )
  34. )

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: (entprev)
« Reply #12 on: November 22, 2013, 09:38:49 AM »
My test on empty DWG:

draw a line
draw a circle
Comando: PELLIPSE
Digitare nuovo valore per PELLIPSE <0>: 1
draw a ELLISSE
draw a ELLISSE

Comando: (setq kkkk (car (entsel)))  select last ellipse

Comando: !kkkk
<Nome entità: 7ffffb08090>

Comando: (entnext (entprev kkkk))
<Nome entità: 7ffffb18f30>

Comando: (entnext (ALE_NEntPrevious kkkk))
<Nome entità: 7ffffb08090>

Comando: (entnext (LM_entprev1 kkkk))
<Nome entità: 7ffffb18f30>

Comando: (entnext (LM_entprev2 kkkk))
<Nome entità: 7ffffb08010>

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: (entprev)
« Reply #13 on: November 22, 2013, 09:47:15 AM »
I was a little too hasty with my fix, try this:
Code - Auto/Visual Lisp: [Select]
  1. ;; entprev  -  Lee Mac 2013-11-22
  2. (defun entprev ( e1 / e2 h1 h2 )
  3.     (setq h1 (cdr (assoc 5 (entget (entnext))))
  4.           h2 (cdr (assoc 5 (entget e1)))
  5.     )
  6.     (if (/= h1 h2)
  7.         (while
  8.             (not
  9.                 (or (= h1 (setq h2 (hex-- h2)))
  10.                     (and (setq e2 (handent h2)) (entnext e2))
  11.                 )
  12.             )
  13.             (setq e2 nil)
  14.         )
  15.     )
  16.     (while (and e2 (not (eq e1 (entnext e2))))
  17.         (setq e2 (entnext e2))
  18.     )
  19.     e2
  20. )
  21.  
  22. ;; Decrement Hex  -  Lee Mac 2013-11-22
  23. (defun hex-- ( h )
  24.     (   (lambda ( f ) (vl-list->string (reverse (f (reverse (vl-string->list h))))))
  25.         (lambda ( l )
  26.             (cond
  27.                 (   (null l) nil)
  28.                 (   (= 65 (car l)) (cons 57 (cdr l)))
  29.                 (   (= 48 (car l)) (cons 70 (f (cdr l))))
  30.                 (   (cons (1- (car l)) (cdr l)))
  31.             )
  32.         )
  33.     )
  34. )

Thanks for testing Marc  :-)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: (entprev)
« Reply #14 on: November 22, 2013, 09:54:23 AM »
Now I think is OK:

Code: [Select]
(defun c:Test ( )
(setq kkkk (car (entsel)))
(princ "\nkkkk              ") (princ kkkk)
(princ "\nALE_NEntPrevious  ") (princ (entnext (ALE_NEntPrevious kkkk)))
(princ "\nLM_entprev3       ") (princ (entnext (LM_entprev3 kkkk)))
(princ "\nentprev           ") (princ (entnext (entprev kkkk)))
(princ)
)
Code: [Select]
Selezionare oggetto:
kkkk              <Nome entità: 7ffffb081b0>
ALE_NEntPrevious  <Nome entità: 7ffffb081b0>
LM_entprev3       <Nome entità: 7ffffb081b0>
entprev           <Nome entità: 7ffffb080d0>