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

0 Members and 1 Guest are viewing this topic.

ronjonp

  • Needs a day job
  • Posts: 7526
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: 7526
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: 7526
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: 7526
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: 7526
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: 7526
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