Xref loop routine

Xref loop routine

andreas7ZYXQ
Advocate Advocate
1,830 Views
13 Replies
Message 1 of 14

Xref loop routine

andreas7ZYXQ
Advocate
Advocate

Im working on a script that will change my xrefs and save it.

Im planning to run a foreach on the code and i would like to replace StringtoFind with "a b c d" stored in a list.
I would like to change a part of the text inside a attribute, a part of a xrefs name and path and then save.

Then step to the next in my list and so on.

When i run my code i get a error message like "error: bad argument type: consp "RITNINGSNR.""
Im guessing the datatype is wrong? I was searching about (vl-princ-to-string) but im not sure if this is the way to go.

I got stuck how to fix that, any suggestions?
Other ideas and thoughts about the code are warm welcome since im a beginner. 

 

 

(vl-load-com)
;------------------------------------------------
;------User input here---------------------------
;------------------------------------------------ (setq a "63B");<---- Category 1 here (setq b "63F");<---- Category 2 here (setq c "64");<----Category 3 here (setq d "64CBB");<----Category 4 here
;------------------------------------------------ (setq StringtoPut(list a b c d)) (foreach StringtoPut (setq StringtoFind "61" Attnamn "RITNINGSNR.");setq (setq ss (entnext)) (while ss (setq ed (entget ss)) (if (= Attnamn (cdr (assoc 2 ed))) (entmod (subst (cons 1 (vl-string-subst StringtoPut StringtoFind (cdr (assoc 1 ed))))(assoc 1 ed)ed)) ) (setq ss (entnext ss)) ) (vlax-for MyXref (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)));<--go through all block objects (if (and (vl-string-search (strcase StringtoFind) (strcase (vla-get-name MyXref)));<---if the name contains your string (= (vla-get-isxref MyXref) :vlax-true);<---if it's an xref ) (progn (vla-put-name myxref (vl-string-subst StringtoPut;<---Change the name (substr (vla-get-name MyXref) (1+ (vl-string-search (strcase StringtoFind) (strcase (vla-get-name MyXref)))) (strlen StringtoFind));substr (vla-get-name myxref) );string subst );vla-put-name (vla-put-path myxref (vl-string-subst StringtoPut;<---Change the Path (substr (vla-get-path MyXref) (1+ (vl-string-search (strcase StringtoFind) (strcase (vla-get-path MyXref)))) (strlen StringtoFind));substr (vla-get-path myxref) );string subst );vla-put-path (vla-reload myxref) );progn );if );vlax-for (princ) ;(command "qsave") ;(command "close") (setq filnamn(vl-filename-base(getvar "dwgname"))) (setq newname(vl-string-subst StringtoPut StringtoFind filnamn)) (setq file (strcat (getvar 'dwgprefix) newname ".dwg")) (if (findfile file) (command "_.-wblock" file "Y" "*") (command "_.-wblock" file "*") ) )

 

0 Likes
Accepted solutions (2)
1,831 Views
13 Replies
Replies (13)
Message 2 of 14

cadffm
Consultant
Consultant
Without testing your code and my try for solution

