Visual LISP, AutoLISP and General Customization

Reply
Active Member
Mihai.Tanase
Posts: 7
Registered: ‎11-15-2010
Message 1 of 6 (205 Views)
Accepted Solution

Insert Multiple Xrefs

205 Views, 5 Replies
02-27-2014 02:15 PM

Hi

 

I have a few .dwgs (test1 and test2) that i want to insert as xrefs. So far with the help of other autolisp on the site i have come to this :

 

(defun C:MultipleXr ( / myxref1 myxref2) ; define the function
  (setvar "ctab" "Model") ; move to modelspace
  (setvar "clayer" "0") ; set the current layer to 0
  (command "ucs" "w")
  (setq myxref1 (findfile "C:\\Dropbox\\Acad stuff\\Test\\Test1.dwg")
          myxref2 (findfile "C:\\Dropbox\\Acad stuff\\Test\\Test2.dwg")
  );; end steqs
  (foreach myxref (list myxref1 myxref2)
  (command "-xref" "Overlay" myxref "0,0,0" "1" "1" "0")
  );foreach
  (princ)
)

 

It does the job but if i run the command one more time i end up with the same xrefs on top of eachother.

I was thinking about creating some kind of search function to see if the xref is already in the my parent .dwg . Maybe using "ssget" to create a list and using "if" but ....... :smileysad:

 

I realy have to warn you that i am pretty new to autolisp but i am weling to learn.

 

Thanks 

*Expert Elite*
hmsilva
Posts: 2,701
Registered: ‎12-17-2004
Message 2 of 6 (188 Views)

Re: Insert Multiple Xrefs

02-27-2014 03:59 PM in reply to: Mihai.Tanase

Something like this perhaps.

 

(defun C:MultipleXr (/ ENT HND I LST MYXREF MYXREFS OBJ SS XREFLST); define the function
  (setq	myxrefs	'("C:\\Dropbox\\Acad stuff\\Test\\Test1.dwg" "C:\\Dropbox\\Acad stuff\\Test\\Test2.dwg"))
  (foreach xref	myxrefs
    (if	(findfile xref)
      (setq xreflst (cons xref xreflst))
    );; if
  );; foreach
  (if xreflst
    (progn
      (if (setq ss (ssget "_X" '((0 . "INSERT"))))
	(progn
	(repeat	(setq i (sslength ss))
	  (setq	hnd (ssname ss (setq i (1- i)))
		ent (entget hnd)
		obj (vlax-ename->vla-object hnd)
	  );; setq
	  (if (vlax-property-available-p obj 'Path)
	    (if	(findfile (vla-get-path obj))
	      (setq lst (cons (cdr (assoc 2 ent)) lst))
	    );; if
	  );; if
	);; repeat
	(setvar "ctab" "Model"); move to modelspace
	(setvar "clayer" "0"); set the current layer to 0
	(command "ucs" "w")
	(if lst
	  (foreach xref	xreflst
	    (if	(not (member (vl-filename-base xref) lst))
	      (command "-xref" "Overlay" xref "0,0,0" "1" "1" "0")
	    );; if
	  );; foreach
	  (foreach xref	xreflst
	    (command "-xref" "Overlay" myxref "0,0,0" "1" "1" "0")
	  );; foreach
	);; if
	);; progn
      );; if
    );; progn
  );; if
  (princ)
);; MultipleXr

 

HTH

Henrique

Active Member
Mihai.Tanase
Posts: 7
Registered: ‎11-15-2010
Message 3 of 6 (165 Views)

Re: Insert Multiple Xrefs

02-28-2014 12:49 AM in reply to: hmsilva

Tank you for the quick reply but i doesnt seem to work.

I load it up but when i run the command nothing hapens.

I wish i could say more but i have to say i understand maybe half of it. :smileytongue:

 

 

*Expert Elite*
hmsilva
Posts: 2,701
Registered: ‎12-17-2004
Message 4 of 6 (161 Views)

Re: Insert Multiple Xrefs

02-28-2014 01:20 AM in reply to: Mihai.Tanase

Sorry Mihai,

the code had a typo!

 

I have commented the code to facilitate its comprehension.

The code is untested, I don't have AutoCAD in this laptop...

 

EDIT: after reread the code, I found an error, I think it is already fixed...

 

(defun C:MultipleXr (/ ENT HND I LST MYXREF MYXREFS OBJ SS XREFLST); define the function
  ;; sets a list with your xref names
  (setq	myxrefs	'("C:\\Dropbox\\Acad stuff\\Test\\Test1.dwg"
		  "C:\\Dropbox\\Acad stuff\\Test\\Test2.dwg"
		 )
  );; test if find each file
  (foreach xref	myxrefs
    (if	(findfile xref)
      ;; if find, create a list with the xrefs
      (setq xreflst (cons xref xreflst))
    );; if
  );; foreach
  ;; if found xref list exist
  (if xreflst
    (progn
      (setvar "ctab" "Model"); move to modelspace
      (setvar "clayer" "0"); set the current layer to 0
      (command "ucs" "w")
      ;; try to select all inserts
      (if (setq ss (ssget "_X" '((0 . "INSERT"))))
	(progn
	  ;; if exist inserts
	  (repeat (setq i (sslength ss))
	    ;; step thru the selection set
	    (setq hnd (ssname ss (setq i (1- i)))
		  ent (entget hnd)
		  obj (vlax-ename->vla-object hnd)
	    );; setq
	    ;; to test if the insert have the Path propertie (xref)
	    (if	(vlax-property-available-p obj 'Path)
	      ;; if is a xref create a xref exclusion list,
	      ;; with only the xref name, not path or extension
	      (setq lst (cons (cdr (assoc 2 ent)) lst))
	    );; if
	  );; repeat
	  ;; if the exclusion list exist
	  (if lst
	    ;; test the if the xref to overlay don't
	    ;; exist in the exclusion list
	    (foreach xref xreflst
	      (if (not (member (vl-filename-base xref) lst))
		;; if not member, do the xref command
		(command "-xref" "Overlay" xref "0,0,0" "1" "1" "0")
	      );; if
	    );; foreach
	  );; if
	);; progn
	;; if don't exist inserts
	;; do the xref command to all the list elements
	(foreach xref xreflst
	  (command "-xref" "Overlay" xref "0,0,0" "1" "1" "0")
	);; foreach
      );; if
    );; progn
  );; if
  (princ)
);; MultipleXr

 

HTH

Henrique

Active Member
Mihai.Tanase
Posts: 7
Registered: ‎11-15-2010
Message 5 of 6 (140 Views)

Re: Insert Multiple Xrefs

02-28-2014 02:38 PM in reply to: hmsilva

Thank you very much for your help.

 

I have done some testing and works like a charm :smileyvery-happy:

 

One thing i had to do put the list components from setq myxref on a row with blankspace between.

 

Amazing that's all that i can say.

*Expert Elite*
hmsilva
Posts: 2,701
Registered: ‎12-17-2004
Message 6 of 6 (137 Views)

Re: Insert Multiple Xrefs

02-28-2014 02:41 PM in reply to: Mihai.Tanase
You're welcome.
Glad I could help

Henrique

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community