Jump to content

Wrong value from block insert


ElAmigo

Recommended Posts

Hello friends, I have a weird problem of which I cannot find the source of trouble. I have a block insert I get the values of. This has been going great with the following piece of code:

(defun c:getAttributeValue (AttributeVal / )
  (setq ss (ssget "X" (list (cons 0 "INSERT")(cons 2 "Title Blocks V3.0"))))
  (if ss
    (progn
      (setq en (ssname ss 0))                                                         ; Take the first selected object
      (setq enx (entget en))                                                          ; Retrieve DXF data from the object
      (if (= (cdr (assoc 0 enx)) "INSERT")                                            ; Check if it's an INSERT
        (progn                                                                        ; Find the start of the attributes
          (setq aten (entnext en))
          (setq atenx (entget aten))
          (while (and atenx (/= (cdr (assoc 0 atenx)) "SEQEND"))
            (if (= (cdr (assoc 0 atenx)) "ATTRIB")
              (progn                                                                  ; Check for a specific attribute tag
                (if (= (cdr (assoc 2 atenx)) AttributeVal)
                  (setq NewVal (cdr (assoc 1 atenx)))                                 ; Save the value
                )
              )
            )
            (setq aten (entnext aten))                                                ; Move to the next attribute
            (setq atenx (entget aten))
          )
        )
      )
    )
    (princ "\nNo object selected.")
  )
  (princ)
)
(defun c:GetAllattributeVals (/)
  (c:haalAttribuutWaarde "Initial_View_Scale")
    (setq Schaal NewVal)
;; More of these but only the scale is relevant for this post
)

Now the problem is that I cannot seem to get the right value with a few of my drawings. The block shows the scale as 1:15 in modelspace (see picture). But when I use my code it gets 1:20. When I go into the block editor I see that the value is set to 1:20 (see picture). I manually change this value to 1:15 but even then I only get the old value with my code. I can't change any of the other values manually either. Does anyone know how I fix this?

 

Naamloos.jpeg

Naamloos-1.jpeg

Link to comment
Share on other sites

Cleaned up the first bit of code. untested but should work the same. Also need to localize your variables, not localizing variables can lead to unexpected behavior and errors, especially in larger programs or when working with nested functions. When you don't localize a variable, it becomes a global variable, meaning its value can be changed by any part of the program. This can cause unintended side effects or make it difficult to track the flow of data in your code.

 

(defun C:GetAttributeValue (AttributeVal / atenx NewVal) ;local Variable
  (if (setq ss (ssget "X" '((0 . "INSERT") (2 . "Title Blocks V3.0")))) 
    (progn                                                              
      (setq atenx (entget (entnext (ssname ss 0)))) ;don't need to test for insert ssget has only selected blocks.
      (while (and atenx (/= (cdr (assoc 0 atenx)) "SEQEND"))
        (if (and (= (cdr (assoc 0 atenx)) "ATTRIB") (= (cdr (assoc 2 atenx)) AttributeVal)) ;join ifs with and both have to be true to pass
          (setq NewVal (cdr (assoc 1 atenx)))                                 
        )
        (setq atenx (entget (entnext aten)))
      )
    )
	(princ "\nNo object selected.")
  )
  (princ)
)

This should do the same thing
(setq str (GetAttValue "SEQEND")) ;this will set str the last NewVal set in GetAttValue

(defun GetAttValue (AttVal / SS)
  (if (setq ss (ssget "X" '((0 . "INSERT") (2 . Title Blocks V3.0"))))
    (setq NewVal (getpropertyvalue (ssname ss 0) AttVal)) 
  )
  NewVal
)

 

more about  getpropertyvalue and setpropteryvalue Not use here but follow the same logic. and only works with AutoCAD.

Edited by mhupp
Link to comment
Share on other sites

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...