What is the problem with this code?

What is the problem with this code?

Anonymous
Not applicable
1,065 Views
24 Replies
Message 1 of 25

What is the problem with this code?

Anonymous
Not applicable

Hi

 

Please take a look at this code (I have made the text red on the line that has an error, and I don't know why?):

 

I am trying to extract the vertices of a polyline.

 

(defun c:r15 ()

      (setq e (entget (car (entsel "Select polyline to analyse: "))))
    (setq p1 (getpoint "Click where you want the table: "))
      (setq p2 p1)
      (setq tz (getreal "Enter text height: "))
      (setq ct 0)
      (setq lnth (length e))
      (setq tbly 0)
      (setq n 1)
      
  (repeat lnth
        (setq ct (+ 1 ct))
            (setq tx (nth ct e))
            (setq ts (car tx))
                        
            (if (= ts 10)
                (progn
                (setq crd (cdr tx))
                (set (read (strcat "pt" (itoa n) "x")) (car crd))
                (set (read (strcat "pt" (itoa n) "y")) (cadr crd))
                (set (read (strcat "pt" (itoa n) "xs")) (rtos (strcat "pt" (itoa n) "x") 2 3))
                (set (read (strcat "pt" (itoa n) "ys")) (rtos (strcat "pt" (itoa n) "y") 2 3))
                (command "text" p2 tz 0 (read (strcat "pt" (itoa n) "xs")))
                (setq tblx (polar p2 0 (* 10 tz)))
                (command "text" tblx tz 0 (read (strcat "pt" (itoa n) "ys")))
                (setq n (+ 1 n))
                (setq tbly (+ (* tz 1.5) tbly))
                (setq p2 (polar p1 (dtr 270) tbly))
                )
                
            )
    )

  )

  (defun dtr (deg)(* pi (/ deg 180.0)))

0 Likes
Accepted solutions (1)
1,066 Views
24 Replies
Replies (24)
Message 2 of 25

ВeekeeCZ
Consultant
Consultant

The line has to be like this:

 

(set (read (strcat "pt" (itoa n) "xs")) (rtos (eval (read (strcat "pt" (itoa n) "x"))) 2 3))

The others are similar.

0 Likes
Message 3 of 25

Anonymous
Not applicable
Hi BeekeeCZ

This did work. Except that the coordinates are not rounded off to three decimal places?

Kind regards

Derryck
0 Likes
Message 4 of 25

ВeekeeCZ
Consultant
Consultant

The coordinates are rounded off to three decimal places correctly.

You are doing some exercises on dynamic variables? Or is it part of some bigger project? This whole eval-read thing is not really necessary...

 

Spoiler
(defun c:r15 (/ dtr oDIMZIN esel p1 tz e ct n tx)

  (defun dtr (deg) (* pi (/ deg 180.0)))

  (setq oDIMZIN (getvar 'DIMZIN))
  (setvar 'DIMZIN 0)

  (if (and (setq esel (entsel "\nSelect polyline to analyse: "))
	   (setq p1 (getpoint "\nClick where you want the table: "))
	   (not (initget 7))
	   (setq tz (getreal "\nEnter text height: "))
	   (setq e  (entget (car esel))
		 ct 1
		 n  1
	   )
      )
    (repeat (length e)
      (setq tx (nth ct e))
      (if (= (car tx) 10)
	(progn
	  (command "_.text"
		   p1
		   tz
		   0
		   (rtos (cadr tx) 2 3)
	  )
	  (command "_.text"
		   (polar p1 0 (* 10 tz))
		   tz
		   0
		   (rtos (caddr tx) 2 3)
	  )
	  (setq	p1 (polar p1 (dtr 270) (* tz 1.5))
		n  (1+ n)
	  )
	)
      )
      (setq ct (1+ ct))
    )
  )
  (setvar 'DIMZIN oDIMZIN)
  (princ)
)
Message 5 of 25

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:
.... the coordinates are not rounded off to three decimal places?
....

If it's a matter of not having enough zeros at the end to fill out three decimal places, look into the DIMZIN System Variable.

Kent Cooper, AIA
0 Likes
Message 6 of 25

Anonymous
Not applicable

I need to be able to use the vertices as variables because I need to perform calculations with them. Have you tried the program, for me it is not rounding off at all? I get like 20 decimal places.

0 Likes
Message 7 of 25

ВeekeeCZ
Consultant
Consultant

Yes I had, and it worked as espected.

Please post your final code, I take a look.

0 Likes
Message 8 of 25

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

I need to be able to use the vertices as variables because I need to perform calculations with them. Have you tried the program, for me it is not rounding off at all? I get like 20 decimal places.


Could it be that the Text command parts also need to have (eval) wrapped around the text-content part, to correctly use the result of the (rtos ... 2 3) setting the variables?

 

 (command "text" p2 tz 0 (eval (read (strcat "pt" (itoa n) "xs"))))

Kent Cooper, AIA
0 Likes
Message 9 of 25

Anonymous
Not applicable
Thanks Kent I tried that. It still comes out with massive decimal places.
0 Likes
Message 10 of 25

Anonymous
Not applicable
 
0 Likes
Message 11 of 25

ВeekeeCZ
Consultant
Consultant
Accepted solution

The problem is very simple. Now you did a typo that you did not before. Missing "s"!!!

 

(command "text" p2 tz 0 (eval (read (strcat "pt" (itoa n) "xs"))))

 Edit: I am afraid that these kinds of errors arising from the fact that it unnecessarily complicated. I could have that code for you to simplify and clarify - if you would be interested. I understand the need for additional calculations have the coordinates of individual points of the curve ...

0 Likes
Message 12 of 25

Anonymous
Not applicable
Hi thanks BeekeeCZ

I put the 's'' back, but the program still returns huge amounts of decimal places 😞

I am very new to Lisp, so my programs unfortunately are having to be unnecessarily complicated because I do not know many commands...

0 Likes
Message 13 of 25

Kent1Cooper
Consultant
Consultant

You seem to be reading from the wrong source variables for the text content in the Text commands.  Try:

 

....

(command "text" p2 tz 0 (eval (read (strcat "pt" (itoa n) "xs"))))
....
(command "text" tblx tz 0 (eval (read (strcat "pt" (itoa n) "ys"))))

....

Kent Cooper, AIA
0 Likes
Message 14 of 25

Anonymous
Not applicable
I already have and still huge amounts of decimal places 😞
0 Likes
Message 15 of 25

ВeekeeCZ
Consultant
Consultant
Dunno, I have no such problem.
Did you try the code of mine from post 4? Does it work correctly?
0 Likes
Message 16 of 25

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:
I am very new to Lisp, so my programs unfortunately are having to be unnecessarily complicated because I do not know many commands...
I already have and still huge amounts of decimal places 😞

I tried it that way and got only three, so I'm not sure what else to suggest about that.  But as BeekeeCZ pointed out, there are ways to do these things that can be a lot shorter and use fewer variables.  Here's one [lightly-tested] consolidation of the same functionality, that you can study the pieces of, to add to your arsenal:

 

(defun c:r15 (/ e p1 tz)
  (setq
    e (entget (car (entsel "Select polyline to analyse: ")))
    p1 (getpoint "Click where you want the table: ")
    tz (getreal "Enter text height: ")
  ); setq
  (foreach vert
    (mapcar 'cdr ; strip 10 association numbers from beginnings of:
      (vl-remove-if-not '(lambda (x) (= (car x) 10)) e); list of only 10-code [vertex] entries
    ); mapcar
    (command

      "text" p1 tz 0 (rtos (car vert) 2 3); draw X coordinate
      "text" (polar p1 0 (* 10 tz)) tz 0 (rtos (cadr vert) 2 3); draw Y coordinate

    ); command
    (setq p1 (polar p1 (* pi 1.5) (* tz 1.5))); drop for next row
  ); foreach
); defun

 

I'd be interested to know whether that also gives you more decimal places than 3.

 

Additional things that you could do:  It could be made to remember your chosen text height and offer it as default on later use.  It could suppress all the echoing of command prompts and answers at the Command: line so you don't see all of that go by.  It could check whether you actually picked something, and whether it was actually a LWPolyline, before proceeding.  It could force a specific Layer and/or Text Style.  It could approach certain things in a different way so that it would also work with "heavy" Polylines [both 2D and 3D].  Etc.

Kent Cooper, AIA
0 Likes
Message 17 of 25

Anonymous
Not applicable
Ok. Thanks alot Kent. I would just like to know though, what are your current Lunits, Luprec and DIMZIN set to?
0 Likes
Message 18 of 25

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:
Ok. Thanks alot Kent. I would just like to know though, what are your current Lunits, Luprec and DIMZIN set to?

LUNITS = 4 [Architectural]

LUPREC = 4 [to 1/16"]

DIMZIN = 3 [include 0", suppress 0']

Kent Cooper, AIA
0 Likes
Message 19 of 25

Anonymous
Not applicable
Really very strange man. I changed my settings to yours, and it still gives me huge amounts of decimal places ?? How does it give you three on yours with the same program? It doesn't make any sense?
0 Likes
Message 20 of 25

ВeekeeCZ
Consultant
Consultant

What (rtos pi 2 4) returns?
Since you are defining 2nd and 3th position of rtos (in this case 2 3), then LUNITS and LUPREC are not affecting a result. Since you have a decimals (2), then UNITMODE has no affect. Only variable that you have to take care of is DIMZIN - you should take care of it as I did in my code in post 4 (click on spoiler). Since you have decimals, you should have DIMZIN set to 0.

 

That's all I know... and what says a help. If you still have trouble and you think your program is correct, the try reset Autocad settings to default (Start menu / Autodesk...), maybe it is stucked somewhere.. 

0 Likes