Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LISP program Copy dimensions and to clipboard

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
ancrayzy
786 Views, 11 Replies

LISP program Copy dimensions and to clipboard

I am looking for a LISP program that will allow me to select multiple dimensions, copy them to the clipboard, and paste into Excel  in the following format (include "=")

 

=dimensions1+dimensions2+dimensions3+.......+dimensionsn

 

The command should have the following prompts:

1. Select dimensions in order by click then hit the space bar to finish the command and it auto copy the result to Clipboard

2. Then I go over to an open excel file and CTRL+V to paste the result

 

Results must be calculated in Excel, not on autocad

 

Is this possible? If not could someone suggest an alternative such that I can easily transfer these dimensions to a preexisting excel file?

11 REPLIES 11
Message 2 of 12
ancrayzy
in reply to: ancrayzy

I used this code but got an error

 

--------------------------------------------------------------

 

(defun c:copydimsexcel2 ()
(setq dimlst '())
(setq total 0.0)
(setq dimstr "")

(prompt "\nSelect dimensions in order by clicking. Press spacebar to finish and copy the result to clipboard:\n")

(while
(setq dim (car (entsel "\nSelect dimension: ")))
(if (and (eq (cdr (assoc 0 (entget dim))) "DIMENSION")
(not (member dim dimlst)))
(progn
(setq dimlst (cons dim dimlst))
(setq dimval (cdr (assoc 42 (entget dim))))
(setq total (+ total dimval))
(setq dimstr (strcat dimstr (rtos dimval) "+"))))
)

(setq dimstr (substr dimstr 1 (- (strlen dimstr) 1)))
(setq result (strcat "=" dimstr))

(vl-cmdf "CLIPBOARD" "SET" result)
(prompt "\nResult copied to clipboard. Paste into Excel using CTRL+V.")
(princ)
)

Message 3 of 12
ВeekeeCZ
in reply to: ancrayzy

Here it is. 

 

(vl-load-com)

(defun c:Dims2Clip ( / s pt tx html) ; Copy Text To Clipboard
  
  (and (princ "\nSelect dimensions,")
       (setq s (ssget '((0 . "DIMENSION"))))
       (setq s (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))))
       (setq s (mapcar '(lambda (e / s) (if (not (vl-catch-all-error-p (setq x (vl-catch-all-apply 'getpropertyvalue (list e "Measurement"))))) x)) s))
       (setq s (mapcar '(lambda (x) (atoi x)) s))
       (setq s (strcat "=" (substr (apply 'strcat (mapcar '(lambda (x) (strcat "+" x)) s)) 2)))
       (princ "\nClipboard: ")
       (princ s)
       (vlax-invoke (vlax-get (vlax-get (setq html (vlax-create-object "htmlfile")) 'ParentWindow) 'ClipBoardData) 'setData "Text" s)
       (vlax-release-object html)
       )
  (princ)
  )

 

Message 4 of 12
kajanthangavel
in reply to: ancrayzy

Try This,

(vl-load-com)
(defun c:Dims2Clip ( / ss d a tx html c) ; Copy Text To Clipboard
(princ "\nSelect dimensions,")
       (if (and (setq ss (ssget '((0 . "DIMENSION"))) x 0.))
			(repeat (setq i (sslength ss))
				(setq o (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
				(setq a (vlax-get-property o 'Measurement))
				(setq d (cons a d))
			)
		)
		(foreach c d
			(setq x (+ c x))
		)
 (princ x)
 (vlax-invoke (vlax-get (vlax-get (setq htmlfile (vlax-create-object "htmlfile")) 'ParentWindow) 'ClipBoardData) 'SetData "Text"  (rtos x))
 (vlax-release-object htmlfile)
(princ)
)
Message 5 of 12
ancrayzy
in reply to: ВeekeeCZ

The code does not produce results and reports the following error:

Command: DIMS2CLIP
Select dimensions,
Select objects: 1 found
Select objects: 1 found, 2 total
Select objects: 1 found, 3 total
Select objects: ; error: bad argument type: stringp 1600.0
Message 6 of 12
ancrayzy
in reply to: kajanthangavel

This code automatically calculates the results in autocad, but what I need is the results in excel formula format:
For example, after finishing the command, the result is automatically copied to the clipboard in the format: =1600+2000+1800
not 5400
Message 7 of 12
Sea-Haven
in reply to: ancrayzy

Acad, Bricscad and others can talk to Excel so could write the answers into individual cells then use Excel sum to add them up. That would give you a presentation of the values picked. No need for copy and paste, Cad opens Excel and fills in the cells. Is that what you want.

Message 8 of 12
kajanthangavel
in reply to: ancrayzy

Try this.

(vl-load-com)
(defun c:Dims2Clip ( / ss d a htmlfile c b) ; Copy Text To Clipboard
(princ "\nSelect dimensions,")
       (if (and (setq ss (ssget '((0 . "DIMENSION"))) x 0.))
			(repeat (setq i (sslength ss))
				(setq o (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
				(setq a (vlax-get-property o 'Measurement))
				(setq d (cons a d))
			)
		)
		(foreach c d
			(setq x (+ c x))
		)
	
	(setq b (strcat "=" (substr (apply 'strcat (mapcar '(lambda (x) (strcat "+" (rtos x))) d)) 2)))
	(princ (rtos x))(princ b)
 (vlax-invoke (vlax-get (vlax-get (setq htmlfile (vlax-create-object "htmlfile")) 'ParentWindow) 'ClipBoardData) 'SetData "Text"  b)
 (vlax-release-object htmlfile)
(princ)
)

Dims2Clip.gif

Thank you ВeekeeCZ I got ideas from your code.

Message 9 of 12
ancrayzy
in reply to: kajanthangavel

Thank you very much kajanthangavel, this all what I need !
Message 10 of 12
ancrayzy
in reply to: kajanthangavel

I want to change the result in a different way, the result is a formula that calculates the average value and is still automatically copied to the clipboard like the lisp above.

For example

=(dimension_1 + dimension _2 + dimension _3 + .... + dimension_2)/n

Example 1:

=(2000+3000+1000+4000)/4

Example 2:

=(2000+3000+1000)/3

 

Can someone help me?

Message 11 of 12
kajanthangavel
in reply to: ancrayzy

Try this,

(vl-load-com)
(defun c:Dims2Clip_average ( / ss d a htmlfile c b)
(princ "\nSelect dimensions,")
       (if (setq ss (ssget '((0 . "DIMENSION"))))
			(repeat (setq i (sslength ss))
				(setq o (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
				(setq a (vlax-get-property o 'Measurement))
				(setq d (cons a d))
			)
		)
	(setq e (apply '+ (mapcar '(lambda (x) x) d)))
	(setq b (strcat "=(" (substr (apply 'strcat (mapcar '(lambda (x) (strcat "+" (rtos x 2 0))) d)) 2) ")/" (rtos (sslength ss) 2 0)))
	(princ (rtos (/ e (sslength ss)) 2 0))(princ b)
	(vlax-invoke (vlax-get (vlax-get (setq htmlfile (vlax-create-object "htmlfile")) 'ParentWindow) 'ClipBoardData) 'SetData "Text"  b)
	(vlax-release-object htmlfile)
(princ)
)

 

 

Message 12 of 12
ancrayzy
in reply to: kajanthangavel

It like a magic, thanks you so much @kajanthangavel

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report