TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Mark on January 16, 2008, 12:03:57 PM

Title: Find and Replace layer attributes
Post by: Mark on January 16, 2008, 12:03:57 PM
What I'm thinking about is a type of find and replace for layer names or even linetypes, colors etc., only do it from a simple text file some how. For example. You export your current layer configuration to a text file. Pop it open in your favorite text editor and make your changes. Then bring it back into acad. The program reads the edited version and makes the appropriate changes. Or would this be easier to accomplish within the program itself? The only problem I see is lack of regex support from within autolisp.

Thoughts?
Title: Re: Find and Replace layer attributes
Post by: T.Willey on January 16, 2008, 12:16:11 PM
I have one that works with ObjectDBX already.  I think it is posted here.  Be right back.

Edit: Found it. (http://www.theswamp.org/index.php?topic=12845.msg156899#msg156899)
Title: Re: Find and Replace layer attributes
Post by: daron on January 16, 2008, 02:12:15 PM
I tend to wonder if there would be a way to emulate regex through lisp in order to gain that support? That might be a good challenge.
Title: Re: Find and Replace layer attributes
Post by: Mark on January 16, 2008, 02:26:07 PM
I have one that works with ObjectDBX already.  I think it is posted here.  Be right back.

Edit: Found it. (http://www.theswamp.org/index.php?topic=12845.msg156899#msg156899)

Thanks Tim, I'll give it a look-see.
Title: Re: Find and Replace layer attributes
Post by: MP on January 16, 2008, 02:37:11 PM
I tend to wonder if there would be a way to emulate regex through lisp in order to gain that support? That might be a good challenge.

Why emulate regex? Took less than 10 minutes to translate (admittedly quick and dirty, but still) ...

Code: [Select]
(defun c:RegExExample ( / objRegEx strSearchString colMatches )

[color=green]    ;;  see http://www.microsoft.com/technet/scriptcenter/
    ;;           resources/qanda/sept06/hey0920.mspx
    ;;
    ;;  Set objRegEx = CreateObject("VBScript.RegExp")
    ;; 
    ;;  objRegEx.Global = True   
    ;;  objRegEx.Pattern = "[^A-Z0-9]"
    ;; 
    ;;  strSearchString = "Ken Myer"
    ;; 
    ;;  Set colMatches = objRegEx.Execute(strSearchString) 
    ;; 
    ;;  If colMatches.Count > 0 Then
    ;;      Wscript.Echo "The following characters are not allowed:"
    ;;      For Each strMatch in colMatches   
    ;;          Wscript.Echo  strMatch.Value
    ;;      Next
    ;;  End If[/color]

    (setq objRegEx (vlax-create-object "VBScript.RegExp"))
   
    (vlax-put objRegEx 'Global -1)
    (vlax-put objRegEx 'Pattern "[^A-Z0-9]")
   
    (setq
        strSearchString "Ken Myer"
        colMatches      (vlax-invoke objRegEx 'Execute strSearchString)
    )
   
    (if (< 0 (vlax-get colMatches 'Count))
   
        (progn
   
            (princ "The following characters are not allowed:\n")
   
            (vlax-for strMatch colMatches   
                (print (vlax-get strMatch 'Value))
            )
        )
    )
   
    (vlax-release-object colMatches)
    (vlax-release-object objRegEx)

    (princ)   

)

:)
Title: Re: Find and Replace layer attributes
Post by: JohnK on January 16, 2008, 02:43:31 PM
I can look around my stuff too if you need Mark.
Title: Re: Find and Replace layer attributes
Post by: daron on January 16, 2008, 04:10:56 PM
Nice MP.
Title: Re: Find and Replace layer attributes
Post by: CAB on January 16, 2008, 04:28:46 PM
Can't you just do it in LISP or did I miss something, again. :-)

Code: [Select]
;;  returns t if str has a bad character
(defun badchar (str)
  (vl-some '(lambda (x) (not (or (< 47 x 58) (< 64 x 91))))
           (vl-string->list str))
)


Code: [Select]
;;  returns a list of bad characters
(defun badchar (str / bad)
  (mapcar '(lambda (x)
     (if (not (or (< 47 x 58) (< 64 x 91)))
                (setq bad (cons x bad))))
    (vl-string->list str))
  (mapcar 'chr bad)
)


Code: [Select]
(defun c:test (/ Rejects)
  (if (setq Rejects (badchar "ABCDzXYZ,019"))
    (mapcar '(lambda(x)(print x)) Rejects)
  )
  (princ)
)
Title: Re: Find and Replace layer attributes
Post by: Mark on January 16, 2008, 04:51:35 PM
Can't you just do it in LISP or did I miss something, again. :-)

Don't think so! :-)

Example: ( using regex )
Code: [Select]
cat layers ( FROM THIS )
C-ValveWater_ep
C-MeterWater_ep
C-BoxWater_ep
C-AnnoWater_ep

sed -R "s/^(C)-(.*)(Water)(_ep)/\1U-\3-\2-UNDR/" layers ( TO THIS )
CU-Water-Valve-UNDR
CU-Water-Meter-UNDR
CU-Water-Box-UNDR
CU-Water-Anno-UNDR
Title: Re: Find and Replace layer attributes
Post by: CAB on January 16, 2008, 05:30:03 PM
Never mind :oops: