pick place to text; error: bad argument type: VLA-OBJECT nil

pick place to text; error: bad argument type: VLA-OBJECT nil

Anonymous
Not applicable
2,602 Views
8 Replies
Message 1 of 9

pick place to text; error: bad argument type: VLA-OBJECT nil

Anonymous
Not applicable

I've tried looking around the forums for similar problems (of which there are a lot), but none of the solutions seem to work for me. I've tried adding the line "(SETQ *DOC* (VLA-GET-ACTIVEDOCUMENT (VLAX-GET-ACAD-OBJECT)))"/"(SETQ *ACTIVEDOC* (VLA-GET-ACTIVEDOCUMENT (VLAX-GET-ACAD-OBJECT)))" among other things to try and fix this error, but nothing seems to work.

 

Strangely enough, the script worked fine yesterday, and I've tried restarting AutoCAD since, but the problem persists.

 

The script is supposed to let me click on a closed polyline and generate a text that displays the area in m². As mentioned, the script worked fine yesterday but now only returns the VLA-OBJECT nil error as soon as I try to place the text. In addition to locating what's wrong with the code, I'd like to know if I could improve the script so that I can keep adding text for multiple polylines until I abort the command so that I don't have to input it for each polyline individually.

 

(DEFUN C:am (/
                          ACAD-OBJ
                          ADOC
                          AREA
                          AREA-M^2
                          AREA-TEXT
                          INSERT
                          LWPOLY
                          LWPOLY-OBJ
                          MODEL
                          SELECTIONSETS
                          TEXT-AREAS
                          TEXTSIZE)


  (VL-LOAD-COM)
  (SETQ *ACTIVEDOC* (VLA-GET-ACTIVEDOCUMENT (VLAX-GET-ACAD-OBJECT)))
  (SETQ ACAD-OBJ (VLAX-GET-ACAD-OBJECT))
  (SETQ ADOC (VLA-GET-ACTIVEDOCUMENT ACAD-OBJ))
  (SETQ MODEL (VLA-GET-MODELSPACE ADOC))
  (SETQ SELECTIONSETS (VLA-GET-SELECTIONSETS ADOC))
  (VLA-GET-ACTIVESELECTIONSET ADOC)
  (SETQ LWPOLY (SSGET ":s" '((0 . "LWPOLY*"))))
  (SETQ LWPOLY-OBJ (VLA-ITEM (VLA-GET-ACTIVESELECTIONSET ADOC) 0))
  (SETQ INSERT (GETVAR 'INSUNITS))
  (SETVAR 'INSUNITS 4) ; units in mm
  (SETQ AREA (VLA-GET-AREA LWPOLY-OBJ))
  (SETQ AREA-M^2 (RTOS (/ AREA 1000.0 1000.0) 2 2))
  (SETQ AREA-TEXT (STRCAT AREA-M^2 " m" "\U+00B2"))
  (SETQ TEXTSIZE (GETVAR 'DIMTXT))
 (SETQ OSMODE (GETVAR 'OSMODE))
 (SETVAR 'OSMODE 0)
 (SETQ TEXT-AREAS (VLA-ADDTEXT MODEL AREA-TEXT (VLAX-3D-POINT (GETPOINT "\ pick place to text")) TEXTSIZE))
 (setvar 'OSMODE OSMODE)
 (VLA-DELETE SS)
)

 

0 Likes
Accepted solutions (1)
2,603 Views
8 Replies
Replies (8)
Message 2 of 9

doaiena
Collaborator
Collaborator
Accepted solution

The last line in your code is throwing the error, because there is no object under the symbol "ss". Here is a small function that will add the text at the bounding box center of all selected polylines.

(defun c:test ( / ss doc ctr obj minPt maxPt cenPt)

(if (setq ss (ssget '((0 . "LWPOLYLINE"))))
(progn

(setq doc (vla-get-activeDocument (vlax-get-acad-object)))

(setq ctr 0)
(repeat (sslength ss)
(setq obj (vlax-ename->vla-object (ssname ss ctr)))

(vla-getboundingbox obj 'minPt 'maxPt)
(setq cenPt (mapcar '(lambda (p1 p2) (+ (/ (- p2 p1) 2) p1))
(vlax-safearray->list minPt) (vlax-safearray->list maxPt)))

(vla-addText (vla-get-modelspace doc)
(strcat (rtos (/ (vla-get-area obj) 1000000.0) 2 2) " m" "\U+00B2")
(vlax-3d-point cenPt) (getvar "dimtxt"))

(setq ctr (1+ ctr))
);repeat

))

(princ)
);defun

 

Message 3 of 9

ВeekeeCZ
Consultant
Consultant

Weird... There is 'selectionsets' variable - never used, then there is used 'ss' - never created...

 


... (SETQ ADOC (VLA-GET-ACTIVEDOCUMENT ACAD-OBJ)) (SETQ MODEL (VLA-GET-MODELSPACE ADOC)) ; (SETQ SELECTIONSETS (VLA-GET-SELECTIONSETS ADOC)) (VLA-GET-ACTIVESELECTIONSET ADOC) (SETQ LWPOLY (SSGET ":s" '((0 . "LWPOLY*"))))
... (SETQ TEXT-AREAS (VLA-ADDTEXT MODEL AREA-TEXT (VLAX-3D-POINT (GETPOINT "\ pick place to text")) TEXTSIZE)) (setvar 'OSMODE OSMODE) ; (VLA-DELETE SS) )

 

 

 

 

0 Likes
Message 4 of 9

Sea-Haven
Mentor
Mentor

Simpler you are getting a selection set, add a IF in case selection did not work. No need for like 10 lines of code.

I don't think shortening LWpoly makes any difference but *Polyline does.

 

(SETQ LWPOLY (SSGET ":s" '((0 . "LWPOLY*"))))
  (SETQ LWPOLY-OBJ (VLA-ITEM (VLA-GET-ACTIVESELECTIONSET ADOC) 0))

(SETQ LWPOLY (SSGET ":s" '((0 . "LWPOLYLINE")))) (setq LWPOLY-obj (vlax-ename->vla-object (ssname lwpoly 0)))

also

(setq ans (/ area 1e06))

 

0 Likes
Message 5 of 9

Anonymous
Not applicable

Thank you! This works perfectly!

0 Likes
Message 6 of 9

hak_vz
Advisor
Advisor

Here is a different approach to the subject of expressing entity area. I have a situations when on a drawing

I have to present area of some shape and to categorize it to some predefined group (let say A B P Pi ...).

Unfortunately, for some undetermined reason it trows an error (Unknown command WAREA). Numeric value is represented in a delimited form (for example "1 125 125.125").  It is not limited to lwpolylines.

 

(defun c:warea ( / *error*  barea  tsize stsize tel telname decimals poz del pt  x1 x2 y1 y2  number b  i j category text1 ltext format_number)
    (defun *error* ()(setvar "cmdecho" 1)(princ))
    (defun format_number (num decimals  delimiter / por b i ret neg)
        (if (minusp num) (setq neg "-") (setq neg ""))
        (setq rest(- (abs num) (fix (abs num))))
        (if (and (> rest 0.5) (= decimals 0)) (setq num (+ num 1)))
        (setq rest (substr (rtos rest 2 decimals) 3) ret "")
        (setq poz (vl-string-position (ascii ".") rest))
        (setq num (abs (fix num)) i 0 j 0)

        (while (> num 0.0)
            (setq por (cons (fix (rem num 1000.0)) por)
                  num (/ (- num (car por)) 1000.0)
            )
        )
        (while (< i (length por))
            (setq b (rtos (nth i por) 2 0))
            (if (and (eq (strlen b)1) (> i 0)) (setq b (strcat "00" b)))
            (if (and (eq (strlen b)2) (> i 0)) (setq b (strcat "0" b)))
            (if (< i (- (length por) 1)) (setq ret (strcat ret b " ")) (setq ret (strcat ret b)))
            (setq i (+ 1  i))
        )
        (if (= (strlen ret) 0) (setq ret "0"))
        (if (= decimals 0) (setq delimiter "" rest ""))
        (strcat neg ret delimiter rest)
    )
    
	(setvar "cmdecho" 0)
    (setq tsize (getreal "\n Text size > "))
    (setq stsize (* 0.65 tsize))
    (setq decimals (getint "\n Round to decimal places > "))
    (if (> decimals 0) (setq delimiter (getstring "\nDecimal point delimiter >")) (setq delimiter ""))
    
    (entmakex (list (cons 0 "text") (cons 10 (list 0 0 0)) (cons 40 tsize) (cons 50 0) (cons 1 "A")))
    (setq tel (entget(entlast)))
    (setq telname (cdr (assoc -1 tel)))
    (setq del (car (nth 1 (textbox tel))))
    (entdel telname)
    (setq category (getstring "\nPredefined type P/PI/A/AI/B/BI/C/CI/o/n/j, free type or <enter> >"))

	(while (< j 1000)
    
    (setq barea (getpropertyvalue(car(entsel "\nSelect closed area> ")) "area"))
    (setq pt (getpoint "\nLocation where tekst starts > "))	
    (setq x1 (+ (car pt) 0.5) y1 (+ (cadr pt) stsize)  y2 (- (cadr pt) (* 0.8 stsize)))
    (setq number (format_number barea decimals delimiter))
    
    ;check predefined types
    (if (member (strcase category T) (list "a" "ai" "b" "bi" "c" "ci" "p" "pi" "o" "n" "j")) (setq category (strcase category T)))
    (if (eq category "")  (setq text1 (strcat a " m"))(setq text1 (strcat " = " number " m")))
    (if (member category (list "a" "ai"))(setq text1 (strcat "A" text1)))
    (if (member  category (list "b" "bi"))(setq text1 (strcat "B" text1)))
    (if (member  category (list "c" "ci"))(setq text1 (strcat "C" text1)))
    (if (member category (list "p" "pi"))(setq text1 (strcat "P" text1)))
    (if(eq category "o")(setq text1 (strcat "o" text1)))
    (if(eq category "j")(setq text1 (strcat "j" text1)))
    (if(eq category "n")(setq text1 (strcat "N" text1)))
    
    
    (if (and(> (strlen category) 0)(not(member category (list "a" "ai" "b" "bi" "c" "ci" "p" "pi" "o" "n" "j"))))
        (setq text1 (strcat category text1))
    )
    
    ;create subscript
    (command "text" "R" pt tsize 0 text1 "")
    (setq tel (entget(entlast)))
    (setq telname (cdr (assoc -1 tel)))
    (setq ltext (car (nth 1 (textbox tel))))
    (entdel (entlast))
    (setq x1 (+ (car pt) ltext) y (cadr pt) pt (list x1 y))
    (command "text" "R" pt tsize 0 text1 "")
    (setq x2 (+(- (car pt) ltext) (* 0.8 del)))
    (if (member category (list "ai" "bi" "pi"))(command "text" "c" (list x2 y2) stsize 0 "i"))
    (if (eq category "c")(command "text" (list x2 y2) stsize 0 "1"))
    (if (eq category "ci")(command "text" (list x2 y2) stsize 0 "1i"))
    
    ;create square symbol
    (command "text" "L" (list x1 y1) stsize 0 "2" "")
    (setq j (+ j 1))
	)			
	(setvar "cmdecho" 1)
	(princ)
)


 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 7 of 9

Anonymous
Not applicable

Hi,

 

While this script works very well, I've noticed something strange about the size of the text it generates. No matter how much I try set the default text size of various text types, text generated by this command always comes out a different size. Where does this come from? In my current drawing, I want all text to have a height of 250 mm, which I've set in all places I could find, but when I use the command, the text always comes out with a height of 500 mm no matter what I set as the default. Is there any way to correct this, either in my AutoCAD settings or in the script?

0 Likes
Message 8 of 9

doaiena
Collaborator
Collaborator

The text height is set by the "DIMTXT" variable /you will see that inside the VLA-ADDTEXT method/. You can replace (getvar "DIMTXT") with "250" /as string/. The VLA-ADDTEXT method inserts text using the current text style that is set. If you would like to change that style to something else, you can use this:

(defun c:test ( / ss doc ctr obj minPt maxPt cenPt text)

(if (setq ss (ssget '((0 . "LWPOLYLINE"))))
(progn

(setq doc (vla-get-activeDocument (vlax-get-acad-object)))

(setq ctr 0)
(repeat (sslength ss)
(setq obj (vlax-ename->vla-object (ssname ss ctr)))

(vla-getboundingbox obj 'minPt 'maxPt)
(setq cenPt (mapcar '(lambda (p1 p2) (+ (/ (- p2 p1) 2) p1))
(vlax-safearray->list minPt) (vlax-safearray->list maxPt)))

(setq text (vla-addText (vla-get-modelspace doc)
(strcat (rtos (/ (vla-get-area obj) 1000000.0) 2 2) " m" "\U+00B2")
(vlax-3d-point cenPt) (getvar "dimtxt"))); <--- Text Height
(vla-put-StyleName text "MyTextStyle"); <--- If you want to set a text style do it here, else remove this line

(setq ctr (1+ ctr))
);repeat

))

(princ)
);defun
0 Likes
Message 9 of 9

ronjonp
Advisor
Advisor

You also might look into using fields for this task. Then the numbers will auto update with changes.

0 Likes