Search the Community
Showing results for tags 'functions'.
-
Hello, After a sidestep to Clojure, of course back to AutoLisp. I have quite some exeperience with AutoLisp and on and off I worked with it for almost 20 years now. (Auto)Lisp is known amongst AutoCAD users but further on, the whole lisp-thing doesn't ring a bell with most programmers that I meet. They work with SQL, HTML, XHTML, Java, C# or maybe (V)BA. I stayed interested in Lisp, although limited to AutoLisp. Recently. I discovered that there is a new Lisp called Clojure that is becoming big in the programming world. Possibly due to the fact that it translates to Java or javascript before anything is executed. But this aside. I learned that Clojure is first class because it is capable of accepting a function as a parameter and clojure functions can produce functions. There's a whole lot more to be discoverd about this great language but I leave that to the reader. The subject 'Clojure' is easily found on the web. By taking this interest in Clojure I learned a lot about AutoLisp too. On this page at the subject of 'Closures' halfway the page, there is an example showing how a function is produced in Clojure. Realy, it took me quite some time to grasp it. After that I wanted to know if this could be done with AutoLisp as well. After trials with defun-q-list-set/ref (which also work) I came up with the following. Hart of the solution is (eval (append '(lambda . . Is it the best way to do this? I don't know. Is it useful? I doubt it but see below. Is it interesting? Highly (for me!) (defun messenger-builder (greeting) (setq greeting (strcat "\n" greeting " ")) (eval (append '(lambda (who)) (list (list 'princ (list 'strcat greeting 'who)) '(princ))))) (setq hello-er (messenger-builder "Hello") goodbye-er (messenger-builder "Goodbye")) (defun c:greet_the_world () (hello-er "World!")) (defun c:say_goodbye () (goodbye-er "Everyone!!")) AutoLisp does a great job in AutoCAD. Perfect, but as a Lisp it is less thoroughly designed than Clojure. This shows off in the example above. Clojure handles the subject of producing functions easier. Maybe my example is ill-constructed. I could not find a better solution. Question also is, can we think of any usufull application of this in AutoCAD. Although I took another route in the example, the designated functions for this are (I think) defun-q-list-set/ref. I suppose they are there for a reaon. Or is it all limited to the s::startup example?
-
I have a CAD file that has all the pipe drawn in 2D. I want to be able to determine the total amount of pipe shown on the drawing to help determine my system size. As in a typical piping drawing there are breaks in the pipe as it over laps and it branches of in different directions. The pipe is all on the same layer and just drawn with basic lines. I know you can us the 'list' function to get the length of a selected line but is there a way to get the total length of all the lines drawn in a quicker fashion than adding them all up individually? I'm very new to CAD and trying to learn how to use it on the job. I'm trying to find ways to be more efficient! Any help or suggestions would be greatly appreciated!!! Thanks!
-
extracting individual numbers from one Excel table cell text string such as 12x5.5x36
elmoleaf posted a topic in AutoLISP, Visual LISP & DCL
Hi, I'm attempting to modify Fixo's code at bottom to read data from an excel spreadsheet that's formatted a bit differently than assumed by the code. The code here assumes X, Y, and Z scale each live in a separate exel column/cell. However, I have existing spreadsheets where these dimensions were entered as a text string in a single cell. For example, the cell might read 6x5.5x24. The only constant is the "x" between the dimensions. The text numbers might be one, two or even three characters and may or may not include decimals. My thought is to modify these lines below to somehow check the same string for the X's and truncate according its position to extract the desired X, Y or Z value. But, I don't know enough yet to even know if this is possible. xscale (atof (vl-princ-to-string (nth 5 item))) yscale (atof (vl-princ-to-string (nth 6 item))) zscale (atof (vl-princ-to-string (nth 7 item))) Failing this, I'll make a second version of the existing spreadsheets and have users copy data into new columns formatted to work with the existing code. Many thanks! ;;;---------------------------------------------------------------------------; ;;; ;;; getexel.lsp ;;; ;;; based on code posted by "fixo" on [url]http://forums.augi.com/showthread.php?t=112689&highlight=blocks+excel[/url] ;;; This lisp is used to insert a block multiple times based on coordinates and scale factors listed in an excel spreadsheet ;;; Data in spreadsheet must follow this format: ;;; first row is headers/labels for data ;;; cola-empty, colb-x coord, colc-y coord, cold-z coord, colf-width/x scale, colg-depth/y scale, colh-height/z scale, ;;; coli-layer destination name, colj-equip tag number. ;;; ;;;---------------------------------------------------------------------------; ;;; ;;; DESCRIPTION ;;; ;;; Configure AutoCAD ;;; ;;;****************************************************** ;;; Section 1 ;;;****************************************************** ;;; ;;; --- Load Visual Lisp Support ;;; (VL-LOAD-COM) ;;;****************************************************** ;;; Section 2 ;;;****************************************************** ;;; read used range from Excel ;;; (defun getexcelinfo (Wbk ShtNum / Sht UsdRange ExcData) (setq Sht (vl-catch-all-apply 'vlax-get-property (list (vlax-get-property Wbk "Sheets") "Item" ShtNum ) ) ) (setq UsdRange (vlax-get-property Sht "UsedRange") ExcData (vlax-safearray->list (vlax-variant-value (vlax-get-property UsdRange "Value") ) ) ) (setq ExcData (mapcar (function (lambda (x) (mapcar 'vlax-variant-value x))) ExcData ) ) ExcData ) ;;;****************************************************** ;;; Section 3 ;;;****************************************************** (defun C:getexcel (/ *error* acsp adoc aexc att1 att2 att3 att4 attribs block_data blockname block_obj dwgpath layer rot x xlbook xlbooks xlpath xscale y yscale z zscale) (defun *error* (msg) (vl-bt) (if (vl-position msg '("console break" "Function cancelled" "quit / exit abort" ) ) (princ "Error!") (princ msg)) (vla-endundomark adoc) (vl-exit-with-value "Program bombed, sorry") ) (if (setq xlpath (getfiled "* Select Excel File To Read Data*" "" "xlsx" 4) ) (progn (setq aexc (vlax-get-or-create-object "Excel.Application") xlbooks (vlax-get-property aexc "Workbooks")) (setq xlbook (vlax-invoke-method xlbooks "Open" xlpath)) (setq block_data (getexcelinfo xlbook 1));<-- 1 is excel sheet number, change to suit (setq block_data (vl-remove-if-not (function (lambda (x) (car x))) block_data)) (setq block_data (cdr block_data)) (vl-catch-all-apply (function (lambda () (vlax-invoke-method xlbook "Close" :vlax-false))) ) (mapcar (function (lambda (x) (vl-catch-all-apply (function (lambda () (progn (if x (vlax-release-object x))))))) ) (list xlbook xlbooks aexc) ) (setq aexc nil) (gc) (gc) (setq dwgpath (getfiled "* Select Drawing With Block To Insert *" "" "dwg" 4) ) (setq blockname (vl-filename-base dwgpath)) (setq adoc (vla-get-activedocument (vlax-get-acad-object))) (setq acsp (vla-get-block (vla-get-activelayout adoc))) (vla-startundomark adoc) (vl-catch-all-apply (function (lambda () (progn (setq block_obj (vlax-invoke-method acsp 'InsertBlock (vlax-3d-point '(0 0 0)) dwgpath 1 1 1 0 ) ) (vla-delete block_obj) (vlax-release-object block_obj)))) ) ;; I know nothing what is data format in your Excel ;; thus I have added extrafluous convertion of them (foreach item block_data (setq x (atof (vl-princ-to-string (nth 1 item))) y (atof (vl-princ-to-string (nth 2 item))) z (atof (vl-princ-to-string (nth 3 item))) rot (atof (vl-princ-to-string (nth 4 item))) xscale (atof (vl-princ-to-string (nth 5 item))) yscale (atof (vl-princ-to-string (nth 6 item))) zscale (atof (vl-princ-to-string (nth 7 item))) layer (vl-princ-to-string (nth 8 item)) att1 (vl-princ-to-string (nth 9 item)) att2 (vl-princ-to-string (nth 5 item)) att3 (vl-princ-to-string (nth 6 item)) att4 (vl-princ-to-string (nth 7 item)) ) (setq block_obj (vlax-invoke-method acsp 'InsertBlock (vlax-3d-point (list x y z)) dwgpath xscale yscale zscale rot) ) (if (tblsearch "layer" layer) (vla-put-layer block_obj layer) (princ (strcat "\nLayer " "\"" layer "\"" " does not exist")) ) (setq attribs (vlax-invoke block_obj 'GetAttributes)) (foreach att attribs (cond ((eq "EQ-TAG" (vla-get-tagstring att));<--"EQ-TAG" is the first tag (vla-put-textstring att att1)) ((eq "EQ-WIDTH" (vla-get-tagstring att));<--"EQ-WIDTH" is the second tag (vla-put-textstring att att2)) ((eq "EQ-DEPTH" (vla-get-tagstring att));<--"EQ-DEPTH" is the THIRD tag (vla-put-textstring att att3)) ((eq "EQ-HGHT" (vla-get-tagstring att));<--"EQ-HGHT" is the FOURTH tag (vla-put-textstring att att4)) ;<-- add other tags here by the same way as above (T nil) ) ) ) ) ) (*error* nil) (prin1) ) (prompt "\n\t\t>>>\tType getexcel to execute\t<<<") (prin1) (vl-load-com)~'J'~- 4 replies
-
- spreadsheet
- functions
-
(and 3 more)
Tagged with: