Author Topic: xref layer name conversion  (Read 10470 times)

0 Members and 1 Guest are viewing this topic.

ronjonp

  • Needs a day job
  • Posts: 7529
xref layer name conversion
« on: March 28, 2006, 10:49:44 AM »
I'm gathering all the layers in a drawing and want to convert the names of xref layers from: dwg|nesteddwg|layname to *|layname. What would be the easiest way to accomplish this?

Thanks,

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Slim©

  • Needs a day job
  • Posts: 6566
  • The Dude Abides...
Re: xref layer name conversion
« Reply #1 on: March 28, 2006, 11:01:09 AM »
Ron, if the XREFs have been bound and exploded, you could try this routine:

Code: [Select]
;REMVLONG.lsp
;
;OBJECTIVE***
;The purpose of this routine is to allow the user to strip
;the lengthy portion of layer name including the "|0|" which
;result from the binding of external reference files.
;
;SPECIAL INSTRUCTIONS***
;1.  Explode all Bound External Reference Files before beginning.
;2.  Due to the nature of this routine, it should be understood
;that the layers are not renamed, therefore, the layer name
;remains.  To remove it, you must DXFOUT your file, recreate
;the file from the DXF and then purge to rid the layers, blocks,
;and linetypes.
;

(Defun c:REMVLONG ()
  (prompt "\nRoutine to remove Bound Layer Names")
  (command "undo" "m")
  (setq c_lyr (getvar "CLAYER"))
  (COLLECT_DATA)
)

(Defun COLLECT_DATA ()
  (setq lyrs (tblnext "LAYER" T))
  (while (/= lyrs nil)
    (setq lyr_nam (cdr (assoc 2 lyrs)))
    (setq lyr_lt (cdr (assoc 6 lyrs)))
    (setq lyr_clr (cdr (assoc 62 lyrs)))
    (if (= (wcmatch lyr_nam "*$?$*") T)
      (progn
        (setq ss_dat (ssget "X" (list (cons 8 lyr_nam))))
        (EVAL_LYR)
        (EVAL_LT)
        (if (= (tblsearch "LAYER" lyr) nil)
          (progn
            (if (= (wcmatch lyr_lt "*$?$*") T)
              (command "layer" "m" lyr "c" lyr_clr "" "lt" lt "" "")
              (command "layer" "m" lyr "c" lyr_clr "" "lt" lyr_lt "" "")
            )
          )
        )
        (if (/= ss_dat nil)
          (command "change" ss_dat "" "p" "la" lyr "")
        )
      )
    )
    (setq lyrs (tblnext "LAYER"))
  )
  (setvar "CLAYER" c_lyr)
  (if (= (substr (getvar "acadver") 1 2) "13")
    (repeat 3
      (command "purge" "b" )
      (while (= (getvar "CMDNAMES") "PURGE")
            (command "y" )
      );end while
    )
  )
)

(Defun EVAL_LYR ()
  (if (= (wcmatch lyr_nam "*$?$*") T)
    (progn
      (setq
        nam_len (strlen lyr_nam)
        cnt 1
        fnd (substr lyr_nam cnt 3)
        fndit (wcmatch fnd "$?$")
      )
      (while (/= fndit T)
        (setq
          cnt (+ 1 cnt)
          fnd (substr lyr_nam cnt 3)
          fndit (wcmatch fnd "$?$")
        )
      )
      (setq lyr (substr lyr_nam (+ cnt 3)))
    )
  )
)

(Defun EVAL_LT ()
  (if (= (wcmatch lyr_lt "*$?$*") T)
    (progn
      (setq
        lt_len (strlen lyr_lt)
        cnt 1
        fnd (substr lyr_lt cnt 3)
        fndit (wcmatch fnd "$?$")
      )
      (while (/= fndit T)
        (setq
          cnt (+ 1 cnt)
          fnd (substr lyr_lt cnt 3)
          fndit (wcmatch fnd "$?$")
        )
      )
      (setq lt (substr lyr_lt (+ cnt 3)))
      (setq is_lt (ssget "X" (list (cons 6 lyr_lt))))
      (if (= is_lt nil)
        (progn
          (command "filedia" "0")
          (command "linetype" "l" lt "" "" "")
          (command "filedia" "1")
        )
      )
    )
  )
)

