Why does the code stop in the middle of the process?

Why does the code stop in the middle of the process?

Anonymous
Not applicable
1,629 Views
14 Replies
Message 1 of 15

Why does the code stop in the middle of the process?

Anonymous
Not applicable

This code works relatively well, however when I run it for the first time it generates 

Error: bad argument type: stringp nilbad argument type: stringp #<FILE internal> 

and does not end. I've done it step by step and I don't understand why, any guidance?

 

;;--------------------- { Get Attribute Value  -  Lee Mac } --------------------;;
;;                                                                              ;;
;;------------------------------------------------------------------------------;;
;;  Autor: Lee Mac                                                              ;;
;;------------------------------------------------------------------------------;;
;;  Argumentos:                                                                 ;;
;;   blk - [vla] VLA Block Reference Object                                     ;;
;;   tag - [str] Attribute TagString                                            ;;
;;------------------------------------------------------------------------------;;

(defun LM:vl-getattributevalue ( blk tag )
  (setq tag (strcase tag))
  (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (vla-get-textstring att))) (vlax-invoke blk 'getattributes))
)

;;--------------------- { Set attributevalue  -  Lee Mac- } --------------------;;
;;                                                                              ;;
;;------------------------------------------------------------------------------;;
;;  Autor: Lee Mac                                                              ;;
;;------------------------------------------------------------------------------;;
;;  Argumentos:                                                                 ;;
;;   blk - [vla] VLA Block Reference Object                                     ;;
;;   tag - [str] Attribute TagString                                            ;;
;;   val - [str] Attribute Value                                                ;;
;;------------------------------------------------------------------------------;;


(defun LM:vl-setattributevalue ( blk tag val )
  (setq tag (strcase tag))
    (vl-some '(lambda ( att )(if (= tag (strcase (vla-get-tagstring att))) (progn (vla-put-textstring att val) val)))(vlax-invoke blk 'getattributes))
)

;;------------------------------------------------------------------------------;;

