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

Debugging our Survey Utility LISP

8 REPLIES 8
Reply
Message 1 of 9
wsargent
1172 Views, 8 Replies

Debugging our Survey Utility LISP

A person that no longer works at our company created the original Utility Block Insert lisp that keeps giving us problems. A csv file is created then imported using this lisp that inserts the corresponding blocks. The problem is that if a power pole is missed that has a guy wire the lisp exits without inserting the guy block or any block after. I am only a beginner at AutoLISP (I can read most of it, but only wright basic things). So any help would be much appreciated.

 

My first attempt was to create a if-then statment that looks for the pole (the surveyor is supposed to shoot the pole then the guy wires) so there should be a pole within the five previous points numbers (for a pole with multiple wires). If a pole is not found a circle is placed on the "suevey error" layer inplace of the guy block.

 

Here is what I have:

 

(defun C:UT ()
  
;begin layer creation check

(setq snp(getvar "OSMODE"))
    (setvar "OSMODE" 0)
    (setq ts "l60")                                                                         
    (command "TEXT" "S" ts "0,0" "" "")                                                     
    (setq ht (cdr (nth 3(tblsearch "style" ts))))                                           
      (setq ht 2.3)                                                                        
      (setq len1 (* ht 1))                                                                  
      (setq ang 0)                                                                          
      (setq ang2 0.785)
      (setq ang3 0.707)                                                                     
      (setq qt (chr 34))                                                                     
      (setq rnd 3.333)
		(setq lyr (tblsearch "LAYER" "TREE-T"))                                             
               (if (= lyr nil) (command "layer" "n" "TREE-T" "c" "r" "TREE-T" ""))          
		(setq lyr (tblsearch "LAYER" "TREE-SHRUBS"))                                             
               (if (= lyr nil) (command "layer" "n" "TREE-SHRUBS" "c" "9" "TREE-SHRUBS" ""))  
		(setq lyr (tblsearch "LAYER" "TREE-SYMBOL"))                                             
               (if (= lyr nil) (command "layer" "n" "TREE-SYMBOL" "c" "r" "TREE-SYMBOL" ""))
  	        (setq lyr (tblsearch "LAYER" "E-UTIL")) ; look for utility layer
               (if (= lyr nil) (command "-layer" "n" "E-UTIL" "c" "r" "E-UTIL" "")) ;If none creat layer
                (command "-layer" "Thaw" "E-UTIL" "")
  	        (setq lyr (tblsearch "LAYER" "SHEET-INFO")) ; look for utility layer
               (if (= lyr nil) (command "-layer" "n" "SHEET-INFO" "c" "G" "SHEET-INFO" "")) ;If none creat layer
                (command "-layer" "Thaw" "SHEET-INFO" "")
        (princ)                                                                             
	(SETVAR "CLAYER" "E-UTIL")
	
	(setq ds (getstring T "\nEnter the drawing scale:"))

;end layer creation check
       
  (setq FILEPATH (getvar "dwgprefix")) 
       
  (setq FILE (getfiled "Select point data file:" FILEPATH "csv" 8))

  (setvar "CLAYER" "SHEET-INFO")
  (setq DATA (open FILE "r"))
    (while (setq row (read-line DATA))
    (setq list_column (dos_strokens row "`,"))
    (setq column_A (nth 0 list_column)); sets column_A to = column A in excel file (pnt number)
    (setq column_C (nth 1 list_column)); sets column_B to = column B in excel file (northing/need to check)
    (setq column_B (nth 2 list_column)); sets column_C to = column C in excel file (easting/need to check)
    (setq column_D (nth 3 list_column)); sets column_D to = column D in excel file (elevation)
    (setq d1 (nth 5 list_column)); sets d1 to = column F in excel file (extracted desc key)    

    (cond ((= d1 "PP")
		(setq Desc (nth 6 list_column))
		(setq pnt (strcat column_B "," column_C))
		(setq SPN (distof column_B 2))
		(setq SPE (distof column_C 2))
	   	(setq lastppNo (strcat column_A))
		(setq lastpp (strcat column_B "," column_C))
		(setvar "clayer" "E-UTIL")
		(command "insert" "Pp" pnt ds "" "0" Desc "")
    )

    ((= d1 "PPL")
		(setq Desc (nth 6 list_column))
		(setq pnt (strcat column_B "," column_C))
		(setq SPN (distof column_B 2))
		(setq SPE (distof column_C 2))
     		(setq lastppNo (strcat column_A))
		(setq lastpp (strcat column_B "," column_C))
		(setvar "clayer" "E-UTIL")
		(command "insert" "Pp-ls" pnt ds "" "0" Desc "")
    )

    ((= d1 "GUY")
		(setq pnt (strcat column_B "," column_C))
		(setq EPN (distof column_B 2))
		(setq EPE (distof column_C 2))
		(setq guy (strcat column_A))
		(if (>= 5 (- guy lastppNo))   ;;; Checking for PP within 5 prevous points
		   ((setvar "clayer" "F99")   ;;; If no PP found insert a circle around the point
		     (command "_circle" pnt ds))
 		  ((setq XDIF (- SPE EPE))    ;;; If PP found insert guy
 		    (setq YDIF (- SPN EPN))
 		    (setq THETA (atan XDIF YDIF))
 		    (setvar "clayer" "E-UTIL")
  		   (command "_line" lastpp pnt "")
  		   (setq DEG (RTD THETA))
  		   (princ DEG)
  		   (command "insert" "guy" pnt ds "" DEG))
 		  )
     )

 The sceipt continues, but this is what I have modified. Prior to my changes the LISP worked aslong as there was a PP preceding the guy wire. Now I am getting the message

     error: bad argument type: numberp: "30242"

What does that mean?

__________

AutoCAD Civil 3D 2016 SP3.0, built on: AutoCAD 2016 SP1, Map 3D 2016 SP2
Dell Percision T5810: MS Windows 10 Pro; Intel Xeon CPU E5-1620 v3 @ 3.50 GHz, 16.00 GB RAM, NVIDIA K2200 4.00 GB Memory
8 REPLIES 8
Message 2 of 9
Kent1Cooper
in reply to: wsargent


@wsargent wrote:

....

 Now I am getting the message

     error: bad argument type: numberp: "30242"

What does that mean?


Without delving into all the rest, I can at least quickly answer that question.  It's getting "30242" as a text string at some point where it wants a number.  You probably need to apply (atoi) or (atof) to some variable that's coming from (read-line) and is therefore arriving as a text string that needs conversion [I didn't see an obvious place, but I didn't evaluate everything in great detail].

Kent Cooper, AIA
Message 3 of 9
wsargent
in reply to: Kent1Cooper

It has been longer than I would have liked to get back to this...

 

I have the script working with the error file that I was given, but only partly.

 

Below is the working script section that I am attempting to modify.

   ((= d1 "GUY")
		(setq pnt (strcat column_B "," column_C))
		(setq EPN (distof column_B 2))
		(setq EPE (distof column_C 2))
		(setq guy (atof (strcat column_A)))
		(if (<= 5 (- guy lastppNo))     ;;; Checking for PP within 5 previous points
		   (command "_circle" pnt ds)     ;;; If True circle around the point
 		   ((setq XDIF (- SPE EPE))     ;;; If False (PP found) insert guy
                      (setq YDIF (- SPN EPN))
 		      (setq THETA (atan XDIF YDIF))
 		      (setvar "clayer" "E-UTIL")
  		      (command "_line" lastpp pnt "")
  		      (setq DEG (RTD THETA))
 		      (princ DEG)
  		      (command "insert" "guy" pnt ds "" DEG)
 		   )
		)
    )

 

What I want to do is insert the “guy” block scaled to DS, not rotated on the E-UTIL layer and draw the circle on layer “F99”.

 

The problem I am having is that I can only get it to perform one action then error.

I want to change “(command "_circle" pnt ds)” to:

((setvar “clayer” “F99”) (command "_circle" pnt ds)

   (setvar “clayer” “E-UTIL”) (command "insert" "guy" pnt ds "0" ""))

 

What I keep getting is a circle on the E-UTIL layer and this on the command line:

 

Command: _circle Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: 4637.564,5227.361

Specify radius of circle or [Diameter]: 40

Command: ; error: bad argument type: (or stringp symbolp): nil

 

What am I doing wrong?

__________

AutoCAD Civil 3D 2016 SP3.0, built on: AutoCAD 2016 SP1, Map 3D 2016 SP2
Dell Percision T5810: MS Windows 10 Pro; Intel Xeon CPU E5-1620 v3 @ 3.50 GHz, 16.00 GB RAM, NVIDIA K2200 4.00 GB Memory
Message 4 of 9
Kent1Cooper
in reply to: wsargent


@wsargent wrote:

.... 

Below is the working script section that I am attempting to modify.

   ....
		(if (<= 5 (- guy lastppNo))     ;;; Checking for PP within 5 previous points
		   (command "_circle" pnt ds)     ;;; If True circle around the point
....

 

.....

I want to change “(command "_circle" pnt ds)” to:

((setvar “clayer” “F99”) (command "_circle" pnt ds)

   (setvar “clayer” “E-UTIL”) (command "insert" "guy" pnt ds "0" ""))

....


An (if) function can take only three arguments: some kind of test or check, a 'then' argument for if that does not return nil, and [optionally] an 'else' argument for if the test returns nil.  If you want to throw more than one function into either the 'then' or 'else' argument, you need to make them into a "package deal" in one function, which you do by "wrapping" them together into a (progn) function.  Your extra set of parentheses around them, without the progn in there, will be read as though the first thing inside is somehow a function name, which as you have it, it isn't.

 

(if (<= 5 (- guy lastppNo))

  (progn ; then argument as single function

    (setvar “clayer” “F99”)

    (command "_circle" pnt ds)

    (setvar “clayer” “E-UTIL”)

    (command "insert" "guy" pnt ds "0" "")

  ); end progn

  .... {your 'else' argument} ....

Kent Cooper, AIA
Message 5 of 9
wsargent
in reply to: wsargent

Thanks for the help.

 

I am still getting an error when I run the LISP. This time it is "; error: bad argument type: (or stringp symbolp): nil"

I have isolated the error to this line (which was missing some quotations):

(command "insert" "guy" pnt ds "" "0" "")

 

I type the above script into the command line with the variable values entered as below

(command "insert" "guy" "5227.361,4637.564,496.377" "40" "" "0" "")

 

and I keep getting the last command the user entered activating, in this case UTIL, as an unknown command and then a new line with nil on it.

 

Wow, I need to take a class on this stuff if I am going to be doing any additional scripting…

__________

AutoCAD Civil 3D 2016 SP3.0, built on: AutoCAD 2016 SP1, Map 3D 2016 SP2
Dell Percision T5810: MS Windows 10 Pro; Intel Xeon CPU E5-1620 v3 @ 3.50 GHz, 16.00 GB RAM, NVIDIA K2200 4.00 GB Memory
Message 6 of 9
Kent1Cooper
in reply to: wsargent


@wsargent wrote:
,,,,

I have isolated the error to this line (which was missing some quotations):

(command "insert" "guy" pnt ds "" "0" "")

I type the above script into the command line,,,, 

and I keep getting the last command the user entered activating, in this case UTIL, as an unknown command and then a new line with nil on it.

....


Giving it the rotation angle [which can be either "0" as a text string or 0 as a number or "" as an Enter, since 0 is always the default rotation] completes the Insert command.  Stop there -- the "" you have after that is extraneous, and being an Enter, tries to recall the latest command.  If that's a (defun)-defined command name and not a native AutoCAD command, it won't be recognized inside a (command) function, which is the reason for the error message.  The nil is just what the (command) function always returns when it's done.

 

[And by the way, the word "Script" has a specific meaning in AutoCAD, and this isn't one.  This particular line is a function, which in this case is part of a defined command or routine; some would call the overall thing a function, because (defun) stands for DEfine FUNction.  But I think it's more common to use "function" to mean the things listed in the AutoLISP Reference that you use inside parentheses -- a (command) function, an (if) function, etc.]

Kent Cooper, AIA
Message 7 of 9
wsargent
in reply to: wsargent

The new code for this section is as follows:

    ((= d1 "GUY")
		(setq pnt (strcat column_B "," column_C))
		(setq EPN (distof column_B 2))
		(setq EPE (distof column_C 2))
		(setq guy (atof (strcat column_A)))
		(if (<= 5 (- guy lastppNo))                ;;; Checking for PP within 5 prevous points
                   (progn                                  ;;; If True insert block and circle around the point
                      (setvar “clayer” “F99”)
                      (command "_circle" pnt ds)
                      (setvar “clayer” “E-UTIL”)
                      (command "insert" "guy" pnt ds "" "0")
                   ) ;;; end progn
 		   (progn                                  ;;; If False (PP found) insert guy
                      (setq XDIF (- SPE EPE))
                      (setq YDIF (- SPN EPN))
 		      (setq THETA (atan XDIF YDIF))
 		      (setvar "clayer" "E-UTIL")
  		      (command "_line" lastpp pnt "")
  		      (setq DEG (RTD THETA))
 		      (princ DEG)
  		      (command "insert" "guy" pnt ds "" DEG)
 		   ) ;;; end progn
		) ;;; end if
    ) ;;; end guy

When I run the command using the excel file that I know will trigger the first part of the if statment the lisp is not drawing the circle, or inserting the block. It stops inserting the blocks at the point before and gives me the same message "; error: bad argument type: (or stringp symbolp): nil"

 

Changing the setvar "clayer" lines to notes allows the script to run without the error. I have checked the syntax of theses lines on the command line and they appear correct. Am I not allowed to set variables in a progn?

 

__________

AutoCAD Civil 3D 2016 SP3.0, built on: AutoCAD 2016 SP1, Map 3D 2016 SP2
Dell Percision T5810: MS Windows 10 Pro; Intel Xeon CPU E5-1620 v3 @ 3.50 GHz, 16.00 GB RAM, NVIDIA K2200 4.00 GB Memory
Message 8 of 9
Kent1Cooper
in reply to: wsargent


@wsargent wrote:

....

....
  (if (<= 5 (- guy lastppNo))
    (progn
      (setvar “clayer” “F99”)
....

When I run the command using the excel file that I know will trigger the first part of the if statment the lisp is not drawing the circle, or inserting the block. It stops inserting the blocks at the point before and gives me the same message "; error: bad argument type: (or stringp symbolp): nil"

 

Changing the setvar "clayer" lines to notes allows the script to run without the error. I have checked the syntax of theses lines on the command line and they appear correct. Am I not allowed to set variables in a progn?

 


I'm not versed in bringing things in from spreadsheet files, but....

 

Stupid question, but I have to ask:  Is Layer F99 in the drawing?

 

Way up above, you have this:

 

  (setq lastppNo (strcat column_A))

 

Aside from the fact that the (strcat) isn't doing anything for you if you're not conCATenating more than one STRing together, that line means the lastppNo variable will contain a text string.  You can't subtract a string from a number.  Try (if (<= 5 (- guy (atof lastppNo))), or incorporate the (atof) where you set the lastppNo variable further up, just as you do where you set the guy variable in this section [which also doesn't need the (strcat) wrapper].

 

If that doesn't fix it, and since the error message is about finding a nil value, try testing all the variables after you run it, to make sure they have been established and contain the right kind of information, by typing them at the Command: line preceded by exclamation points:

!pnt

!EPN

!EPE

!guy

!lastppNo

...etc....

The first one that returns nil or some incorrect type of information will at least be a clue as to where the problem lies.

Kent Cooper, AIA
Message 9 of 9
wsargent
in reply to: wsargent

Yes the layer exist in the test file I am working with. I have made the mistake to use the default acad.dwt file instead of our templates, but the lisp errors out as soon as it cannot find the block it needs.

 

Thanks again for your help. I will see what each variable contains and go from there.

__________

AutoCAD Civil 3D 2016 SP3.0, built on: AutoCAD 2016 SP1, Map 3D 2016 SP2
Dell Percision T5810: MS Windows 10 Pro; Intel Xeon CPU E5-1620 v3 @ 3.50 GHz, 16.00 GB RAM, NVIDIA K2200 4.00 GB Memory

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

Post to forums  

Autodesk Design & Make Report

”Boost