BlackAlnet Posted June 5, 2009 Posted June 5, 2009 Hi(again), can anyone help me with this lisp?? It's supose to select one text, and many closed plines. Then, extract to .csv with "area" of eache pline, and the text in front of... But something is wrong... (defun c:pad () (setq file (getfiled "Output File" "" "csv" 9)) (setq elist (entget (car (entsel "\nSelecione a estaca: ")))) (setq exlist (assoc 1 elist)) (while (setq pl (entsel "\nSelecione a pline: ")) (command "area" "o" pl) (setq area (getvar "area")) (setq arealst (cons area arealst))) (setq file (open file "w")) (mapcar (function (lambda (x) (write-line (strcat (rtos (car x)) "," (rtos (cdr exlist)))file)))arealst) (close file) (princ) ) It's returning me "bad argument type: consp "last area value" Quote
Lee Mac Posted June 5, 2009 Posted June 5, 2009 I would approach it like this: (defun c:pad (/ file tEnt pl arealst) (if (and (setq file (getfiled "Output File" "" "csv" 9)) (setq tEnt (car (entsel "\nSelecione a estaca: "))) (wcmatch (cdr (assoc 0 (setq elst (entget tEnt)))) "*TEXT")) (progn (while (and (setq pl (car (entsel "\nSelecione a pline: "))) (wcmatch (cdr (assoc 0 (entget pl))) "*POLYLINE")) (setq arealst (cons (vla-get-Area (vlax-ename->vla-object pl)) arealst))) (if arealst (progn (setq file (open file "w")) (mapcar (function (lambda (x) (write-line (strcat (rtos x) "," (cdr (assoc 1 elst))) file))) arealst) (close file)))) (princ "\n<< Incorrect Selection >>")) (princ)) I don't trust the "area" command, so I would opt for the VL approach. Take note of how I have structured the mapcar expression. localise the variables! - the arealst variable will be wrong otherwise! If you have any questions about the code i have posted, just ask Lee Quote
BlackAlnet Posted June 5, 2009 Author Posted June 5, 2009 Your code works very well... like i wish my code works. What's wrong with my code? All this "vl-"..It's visual lisp cmd? Quote
Lee Mac Posted June 5, 2009 Posted June 5, 2009 Judging from the error - I think it was something to do with using the "area" command to retrieve your area - which is why I avoid it. But also, your mapcar statement would not have worked, as the DXF 1 value of the text is a string, so you can't use rtos on it. Also, you used car x when the argument supplied to the lambda function is not a list. Quote
Lee Mac Posted June 5, 2009 Posted June 5, 2009 All this "vl-"..It's visual lisp cmd? Yes, its a Visual LISP method I would rather use Visual LISP over "command", as they are faster and more reliable. Quote
alanjt Posted June 5, 2009 Posted June 5, 2009 i too prefer to extract the data via VL, but it will help you to understand where you went wrong. other than not localizing your variables and a minor mistake in your strcat for output, the routine works fine. you had this: (strcat (rtos (car x)) "," (rtos (cdr exlist))) as you can see, you are trying to take the first item of list x, x is not a list to extract from. mapcar is allowing you to iterate through each item in the list. by removing the car extraction, that portion will work. the other problem was, you were trying to rtos a string. with the below coding replacement, everything works as it should. [color=Red](strcat (rtos x) "," (cdr exlist))[/color] lee is exactly right on localizing your variables. you don't want to leave the variables tied to data that could hurt you later down the line. while i did not fix your code for this, i would definately use error trapping "if" or "cond" (shown by lee) to prevent your routine from trying to continue without all the required variables. hope this helps below is your code fixed with the above notations: (defun c:pad [color=Red](/ file elist exlist pl area arealst)[/color] (setq file (getfiled "Output File" "" "csv" 9)) (setq elist (entget (car (entsel "\nSelecione a estaca: ")))) (setq exlist (assoc 1 elist)) (while (setq pl (entsel "\nSelecione a pline: ")) (command "area" "o" pl) (setq area (getvar "area")) (setq arealst (cons area arealst)) ) ;_ while (setq file (open file "w")) (mapcar (function (lambda (x) (write-line [color=Red](strcat (rtos x) "," (cdr exlist))[/color] file ) ;_ write-line ) ;_ lambda ) ;_ function arealst ) ;_ mapcar (close file) (princ) ) ;_ defun Quote
Lee Mac Posted June 5, 2009 Posted June 5, 2009 i too prefer to extract the data via VL, but it will help you to understand where you went wrong. other than not localizing your variables and a minor mistake in your strcat for output, the routine works fine. Thanks Alan for the added info I might add that when creating routines like this, I try to think of every possible way that someone could make the routine fail (except hitting Esc) and then try to account for it. ie. Using IF statements, to check, not only if a user has selected something, but if it is the correct object. Also, after the WHILE statement, there is a chance that the user did not select any plines, so I put in a check that the arealst existed before proceeding. Lee Quote
alanjt Posted June 5, 2009 Posted June 5, 2009 oh yeah, i forgot to add: always steer clear of combining this type of coding (entget (car (entsel "\nSelecione a estaca: "))) if you miss your pick there, it will error out because there's nothing for it to 'entget. the safest way would be to (if or cond, i just prefer cond) select your object, then begin extracting the data: (cond ((and (setq file (getfiled "Output File" "" "csv" 9)) (setq ent (entsel "\nSelecione a estaca: ")) (setq entg (entget (car ent))) ;; even add a object type filter (wcmatch (cdr (assoc 0 entg)) "MTEXT,TEXT") ) ;;do stuff here ) this will guarantee that it will only proceed if you have filled the requirements. Quote
alanjt Posted June 5, 2009 Posted June 5, 2009 Thanks Alan for the added info I might add that when creating routines like this, I try to think of every possible way that someone could make the routine fail (except hitting Esc) and then try to account for it. ie. Using IF statements, to check, not only if a user has selected something, but if it is the correct object. Also, after the WHILE statement, there is a chance that the user did not select any plines, so I put in a check that the arealst existed before proceeding. Lee i totally agree, i was getting to the error trapping, but his routine does work after fixing his strcat issue; that's what i really wanted to stress first. Quote
Lee Mac Posted June 5, 2009 Posted June 5, 2009 i totally agree, i was getting to the error trapping, but his routine does work after fixing his strcat issue; that's what i really wanted to stress first. I think you're better at the "teaching" aspect than me - I tend to just shoot out a solution, without much explanation... Quote
alanjt Posted June 5, 2009 Posted June 5, 2009 I think you're better at the "teaching" aspect than me - I tend to just shoot out a solution, without much explanation... it's more fun that way, but i thought it better to learn from mistakes. Quote
BlackAlnet Posted June 5, 2009 Author Posted June 5, 2009 WOW, thanks for helping me!!! That explain MANY things to me. Always locate variables. Write-file is really very wrong...Thanks for help me and teach me.Living and learning On another lisp i'm making... that part wont work!! (setq p(getpoint)) (command "-boundary" (list p) "") What's the similar "vl-something" to boundary cmd? Quote
Lee Mac Posted June 5, 2009 Posted June 5, 2009 (setq p(getpoint)) (command "-boundary" (list p) "") What's the similar "vl-something" to boundary cmd? Visual LISP does not have an alternative to every single command, however you are receiving a problem because getpoint returns a point..., and you are supplying the boundary command with (list pt), try just p 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.