Author Topic: Extract and Use First Group of Number in String  (Read 2511 times)

0 Members and 1 Guest are viewing this topic.

davidratx

  • Guest
Extract and Use First Group of Number in String
« on: August 11, 2015, 08:38:12 AM »
Guys, I am new, this is my first post. I am working with a plugin to CAD for plumbing and duct. I am trying to extract the XYZ coordinates and insert a block at the Endpoint.

Basically, how can I truncate the list of XYZ values in the image and only keep the first?

Below is as far as I've gotten. Any thoughts or directions would be appreciated!

Code: [Select]
(defun c:mapend (/ data ent pt-lst pt-str start vla ctr nme dscr2)
  (svars)
 
  (setvar "osmode" 0)
  (setvar "orthomode" 0)
  (setvar "cmdecho" 0)

   (mapfilter "#39 = 2882" 0 0 1)

  (while
    (setq ent (ssget))
    (setq ctr 0)
      (repeat (sslength ent)
(setq nme (ssname ent ctr))
(setq vla (vlax-ename->vla-object nme))

  ;; Ip Points data exists, and Midpoint is included
        (if (and (setq data (vlax-get-property vla "Points"))
                 (setq start (vl-string-search "ENDPOINTS:"  (strcase data)))
                 (setq svcnm (vlax-get-property vla "ServiceName"))
    )
    (progn
    ;; Set Midpoint (as String)
    (setq pt-str (vl-string-trim "ENDPONITS:" data))

    ----truncate coordinates here----
   
    (alert pt-str)

    ;; Set Midpoint (as List)
    (setq pt-lst2 (read (vl-string-translate "," " " (strcat "(" pt-str ")"))))
    )
       );endif
  (command "_insert" "*N:\\1_CAD Department\\700_CADMEP_BD\\Porter Database\\Templates\\Blocks\\ChangePoint.dwg" pt-lst "" "")

     (setq new (ssget "l"))
     (setq f (open "N:\\1_CAD Department\\700_CADMEP_BD\\tmp.cod" "w"))
     (setq script (strcat "item.service ="  "\""svcnm "\""))         
     (write-line script f)
     (close f)

(executescript "N:\\1_CAD Department\\700_CADMEP_BD\\tmp.cod" new)

      (setq ctr (1+ ctr))
);end repeat
  );end while
  (rvars)
  (princ)
)
;enddefun

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Extract and Use First Group of Number in String
« Reply #1 on: August 11, 2015, 08:53:17 AM »
Assuming the semicolon will always be the delimiter, give this a try:
Code - Auto/Visual Lisp: [Select]
  1. (setq string "4.15,11.86,0.00;4.15yadayada")
  2. (substr string 1 (vl-string-search ";" string))
Welcome the to TheSwamp  :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cgeorg13

  • Guest
Re: Extract and Use First Group of Number in String
« Reply #2 on: August 11, 2015, 08:54:27 AM »
I'm unsure if this would help or not, due to being rather new myself, but this is what I've used for aligning MText along a line/polyline by finding the startpoint and endpoint of a line
Code: [Select]
(setq ent (car (entsel "select object")))
(setq startpoint (vlax-curve-getstartpoint ent))
(setq endpoint (vlax-curve-getendpoint ent))

davidratx

  • Guest
Re: Extract and Use First Group of Number in String
« Reply #3 on: August 11, 2015, 09:22:09 AM »
Assuming the semicolon will always be the delimiter, give this a try:
Code - Auto/Visual Lisp: [Select]
  1. (setq string "4.15,11.86,0.00;4.15yadayada")
  2. (substr string 1 (vl-string-search ";" string))
Welcome the to TheSwamp  :)
Yes, the Semicolon will always be the delimiter. Although this does not seem to be affecting the string.

Code: [Select]
    (setq pt-str (vl-string-trim "ENDPONITS:" data))

    (substr pt-str 1 (vl-string-search ";" pt-str))
       
    (alert pt-str)

davidratx

  • Guest
Re: Extract and Use First Group of Number in String
« Reply #4 on: August 11, 2015, 09:38:38 AM »
Assuming the semicolon will always be the delimiter, give this a try:
Code - Auto/Visual Lisp: [Select]
  1. (setq string "4.15,11.86,0.00;4.15yadayada")
  2. (substr string 1 (vl-string-search ";" string))
Welcome the to TheSwamp  :)
Yes, the Semicolon will always be the delimiter. Although this does not seem to be affecting the string.

Code: [Select]
    (setq pt-str (vl-string-trim "ENDPONITS:" data))

    (substr pt-str 1 (vl-string-search ";" pt-str))
       
    (alert pt-str)

I take that back. Got it to work! Love it! Thanks so much!

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Extract and Use First Group of Number in String
« Reply #5 on: August 11, 2015, 11:18:57 AM »
Assuming the semicolon will always be the delimiter, give this a try:
Code - Auto/Visual Lisp: [Select]
  1. (setq string "4.15,11.86,0.00;4.15yadayada")
  2. (substr string 1 (vl-string-search ";" string))
Welcome the to TheSwamp  :)
Yes, the Semicolon will always be the delimiter. Although this does not seem to be affecting the string.

Code: [Select]
    (setq pt-str (vl-string-trim "ENDPONITS:" data))

    (substr pt-str 1 (vl-string-search ";" pt-str))
       
    (alert pt-str)

I take that back. Got it to work! Love it! Thanks so much!


Glad to help :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

davidratx

  • Guest
Re: Extract and Use First Group of Number in String
« Reply #6 on: August 11, 2015, 04:26:16 PM »
One more thing, how can get rid or replace the [ in the image?

I am trying

Code: [Select]
    (setq dscr-str (substr dscr 1 (vl-string-search "\"" dscr)))
    (setq dscr-str (vl-string-trim "[" dscr-str))

but it does not seem to be affecting it at all.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Extract and Use First Group of Number in String
« Reply #7 on: August 11, 2015, 05:34:37 PM »
(vl-string-trim) trims from the start or end of the string.
Try using (vl-string-subst).

davidratx

  • Guest
Re: Extract and Use First Group of Number in String
« Reply #8 on: August 12, 2015, 08:49:32 AM »
(vl-string-trim) trims from the start or end of the string.
Try using (vl-string-subst).
Thanks Roy, Ill see what I can do with that.