AutoLISP Routine Issue – Some Rectangles Not Processed or Colored

AutoLISP Routine Issue – Some Rectangles Not Processed or Colored

archana96
Enthusiast Enthusiast
488 Views
15 Replies
Message 1 of 16

AutoLISP Routine Issue – Some Rectangles Not Processed or Colored

archana96
Enthusiast
Enthusiast

I'm facing an issue with my AutoLISP routine. The script is designed to detect rectangles that contain inner text and two points, then export that data to Excel. After exporting, the processed rectangles should change color to green in AutoCAD. However, some rectangles are not changing color and are not being printed to Excel—even though they appear identical to others (same layer, same line weight). I've tried many approaches but haven't found a solution.

 I'm attaching both the LISP file and the drawing file to help you understand the issue better.

 

@BIGAL

@lee mack

 @_Tharwat 

0 Likes
Accepted solutions (1)
489 Views
15 Replies
Replies (15)
Message 2 of 16

komondormrex
Mentor
Mentor

what is contain for you?

does this contain or not?

komondormrex_0-1756066017978.png

 

0 Likes
Message 3 of 16

Kent1Cooper
Consultant
Consultant

I understand the need for the padding, because of the instances where the Text is not fully within the rectangle, as @komondormrex asked about.  The ones that fail all seem to be where two are close together, close enough apparently that the selection is finding more than one piece of Text and/or more than two Points.  It's probably related to screen resolution vs. Zoom level, because I find that if I Zoom in closer, it gets some that it missed when Zoomed out.

If you reduce the pad value back to 1, it may prevent that, but will probably mean some of the Text is not found [I suspect that's why you changed the pad value to 2].

Would it make sense to have it first find all the Text and force it to the smaller height, and run it with a reduced pad value?  Or maybe with no padding, if there's a height that will have all of them fully contained inside their rectangles.

Kent Cooper, AIA
0 Likes
Message 4 of 16

paullimapa
Mentor
Mentor

What I added to your code is:

1) Cmdecho: At beginning of code save current setting, next disable it by setting it to 0 and then at end restore the original setting

  (setq acadobj (vlax-get-acad-object)
        adoc (vla-get-ActiveDocument acadobj)
        cmdecho (getvar "cmdecho") ; save current setting
  )
  (setvar "cmdecho" 0) ; disable 
  (setvar "cmdecho" cmdecho)
  (princ)
)

2) Zoom: At the beginning of the repeat loop, Zoom to Object which is the Pline, then at the end of loop Zoom back to Previous location:

      ;; Loop through each rectangle
      (repeat (setq i (sslength ss))
        (setq ent (ssname ss (setq i (1- i))))
        (command"_.Zoom""_OB" ent "") ; zoom close up to pline
        (command"_.Zoom""_P") ; restore back to original view
      ) ; repeat

This seem to work but increases the time to process tremendously.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 16

archana96
Enthusiast
Enthusiast

Yes but once you check some objects inner . also thats also not export. 

0 Likes
Message 6 of 16

archana96
Enthusiast
Enthusiast

yes some text outer but  not all some in inner but its also not export to csv please check . and give final solution

0 Likes
Message 7 of 16

archana96
Enthusiast
Enthusiast

Yes but some texts are inner the rectangular that one also not export please once you check and give solution

0 Likes
Message 8 of 16

komondormrex
Mentor
Mentor

@archana96 

you mean you need one certain text just quite close to a certain rectangle? 

0 Likes
Message 9 of 16

archana96
Enthusiast
Enthusiast

YES.

0 Likes
Message 10 of 16

archana96
Enthusiast
Enthusiast

ITS LITTLE HELP BUT SOME OUT BOX THATS ALSO ITS TAKING(SMALL CONFUSION IS IT TAKING THAT OR ANOTHER ) AND SOME OBJECTS PROPER BUTS ITS NOT TAKING SO PLEASE PROVIDE BETTER SOLUTION 

0 Likes
Message 11 of 16

-didier-
Advisor
Advisor

Bonjour @archana96 

 

They are humans (volunteers) who answer you so we thank you for not talking to them as if you were chatting with an AI.

For example : SO PLEASE PROVIDE BETTER SOLUTION.

 

Back to the question:
I think we should change the insertion point of the text to "middle center" and if this insertion point is in the rectangle, launch the function.

 

2025-08-26_10-22-01.png

 

Amicalement

Éternel débutant.. my site for learning : Programmer dans AutoCAD

DA

EESignature

0 Likes
Message 12 of 16

komondormrex
Mentor
Mentor
Accepted solution

@archana96

check this one

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

;	komondormrex, aug 2025

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

