Extract from text string to attribute values.

Extract from text string to attribute values.

Anonymous
Not applicable
3,993 Views
23 Replies
Message 1 of 24

Extract from text string to attribute values.

Anonymous
Not applicable

I am trying to search a single text string and extract values to "paste" into attribute values in a block.

 

Text string example: "PoleNumber=3-146592$Owner=APCO$Attachment1=57'-4"

 

Is it possible to search out PoleNumber=, retrieve the 3-146592 and populate an attribute value for PoleNumber based on each search criteria? As in, then search for Owner= and populate attribute Owner with APCO?

 

I feel like this is possible but have no idea how to begin to achieve this. Any help would be greatly appreciated.

0 Likes
Accepted solutions (2)
3,994 Views
23 Replies
Replies (23)
Message 2 of 24

dlanorh
Advisor
Advisor

Are all the values in the same text string, and is this Text or MText?

 

Do the text values all go into the same block?

 

Is it always the same named block?

 

Is the block already inserted into the drawing or does the lisp need to do this?

 

Do you require the Text/Mtext deleting once the values are transfered?

 

It would be easier if you posted a drawing containing the block and the text string for testing purposes. AutoCAD 2010 for me which would also be accessable for others.

I am not one of the robots you're looking for

0 Likes
Message 3 of 24

Moshe-A
Mentor
Mentor

@Anonymous  hi,

 

check (get-kword-value) function...can you handle attributes setting on your own?

 

moshe

 

 

(defun c:test (/ get-kword-value ; local function
	         data_text)
  
 (defun get-kword-value (text kword / p0 p1 p2)
  (if (and
	(setq p0 (vl-string-search (strcase (strcat "$" kword)) (strcase text)))
        (setq p1 (vl-string-position (ascii "=") (substr text (1+ p0))))
      )	
   (if (not (setq p2 (vl-string-position (ascii "$") (substr text (+ 1 p1 p0)))))
    (substr text (+ 2 p0 p1))
    (substr text (+ 2 p0 p1) (- (+ p0 p1 p2) (+ p0 p1) 1))
   ); if
  ); if
 ); get-kword-value

  
 (setq data_text "$PoleNumber=3-146592$Owner=APCO$Attachment1=57'-4")

 (foreach kword '("PoleNumber" "Owner" "Attachment1")
  (terpri) 
  (princ (get-kword-value data_text kword))
 )
  
 (princ)
); c:test
0 Likes
Message 4 of 24

Anonymous
Not applicable

The values are in one continuous string of text, not Mtext. All values will be transferred into the same block and will be named the same. I had assumed to insert the block and position it near the text and select the origin (text), then select the block for the lisp to fill in the attributes. The text will remain after extraction and need no deletion. I currently don't have the block built, just more in the stages of "is this possible" at the moment.

0 Likes
Message 5 of 24

Moshe-A
Mentor
Mentor

@Anonymous ,

 

Attach the complete solution. i defined a block blktag + 3 attributes:

PoleNumber, Owner, Attachment1

this means the format of the text(s) you select must have these key words otherwise it won't work

 

enjoy

moshe

 

