Jump to content

Recommended Posts

Posted (edited)

Good afternoon, everyone.
I am trying to select a arrayrect and insert a TEXT with the number of array elements into the drawing.
But the array is not selected.
How do I set the array selection correctly?

(defun c:CountElemArrayrect (/ ss n pt)
  (princ "Select a arrayrect: ")
  (setq ss (ssget '((0 . "ARRAY"))) 
        n 0)
  (if ss
    (progn
      (setq n (vla-get-Count
                (vlax-ename->vla-object (ssname ss 0))))
      (princ (strcat "The number of elements in the array: " (itoa n)))
      (princ "Specify the insertion point of the text: ")
      (setq pt (getpoint))
      (if pt
        (entmakex
          (list
            (cons 0 "TEXT")
            (cons 10 pt)
            (cons 1 (itoa n))
            (cons 40 25) ; text height
            (cons 7 (getvar "TEXTSTYLE"))
          )
        )
      )
    )
  )
  (princ)
)

 

 

arrayrect.png

Edited by Nikon
Posted

If you inspect the DXF data for a rectangular array (e.g. using my Entity List program, for example), you will see that it is in fact a block reference (INSERT).

  • Thanks 1
Posted

Consider the following function to obtain the data from the array:

(defun LM:arraydata ( ent / enx lst rtn )
    (if
        (and
            (setq enx (entget ent))
            (= "INSERT" (cdr (assoc 0 enx)))
            (setq lst
                (vl-some
                    (function
                        (lambda ( x )
                            (if (and (= 330 (car x)) (= "ACDBASSOCDEPENDENCY" (cdr (assoc 0 (entget (cdr x))))))
                                (cdr (assoc 330 (entget (cdr x))))
                            )
                        )
                    )
                    (member '(102 . "{ACAD_REACTORS") enx)
                )
            )
            (setq lst (entget lst))
            (setq lst (cons nil (member (assoc 1 lst) lst)))
        )
        (while lst
            (setq rtn (cons (cons (cdadr lst) (cdar (cddddr lst))) rtn)
                  lst (cdddr (cddddr lst))
            )
        )
    )
    (reverse rtn)
)


Call with the array block reference entity, e.g.:

(LM:arraydata (car (entsel)))

 

  • Thanks 1
Posted (edited)

The code writes the same amount for arrays with different numbers of elements...

(defun c:ArrCountTxt ( / ent arrData count insPt txtHeight)
  (prompt "Select a arrayrect: ")
  (setq ent (car (entsel)))
  (if (and ent (= "INSERT" (cdr (assoc 0 (entget ent)))))
    (progn
            (setq arrData (LM:arraydata ent))
      ;; count the number of elements
      (setq count (length arrData))
      
      (princ (strcat "Number of array elements: " (itoa count)))
     
      (prompt "Specify the insertion point of the text: ")
      (setq insPt (getpoint))
      
      (setq txtHeight 20)
      
      (entmake
        (list
          (cons 0 "TEXT")
          (cons 8 (cdr (assoc 8 (entget ent)))) ; 
          (cons 10 insPt)
          (cons 40 txtHeight)
          (cons 1 (strcat "Quantity: " (itoa count)))
          (cons 7 "Standard") 
          (cons 50 0.0)
        )
      )
    )
      )
  (princ)
)

(defun LM:arraydata ( ent / enx lst rtn )
  (if
    (and
      (setq enx (entget ent))
      (= "INSERT" (cdr (assoc 0 enx)))
      (setq lst
        (vl-some
          (function
            (lambda ( x )
              (if (and (= 330 (car x)) (= "ACDBASSOCDEPENDENCY" (cdr (assoc 0 (entget (cdr x))))))
                  (cdr (assoc 330 (entget (cdr x))))
              )
            )
          )
          (member '(102 . "{ACAD_REACTORS") enx)
        )
      )
      (setq lst (entget lst))
      (setq lst (cons nil (member (assoc 1 lst) lst)))
    )
    (while lst
      (setq rtn (cons (cons (cdadr lst) (cdar (cddddr lst))) rtn)
            lst (cdddr (cddddr lst))
      )
    )
  )
  (reverse rtn)
)

 

ArrCountTxt.png

Edited by Nikon
Posted

Did you even look at the data that my function returns?

Posted (edited)
54 minutes ago, Lee Mac said:

Did you even look at the data that my function returns?

DXF data
 (
    (-1 . <Object name: 233aa8f9cf0>)
    (0 . "INSERT")
    (5 . "17957")
    (102 . "{ACAD_REACTORS")
    (330 . <Object name: 233aa8f9d80>)
    (102 . "}")
    (330 . <Object name: 23bc15009f0>)
    (100 . "AcDbEntity")
    (67 . 0)
    (410 . "Model")
    (8 . "Hatching")
    (100 . "AcDbBlockReference")
    (2 . "*U268")
    (10 77260.1 54657.3 0.0)
    (41 . 1.0)
    (42 . 1.0)
    (43 . 1.0)
    (50 . 0.0)
    (70 . 0)
    (71 . 0)
    (44 . 0.0)
    (45 . 0.0)
    (210 0.0 0.0 1.0)
  )
 

I've looked at the data, but I need professional help...

How can I count elements in an array in lisp?

Edited by Nikon

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...