(vl-catch-all-apply 'load (list "acetutil.fas"))
(vl-catch-all-apply 'load (list "acettest.fas"))

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

(defun get_coords (pline)
  	(mapcar 'cdr (vl-remove-if-not '(lambda (group) (= (car group) 10)) (entget pline)))
)

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

(defun c:FORSCB (/ count rect_proximity text_sset count_all text_dxf margin rect_sset point_sset point_list csv_line)
	(setq count 0
		  rect_proximity 40		;	quadrangle proximity in text heights to a given text. change to fine tune.
	)
    (if (and (setq text_sset (ssget '((0 . "text,mtext") (1 . "*SCB*"))))
			 (setq count_all (itoa (sslength text_sset)))
			 (setq csv_full_filename (getfiled "Select CSV Output file" "" "csv" 1))
			 (if (null (setq csv_id (open csv_full_filename "w")))
			 	 (alert "Can't open that CSV file. Please close it and try again.")
				 t
			 )
		)
		(progn
        (mapcar '(lambda (text) (if (and (or
											 (and
												  (equal '(0 . "TEXT") (assoc 0 (setq text_dxf (entget text))))
												  (setq margin -1)
										 		  (not (while (and (null (setq rect_sset (ssget "_cp" (mapcar '(lambda (vertex) (mapcar '+ '(0 0) vertex))
												  														  	   (acet-geom-txtbox text_dxf (setq margin (1+ margin)))
																								 	  )
																							         '((0 . "lwpolyline") (70 . 1) (90 . 4))
																						 )
																	   	 )
																   )
																   (< margin rect_proximity)
															  )
														)
												  )
												  rect_sset
											 )
											 (and
												  (equal '(0 . "MTEXT") (assoc 0 (setq text_dxf (entget text))))
												  (setq margin -1)
										 		  (not (while (and (null (setq rect_sset (ssget "_cp" (mapcar '(lambda (vertex) (mapcar '+ '(0 0) vertex))
												  														  	   (acet-geom-mtxtbox text_dxf (setq margin (1+ margin)))
																								 	  )
																							    	 '((0 . "lwpolyline") (70 . 1) (90 . 4))
															  			 				 )
																		 )
																   )
																   (< margin rect_proximity)
															  )
													   )
												  )
												  rect_sset
											 )
										 )
                 				  		 (= 1 (sslength rect_sset))
										 (setq point_sset (ssget "_cp" (get_coords (setq rect (ssname rect_sset 0))) '((0 . "point"))))
                 				  		 (= 2 (sslength point_sset))
            				    	)
							  		(progn
            				  				(mapcar '(lambda (ename) (vla-put-color (vlax-ename->vla-object ename) 3))
													 (list rect text (ssname point_sset 0) (ssname point_sset 1))
											)
											(setq point_list (vl-sort (list (cdr (assoc 10 (entget (ssname point_sset 0)))) (cdr (assoc 10 (entget (ssname point_sset 1)))))
																	 '(lambda (point_1 point_2) (< (car point_1) (car point_2)))
															 )
												  csv_line (strcat (cdr (assoc 1 text_dxf)) ",P1," (rtos (caar point_list) 2 4) "," (rtos (cadar point_list) 2 4)
                              															    ",P2," (rtos (caadr point_list) 2 4) "," (rtos (cadadr point_list) 2 4)
														   )
											)
        									(write-line csv_line csv_id)
							  				(princ (strcat "\rProcessed rects: " (itoa (setq count (1+ count))) " of " count_all))
							  		)
         		 		 		)
           		 )
          		 (vl-remove-if 'listp (mapcar 'cadr (ssnamex text_sset)))
  		)
        (close csv_id)
		(if (not (zerop count))
        	(princ (strcat "\n\\U+2705 CSV Export Complete: " csv_full_filename))
        	(princ "\n\\U+2639 No data found to export")
		)
		)
    )
	(princ)
)

;***************************************************************************************************************************************************************************
0 Likes
Message 13 of 16

archana96
Enthusiast
Enthusiast

CHECK THIS ONE SIR 

0 Likes
Message 14 of 16

archana96
Enthusiast
Enthusiast

INSERT MIDDLE CENTER IS FINE BEACUSE ITS ALMOST INNER 

0 Likes
Message 15 of 16

archana96
Enthusiast
Enthusiast

NICE BUT THE P1 P2 CAME SAME LINE BUT NEED P1 BELOW P2 

0 Likes
Message 16 of 16

komondormrex
Mentor
Mentor

change 3 lines starting with csv_line with these

csv_line (strcat (cdr (assoc 1 text_dxf)) ",P2," (rtos (caadr point_list) 2 4) "," (rtos (cadadr point_list) 2 4) "\n"
                                                ",P1," (rtos (caar point_list) 2 4) "," (rtos (cadar point_list) 2 4)
                               )
0 Likes