(defun c:test (/ get-kword-value ; local function
	         savAttDia savAttReq ss ename1 elist data_text value)
  
 (defun get-kword-value (text kword / p0 p1 p2)
  (if (and
	(setq p0 (vl-string-search (strcase (strcat "$" kword)) (strcase text)))
        (setq p1 (vl-string-position (ascii "=") (substr text (1+ p0))))
      )	
   (if (not (setq p2 (vl-string-position (ascii "$") (substr text (+ 1 p1 p0)))))
    (substr text (+ 2 p0 p1))
    (substr text (+ 2 p0 p1) (- (+ p0 p1 p2) (+ p0 p1) 1))
   ); if
  ); if
 ); get-kword-value


 (setvar "cmdecho" 0)
 (command "._undo" "_begin")
  
 (if (setq ss (ssget '((0 . "text"))))
  (progn
   (setq savAttDia (getvar "attdia")) 
   (setvar "attdia" 0)
   (setq savAttReq (getvar "attreq"))   
   (setvar "attreq" 0)
   
   (foreach ename0 (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
    (setq elist (entget ename0)) 
    (setq data_text (cdr (assoc '1 elist)))
    (command-s "._insert" "blktag")
    (setq ename1 (entlast))  
    (foreach kword '("PoleNumber" "Owner" "Attachment1")
     (if (/= (setq value (get-kword-value data_text kword)) "")
      (setpropertyvalue ename1 kword value)
     )
    ); foreach
   ); foreach
   
   (setvar "attreq" savAttReq)
   (setvar "attdia" savAttDia) 
  ); progn
 ); if
  
 (command "._undo" "_end")
 (setvar "cmdecho" 0)
  
 (princ)
); c:test

0 Likes
Message 6 of 24

Anonymous
Not applicable

I get ; error: ADS request error once I click on the text string.

0 Likes
Message 7 of 24

Moshe-A
Mentor
Mentor

@Anonymous ,

 

work prefect for me, note that the text value must start with "$PoleNumber=...."

can you post a sample dwg?

 

0 Likes
Message 8 of 24

dlanorh
Advisor
Advisor

It is eminently possible, and would probably be easiest to select the text, then insert the block with the attributes filled. I would suggest setting the attributes to preset value wise.

I am not one of the robots you're looking for

0 Likes
Message 9 of 24

Anonymous
Not applicable

Here is the drawing I'm testing in.

0 Likes
Message 10 of 24

Moshe-A
Mentor
Mentor

@Anonymous ,

 

On message #5 i also attached blktag.dwg did you use it? guess not!

the lisp was design to use this block (only because you did not provide something else before) so you can not expect it will work with POLE_ID_BLOCK?!

 

when prompt for select object(s) you should select only the text than the program will let you insert your block and assign the attributes.

 

i'm sending a fix to support POLE_ID_BLOCK as default block name but if you like another name, just set USERS1 (sysvar) with another default block name.

 

enjoy

moshe

 

(defun c:test (/ get-kword-value ; local function
	         bname savAttDia savAttReq ss ename1 elist data_text value)
  
 (defun get-kword-value (text kword / p0 p1 p2)
  (if (and
	(setq p0 (vl-string-search (strcase (strcat "$" kword)) (strcase text)))
        (setq p1 (vl-string-position (ascii "=") (substr text (1+ p0))))
      )	
   (if (not (setq p2 (vl-string-position (ascii "$") (substr text (+ 1 p1 p0)))))
    (substr text (+ 2 p0 p1))
    (substr text (+ 2 p0 p1) (- (+ p0 p1 p2) (+ p0 p1) 1))
   ); if
  ); if
 ); get-kword-value

 (setvar "cmdecho" 0)
 (command "._undo" "_begin")

 ; initialize 
 (if (eq (getvar "users1") "")
  (setq bname (setvar "users1" "pole_id_block"))
  (setq bname (getvar "users1"))
 )
  
 (if (setq ss (ssget '((0 . "text"))))
  (progn
   (setq savAttDia (getvar "attdia")) 
   (setvar "attdia" 0)
   (setq savAttReq (getvar "attreq"))   
   (setvar "attreq" 0)
   
   (foreach ename0 (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
    (setq elist (entget ename0)) 
    (setq data_text (cdr (assoc '1 elist)))
    (command-s "._insert" bname)
    (setq ename1 (entlast))  
    (foreach kword '("PoleNumber" "Owner" "Attachment1")
     (if (/= (setq value (get-kword-value data_text kword)) "")
      (setpropertyvalue ename1 kword value)
     )
    ); foreach
   ); foreach
   
   (setvar "attreq" savAttReq)
   (setvar "attdia" savAttDia) 
  ); progn
 ); if
  
 (command "._undo" "_end")
 (setvar "cmdecho" 0)
  
 (princ)
); c:test

 



0 Likes
Message 11 of 24

Anonymous
Not applicable

I must have overlooked where you had attached that file. I see it now. I did however replace in the lisp the POLE_ID_BLOCK instead of where you had blktag listed. Granted there could've been somewhere else that I missed something when I replaced it. And when I tested it I did only select the text first and it would give me the error before it got to the point of inserting the block.

 

Let me check this one out and get back with you.

0 Likes
Message 12 of 24

Moshe-A
Mentor
Mentor

@Anonymous ,

 

of course there is a possibility that you insert POLE_ID_BLOCK in advance and let lisp to select text + blockref

 

from where this text is coming? (you do not add it manually?)

does the key words (PoleNumber,Owner,Attachment1) fixed? only what comes after equality sign varies?

 

moshe

 

0 Likes
Message 13 of 24

Anonymous
Not applicable

The text is being imported from a GPS Unit. Shots are taken with these "attributes" filled out and the only way to bring them in to Autocad is a continuous text string per shot. So each shot will contain a string of text that has to same fields, i.e. PoleNumber, Owner, Attachment1 as well as multiple other entries. The input after "=" is the information entered from the field. So basically trying to take the raw data and make it more presentable in Autocad.

0 Likes
Message 14 of 24

Moshe-A
Mentor
Mentor

@Anonymous ,

 

OK, please close this thread and mark this as your solution.

 

Moshe

 

0 Likes
Message 15 of 24

Moshe-A
Mentor
Mentor

@Anonymous ,

 

mark this as your solution please

 

 

 

0 Likes
Message 16 of 24

Anonymous
Not applicable

I can't mark this as solution when it's not my solution. I still receive "; error: ADS request error" as I have every time I've tried any of these routines, regardless of what I do.

0 Likes
Message 17 of 24

Anonymous
Not applicable

Now I'm getting "Point or option keyword required. ; error: Function cancelled"

0 Likes
Message 18 of 24

Moshe-A
Mentor
Mentor

@Anonymous ,

 

post a sample dwg so i can see it.

 

 

0 Likes
Message 19 of 24

Anonymous
Not applicable

It's the same DWG that I attached previously in Message 9.

0 Likes
Message 20 of 24

dlanorh
Advisor
Advisor
Accepted solution

Try the attached. Type T2A on the commandline once loaded. 

 

If it errors it may be your setup. Unfortunately I cannot test it on your drawing as I require AutoCAD 2010.

I am not one of the robots you're looking for

0 Likes