Author Topic: rename layer?  (Read 5062 times)

0 Members and 1 Guest are viewing this topic.

daron

  • Guest
rename layer?
« on: August 21, 2006, 11:04:55 AM »
Was wondering if anyone has a routine already on hand that they could throw out here. I have a drawing with xref's that have been binded. So now it has layers named "old drawing$0$layer name". Does anyone have a routine that will get rid of the prefix, leaving just the layer name? If not, don't worry about it.

Thanks

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: rename layer?
« Reply #1 on: August 21, 2006, 11:13:54 AM »
It's old but it still may work => link.

Look for the 'topmost' version of BindFix15.lsp.

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

daron

  • Guest
Re: rename layer?
« Reply #2 on: August 21, 2006, 11:56:39 AM »
Thanks Michael. That worked sweet.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: rename layer?
« Reply #3 on: August 21, 2006, 08:39:17 PM »
You're very welcome Daron.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Andrea

  • Water Moccasin
  • Posts: 2372
Re: rename layer?
« Reply #4 on: August 22, 2006, 09:14:51 AM »
Code: [Select]
(defun c:Lx ()
(setq las (ai_table "layer" 0))

(foreach n las
  (setq pos1 (vl-string-search "$0$" n))
(if pos1 
      (command "_.-rename" "_LA" n (substr n (+ pos1 4)))
)
(setq pos1 nil)
)
)
Keep smile...

Joe Burke

  • Guest
Re: rename layer?
« Reply #5 on: August 23, 2006, 12:14:51 PM »
This may seem like the long way around. But I assume the veterans
here will appreciate the implications. RegExp:Replace is similar to
(vl-string-subst new-str pattern string [start-pos]). But it allows
global replace and not case sensitive options. Plus wildcard characters.

The downside is understanding the syntax rules of regular expressions.

Code: [Select]
;; demo only
(defun c:StripBlockLayerNames ( / layers)
  (setq layers
    (vla-get-layers
      (vla-get-activedocument
        (vlax-get-acad-object))))
  (vlax-for lay layers
    (vl-catch-all-apply
      '(lambda ( )
        (vlax-put lay 'Name (RegExp:Replace "" ".{1,20}[$0$]" (vlax-get lay 'Name)))
      )
    )
  )
) ;end

