Author Topic: need some suggestions on converting string to list  (Read 4726 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
Re: need some suggestions on converting string to list
« Reply #15 on: April 09, 2012, 09:38:59 AM »
now its not working

tried on a new dwg
PLEASE PICK DESIRED TEXT; error: bad argument type: VLA-OBJECT nil

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: need some suggestions on converting string to list
« Reply #16 on: April 09, 2012, 10:31:12 AM »
it seems that if i use your search function from the link you gave, i need the rest of the functions also for it to work? it keeps asking for the other functions of your code to use the search function... or am i missing something in the translation?
Yes you are correct, you need an absolute minimum of the RX:Search, RX:Start & RX:Set functions to be loaded in order to use my regular expression library. Fore ease, here's the 3 defuns:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. ;;; -------------------------------------------------------------------------------------
  3. ;;; Get a reference to the Regular Expression Object object
  4. ;;; -------------------------------------------------------------------------------------
  5. ;;; Result: a vla object for the regular expression tools
  6. ;;; -------------------------------------------------------------------------------------
  7. (defun RX:Start (/)
  8.   (if (or (not *RX-Link*)
  9.           (not (setq *RX-Link* (vl-bb-ref '*RX-Link*)))
  10.           (vlax-object-released-p *RX-Link*)
  11.       ) ;_ end of or
  12.     (progn
  13.       (vl-bb-set '*RX-Link* (setq *RX-Link* (vlax-get-or-create-object "VBScript.RegExp")))
  14.       (RX:Set t t t)
  15.     ) ;_ end of progn
  16.   ) ;_ end of if
  17.   *RX-Link*
  18. ) ;_ end of defun
  19.  
  20. ;;; -------------------------------------------------------------------------------------
  21. ;;; Change settings for regexp
  22. ;;; -------------------------------------------------------------------------------------
  23. ;;; Arguments:
  24. ;;; multiline  = nil -> Stop- / t -> Don't stop at line ends
  25. ;;; global     = nil -> only find 1st occurence; t -> find all occurences
  26. ;;; ignorecase = nil -> case sensitive; t -> case insensitive
  27. ;;; -------------------------------------------------------------------------------------
  28. ;;; Result: undefined
  29. ;;; -------------------------------------------------------------------------------------
  30. (defun RX:Set (multiline global ignorecase)
  31.   (if *RX-Link*
  32.     (progn
  33.       (vlax-put *RX-Link*
  34.                 'Multiline
  35.                 (if multiline
  36.                   -1
  37.                   0
  38.                 ) ;_ end of if
  39.       ) ;_ end of vlax-put
  40.       (vlax-put *RX-Link*
  41.                 'Global
  42.                 (if global
  43.                   -1
  44.                   0
  45.                 ) ;_ end of if
  46.       ) ;_ end of vlax-put
  47.       (vlax-put *RX-Link*
  48.                 'IgnoreCase
  49.                 (if ignorecase
  50.                   -1
  51.                   0
  52.                 ) ;_ end of if
  53.       ) ;_ end of vlax-put
  54.     ) ;_ end of progn
  55.   ) ;_ end of if
  56. ) ;_ end of defun
  57.  
  58. ;;; -------------------------------------------------------------------------------------
  59. ;;; Find positions matching the pattern in the string
  60. ;;; -------------------------------------------------------------------------------------
  61. ;;; Arguments:
  62. ;;; string      = The string to search through
  63. ;;; pattern     = The regular expression patter to search for
  64. ;;; -------------------------------------------------------------------------------------
  65. ;;; Result: A list containing dotted pairs of (Position . Value)
  66. ;;; -------------------------------------------------------------------------------------
  67. ;;; Example
  68. ;;; (RX:Search "\"Test,6\",34,45.6,\"48\"" "(\\s*\"[^\"]*\"\s*,)|(\\s*[^,]*\\s*,)")
  69. ;;; Will return a list containing the positions & values after parsing the CVS string.
  70. ;;; ((0 . "\"Test,6\",") (9 . "34,") (12 . "45.6,"))
  71. ;;; -------------------------------------------------------------------------------------
  72. (defun RX:Search (string pattern / lst col item)
  73.   (if (RX:Start)
  74.     (progn
  75.       (vlax-Put *RX-Link* 'Pattern pattern) ;Set the pattern to match
  76.       ;; Get the collection of match objects
  77.       (setq col (vl-catch-all-apply 'vlax-Invoke (list *RX-Link* 'Execute string)))
  78.       (if (not (vl-catch-all-error-p col))
  79.         (progn
  80.           ;; Loop through collection & place into return list
  81.           (vlax-for item col
  82.             ;; Get the position & length
  83.             (setq lst (cons (cons (vlax-Get item 'FirstIndex) (vlax-Get item 'Value)) lst))
  84.             (vlax-release-object item) ;Release the match object
  85.           ) ;_ end of while
  86.           (vlax-release-object col) ;Release the collection object
  87.         ) ;_ end of progn
  88.       ) ;_ end of if
  89.       (reverse lst) ;Return list in correct order
  90.     ) ;_ end of progn
  91.   ) ;_ end of if
  92. ) ;_ end of defun

As for the VLA-OBJECT nil error, ensure that vl-load-com is called at least once somewhere in your code. The Regular Expression functions uses the VBScripting RegExp ActiveX library - and you can only use that if the visual lisp com interface is loaded.

It seems to work fine for me with this code:
Code - Auto/Visual Lisp: [Select]
  1. (defun AddMetric (str /)
  2.   (foreach val (reverse (RX:Search str "((\\d+'[ -]\\d \\d+/\\d+\")|(\\d+'[ -]\\d\")|(\\d+')|(\\d+ \\d+/\\d+\")|(\\d+/\\d+\")|(\\d+\"))"))
  3.     (setq str (vl-string-subst (strcat (cdr val) " (" (rtos (cvunit (distof (cdr val) 4) "inch" "mm") 2 0) "mm)") (cdr val) str (car val))))
  4.   str)
  5.  
  6. (defun c:AddMetric (/ ss str)
  7.   (if (and (ssget '((0 . "TEXT,MTEXT")))
  8.     (progn
  9.       (vlax-for eo ss
  10.         (setq str (AddMetric (vla-get-TextString eo)))
  11.         (if (not (eq str (vla-get-TextString eo)))
  12.           (vla-put-TextString eo str)))
  13.       (vla-Delete ss)))
  14.   (progn))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

andrew_nao

  • Guest
Re: need some suggestions on converting string to list
« Reply #17 on: April 09, 2012, 02:47:37 PM »
got it working, i was missing the vl-load-com

super thanks irneb, much appreciated

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: need some suggestions on converting string to list
« Reply #18 on: April 09, 2012, 11:33:35 PM »
Glad you got it working finally.  ;D
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

andrew_nao

  • Guest
Re: need some suggestions on converting string to list
« Reply #19 on: April 10, 2012, 12:13:43 PM »
ive spoken to soon again


example:
EL. 123'-10"
results in
EL. 123' (37490mm)-10" (254mm)

would this have something to do with the rs:search function inside the addmetric function?


fixed... had to add this in the search
(\\d+'[- ]\\d\\d+ \\d+/\\d+\")|(\\d+'[- ]\\d\\d+\")
« Last Edit: April 10, 2012, 02:37:52 PM by andrew_nao »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: need some suggestions on converting string to list
« Reply #20 on: April 11, 2012, 01:04:48 AM »
Awesome! Glad you could figure out those RegExp codes!

Though my RegExp.LSP contains a comment at the end describing the special characters, I find it useful to search through these 2 sites for some tricks:
http://www.regular-expressions.info/examples.html
http://regexlib.com/?AspxAutoDetectCookieSupport=1
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.