(prompt "\nCommand Name is Remvlong\n")
I drink beer and I know things....

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #2 on: March 28, 2006, 11:21:45 AM »
Thanks for the reply Slim....I'll try and digest what you've posted :).

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CADaver

  • Guest
Re: xref layer name conversion
« Reply #3 on: March 28, 2006, 12:52:32 PM »
Have you tried the RENAME command?

Slim©

  • Needs a day job
  • Posts: 6566
  • The Dude Abides...
Re: xref layer name conversion
« Reply #4 on: March 28, 2006, 01:19:03 PM »
The RENAME command works well too, it uses wildcards & such.
I drink beer and I know things....

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #5 on: March 28, 2006, 01:27:52 PM »
OK...so I've taken this as a learning experience and wanted to try my hand at a function......this is what I came up with:

Code: [Select]
(defun striptext (lyr_nam / pstn X)
  (if (= (wcmatch lyr_nam "*|*") T)
    (progn
      (setq X T)
      (while (= X T)
(setq
  pstn   (+ (vl-string-position (ascii "|") lyr_nam) 2)
  lyr_nam (substr lyr_nam pstn)
)
(if (= (wcmatch lyr_nam "*|*") T)
  (setq X T)
  (setq X nil)
)
      )
      lyr_nam
    )
  )
)
(striptext "123|456|789")
"789"

Any suggestions?

Thanks,

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #6 on: March 28, 2006, 01:36:37 PM »
Written and posted for fun --

Code: [Select]
(defun StripXrefPrefix ( string )
    (   (lambda ( position )
            (if position
                (substr string (+ 2 position))
                string
            )
        )
        (vl-string-position 124 string 0 t)
    )   
)

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #7 on: March 28, 2006, 01:49:26 PM »
Written and posted for fun --

Code: [Select]
(defun StripXrefPrefix ( string )
    (   (lambda ( position )
            (if position
                (substr string (+ 2 position))
                string
            )
        )
        (vl-string-position 124 string 0 t)
    )   
)

:)

Show off  :-D just kidding....you make it look so easy.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #8 on: March 28, 2006, 02:04:08 PM »
Show off  :-D just kidding....you make it look so easy.

Just trying to get you to look at the problem / solution another way .. and have some fun at the same time .. honest.

If you find the lambda stuff annoying here's a lambdaless equivalent --

Code: [Select]
(defun StripXrefPrefix ( string / position )
    (if (setq position (vl-string-position 124 string 0 t))
        (substr string (+ 2 position))
        string
    )
)

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: xref layer name conversion
« Reply #9 on: March 28, 2006, 02:04:47 PM »
Doesn't he though. :-)
Here with lists:
Code: [Select]
(defun stripxrefprefix (string / result lst)
  (setq result (vl-string->list string))
  (while (setq lst (cdr (member (ascii "|") result)))
    (setq result lst)
  )
  (vl-list->string result)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #10 on: March 28, 2006, 02:08:10 PM »
Thanks for the examples :). You know it took me two hours to write mine?  :oops::pissed:

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #11 on: March 28, 2006, 02:13:22 PM »
Thanks for the examples :). You know it took me two hours to write mine?  :oops::pissed:

♪ Fret not, we all start from the same point ♪

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

T.Willey

  • Needs a day job
  • Posts: 5251