(defun c:EXTRELATORIO (/ ss file i blk vatt ratt ip ss2 att1 att2 att3 att4 att5 )
    (defun *error* (errmsg)
        (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
            (princ (strcat "\nError: " errmsg))
        )
        (close file)
        (vl-file-delete file)
      (princ)
    )
    (setq ss (ssget '((0 . "INSERT") (66 . 1)(2 . "Poste_Enel,`*U*"))))
    (setq file (open (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) ".txt") "w"))
    (write-line "Numero;Latitude;Longitude;Relatorio" file )
    (repeat (setq i (sslength ss))
        (setq blk (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
        (setq vatt '("NUM" "LATITUDE" "LONGITUDE" "RELATORIO"))
        (setq ratt (mapcar '(lambda (x) (LM:vl-getattributevalue blk x)) vatt))
        ;(setq ratt (mapcar '(lambda (x) (vl-some '(lambda ( att ) (if (= (strcase x) (strcase (vla-get-tagstring att))) (vla-get-textstring att))) (vlax-invoke blk 'getattributes))) vatt)) <- Melhor!
        (mapcar 'set '(att1 att2 att3 att4) ratt)
        (setq ip (vlax-get blk 'insertionpoint))
        (setq ss2 (ssget "_C" (mapcar '+ ip '(5 5)) (mapcar '- ip '(5 5)) '((0 . "INSERT")(66 . 1)(2 . "Caixa001,Caixa002,Caixa003,Caixa004,Caixa005,Caixa006"))))
        (if (/= ss2 nil)
            (if (= (sslength ss2) 1)
                (progn
                    (setq blk2 (vlax-ename->vla-object (ssname ss2 0)))
                    (LM:vl-setattributevalue blk2 "IDCX" att1)
                    (setq att5 (LM:vl-getattributevalue blk2 "NOM"))
                )
            )
            (setq att5 "")
        )
        (write-line (strcat  att1 ";" att2 ";" att3 ";" att4 ";" att5 ) file )
     )
    (close file)
    ;(startapp "notepad" "G:\\Meu Drive\\Brisanet Programas\\Desenhos\\Extrator Material\\Teste-.txt")  
    (princ)
)
0 Likes
Accepted solutions (1)
1,630 Views
14 Replies
Replies (14)
Message 2 of 15

ВeekeeCZ
Consultant
Consultant
Accepted solution

Because your selection filter is not good enough to filter out all useless blocks.

They are dynamic blocks, anonymous, you need to filter them by their Effective name!

Message 3 of 15

Anonymous
Not applicable

@ВeekeeCZ  Thanks!! it works.

0 Likes
Message 4 of 15

ВeekeeCZ
Consultant
Consultant

you're welcome, @Anonymous 

Message 5 of 15

ronjonp
Mentor
Mentor

FWIW .. a quick observation: 

 

Might want to check that these two are valid before continuing:

(if (and (setq ss (ssget '((0 . "INSERT") (66 . 1) (2 . "Poste_Enel,`*U*"))))
	   (setq
	     file (open (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) ".txt") "w")
	   )
      )
    (progn . . .)
  )

 

This logic:

    (setq ss2
	   (ssget
	     "_C"
	     (mapcar '+ ip '(5 5))
	     (mapcar '- ip '(5 5))
	     '((0 . "INSERT") (66 . 1) (2 . "Caixa001,Caixa002,Caixa003,Caixa004,Caixa005,Caixa006"))
	   )
    )
    (if	(/= ss2 nil)
      (if (= (sslength ss2) 1)
	(progn (setq blk2 (vlax-ename->vla-object (ssname ss2 0)))
	       (lm:vl-setattributevalue blk2 "IDCX" att1)
	       (setq att5 (lm:vl-getattributevalue blk2 "NOM"))
	)
      )
      (setq att5 "")
    )

Could be written like this:

(if	(and (setq ss2 (ssget "_C"
			      (mapcar '+ ip '(5 5))
			      (mapcar '- ip '(5 5))
			      '((0 . "INSERT")
				(66 . 1)
				(2 . "Caixa001,Caixa002,Caixa003,Caixa004,Caixa005,Caixa006")
			       )
		       )
	     )
	     (= (sslength ss2) 1)
	)
      (progn (setq blk2 (vlax-ename->vla-object (ssname ss2 0)))
	     (lm:vl-setattributevalue blk2 "IDCX" att1)
	     (setq att5 (lm:vl-getattributevalue blk2 "NOM"))
      )
      (setq att5 "")
    )

And don't need to set all the 'att#' variables:

    ;; (write-line (strcat att1 ";" att2 ";" att3 ";" att4 ";" att5) file)
    (write-line
      (apply 'strcat (mapcar '(lambda (x) (strcat x ",")) (append ratt (list att5))))
      file
    )

That being said ... either way works :).

 

Message 6 of 15

cadffm
Consultant
Consultant

@ronjonp 

You quote the SSGET expression, but this part was the first mistake, the TO solved it and check the effectivenames now.

(both ssget parts were a problem in the original post)

 

@Anonymous 

You fixed the filterproblem for Poste_Enel, but did you also changed the second ssget part too?

There is the same problem.

 

And there is another thing what can be the next problem.

Even if you filters the right blockreferences (effectivename) well, it is still possible

that your block reference does not contain all expected attributes!

 

This causes the STRCAT  fail again (STRCAT was function where the ERROR started)

because a variable does not contain a string, but is nil.

 

Try it in my attached File, i edited the Blockreference in the red border- what is often a problem in files from outside.

Your code will work with the item in the green border, but not with the one in the red border.

 

Better to check that att1-4 all true

(and att1 att2 att3 att4) => T = okay

(and att1 att2 att3 att4) => nil = something wrong

 

--

 

And there is another thing.

You are using a graphic depend ssget mode, the "C ross" mode.

In AutoCAD the window, cross, a,o. are depends of the current VIEW

(and the given coordinates are depending on the current UCS, which can be different from the current view)

SSGET works equal to the standard objectselection method, so it is true for ssget too.

 

Because of this i miss your View Handling, a ZOOM Object SS   and ZOOM Previous for example.

Test this also in my attached file,

Re- open my file, delete the edited blockreference in the red border.

zoom to the left blockreference, so the right couple is outside your file window.

run your code - (use selection method ALL) -it works but it doesn't change the IDX value of the Caixa003

ZOOM EXTENTS

run your code - (use selection method ALL) - it works and it  change the IDX value of the Caixa003

That how some autocad selection methods works.

 

Instead of  selection method ALL you can also select all objects by window selection, but zoom to only the left item before you end the selection method.

 

[EDIT: File]

Sebastian

Message 7 of 15

ronjonp
Mentor
Mentor

@cadffm wrote:

@ronjonp 

You quote the SSGET expression, but this part was the first mistake, the TO solved it and check the effectivenames now.

(both ssget parts were a problem in the original post)

[EDIT: File]


@cadffm Not sure what you're saying here ? My response was a quick observation and had nothing to do directly with the problem at hand, only refactoring for logic. 🍻

Message 8 of 15

cadffm
Consultant
Consultant

>"only refactoring for logic"

and these are good hints for the TO 👍

Sebastian

Message 9 of 15

Anonymous
Not applicable

Thanks for the suggestions, you are always fantastic
Follow your advice and I'm still working on the final result.

 

@cadffm  Yes yes I made some changes to the selection set and it worked better !!! And in relation to the block "LATI_WHAT" I will analyze.

Attaching what I have so far, I changed the name of the variables to better understand.
The problem at the moment is that some values are exported in duplicate. But I will find out how to fix this.


Thank you so much guys.

 

;;Get Attribute Value  -  Lee Mac
(defun LM:vl-getattributevalue ( blk tag )
  (setq tag (strcase tag))
  (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (vla-get-textstring att))) (vlax-invoke blk 'getattributes))
)

;; Set attributevalue -  Lee Mac
(defun LM:vl-setattributevalue ( blk tag val )
  (setq tag (strcase tag))
    (vl-some '(lambda ( att )(if (= tag (strcase (vla-get-tagstring att))) (progn (vla-put-textstring att val) val)))(vlax-invoke blk 'getattributes))
)
    
;;-------------------------------------------------------------------------;;

(defun c:TESTE (/ file selection contador blkobj attributes stealattribute Numero latitudeude longitude relatorio baseblkselection selecionXdistancetxtCX  )

    (defun *error* (errmsg)
        (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
            (princ (strcat "\nError: " errmsg))
        )
        (close file)
        (vl-file-delete file)
        (princ)
    )
  
    (if
        (and
            (setq selection
                (ssget "_X"
                    '( 
                        (-4 . "<OR")
                            (-4 . "<AND")(0 . "INSERT")(66 . 1)(2 . "Poste_Enel")(8 . "D_Poste")(-4 . "AND>")
                            (-4 . "<AND")(0 . "INSERT")(66 . 1)(2 . "`*U*")(8 . "D_Poste")(-4 . "AND>")
                        (-4 . "OR>")
                    )
                )
            )
            (setq file (open (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) ".csv") "w"))
        )
        (progn
            (write-line "Numero,Latitude,Longitude,Relatorio,Caixa" file )
            (repeat (setq contador (sslength selection))
                (setq blkobj (vlax-ename->vla-object (ssname selection (setq contador (1- contador)))))
                (setq attributes '("NUM" "LATITUDE" "LONGITUDE" "RELATORIO"))
                (setq stealattribute (mapcar '(lambda ( x ) (LM:vl-getattributevalue blkobj x )) attributes ))
                (mapcar 'set '(Numero latitudeude longitude relatorio) stealattribute)
                (setq baseblkselection (vlax-get blkobj 'insertionpoint))
                (if
                    (and
                        (setq selecionXdistance
                            (ssget "_C"
                                (mapcar '+ baseblkselection '(5 5)) 
                                (mapcar '- baseblkselection '(5 5))
                                '(
                                    (-4 . "<AND")
                                        (0 . "INSERT")
                                        (66 . 1)
                                        (2 . "`*U*,Caixa001,Caixa002,Caixa003,Caixa004,Caixa005,Caixa006")
                                        (8 . "P_Caixa001,P_Caixa002,P_Caixa003,P_Caixa004,P_Caixa005,P_Caixa006")
                                    (-4 . "AND>")
                                )
                            )
                        )
                        (= (sslength selecionXdistance) 1)
                    )
                    (progn 
                        (setq blkobjCX (vlax-ename->vla-object (ssname selecionXdistance 0)))
                        (LM:vl-setattributevalue blkobjCX "IDCX" Numero)
                        (setq txtCX (LM:vl-getattributevalue blkobjCX "NOM"))
                    )
                    (setq txtCX "")
                )
                (write-line
                    (apply 'strcat (mapcar '(lambda (x) (strcat x ",")) (append stealattribute (list txtCX))))
                    file
                )
            )
          (close file)
        )
    )
  (startapp "notepad" (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) ".txt"))
  (princ)
)
;;(c:TESTE)

duplicate.JPG

0 Likes
Message 10 of 15

cadffm
Consultant
Consultant

oh.

near to "nothing" is really fixed in this version,

I see the problems, know how to fix it, but no time for this at the moment, wish you a lot of support from the others, I am withdrawing.

 

 

 

Sebastian

Message 11 of 15

ronjonp
Mentor
Mentor

Sorry .. I don't know what you are trying to accomplish.

Another tip to simplify your filter though 🙂

(2 . "`*U*,Caixa00[1-6]")
(8 . "P_Caixa00[1-6]")

 

 

0 Likes
Message 12 of 15

ВeekeeCZ
Consultant
Consultant

I did dig into it very much... I can see this:

image.png 

 

Should not the second selection (crossing one) select only the block that matches this number?  076 vs 076

0 Likes
Message 13 of 15

Anonymous
Not applicable

 

@cadffm Thanks, I will follow your suggestions and search on (effectivename) !!!

 

@ronjonp Thanks, I'll use it 🙂 I'm trying to collect and export data from a few different blocks.

 

@ВeekeeCZ Exactly!! now how to fix it. . .? I need to select with "selectXdistance" if it is in the same rotation. . . ? I have to think of a solution.

 

0 Likes
Message 14 of 15

cadffm
Consultant
Consultant

@ВeekeeCZ  schrieb:

Should not the second selection (crossing one) select only the block that matches this number?  076 vs 076


Yes and no, the second searchs for

Caixa001,Caixa002,Caixa003,Caixa004,Caixa005,Caixa006

 

and if found, override this attributvalue with the value from the block Poste_Enel (to make sure the "couples have the same idx number. )

 

The TO needs a "remember Handle-list": >If found more than one time => ALERT

Sebastian

Message 15 of 15

Anonymous
Not applicable

@cadffm  when you talk about effectivename is that what you mean?

(setq blkname (vla-get-effectivename (setq blkobj (vlax-ename->vla-object (ssname selection (setq contador (1- contador)))))))

Verification

(if (wcmatch  blkname "Poste_Enel")
[...]

how to store all the tags of my block in the list ?????

(setq attributes '("NUM" "LATITUDE" "LONGITUDE" "RELATORIO")) ;;<- Como armazenar todas as tags do meu bloco na lista ?????
0 Likes