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

Text requires numeric value error while inserting text

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
nedo_alaimo
332 Views, 5 Replies

Text requires numeric value error while inserting text

Hello

I have written this AutoLISP code, but when it tries to insert the text label at the end of the function, the text command fails return the error "._text Requires a numeric value"

(defun c:CreateAreaBlock (/ blkName num layerName pt1 pt2 midPt ent rect label)
  ; Check if a block with the given name exists
  (defun blockExists (name)
    (tblsearch "BLOCK" name)
  )

  ; Check if a block with the given number exists
  (defun blockWithNameExists (num)
    (blockExists (strcat "Area-" (itoa num)))
  )

  ; Set the layer name for the block
  (setq layerName "TrafficAreas")

  ; Prompt the user to specify the rectangle's corners
  (setq pt1 (getpoint "\nSpecify first corner of the rectangle: "))
  (setq pt2 (getcorner pt1 "\nSpecify opposite corner of the rectangle: "))
  (setq midPt (list (/ (+ (car pt1) (car pt2)) 2) (/ (+ (cadr pt1) (cadr pt2)) 2)))

  ; Create the rectangle using the command function
  (command "._rectang" pt1 pt2)
  (princ "\nCreated rectangle\n")
  (command "")

  ; Get the rectangle entity
  (setq ent (entlast))

  ; Prompt the user to specify the block number
  (setq num nil blkName nil)
  (while (or (not num) (blockWithNameExists num))
    (setq num (getint "\nInsert a number for the area block: "))
    (if (and num (blockWithNameExists num))
      (alert "Error: Block with the same number already exists.")
    )
  )

  ; Set the block name based on the block number
  (setq blkName (strcat "Area-" (itoa num)))

  ; Create a layer for the block and make it current
  (command "_-layer" "_make" layerName "_on" layerName)
  (command "")

  ; Create the block using the rectangle entity
  (command "._-block" blkName midPt ent)
  (command "")
  (princ "\nCreated block\n")
  
  ; Delete the rectangle 
  (entdel ent)
  (entdel ent)

  ; Insert the block at the midpoint of the rectangle
  (command "_-insert" blkName midPt)
  (command "")
  (princ "\nInserted block\n")

  ; Insert a label in the midpoint of the block
  (setq labelText blkName)
  (setq labelHeight 200.0)
  (setq rotation 0)

  ; Set the first point to the midpoint
  (setq firstPt midpt)

  ; Create the single-line text using the "._text" command
  (command "._text" firstPt labelHeight labelText)

  ; Return control to user
  (princ)
)

what am I missing?

 

regards

 

5 REPLIES 5
Message 2 of 6
pendean
in reply to: nedo_alaimo

Your LISP fails here for me before it gets that far, I suspect it does for you too but your commandline scroll by too fast?

pendean_1-1682008966830.png

 

Message 3 of 6
Sea-Haven
in reply to: nedo_alaimo

A suggestion, would you not enter length and width for rectang, change code below about corner point values. Saves drawing the rectang as it makes the block direct.

 

 

(defun recblk (blkname / )
 (entmake (list (cons 0 "BLOCK")
    (cons 2 Blkname)
    (cons 70 2)
    (cons 10 (list 0 0 0))
    (CONS 8 "0")
  ))
(setq vertexList
  (list
  (list -3.25 -3.25 0.)
  (list 3.25 -3.25 0.)
  (list 3.25 3.25 0.)
  (list -3.25 3.25 0.)
  )
)
(entmake
    (append 
    (list '(0 . "LWPOLYLINE")
    '(100 . "AcDbEntity")
    '(100 . "AcDbPolyline")
    (cons 90 (length vertexList))
    (cons 70 1)			; 1 closed : 0 open
    (cons 8 "0")
    (cons 38 0.0)
    (cons 210 (list 0.0 0.0 1.0))
    )
    (mapcar '(lambda (pt) (cons 10 pt)) vertexList)
    )
)
(entmake (list (cons 0 "ENDBLK")))
(princ)
)

 

 

,Part 2 

(command "._text" firstPt labelHeight labelText)

If the current text style has a text height set then the command will fail. You can check for textheight and do a choice of with or without text height, but I always made the text style  "standard" with a 0 text height set in that text style..

Message 4 of 6
komondormrex
in reply to: nedo_alaimo

hey,

check this

 

;*****************************************************************************************************************

;	komondormrex, apr 2023

;*****************************************************************************************************************

(defun set_layer (layer_name / added_layer)
	(if (vl-catch-all-error-p
			(setq added_layer
		       	(vl-catch-all-apply
			   	 'vla-item
			   		(list (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
			   			layer_name
			   		)
	   			)
			)
		)
		(setq added_layer (vla-add (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
					   layer_name
				)
		)
		(if (minusp (vlax-get added_layer 'freeze))
				(vla-put-freeze added_layer :vlax-false)
				(if (minusp (vlax-get added_layer 'lock))
					(vla-put-lock added_layer :vlax-false)
				)
		)
	)
	(vla-put-activelayer (vla-get-activedocument (vlax-get-acad-object)) added_layer)
	(princ)
)

;*****************************************************************************************************************

(defun c:create_area_block (/ pt1 pt2 midPt rect_object area_block text_object block_name area_block_reference)
	(set_layer "TrafficAreas")
	(if (null block_number)
		(setq block_number 1)
		(setq block_number (1+ block_number))
	)
	(while (and
			(null (initget 5))
			(if (vl-catch-all-error-p
	   			(vl-catch-all-apply
	   				'vla-item
	   				(list
						(vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
	   					(setq block_name (strcat "Area-"
									 (itoa
										(setq block_number
											(getint (strcat "\nUnhighlight or Enter number for area block <"
															(itoa block_number)
															">: "
													)
											)
										)
									  )
								)
						)
	   				)
				)
			    )
				nil
				(not (alert (strcat "Block \"" block_name "\" is defined already")))
			)
		)
	)
	(setq pt1 (getpoint "\nSpecify first corner of the rectangle: ")
  		pt2 (getcorner pt1 "\nSpecify opposite corner of the rectangle: ")
  		midPt (mapcar '* '(0.5 0.5) (mapcar '+ pt1 pt2))
		pt1 (mapcar '- pt1 midPt)
		pt2 (mapcar '- pt2 midPt)
		rect_vertices_raw_list (list (car pt1) (cadr pt1) (car pt1) (cadr pt2) (car pt2) (cadr pt2) (car pt2) (cadr pt1))
		area_block (vla-add
				(vla-get-blocks
					(vla-get-activedocument
						(vlax-get-acad-object)
					)
				)
				(vlax-3d-point '(0 0 0))
				block_name
			  )
	)
	(vla-put-closed
		(vla-addlightweightpolyline
			area_block
			(vlax-safearray-fill
				(vlax-make-safearray vlax-vbdouble '(0 . 7))
				rect_vertices_raw_list
		    )
		)
		:vlax-true
	)
	(vla-addtext
   		area_block
		block_name
		(vlax-3d-point '(0 0 0))
		200
	)
	(setq area_block_reference
		(vla-insertblock
			(vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
			(vlax-3d-point (trans midPt 1 0))
			block_name
			1 1 1 0
		)
	)
  	(princ "Done")
  	(princ)
)

;*****************************************************************************************************************
Message 5 of 6
Kent1Cooper
in reply to: nedo_alaimo


@nedo_alaimo wrote:
....
  ; Create the block using the rectangle entity
  (command "._-block" blkName midPt ent)
  (command "")
  (princ "\nCreated block\n")
  
  ; Delete the rectangle 
  (entdel ent)
  (entdel ent)
....

I don't know whether this could be related to the problem, but a command-line Block command always removes the selected object(s) from the drawing in creating the Block.  It's not like the Block dialog box, where you get a choice of whether or not to do that.  So your (entdel ent) lines will fail, because 'ent' will already be gone.  I haven't tried the routine -- maybe it will carry on anyway, but you can get rid of that part even if it isn't the troublemaker.

 

And by the way, there's no need to put the concluding Enter completing the object selection in a separate (command) function.  This:

  (command "._-block" blkName midPt ent)
  (command "")

can be just this:

  (command "._-block" blkName midPt ent "")

Kent Cooper, AIA
Message 6 of 6
nedo_alaimo
in reply to: Kent1Cooper

Thank you! this works exacly as expected!

 

this is the solution I came up with in the meantime:

(defun c:CREATEAREABLOCK ()

  ; Set the layer name for the block
  (setq layerName "TrafficAreas")
       
  ; Create a layer for the block and make it current
  (command "_-layer" "_make" layerName "_on" layerName)
  (command "")

  ; Check if a block with the given number exists
  (defun BLOCKWITHNAMEEXISTS (num)
    (tblsearch "BLOCK" (strcat "Area-" (itoa num)))
  )

  ; Prompt the user to specify the rectangle's corners
  (setq pt1 (getpoint "\nSpecify first corner of the rectangle: "))
  (setq pt2 (getcorner pt1 "\nSpecify opposite corner of the rectangle: "))
  (setq midPt (list (/ (+ (car pt1) (car pt2)) 2) (/ (+ (cadr pt1) (cadr pt2)) 2)))

  ; Create the rectangle using the command function
  (command "._rectang" pt1 pt2)
  (princ "\nCreated rectangle\n")
  (command "")

  ; Get the rectangle entity
  (setq ent1 (entlast))

  ; Prompt the user to specify the block number
  (setq num nil blkName nil)
  (while (or (not num) (BLOCKWITHNAMEEXISTS num))
    (setq num (getint "\nInsert a number for the area block: "))
    (if (and num (BLOCKWITHNAMEEXISTS num))
      (alert "Error: Block with the same number already exists.")
    )
  )

  ; Set the block name based on the block number
  (setq blkName (strcat "Area-" (itoa num)))
  
  ; Insert a label in the midpoint of the block
  (command "._text" midpt 50.0 0.0 blkName)
  (command "")

  (setq ss (ssget "x" (list (cons 0 "TEXT") (cons 1 blkName))))
  (setq ent2 (ssname ss 0))

  ; Create the block using the rectangle and label entities
  (command "-block" blkName midPt "_non" ent1 ent2 "" "_-insert" blkName midPt 1.0000 1.0000 0.0)
  (princ "\nCreated block\n")

  ; Return control to user
  (princ)
)

 

but your solution is way cleaner.

 

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

Post to forums  

Forma Design Contest


Autodesk Design & Make Report