Re: xref layer name conversion
« Reply #12 on: March 28, 2006, 02:26:44 PM »
Here is my recursive approach.
Code: [Select]
(defun StripString (String Delim / Pos)

(if (setq Pos (vl-string-search Delim String))
 (StripString (substr String (1+ (+ Pos (strlen Delim)))) Delim)
 String
)
)
Quote
Command: (StripString "123|456|768|596" "|")
"596"
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #13 on: March 28, 2006, 03:11:48 PM »
For fun I benched all the variants --

Code: [Select]
Using testString = "123" ...

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

    (MP_STRIPXREFPREFIX TESTSTRING)......1156 / 1.05 <fastest>
    (TW_STRIPSTRING TESTSTRING "|")......1203 / 1.01
    (RP_STRIPTEXT TESTSTRING)............1218 / 1.00
    (CAB_STRIPXREFPREFIX TESTSTRING).....1219 / 1.00 <slowest>

Using testString = "123|456" ...

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

    (MP_STRIPXREFPREFIX TESTSTRING)......1265 / 1.14 <fastest>
    (CAB_STRIPXREFPREFIX TESTSTRING).....1313 / 1.10
    (RP_STRIPTEXT TESTSTRING)............1359 / 1.06
    (TW_STRIPSTRING TESTSTRING "|")......1438 / 1.00 <slowest>

Using testString = "123|456|789" ...

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

    (MP_STRIPXREFPREFIX TESTSTRING)......1266 / 1.15 <fastest>
    (CAB_STRIPXREFPREFIX TESTSTRING).....1359 / 1.07
    (RP_STRIPTEXT TESTSTRING)............1438 / 1.01
    (TW_STRIPSTRING TESTSTRING "|")......1453 / 1.00 <slowest>

Using testString = "123|456|789|ABC" ...

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

    (MP_STRIPXREFPREFIX TESTSTRING)......1266 / 1.21 <fastest>
    (CAB_STRIPXREFPREFIX TESTSTRING).....1390 / 1.10
    (RP_STRIPTEXT TESTSTRING)............1515 / 1.01
    (TW_STRIPSTRING TESTSTRING "|")......1531 / 1.00 <slowest>

Using testString = "123|456|789|ABC|DEF" ...

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

    (MP_STRIPXREFPREFIX TESTSTRING)......1281 / 1.28 <fastest>
    (CAB_STRIPXREFPREFIX TESTSTRING).....1437 / 1.14
    (RP_STRIPTEXT TESTSTRING)............1594 / 1.03
    (TW_STRIPSTRING TESTSTRING "|")......1640 / 1.00 <slowest>

Using testString = "123|456|789|ABC|DEF|GHI|JKL|MNO|PQR" ...

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

    (MP_STRIPXREFPREFIX TESTSTRING)......1265 / 1.57 <fastest>
    (CAB_STRIPXREFPREFIX TESTSTRING).....1734 / 1.14
    (RP_STRIPTEXT TESTSTRING)............1906 / 1.04
    (TW_STRIPSTRING TESTSTRING "|")......1984 / 1.00 <slowest>

Here's the bench code --

Code: [Select]
(   (lambda

        (
            /
            TW_StripString
            CAB_stripxrefprefix
            MP_StripXrefPrefix
            RP_StripText
           
        )


        (defun TW_StripString (String Delim / Pos)
            (if (setq Pos (vl-string-search Delim String))
                (TW_StripString
                    (substr String (1+ (+ Pos (strlen Delim))))
                    Delim
                )
                String
            )
        )
       
        (defun CAB_stripxrefprefix (string / result lst)
            (setq result (vl-string->list string))
            (while (setq lst (cdr (member (ascii "|") result)))
                (setq result lst)
            )
            (vl-list->string result)
        )       
       
        (defun MP_StripXrefPrefix ( string / position )
            (if (setq position (vl-string-position 124 string 0 t))
                (substr string (+ 2 position))
                string
            )
        )
       
        (defun RP_StripText (lyr_nam / pstn X)
          (if (= (wcmatch lyr_nam "*|*") T)
            (progn
              (setq X T)
              (while (= X T)
            (setq
              pstn    (+ (vl-string-position (ascii "|") lyr_nam) 2)
              lyr_nam (substr lyr_nam pstn)
            )
            (if (= (wcmatch lyr_nam "*|*") T)
              (setq X T)
              (setq X nil)
            )
              )
              lyr_nam
            )
          )
        )
       
        ;;
        ;; main
        ;;
       
        (foreach testString
       
           '(   "123"
                "123|456"
                "123|456|789"
                "123|456|789|ABC"
                "123|456|789|ABC|DEF"
                "123|456|789|ABC|DEF|GHI|JKL|MNO|PQR"
            )
           
            (princ (strcat "\nUsing testString = \"" testString "\" ...\n\n"))
           
            (BenchMark               
               '(
                    (TW_StripString testString "|")
                    (CAB_stripxrefprefix testString)
                    (MP_StripXrefPrefix testString)
                    (RP_StripText testString)
                )
            )
        )
    )   
)

