This tool used to work, but no longer in AutoCAD2016

This tool used to work, but no longer in AutoCAD2016

Anonymous
Not applicable
931 Views
3 Replies
Message 1 of 4

This tool used to work, but no longer in AutoCAD2016

Anonymous
Not applicable

Greetings all! I just started a new position at a steel detailing company. From what I hear, they just recently upgraded to AutoCAD 2016 and some of the "tools" they have been using are no longer functioning correctly. 

 

I have a Python programming background, so learning autoLisp was pretty straightforward, but I have -zero- experience with AutoCAD itself. After looking through and reengineering the code, I don't see why it isn't functioning correctly, and was hoping someone here might be able to peruse and find any glaring issues.

 

The routine stops after creating a rectangle and says: error bad argument type: consp 0.0

 

Thanks for any and all help. Cheers!

 

 

(setvar "cmdecho" 0)

(setq deckwidth 0 decklength 0 lapleft 0 lapright 0 l_text 0 ang 0 t_distance 0 pt1 '(0 0)
pt2 '(0 0) pt3 '(0 0) pt4 '(0 0) x1 0 x2 0 x3 0)
;this zeroes the variables for the new rectangle

(setq deckcolor (strcase (getstring T "\nEnter the deck color code: ")))
;this sets the color code for the deck sequence color

(setq current_ltype (getvar "celtype"))
;this retrieves the current linetype and sets it to "current_ltype".

(command "setvar" "celtype" "bylayer")
;this sets the linetype to "bylayer" for this routine.

(setq current_lcolor (getvar "cecolor"))
;this retrieves the current line color and sets it to "current_lcolor".

(command "setvar" "cecolor" "bylayer")
;this sets the line color to "bylayer" for this routine.

(initget 7 "Y N")
(setq lapleft (getkword "\nIs deck lapped at the left end? <Y/N>: "))
;this prompts the user for the left end lap condition and sets it to "lapleft"

(initget 7 "Y N")
(setq lapright (getkword "\nIs deck lapped at the right end? <Y/N>: "))
;this prompts the user for the right end lap condition and sets it to "lapright"

(initget 7 "Y N")
(setq prt_text (getkword "\nPrint the deck length? <Y/N>: "))
;this prompts the user to decide on printing the deck length.

(initget 7 "Y N")
(setq vdecking (getkword "\nIs this 36 inch decking? <Y/N>: "))
;this asks the user if the deck width is 36inch standard

(setq deckwidth 36)
;this sets the width of the rectangle in inch units. (Also-start here for slope function)

(setq pt1 (getpoint "\nPick the deck starting point: "))
;this prompts the user for a starting point and sets it to "pt1"

(setq pt2 (getpoint "\nPick the deck ending point: "))
;this prompts the user for an ending point and sets it to "pt2"

(setq x1 (car pt1))
;this extracts the "x" coordinate from "pt1" and sets it to "x1"

;lapped condition-left
(setq x3 (- (car pt1) 2))
;this extracts the "x" coordinate from "pt1", subtracts 3 inches from it
;and sets it to "x3". Changed subtraction of 3 to 2.

(setq pt3 (cons x3 (cdr pt1)))
;this combines the new "x3" coordinate and the old "y" coordinate of "pt1"
;and sets "pt3", the point where the rectangle starts with panel lap on the
;left end

(setq y1 (cadr pt1))
;this extracts the "y" coordinate from pt1 and sets it to "y1"

(setq x2 (car pt2))
;this extracts the "x" coordinate from pt2 and sets it to "x2"

(setq decklength (- x2 x1))
;this finds the length between the two coordinates and sets it to "decklength"

(command "osmode" 0)
;turns running object snap modes off

;lapped condition-right
(setq pt4 (list (if (= lapright "Y") (+ (+ decklength x1) 2) (+ decklength x1)) (+ deckwidth y1)))
;this sets the proper corner for the right end lap condition. Changed add of 3 to add
;of 2.

(command "rectang" (if (= lapleft "N") pt1 pt3) pt4)
;this enters the rectangle command at the command line, starts the rectangle
;at the proper point per the "lapleft" condition then places the opposite corner
;using the variables "decklength", "deckwidth" and "lapright".

(command "osmode" 35)
;turns running object snap modes on

(setq l_text (rtos (- (cadr (nth 16 (entget (entlast)))) (cadr (assoc 10 (entget (entlast))))) 4 3))
;this finds the database information for the last (rectangle) drawn, retrieves the left and right
;points, calculates the length (in foot-inch format) and sets it to the variable "l_text"

(setq ang (eval (angle pt1 pt4)))
;this finds the angle between the first pick point and the opposite corner and sets it to "ang"
;(variable in degree units)

(setq t_distance (/ (distance pt1 pt4) 2))
;this finds the distance between the first pick point and the opposite corner, divides it by two,
;and sets it to "t_distance"

(if (= prt_text "Y")
(command "insert" "deck_length_text" (polar pt1 ang t_distance) "" "" "0" l_text)
;this inserts a visible attributed text for deck length

(command "insert" "deck_length_text_inv" (polar pt1 ang t_distance) "" "" "0" l_text)
;if "prt_text="N", then an invisible attributed text will be inserted at the same point
) ;closes the 'if' statement

;(if (= prt_text "Y")
;(setq key (append (list l_text) (list key)))
;this sets and updates the "key" deck lengths of variable "l_text". "key" is not zeroed
;when the Deck36 routine is re-run.
😉 ;closes the 'if' statement

(command "setvar" "celtype" current_ltype)
;this restores the previous linetype.

(command "setvar" "cecolor" current_lcolor)
;this restores the previous line color.
(princ)

0 Likes
Accepted solutions (1)
932 Views
3 Replies
Replies (3)
Message 2 of 4

CodeDing
Advisor
Advisor

@Anonymous,

 

Although this may only be a partial solution, it should help get you on the right track..

This code is causing your issue:

(setq l_text (rtos (- (cadr (nth 16 (entget (entlast)))) (cadr (assoc 10 (entget (entlast))))) 4 3))
;this finds the database information for the last (rectangle) drawn, retrieves the left and right
;points, calculates the length (in foot-inch format) and sets it to the variable "l_text"

...you are using "cadr" when you should be using "cdr" (cdr is used strictly for Dotted Pairs).

HOWEVER, you will be running into another issue. Your line of code is trying to find the Left-most and Right-most points in your rectangle... Assuming your rectangle is never rotated, you should look into searching your entity for the FIRST TWO instances of a vertex (group 10 dxf code). 

I don't have time for a full-blown explanation of searching your entity for vertexes, but if you get some time, use THIS link as a resource and experiment with creating a rectangle, then typing... (entget (entlast)) ...into the commmand line.

Try this code to replace the example above (not tested, and rest of your code not reviewed)...

(setq e (entget (entlast)) tmpcnt 0)
(repeat (length e)
(setq etmp (nth tmpcnt e))
(if (= (car etmp) 10) (setq corners (cons (cadr etmp) corners)))
(setq tmpcnt (1+ tmpcnt))
);repeat
(setq l_text (rtos (- (nth 1 (reverse corners)) (nth 0 (reverse corners))) 4 3))

Best,

~DD

0 Likes
Message 3 of 4

Kent1Cooper
Consultant
Consultant

@CodeDing wrote:

This code is causing your issue:

(setq l_text (rtos (- (cadr (nth 16 (entget (entlast)))) (cadr (assoc 10 (entget (entlast))))) 4 3))
....

...you are using "cadr" when you should be using "cdr" ....


 

I don't think that's the issue.  Using (cadr) is appropriate to pull the X coordinate out -- note that they're not pulling the X coordinate as the first item in a point list, because they're not pulling from the point list only, but from the full entity-data-entry list with the 10 in front, so the X coordinate is the second item, which is what (cadr) gets from a list.

 

I think the problem is likely to be in this part:

    (nth 16 (entget (entlast)))

That's pulling the 17th entry from the entity data list [the first is (nth 0)].  But version changes occasionally bring changes in the format of entity data, and in particular, somewhere along the way, such a list for a Polyline acquired additional entries.  If the code was written in a version before that change, then it fails because the 17th entry in such a list for a rectangle [here from Acad2019] is no longer a vertex [(10)-code] point list, but is now:

    (41 . 0.0)

-- there's the 0.0 that the error message is talking about -- since the vertex entry it was looking for got kicked down the road by the additional entries in the new data format.

 

Don't pull entity data from an entry in a specific position  in an entity data list -- that can [and does!] vary.  Always pull it from either a specific association  [10 in the case of Polyline vertices] or by other means such as [in the case of Polyline vertices] (vlax-curve...) functions.  Since you know where the rectangle started [whether that was pt1 or pt3], and that pt4 is the opposite corner, this particular task could be done this way, :

 

  (setq l_text

    (rtos

      (-  ; difference between X coordinates:

        (car pt4); i.e. the opposite corner from the start

        (cadr (assoc 10 (entget (entlast)))); start point, i.e. first vertex, which was either pt1 or pt3

      ); -

      4 3

    ); rtos

  ); setq

 

Or, back into a single line without the commentary:

(setq l_text (rtos (- (car pt4) (cadr (assoc 10 (entget (entlast))))) 4 3))

Kent Cooper, AIA
Message 4 of 4

dbhunia
Advisor
Advisor
Accepted solution

Hi,

 

First thing, @Kent1Cooper said to replace the line with ........

"(setq l_text (rtos (- (car pt4) (cadr (assoc 10 (entget (entlast))))) 4 3))is needed.......

 

But still you need to make some changes........

 

1.    You have to add the line "(command "attreq" 0)" otherwise you can not enter Attribute value using code....

 

2    And you need to change in the lines ......

 

"(command "insert" "deck_length_text" (polar pt1 ang t_distance) "" "" "0" l_text)" and

"(command "insert" "deck_length_text_inv" (polar pt1 ang t_distance) "" "" "0" l_text)"

 

Because it will place the Default Attribute value.......

 

 

So for this reason some more modifications are needed ....... for that go through the attached code.....

 

Only thing you have to change the Attribute TAG Name (this are case sensitive) as in your Blocks "deck_length_text" and "deck_length_text_inv"...... in the code I have point out those line make the changes there.........

 

 

Hopefully it will work in AutoCAD 2016, it has been tested in AutoCAD 2017........


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes