cadman2009 Posted November 18, 2009 Posted November 18, 2009 Hi friends : anybody know is there any way to use a EXE or COM file in a lisp file ? Quote
alanjt Posted November 18, 2009 Posted November 18, 2009 In what way? Example: (startapp "notepad" "c:\\file.txt") (command "_.start" "file.exe") Quote
Freerefill Posted November 18, 2009 Posted November 18, 2009 Also, (command "shell" "notepad.exe") Quote
alanjt Posted November 18, 2009 Posted November 18, 2009 Also, (command "shell" "notepad.exe") Oh yeah, that one too. Quote
cadman2009 Posted November 18, 2009 Author Posted November 18, 2009 In what way? Example: (startapp "notepad" "c:\\file.txt") (command "_.start" "file.exe") I want select many point in a drawing and put their coordinates to a matrix for inverse it and doing some computational and final , return results to autocad by new points on new layer and color . I want do this without closing autocad. Quote
Freerefill Posted November 18, 2009 Posted November 18, 2009 I want select many point in a drawing and put their coordinates to a matrix for inverse it and doing some computational and final , return results to autocad by new points on new layer and color . I want do this without closing autocad. I admit I've never seen matrix operations in LISP, but I don't see why it can't be done. Further, I don't believe it's possible to send data to AutoCAD from an external executable. I believe the only way you're going to accomplish that is to write a LISP which exports the data in a file, then opening that file with the external programs using one of the above methods. The only way to get the data back into AutoCAD would be to then get that program to save the new data in a file, and have a LISP program read from that file. That's a very hairy operation, and may not work at all depending on your external program. It might be easier to just figure out the matrix formulae in LISP. Quote
alanjt Posted November 18, 2009 Posted November 18, 2009 I admit I've never seen matrix operations in LISP, but I don't see why it can't be done. Further, I don't believe it's possible to send data to AutoCAD from an external executable. I believe the only way you're going to accomplish that is to write a LISP which exports the data in a file, then opening that file with the external programs using one of the above methods. The only way to get the data back into AutoCAD would be to then get that program to save the new data in a file, and have a LISP program read from that file. That's a very hairy operation, and may not work at all depending on your external program. It might be easier to just figure out the matrix formulae in LISP. Matrix operations can be achieved with Lisp, it's just math. I think Lee was playing around with matrices the other day, @theswamp. Quote
Freerefill Posted November 18, 2009 Posted November 18, 2009 Matrix operations can be achieved with Lisp, it's just math. I think Lee was playing around with matrices the other day, @theswamp. I figured as much. I remember them from college, and I'm sure since LISP is good at dealing with lists, it would probably be well suited for matrix operations, especially with large matrices. Problem is, I forgot everything I learned, so I'm out of ideas Quote
alanjt Posted November 18, 2009 Posted November 18, 2009 I figured as much. I remember them from college, and I'm sure since LISP is good at dealing with lists, it would probably be well suited for matrix operations, especially with large matrices. Problem is, I forgot everything I learned, so I'm out of ideas Yeah, I don't have to worry with matrices and if I did, I'd probably just refer to my trusty TI-86. Quote
CALCAD Posted November 18, 2009 Posted November 18, 2009 cadman2009, I have successfully used Freerefill's described procedure to call an external compiled program and speed up a compute-intensive procedure that would otherwise be way too slow in Autolisp. One thing that must be done is to suspend the lisp program while the external process is active and resume the lisp thereafter. The way I've done that is to use FINDFILE in a loop to detect the presence of a file created by the external process. It was a bit 'hairy' the first time, but it's no big deal once you understand what's going on. There may be better ways but this way can work. Quote
Freerefill Posted November 19, 2009 Posted November 19, 2009 Probably completely useless, but this was my first successful recursive loop that I've ever programmed! Determinant of a matrix, when formatted as a list in the form: (list a11 a12 a13 a21 a22 a23 a31 a32 a33) Note that that is a 3x3 matrix. It will only accept square matrices. (defun matrix_det(matrix / i retn) (if (and (listp matrix) (zerop (rem (sqrt (length matrix)) 1))); if <matrix> is a list representing a square matrix (if (/= (length matrix) 4); If <matrix> is a 3x3 or larger (progn (setq i 0) (repeat (fix (sqrt (length matrix))) (setq retn (+ (if retn retn 0) (* (if (zerop (rem (/ i 2.0) 1)) 1 -1) (nth i matrix) (matrix_det (submatrix matrix i))))) (setq i (1+ i)))) (setq retn (- (* (nth 0 matrix) (nth 3 matrix)) (* (nth 1 matrix) (nth 2 matrix)))))) retn ) (defun subMatrix(matrix index / i retn) (if (and (listp matrix) (zerop (rem (sqrt (length matrix)) 1)) (< index (sqrt (length matrix)))) (progn (setq i 0) (repeat (length matrix) (if (not (or (< i (sqrt (length matrix))) (zerop (rem (/ (- i index) (sqrt (length matrix))) 1)))) (setq retn (append retn (list (nth i matrix))))) (setq i (1+ i))))) retn ) Quote
alanjt Posted November 19, 2009 Posted November 19, 2009 There are no useless attempts, only useless people. :wink: Nice work. Probably completely useless, but this was my first successful recursive loop that I've ever programmed! Determinant of a matrix, when formatted as a list in the form: (list a11 a12 a13 a21 a22 a23 a31 a32 a33) Note that that is a 3x3 matrix. It will only accept square matrices. (defun matrix_det(matrix / i retn) (if (and (listp matrix) (zerop (rem (sqrt (length matrix)) 1))); if <matrix> is a list representing a square matrix (if (/= (length matrix) 4); If <matrix> is a 3x3 or larger (progn (setq i 0) (repeat (fix (sqrt (length matrix))) (setq retn (+ (if retn retn 0) (* (if (zerop (rem (/ i 2.0) 1)) 1 -1) (nth i matrix) (matrix_det (submatrix matrix i))))) (setq i (1+ i)))) (setq retn (- (* (nth 0 matrix) (nth 3 matrix)) (* (nth 1 matrix) (nth 2 matrix)))))) retn ) (defun subMatrix(matrix index / i retn) (if (and (listp matrix) (zerop (rem (sqrt (length matrix)) 1)) (< index (sqrt (length matrix)))) (progn (setq i 0) (repeat (length matrix) (if (not (or (< i (sqrt (length matrix))) (zerop (rem (/ (- i index) (sqrt (length matrix))) 1)))) (setq retn (append retn (list (nth i matrix))))) (setq i (1+ i))))) retn ) Quote
alanjt Posted November 19, 2009 Posted November 19, 2009 Thanks Alan I had an LoL over that myself. Quote
gile Posted November 19, 2009 Posted November 19, 2009 Hi, Here's some matrix stuff. Special thanks to Vladimir Nesterovsky and Doug Wilson for, respectively mxv, mxm and trp routines which are, in may opinion the most useful matrix calculus routines. ;; TRP -Doug Wilson- ;; Transpose a matrix ;; ;; Argument: a matrix (defun trp (m) (apply 'mapcar (cons 'list m))) ;; MXV -Vladimir Nesterovsky- ;; Apply a transformation matrix to a vector ;; ;; Arguments a matrix and a vector (defun mxv (m v) (mapcar (function (lambda (r) (apply '+ (mapcar '* r v)))) m) ) ;; MXM-Vladimir Nesterovsky- ;; Multiply two matrices ;; Nota: calls MXV and TRP routines ;; ;; Arguments: two matrices (defun mxm (m q) (mapcar (function (lambda (r) (mxv (trp q) r))) m) ) ;; IMAT (gile) ;; Return a n dimension identity matrix ;; ;; Argument ;; d : the matrix dimension (defun Imat (d / i n r m) (setq i d) (while (<= 0 (setq i (1- i))) (setq n d r nil) (while (<= 0 (setq n (1- n))) (setq r (cons (if (= i n) 1.0 0.0) r)) ) (setq m (cons r m)) ) ) ;; ISORTHO (gile) ;; Evaluate if a matix is orthogonal ;; Nota: calls MXM and TRP routines ;; ;; Argument: a matrix (defun IsOrtho (mat) (equal (mxm (trp mat) mat) (imat (length mat)) 1e-14) ) ;; INVERSEMATRIX (gile) ;; Return the inverse square matrix (Gauss Jordan method) or nil ;; Nota: calls IMAT routine ;; ;; Argument: a matrix (defun InverseMatrix (mat / col piv row res) (setq mat (mapcar '(lambda (x1 x2) (append x1 x2)) mat (Imat (length mat)))) (while mat (setq col (mapcar '(lambda (x) (abs (car x))) mat)) (repeat (vl-position (apply 'max col) col) (setq mat (append (cdr mat) (list (car mat)))) ) (if (equal (setq piv (caar mat)) 0.0 1e-14) (setq mat nil res nil ) (setq piv (/ 1.0 piv) row (mapcar '(lambda (x) (* x piv)) (car mat)) mat (mapcar '(lambda (r / e) (setq e (car r)) (cdr (mapcar '(lambda (x n) (- x (* n e))) r row)) ) (cdr mat) ) res (cons (cdr row) (mapcar '(lambda (r / e) (setq e (car r)) (cdr (mapcar '(lambda (x n) (- x (* n e))) r row)) ) res ) ) ) ) ) (reverse res) ) ;; COFACT (gile) ;; Returns the cofactor associated to ij item of a matrix ;; Nota: calls DETERM and REMOVE-I routines ;; ;; Arguments ;; i = row index (first row = 1) ;; j = column index (first column = 1) ;; m = a matrix (defun cofact (i j m) (* (determ (remove-i (1- i) (mapcar (function (lambda (x) (remove-i (1- j) x))) m) ) ) (expt -1 (+ i j)) ) ) ;; DETERM (gile) ;; return the determinant of a square matrix ;; Nota: calls COFACT routine ;; ;; Argument : a matrix (defun determ (m) (if (= 2 (length m)) (- (* (caar m) (cadadr m)) (* (caadr m) (cadar m))) ((lambda (r n) (apply '+ (mapcar (function (lambda (x) (* x (cofact 1 (setq n (1+ n)) m)))) r ) ) ) (car m) 0 ) ) ) ;; REMOVE-I (gile) ;; Return the list but item at specified index ;; ;; Arguments ;; ind: index (first item = 0) ;; lst a list (defun remove-i (ind lst) (if (or (zerop ind) (null lst)) (cdr lst) (cons (car lst) (remove-i (1- ind) (cdr lst))) ) ) Quote
cadman2009 Posted November 25, 2009 Author Posted November 25, 2009 Hi, Here's some matrix stuff. Special thanks to Vladimir Nesterovsky and Doug Wilson for, respectively mxv, mxm and trp routines which are, in may opinion the most useful matrix calculus routines. ;; TRP -Doug Wilson- ;; Transpose a matrix ;; ;; Argument: a matrix (defun trp (m) (apply 'mapcar (cons 'list m))) ;; MXV -Vladimir Nesterovsky- ;; Apply a transformation matrix to a vector ;; ;; Arguments a matrix and a vector (defun mxv (m v) (mapcar (function (lambda (r) (apply '+ (mapcar '* r v)))) m) ) ;; MXM-Vladimir Nesterovsky- ;; Multiply two matrices ;; Nota: calls MXV and TRP routines ;; ;; Arguments: two matrices (defun mxm (m q) (mapcar (function (lambda (r) (mxv (trp q) r))) m) ) ;; IMAT (gile) ;; Return a n dimension identity matrix ;; ;; Argument ;; d : the matrix dimension (defun Imat (d / i n r m) (setq i d) (while (<= 0 (setq i (1- i))) (setq n d r nil) (while (<= 0 (setq n (1- n))) (setq r (cons (if (= i n) 1.0 0.0) r)) ) (setq m (cons r m)) ) ) ;; ISORTHO (gile) ;; Evaluate if a matix is orthogonal ;; Nota: calls MXM and TRP routines ;; ;; Argument: a matrix (defun IsOrtho (mat) (equal (mxm (trp mat) mat) (imat (length mat)) 1e-14) ) ;; INVERSEMATRIX (gile) ;; Return the inverse square matrix (Gauss Jordan method) or nil ;; Nota: calls IMAT routine ;; ;; Argument: a matrix (defun InverseMatrix (mat / col piv row res) (setq mat (mapcar '(lambda (x1 x2) (append x1 x2)) mat (Imat (length mat)))) (while mat (setq col (mapcar '(lambda (x) (abs (car x))) mat)) (repeat (vl-position (apply 'max col) col) (setq mat (append (cdr mat) (list (car mat)))) ) (if (equal (setq piv (caar mat)) 0.0 1e-14) (setq mat nil res nil ) (setq piv (/ 1.0 piv) row (mapcar '(lambda (x) (* x piv)) (car mat)) mat (mapcar '(lambda (r / e) (setq e (car r)) (cdr (mapcar '(lambda (x n) (- x (* n e))) r row)) ) (cdr mat) ) res (cons (cdr row) (mapcar '(lambda (r / e) (setq e (car r)) (cdr (mapcar '(lambda (x n) (- x (* n e))) r row)) ) res ) ) ) ) ) (reverse res) ) ;; COFACT (gile) ;; Returns the cofactor associated to ij item of a matrix ;; Nota: calls DETERM and REMOVE-I routines ;; ;; Arguments ;; i = row index (first row = 1) ;; j = column index (first column = 1) ;; m = a matrix (defun cofact (i j m) (* (determ (remove-i (1- i) (mapcar (function (lambda (x) (remove-i (1- j) x))) m) ) ) (expt -1 (+ i j)) ) ) ;; DETERM (gile) ;; return the determinant of a square matrix ;; Nota: calls COFACT routine ;; ;; Argument : a matrix (defun determ (m) (if (= 2 (length m)) (- (* (caar m) (cadadr m)) (* (caadr m) (cadar m))) ((lambda (r n) (apply '+ (mapcar (function (lambda (x) (* x (cofact 1 (setq n (1+ n)) m)))) r ) ) ) (car m) 0 ) ) ) ;; REMOVE-I (gile) ;; Return the list but item at specified index ;; ;; Arguments ;; ind: index (first item = 0) ;; lst a list (defun remove-i (ind lst) (if (or (zerop ind) (null lst)) (cdr lst) (cons (car lst) (remove-i (1- ind) (cdr lst))) ) ) thanks gile , These are very good and Mfydhstnd Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.