PS -- RonJonP -- you may wish to examine your function's result when it is passed a string that doesn't host the dilimiter, e.g. "123".

Cheers.

Michael.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #14 on: March 28, 2006, 03:19:20 PM »
I found that one out. My workaround was to enclose my function in an if statement.

Someday I'll write pretty code.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

T.Willey

  • Needs a day job
  • Posts: 5251
Re: xref layer name conversion
« Reply #15 on: March 28, 2006, 03:26:13 PM »
For fun I benched all the variants --

Woooowhoooooo!!!

Mine was the slowest, but it was still something I just learned, so I thought it would be fun to post it.

Is this typical of recursive programs?  or was this a bad example?  Sorry for the thread hijacking.. :cry:
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #16 on: March 28, 2006, 06:29:07 PM »
Edit: Fixed typo in MP_StripPrefixRecEx.

As I noted in another thread, recursive functions typically represent convenience for the programmer, but more work for the computer.

Optimizing recursive functions is a big topic Tim, and not one I'm particularly qualified to author, but I would say that when writing recursive functions some of the things you have to do are eliminate redundant function calls and variable declarations.

For example -- pretending that vl-string-position doesn't have the optional "start from the end" argument let's write a recursive variant that employs a common technique: write core (recursive) functionality as a function that is initially invoked via a wrapper --

Code: [Select]
(defun MP_StripPrefixRecEx ( string code )

    ;;  Not to be called directly, only via wrapper
    ;;  function MP_StripPrefixRec.
    ;;
    ;;  Uses argument 'code' which is calculated
    ;;  using the ascii function only once, by the
    ;;  calling / parent code.
    ;;
    ;;  Uses global scope variable 'position' declared
    ;;  by the calling code (or fully global if the
    ;;  parent code does not declare said variable),
    ;;  so it doen't have to push more onto the stack
    ;;  with each successive iteration than is absolutely
    ;;  esssential.

    (if (setq position (vl-string-position code string))
        (MP_StripPrefixRecEx
            (substr string (+ 2 position))
            code
        )
        String
    )
)

Code: [Select]
(defun MP_StripPrefixRec ( string delim / position )

    ;;  I am the wrapper code, responsible for doing any
    ;;  preprocessing, e.g. the (ascii delim) call as well
    ;;  as declaring any variables that are used in the
    ;;  MP_StripPrefixRecEx function, like the 'position'
    ;;  variable.
    ;;
    ;;  Generally speaking this is bad, bad coding practice as
    ;;  it requires one function to have intimate knowledge
    ;;  of the inner workings of another function, breaking
    ;;  black box rules and making for all kinds of potential
    ;;  maintenance woes. In this case I'm saying "it's ok"
    ;;  because this function is written entirely in the
    ;;  context of the other -- the two work together.

    (MP_StripPrefixRecEx string (ascii delim))

)

Having spent the time to optimize a little bit, the actual benefits may not be realized until the recurse levels get a little deep, as the following illuminates --

