Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

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

Insert Multiple Xrefs

224 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 

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

*Expert Elite*
hmsilva
Posts: 3,330
Registered: ‎12-17-2004
Message 2 of 6 (207 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 (184 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: 3,330
Registered: ‎12-17-2004
Message 4 of 6 (180 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 (159 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: 3,330
Registered: ‎12-17-2004
Message 6 of 6 (156 Views)

Re: Insert Multiple Xrefs

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

Henrique
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.