;; Thanks to Tony Tanzillo for his example of how to use
;; regular expressions within vlisp. Posted 2004 to the
;; Autodesk cutomization NG. Search for REGEXP.LSP.
(defun RegExp:Replace (new-str pattern string / RegExp result)
  (setq RegExp (vlax-create-object "VBScript.RegExp"))
  ;search pattern
  (vlax-put RegExp 'Pattern pattern)
  ;vlax-true replace all occurances
  ;:vlax-false replace first occurance
  (vlax-put RegExp 'Global :vlax-true)
  ;vlax-true ignore case
  ;vlax-false case sensitive
  (vlax-put RegExp 'IgnoreCase :vlax-true)
  (if
    (vl-catch-all-error-p
      (setq result
        (vl-catch-all-apply
          'vlax-invoke (list RegExp 'Replace string new-str))))
    (progn
      (vlax-release-object RegExp)
      (princ "\nInvalid pattern. ")
      nil  ;return nil
    )
    (progn
      (vlax-release-object RegExp)
      result
    )
  )
) ;end


EDIT: Wrapped your code in [ code ] tags
« Last Edit: August 28, 2006, 07:16:19 AM by nivuahc »

Maverick®

  • Seagull
  • Posts: 14778
Re: rename layer?
« Reply #6 on: August 23, 2006, 01:01:56 PM »
  Great job giving us a workable demonstration of how regular expressions can be used in Lisp! That should make this thread a lot more fun.

  Also a good example of how to attribute credit to an original author.

Thanks!




EDIT: Fixed that link for ya
« Last Edit: August 23, 2006, 01:21:28 PM by nivuahc »

nivuahc

  • Guest
Re: rename layer?
« Reply #7 on: August 23, 2006, 01:05:13 PM »
Well said, Mav :)

LE

  • Guest
Re: rename layer?
« Reply #8 on: August 23, 2006, 01:10:40 PM »
Thanks!

The links is not working Mav.... it is giving the "An Error Has Occurred!" - thanks

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: rename layer?
« Reply #9 on: August 27, 2006, 10:33:10 PM »
That's awesome stuff Joe! I didn't know there was such a thing as "VBScript.RegExp". That's pretty cool. This is available to VBA?
TheSwamp.org  (serving the CAD community since 2003)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: rename layer?
« Reply #10 on: August 28, 2006, 06:56:49 AM »
Not used command...
Code: [Select]
(defun test-1 ()
  (vlax-map-collection
    (vla-get-layers
      (vla-get-ActiveDocument
        (vlax-get-acad-object)
      ) ;_  vla-get-ActiveDocument
    ) ;_  vla-get-layers
    (function
      (lambda (x / a1 a2)
        (if (setq a2 (vl-string-search "$0$" (setq a1 (vla-get-name x))))
          (vla-put-name x (substr a1 (+ a2 4)))
        ) ;_  if
      ) ;_  lambda
    ) ;_  function
  ) ;_  vlax-map-collection
) ;_  defun

(defun test-2 ( / I LA)
  (while (setq la (tblnext "layer" (not la)))
    (if (setq i (vl-string-search "$0$" (cdadr la)))
      (entmod
        (subst
          (cons 2 (substr (cdadr la) (+ i 4)))
          (cadr la)
          (entget (tblobjname "LAYER" (cdadr la)))
        ) ;_  subst
      ) ;_  entmod
    ) ;_  if
  ) ;_  while
  (princ)
) ;_  defun

Joe Burke

  • Guest
Re: rename layer?
« Reply #11 on: August 28, 2006, 08:22:28 AM »
That's awesome stuff Joe! I didn't know there was such a thing as "VBScript.RegExp". That's pretty cool. This is available to VBA?


Hi Mark,

I thought it was pretty interesting too. Also curious, the topic I referred
to seems to be the only mention of regular expressions within vlisp in the entire
Autodesk customization NG.

Here's a link to that topic http://tinyurl.com/f9z4t and Tony's example code
below for anyone interested.

Regarding VBA, sorry, I don't know about that.

There's three methods available.
;; RegExp Methods: Execute, Replace and Test
;;
;; Execute
;; Execute the regular expression search.
;;
;; Replace
;; Replace Pattern in string with a new string.
;;
;; Test
;; Returns boolean if search Pattern occurs in string.

Obviously Tony's code is an example of the Execute method. Which might have been
called the search method. That's how I think of it anyway.

His example was all I had to go on while trying to figure out how to use the
Replace method. It looks obvious in hindsight, but I'm sure you know how frustrating
trial and error can be with this stuff.

Sometime soon I'll post my version of Tony's code named RegExp:Search. I added
some error handling like the vl-catch-all-apply in RegExp:Replace. The main reason
for that is an invalid pattern will toss an "Exception occurred" error. And I still
have a lot to learn about the Pattern argument.

I also have a RegExp:Test function which I'll post.

I hope anyone here who is familiar with regular expressions might share what they
know. I'm fumbling around in the dark. What little I've learned comes from searching
the web. What I found does not apply specifically to their use within vlisp.

Maybe this deserves a separate topic? If you think so, please start one.

Regards

Code: [Select]
;;;;;;;;;;; REGEXP.LSP ;;;;;;;;;;;;;;
;; REGEXP.LSP
;; Copyright (c) 2004, Tony Tanzillo
;; Sample showing how to use RegExp from Visual LISP

;; Comments added JB.

;; Returns:
;;  Matches found: 4
;;  Match[0]: IS1
;;  Match[1]: is2
;;  Match[2]: IS3
;;  Match[3]: is40

(defun RegExpTest ( / RegExp cnt match result i)
   (setq RegExp (vlax-create-object "VBScript.RegExp"))
   ;search pattern
   (vlax-put-property RegExp 'Pattern "is.")
   ;search for all occurances or only first match
   (vlax-put-property RegExp 'Global :vlax-true)
   ;search is not case sensitive
   (vlax-put-property RegExp 'IgnoreCase :vlax-true)
   (setq result
      (vlax-invoke-method RegExp 'Execute "IS1 is2 IS3 is4")
   )
   (princ
      (strcat
         "\nMatches found: "
         (itoa (setq cnt (vlax-get-property Result 'Count)))
      )
   )
   (setq i 0)
   (repeat cnt
      (setq match (vlax-get-property Result 'Item i))
      (princ
         (strcat
            "\nMatch[" (itoa i) "]: "
            (vlax-get-property Match 'Value)
         )
      )
      (setq i (1+ i))
   )
   (vlax-release-object result)
   (vlax-release-object RegExp)
)
;;;;;;;;;;; REGEXP.LSP ;;;;;;;;;;;;;;

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: rename layer?
« Reply #12 on: August 28, 2006, 08:34:53 AM »
Good catalyst type post Joe. Just to open the umbrella a bit, there's a ton of stuff that can be done via scripting, just one being the regex. Other stuff folks here might be interested in, like wmi, fso, vbscript lang reference etc. A starting point might be here. Cheers. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: rename layer?
« Reply #13 on: August 28, 2006, 09:10:45 AM »
Apologies .... but is the VBSript stuff available via VBA in ACAD?


Never mind .... *grin*


« Last Edit: August 28, 2006, 09:14:41 AM by Mark Thomas »
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: rename layer?
« Reply #14 on: August 28, 2006, 09:47:54 AM »
Yes sir --

Code: [Select]
Function RegExpTest(pattern As String, Text As String) As String

    ''  Note: Add a reference to 'Microsoft VBScript
    ''  Regular Expressions' via Tools -> References

    ''  Create variables
    Dim RegEx   As Object, _
        Matches As Object, _
        Match   As Variant, _
        Result  As String

    ''  Create a regular expression
    Set RegEx = New RegExp
   
    With RegEx
   
        ''  Set pattern.
        .pattern = pattern
       
        ''  Set case insensitivity
        .IgnoreCase = True
       
        '' Set global applicability
        .Global = True
       
        ''  Execute search.
        Set Matches = .Execute(Text)
       
    End With
   
    ''  Iterate Matches collection
    For Each Match In Matches
        Result = Result & "Match found at position "
        Result = Result & Match.FirstIndex & ". Match Value is '"
        Result = Result & Match.Value & "'." & vbCrLf
   Next

   RegExpTest = Result

End Function

Sub TestIt()

    MsgBox (RegExpTest("is.", "IS1 is2 IS3 is4"))

End Sub

PS, just a slightly cleaned version of this, which is what I believe Tony had translated from.

Cheers.

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