Code: [Select]
Using testString = "123" ...

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

    (TW_STRIPSTRING TESTSTRING "|")........1281 / 1.04 <fastest>
    (MP_STRIPPREFIXREC TESTSTRING "|").....1328 / 1.00 <slowest>

Using testString = "123|456" ...

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

    (MP_STRIPPREFIXREC TESTSTRING "|").....1469 / 1.02 <fastest>
    (TW_STRIPSTRING TESTSTRING "|")........1500 / 1.00 <slowest>

Using testString = "123|456|789" ...

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

    (MP_STRIPPREFIXREC TESTSTRING "|").....1562 / 1.02 <fastest>
    (TW_STRIPSTRING TESTSTRING "|")........1593 / 1.00 <slowest>

Using testString = "123|456|789|ABC" ...

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

    (MP_STRIPPREFIXREC TESTSTRING "|").....1562 / 1.06 <fastest>
    (TW_STRIPSTRING TESTSTRING "|")........1656 / 1.00 <slowest>

Using testString = "123|456|789|ABC|DEF" ...

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

    (MP_STRIPPREFIXREC TESTSTRING "|").....1593 / 1.12 <fastest>
    (TW_STRIPSTRING TESTSTRING "|")........1781 / 1.00 <slowest>

Using testString = "123|456|789|ABC|DEF|GHI|JKL|MNO|PQR" ...

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

    (MP_STRIPPREFIXREC TESTSTRING "|").....1781 / 1.18 <fastest>
    (TW_STRIPSTRING TESTSTRING "|")........2093 / 1.00 <slowest>

Sorry, that's all I've got for now.
« Last Edit: March 28, 2006, 06:38:29 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

T.Willey

  • Needs a day job
  • Posts: 5251
Re: xref layer name conversion
« Reply #17 on: March 28, 2006, 06:43:01 PM »
Thanks Michael.  Something (more) to study.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Slim©

  • Needs a day job
  • Posts: 6566
  • The Dude Abides...
Re: xref layer name conversion
« Reply #18 on: March 28, 2006, 06:47:29 PM »
Sorry for the thread hijacking.. :cry:

Normal around here...

Ron, did you get the help you needed?  :-)
I drink beer and I know things....

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #19 on: March 28, 2006, 06:57:22 PM »
Oops, <big> sorry Ron.

Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #20 on: March 28, 2006, 07:38:41 PM »
Sorry for the thread hijacking.. :cry:

Normal around here...

Ron, did you get the help you needed?  :-)

I got all the help I needed and more....par usual. Thanks guys :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #21 on: May 08, 2006, 05:23:10 PM »
Show off  :-D just kidding....you make it look so easy.

Just trying to get you to look at the problem / solution another way .. and have some fun at the same time .. honest.

If you find the lambda stuff annoying here's a lambdaless equivalent --

Code: [Select]
(defun StripXrefPrefix ( string / position )
    (if (setq position (vl-string-position 124 string 0 t))
        (substr string (+ 2 position))
        string
    )
)

:)

MP,

How could this be modified to strip the string down to thesecond to last bar -->>

ABC|123|456|789 to 456|789

Thanks,

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #22 on: May 08, 2006, 05:46:03 PM »
Not sure I understand. Could you please show original data and desired data, for example --

Original: "ABC|123|456|789 to 456|789"

Desired: "789 to 456|789"

Thanks.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #23 on: May 08, 2006, 06:46:55 PM »
MP,

Does this help?

Original: "ABC|123|456|789"

Desired: "456|789

Original: "098|ABC|123|456|789"

Desired: "456|789

Instead of stripping out all of the "|" it leaves the last one and the text up to the second to last.

Thanks,

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #24 on: May 08, 2006, 07:48:49 PM »
Edit: Simplified / renamed functions, re-ordered post to make comparison easier to discern.

Apologies -- written very quick, very dirty.

I don't really know what to call these functions but figured the candidates are better than foo1 and foo2.