(eval (list 'vl-string-subst StringtoPut StringtoFind (cdr (assoc 1 ed)))

Sebastian

0 Likes
Message 3 of 14

andreas7ZYXQ
Advocate
Advocate

@cadffm I got the same error..

 

	  (entmod (subst (cons 1 (eval (list 'vl-string-subst StringtoPut StringtoFind (cdr (assoc 1 ed)))))(assoc 1 ed)ed))

😕

0 Likes
Message 4 of 14

cadffm
Consultant
Consultant
Accepted solution
Arghh

Skip
(setq StringtoPut(list a b c d))
(foreach StringtoPut

Write
(foreach StringtoPut (list a b c d)

or
(setq StringtoPutList (list a b c d))
(foreach StringtoPut StringtoPutList

--

consp = data type of cons arg is wrong


Sebastian

0 Likes
Message 5 of 14

pbejse
Mentor
Mentor

So there are two items,

1. Rename and repath the xref.

2. Pass the new xrefnaem to an attribute on a block.

 

On your code, you are targeting "61" as part of the xref name / attdef tag name. Is this always the case?  or StringtoFind variable varies from one project to another? "RITNINGSNR." is the targeted tag name? 

 

 

0 Likes
Message 6 of 14

andreas7ZYXQ
Advocate
Advocate

I just got it working

thanks @cadffm

yes @pbejse 61 is always the target. Or i will just change "leta" to whatever i want to replace.

But..
The reason i setq a b c d is to have a easy user imput for those who dont know about lisp. (or maybe there is even a better way, but it the way i know how to  :P)
I just have a small issue. For example "d" is left empty. Then i dont want it to cause problems with my list.



(vl-load-com)
;------------------------------------------------
(setq leta "61")
;------------------------------------------------
(setq a "63B")
(setq b "63F")
(setq c "64")
(setq d "")
;------------------------------------------------
;------------------------------------------------
(setq StringtoFind leta	Attnamn "RITNINGSNR.");setq
(foreach StringtoPut (list a b c d)
	(setq ss (entnext))
	(while ss
	 (setq ed (entget ss))
	 (if (= Attnamn (cdr (assoc 2 ed)))
	  (entmod (subst (cons 1 (vl-string-subst StringtoPut StringtoFind (cdr (assoc 1 ed))))(assoc 1 ed)ed))
	 )
	 (setq ss (entnext ss))
	)

	(vlax-for MyXref (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)));<--go through all block objects
   (if (and
	 (vl-string-search (strcase StringtoFind) (strcase (vla-get-name MyXref)));<---if the name contains your string
	 (= (vla-get-isxref MyXref) :vlax-true);<---if it's an xref
	   )
     (progn
       (vla-put-name myxref (vl-string-subst StringtoPut;<---Change the name
			      (substr (vla-get-name MyXref)
				      (1+ (vl-string-search (strcase StringtoFind) (strcase (vla-get-name MyXref))))
				      (strlen StringtoFind));substr
			      (vla-get-name myxref)
			      );string subst
	 );vla-put-name
       (vla-put-path myxref (vl-string-subst StringtoPut;<---Change the Path
			      (substr (vla-get-path MyXref)
				      (1+ (vl-string-search (strcase StringtoFind) (strcase (vla-get-path MyXref))))
				      (strlen StringtoFind));substr
			      (vla-get-path myxref)
			      );string subst
	 );vla-put-path
       (vla-reload myxref)
       );progn
     );if
   );vlax-for
  (princ)
;(command "qsave")
;(command "close")

(setq filnamn(vl-filename-base(getvar "dwgname")))
(setq newname(vl-string-subst StringtoPut leta filnamn))
(setq file (strcat (getvar 'dwgprefix) newname ".dwg"))
(if (findfile file)
	(command "_.-wblock" file "Y" "*")
	(command "_.-wblock" file "*")
)
(setq StringtoFind StringtoPut)
)

 

0 Likes
Message 7 of 14

pbejse
Mentor
Mentor
Accepted solution

@andreas7ZYXQ wrote:

yes @pbejse 61 is always the target. Or i will just change "leta" to whatever i want to replace.

But..
The reason i setq a b c d is to have a easy user imput for those who dont know about lisp. (or maybe there is even a better way, but it the way i know how to  :P)
I just have a small issue. For example "d" is left empty. Then i dont want it to cause problems with my list.


 


Something very odd the way your code is written. anyways for the issue you can..

 

(foreach StringtoPut  (vl-remove-if '(lambda (v)(eq v ""))(list a b c d))
0 Likes
Message 8 of 14

andreas7ZYXQ
Advocate
Advocate

Thanks @pbejse!

Curious to know, what do you mean by odd. Is there some part that needs improvement?

0 Likes
Message 9 of 14

pbejse
Mentor
Mentor

@andreas7ZYXQ wrote:

Thanks @pbejse!

Curious to know, what do you mean by odd. Is there some part that needs improvement?


 

 

(vlax-for MyXref (vla-get-blocks
                       (vla-get-activedocument (vlax-get-acad-object)))
	   (if (and
		 (vl-string-search
                       (strcase StringtoFind)
                       (strcase (vla-get-name MyXref)))
		 (= (vla-get-isxref MyXref) :vlax-true)
		   )

 

When this is TRUE for the first element on the foreach items (list a b c d), say for the item "63B" , when StringtoFind is found , the xref will be renamed and repath , there would be no point running the rest of the list, unless the StringtoFind variable change for every iteration as well

0 Likes
Message 10 of 14

pbejse
Mentor
Mentor

Consider a re-write for this as well..same issue

 

	(while ss
	 (setq ed (entget ss))....

 

Tell us, what is the block name where the attribute needed to be change?  We can improve the code by running just once and consider all conditions for (list a b c d).

 

I suggest you use some an association list like this to test fro conditions

 

(setq XrefNameOldAndNew
           '(("61" "63B")
             ("54" "63F")
             ("AB" "64")
             ))
0 Likes
Message 11 of 14

andreas7ZYXQ
Advocate
Advocate

I think i just got lost now, hehe. its starting to get above my level of knowledge 😞 .
I will try to explain my process..


I normally have 2 xrefs in my E-61-1-100.dwg.
E-61-100.dwg [..\directory\E-61-100.dwg]
F-61.dwg [..\directory\F-61.dwg]

I have a block name that always change name from project to project and also the attribute. So always change attribute name manually.
Lets say the name is RITNINGSNR. this time, this is where my drawingname is printed "E-61-1-100"


My main goal is to make exact copies of this file and save it. Before its saved i want to change xref and my drawingname inside RITNINGNR.
So my new file should be like..

E-63B-1-100.dwg
Xrefs:
E-63B-100.dwg [..\directory\E-63B-100.dwg]
F-63B.dwg [..\directory\F-63B.dwg]

RITNINGSNR.
E-63B-1-100

E-63F-1-100.dwg
Xrefs:
E-63F-100.dwg [..\directory\E-63F-100.dwg]
F-63F.dwg [..\directory\F-63F.dwg]

RITNINGSNR.
E-63F-1-100


"63B / 63F" can change from project to project so i have to decide that every time i want to run. It could be 65 or 66.


0 Likes
Message 12 of 14

pbejse
Mentor
Mentor

@andreas7ZYXQ wrote:


I will try to explain my process...


I guess you are on the right track then, only thing you need to change is put search for the XREF at the start, (outside of foreach), if found then start iterating the names your list.

 

 

 

 

0 Likes
Message 13 of 14

pbejse
Mentor
Mentor

Apologies for I did not notice this addition at post#6 .

 

...
(setq StringtoFind StringtoPut)
...

 

that really makes a big difference andreas7ZYXQ. 

0 Likes
Message 14 of 14

andreas7ZYXQ
Advocate
Advocate

No worries @pbejse. Im always happy to have differents inputs. Keeps the head going. 😃

Thanks alot for the help, worth alot! 

0 Likes