Nonetheless, observe --

Code: [Select]
(defun KeepNthPlus ( n string delim / i code indexes )
    ;;  n used in same context as nth function
    ;;  delim is a one character string
    (setq i 0 code (ascii delim))
    (while (setq i (vl-string-position code string i))
        (setq indexes (cons (setq i (1+ i)) indexes))
    )
    (cond
        (   (null indexes) nil)
        (   (< n (length indexes))
            (substr string (1+ (nth n (reverse indexes))))
        )
    )
)

Versus --   
           
Code: [Select]
(defun KeepReversedNthPlus ( n string delim / i code indexes )
    ;;  n used in same context as nth function
    ;;  but counting from the end forwards
    ;;  delim is a one character string
    (setq i 0 code (ascii delim))
    (while (setq i (vl-string-position code string i))
        (setq indexes (cons (setq i (1+ i)) indexes))
    )
    (cond
        (   (null indexes) nil)
        (   (< n (length indexes))
            (substr string (1+ (nth n indexes)))
        )
        (   string   )
    )
)

Set up some test data --

Code: [Select]
(setq testString "abc|def|ghi|jkl|mno")
Test KeepNthPlus --

Code: [Select]
(foreach i '(0 1 2 3 4)
    (princ
        (strcat
            "(KeepNthPlus "
            (itoa i)
            " \""
            testString
            "\" \"|\") => "
            (vl-prin1-to-string
                (KeepNthPlus i testString "|")
            )
            "\n"
        )
    )
    (princ)
)

Test KeepReversedNthPlus --

Code: [Select]
(foreach i '(0 1 2 3 4)
    (princ
        (strcat
            "(KeepReversedNthPlus "
            (itoa i)
            " \""
            testString
            "\" \"|\") => "
            (vl-prin1-to-string
                (KeepReversedNthPlus i testString "|")
            )
            "\n"
        )
    )
    (princ)
)

Test KeepNthPlus results --

Quote
(KeepNthPlus 0 "abc|def|ghi|jkl|mno" "|") => "def|ghi|jkl|mno"
(KeepNthPlus 1 "abc|def|ghi|jkl|mno" "|") => "ghi|jkl|mno"
(KeepNthPlus 2 "abc|def|ghi|jkl|mno" "|") => "jkl|mno"
(KeepNthPlus 3 "abc|def|ghi|jkl|mno" "|") => "mno"
(KeepNthPlus 4 "abc|def|ghi|jkl|mno" "|") => nil

Test KeepReversedNthPlus results --

Quote
(KeepReversedNthPlus 0 "abc|def|ghi|jkl|mno" "|") => "mno"
(KeepReversedNthPlus 1 "abc|def|ghi|jkl|mno" "|") => "jkl|mno"
(KeepReversedNthPlus 2 "abc|def|ghi|jkl|mno" "|") => "ghi|jkl|mno"
(KeepReversedNthPlus 3 "abc|def|ghi|jkl|mno" "|") => "def|ghi|jkl|mno"
(KeepReversedNthPlus 4 "abc|def|ghi|jkl|mno" "|") => "abc|def|ghi|jkl|mno"

Something to toy with, cheers.
« Last Edit: May 09, 2006, 08:06:15 AM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #25 on: May 09, 2006, 09:14:03 AM »
MP,

Your knowledge of writing code never ceases to amaze me. Whether it be simple or complicated you always find an elegant way to solve a problem :). This is exactly what I was looking for.

Thanks again,

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: xref layer name conversion
« Reply #26 on: May 09, 2006, 12:27:14 PM »
Awefully nice of you to say Ron, that is the illusion I'm shooting for -- Thanks!

 :lol:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7529
Re: xref layer name conversion
« Reply #27 on: May 09, 2006, 02:38:33 PM »
Awefully nice of you to say Ron, that is the illusion I'm shooting for -- Thanks!

 :lol:


 :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC