Combine code to make it even better

Combine code to make it even better

djurk_haas
Advocate Advocate
2,445 Views
31 Replies
Message 1 of 32

Combine code to make it even better

djurk_haas
Advocate
Advocate

Hello,

The code below does what I want, but of course I want more:

  • How can I integrate Lee_Mac's code (and actually I want 3 digits instead of 4).
  • How can I stote the attribute value of the tag "TAPPUNT/KEERKLEP"  into a variable " tapkeer"  (to make the comment out code work)

 

(defun c:TP-change (/ ss i e tapkeer bouwdeel int)

(vl-load-com)

(if (setq ss (ssget ":S" '((0 . "insert")(66 . 1)(2 . "`*U*,TAPPUNT_CZE"))))
(repeat (setq i (sslength ss))
(setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)) )))
(if (Eq (strcase (vla-get-effectivename e)) "TAPPUNT_CZE")
(foreach itm (vlax-invoke e 'GetAttributes)


; (setq tap "Tappunt")
; (setq keer "Keerklep")
; (setq nrtappunt "NUMMER_TAPPUNT")
; (setq nrkeerklep "NUMMER_KEERKLEP")
; (cond ((= tapkeer tap) (setq nr nrtappunt))
; ((= tapkeer keer) (setq nr nrkeerklep))
; (t (prompt "N.v.t.: Tappunt onder/boven")))


(if (eq (vla-get-tagstring itm) "NUMMER_TAPPUNT")

(vla-put-textstring itm (getstring "\nTappuntnr: ") )))
)))
(princ)
)

 

Code van Lee_Mac:

(defun c:4digits ( / int )
(while
(and
(progn
(initget 6)
(setq int (getint "\nEnter a 4-digit number <Exit>: "))
)
(not (wcmatch (itoa int) "####"))
)
(princ "\nThat's not 4-digits!")
)
(if int
(princ (strcat "\nYou entered " (itoa int) "."))
(princ "\nUser exited.")
)
(princ)
)

 

0 Likes
Accepted solutions (2)
2,446 Views
31 Replies
Replies (31)
Message 2 of 32

Kent1Cooper
Consultant
Consultant

UNTESTED:


@djurk_haas wrote:

....

  • How can I integrate Lee_Mac's code (and actually I want 3 digits instead of 4).
  • How can I stote the attribute value of the tag "TAPPUNT/KEERKLEP"  into a variable " tapkeer"  (to make the comment out code work)

....

(foreach itm (vlax-invoke e 'GetAttributes)

 

(if (eq (vla-get-tagstring itm) "TAPPUNT/KEERKLEP")

  (setq tapkeer (vla-get-textstring itm))

); if

 

; (setq tap "Tappunt")

....

)

 

 

Code van Lee_Mac:

(defun c:3digits ( / int )
(while
(and
(progn
(initget 6)
(setq int (getint "\nEnter a 3-digit number <Exit>: "))
)
(not (wcmatch (itoa int) "###"))
)
(princ "\nThat's not 3-digits!")
)
(if int
(princ (strcat "\nYou entered " (itoa int) "."))
(princ "\nUser exited.")
)
(princ)
)


As for integrating that, for what input(s) do you want the 3-digit number requested?

Kent Cooper, AIA
0 Likes
Message 3 of 32

roland.r71
Collaborator
Collaborator

@djurk_haas wrote:

Hello,

The code below does what I want, but of course I want more:

  • How can I integrate Lee_Mac's code (and actually I want 3 digits instead of 4).
  • How can I stote the attribute value of the tag "TAPPUNT/KEERKLEP"  into a variable " tapkeer"  (to make the comment out code work)

 

(defun c:TP-change (/ ss i e tapkeer bouwdeel int)

(vl-load-com)

(if (setq ss (ssget ":S" '((0 . "insert")(66 . 1)(2 . "`*U*,TAPPUNT_CZE"))))
(repeat (setq i (sslength ss))
(setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)) )))
(if (Eq (strcase (vla-get-effectivename e)) "TAPPUNT_CZE")
(foreach itm (vlax-invoke e 'GetAttributes)

 

; (setq tap "Tappunt")
; (setq keer "Keerklep")
; (setq nrtappunt "NUMMER_TAPPUNT")
; (setq nrkeerklep "NUMMER_KEERKLEP")
; (cond ((= tapkeer tap) (setq nr nrtappunt))
; ((= tapkeer keer) (setq nr nrkeerklep))
; (t (prompt "N.v.t.: Tappunt onder/boven")))

 

(if (eq (vla-get-tagstring itm) "NUMMER_TAPPUNT")

(setq tappuntnr (c:3digits))

(if tappuntnr (vla-put-textstring itm (itoa tappuntnr) ))))
)))
(princ)
)

 


 

0 Likes
Message 4 of 32

roland.r71
Collaborator
Collaborator

@Kent1Cooper wrote:

UNTESTED:


@djurk_haas wrote:

....

  • How can I integrate Lee_Mac's code (and actually I want 3 digits instead of 4).
  • How can I stote the attribute value of the tag "TAPPUNT/KEERKLEP"  into a variable " tapkeer"  (to make the comment out code work)

....

(foreach itm (vlax-invoke e 'GetAttributes)

 

(if (eq (vla-get-tagstring itm) "TAPPUNT/KEERKLEP")

  (setq tapkeer (vla-get-textstring itm))

); if

 

; (setq tap "Tappunt")

....

)

 

 

Code van Lee_Mac:

(defun c:3digits ( / int )
(while
(and
(progn
(initget 6)
(setq int (getint "\nEnter a 3-digit number <Exit>: "))
)
(not (wcmatch (itoa int) "###"))
)
(princ "\nThat's not 3-digits!")
)
(if int
(princ (strcat "\nYou entered " (itoa int) "."))
(princ "\nUser exited.")
)
(princ)
)


As for integrating that, for what input(s) do you want the 3-digit number requested?


I can't imagine Lee Mac writing that the way it is ...

This only tells you what number you just entered, but there's no return value … 🙄

 

 

Code van Lee_Mac:
(defun c:3digits ( / int )
   (while
      (and
         (progn
            (initget 6)
            (setq int (getint "\nEnter a 3-digit number <Exit>: "))
         )
         (not (wcmatch (itoa int) "###"))
      )
      (princ "\nThat's not 3-digits!")
   )
   int
)

 

0 Likes
Message 5 of 32

roland.r71
Collaborator
Collaborator

No response yet, so not sure if you're able to put it all together. As I just had some time to spare, I did just that. It remains UNTESTED, as i don't have a sample to test with.

 

The code looks incomplete, or part of it is obsolete.

The second cond (in red color) sets a var called nr, but nothing is done with that.

 

This should do the trick:

(defun c:TP-change (/ ss i e nr bouwdeel itm tapkeer tap keer nrtappunt nrkeerklep)

(vl-load-com)

 

; Code van Lee_Mac:
(defun 3digits (/ int)
(while
(and
(progn
(initget 6)
(setq int (getint "\nEnter a 3-digit number <Exit>: "))
)
(not (wcmatch (itoa int) "###"))
)
(princ "\nThat's not 3-digits!")
)
int
)

 

(if (setq ss (ssget ":S" '((0 . "insert") (66 . 1) (2 . "`*U*,TAPPUNT_CZE"))))
(repeat (setq i (sslength ss))
(setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
(if (Eq (strcase (vla-get-effectivename e)) "TAPPUNT_CZE")
(foreach itm (vlax-invoke e 'GetAttributes)
(cond
((eq (vla-get-tagstring itm) "TAPPUNT/KEERKLEP")
(setq tapkeer (vla-get-textstring itm))
)
((eq (vla-get-tagstring itm) "TAPPUNT")
(setq tap (vla-get-textstring itm))
)
((eq (vla-get-tagstring itm) "KEERKLEP")
(setq keer (vla-get-textstring itm))
)
((eq (vla-get-tagstring itm) "NUMMER_TAPPUNT")
(if (setq nrtappunt (3digits))
(vla-put-textstring itm (itoa nrtappunt))
)
)
((eq (vla-get-tagstring itm) "NUMMER_KEERKLEP")
(setq nrkeerklep (vla-get-textstring itm))
)
)
(cond
((= tapkeer tap) (setq nr nrtappunt))
((= tapkeer keer) (setq nr nrkeerklep))
(t (prompt "N.v.t.: Tappunt onder/boven"))
)
)
)
)
)
(princ)
)

0 Likes
Message 6 of 32

pbejse
Mentor
Mentor

Now you got me interested too.

@djurk_haas 

Do you want to be prompted for "Tappuntnr" for every object?  IT looks as if thats what you're going for.

...
   (repeat (setq i (sslength ss))
                  (setq e (vlax-ename->vla-object
                                (ssname ss (setq i (1- i)))))
...
(vla-put-textstring itm (getstring "\nTappuntnr: "));;<---
...

And when you say 3 Digits  i believe you are referrring to 3 characters as numbers , to include "001" , "025"  Yes?

 

Whats with the :S in ssget anyway?

:S Force single object selection only 

Is that how you really wanted it? then why the repeat? 

Though it still works with multiple selection < which is weird >, but can please confirm want you really want?

 

 

0 Likes
Message 7 of 32

djurk_haas
Advocate
Advocate

Thanks for  the response!

Unfortunately, I didn't have the opportunity to respond before.

Hopefully I can make it more clear what my intention is with the code:

 

I have a dynamic block with different visibilitystates (Tappunt, Keerklep, Onder-boven).

The selected visibilitystate is stored within the block in the attribute "TAPPUNT/KEERKLEP".

Depending on this value, the associated attribute must be changed.

So with the chosen visibilitystate "Tappunt" only the attribute value "NUMBER_TAPPUNT" must be changed and if the chosen visibilitystate is "Keerklep" only the attribute value "NUMMER_KEERKLEP" must be changed. Both changes must be an 3 digits number.

 

 

 

 

0 Likes
Message 8 of 32

pbejse
Mentor
Mentor

@djurk_haas wrote:

Thanks for  the response!

 

I have a dynamic block with different visibilitystates (Tappunt, Keerklep, Onder-boven).

...changed. Both changes must be an 3 digits number.


We understand that, but question were:

Is this for multiple object selection?

if YES will the user be prompted for the 3 digit value per block?

In NO will it be sequential number based on the one time prompt for 3 digit value?

 

0 Likes
Message 9 of 32

roland.r71
Collaborator
Collaborator

Dus: dit is niet ok?

Enter a 3-digit number <Exit>: 001
That's not 3-digits!
0 Likes
Message 10 of 32

djurk_haas
Advocate
Advocate

It is for changing 1 block at a time.

0 Likes
Message 11 of 32

pbejse
Mentor
Mentor
Accepted solution

@djurk_haas wrote:

It is for changing 1 block at a time.


This is how I understand from your posted code

(defun c:TP-change  (/  Your3digits data ss i e tapkeer attval tagTo)
(vl-load-com)
(defun Your3digits (msg / int nstr)
 (while
	(and	
	    (progn
                (initget 6)
	                (setq int  (getint
	                                 (strcat "\n" msg ": ")))
	                )
                  (> int 999)	          
	          )
         (princ (strcat "\nInvalid value"))
	)
  	(setq nstr (itoa int))
        (strcat  (nth (strlen nstr)'(nil "00" "0" "")) nstr)
    )      
      
(setq data '(("Onder-boven" "NUMMER-ONDER-BOVEN")
             ("Keerklep" "NUMMER_KEERKLEP" )
	     ("Tappunt" "NUMMER_TAPPUNT" )
             )
      )     
      
(if (setq ss (ssget ":L"
                  '((0 . "insert") (66 . 1)
                    (2 . "`*U*,TAPPUNT_CZE"))))
   
       (repeat (setq i (sslength ss))
                  (setq e (vlax-ename->vla-object
                                (ssname ss (setq i (1- i)))))
                  (cond
                        ( (not
                              (and
                           	(Eq (strcase (vla-get-effectivename e)) "TAPPUNT_CZE")
                            	(setq attval (mapcar '(lambda (at)
                                                        (list (vla-get-TagString at)
                                                              (vla-get-TextString at) at))
                                       				(vlax-invoke e 'GetAttributes)))
                                (setq tapkeer (cadr (assoc "TAPPUNT/KEERKLEP" attval)))
                                (setq tagTo (Assoc
                                                  (Cadr (assoc tapkeer data)) attval))
                         	)
                              )
                         )
                        ( (member tapkeer (mapcar 'car (Cdr data)))
			 	 (vlax-invoke e 'Highlight 1)
	                         (vla-put-textstring (caddr tagTo) (Your3digits (car tagTo)))
			 	(vlax-invoke e 'Highlight 0)
                                          )
                        ( t (print "N.v.t.: Tappunt onder/boven")
                         )
                        )
             )
           )
  (princ)
  )

Depending on the value of "TAPPUNT/KEERKLEP", the prompt will switch between Tappunt and Keerler

Command: TP-CHANGE


NUMMER_TAPPUNT: 12

NUMMER_KEERKLEP: 45

 

 HTH

0 Likes
Message 12 of 32

pbejse
Mentor
Mentor

@roland.r71  

Hey that's not fair?  I need to google transate that

Dus: dit is niet ok?

😁

 

0 Likes
Message 13 of 32

roland.r71
Collaborator
Collaborator

But it does so perfectly (in this case) 😛

 

Anyway, i think its not. So a different way to get the number will be (likely) required.

As leading zero's are ignored for integers. (so it doesn't even accept it as "3 digit number")

0 Likes
Message 14 of 32

pbejse
Mentor
Mentor

@roland.r71 wrote:

Anyway, i think its not. So a different way to get the number will be (likely) required.

As leading zero's are ignored for integers. (so it doesn't even accept it as "3 digit number")


 

Yes, hence my question at post # 6. But i went ahead and wrote a different 3digit thingy.

Your3digits"

We'll see what the OP thinks.

 

 

0 Likes
Message 15 of 32

roland.r71
Collaborator
Collaborator

It would appear your thingy got accepted 😄

0 Likes
Message 16 of 32

pbejse
Mentor
Mentor

@roland.r71 wrote:

It would appear your thingy got accepted 😄


 

Gotta to have that thingy in there

I'll tell you a secret [ whispering: I wrote the code and had it-the-ready two days ago  😉 ,]

 

 

0 Likes
Message 17 of 32

djurk_haas
Advocate
Advocate

Here I am again.

In the attachments I have added 2 drawings.
In drawing B the code works fine but in drawing A the code stops halfway?

Maybe a system variable is wrong?

0 Likes
Message 18 of 32

pbejse
Mentor
Mentor
Accepted solution

@djurk_haas wrote:

In drawing B the code works fine but in drawing A the code stops halfway?

Maybe a system variable is wrong?


Drawing A

hint.JPG

And because of that...

hint2.png

You see, the program is reading the attribute value of ""TAPPUNT/KEERKLEP"".. its either of the three

  • Tappunt
  • Keerklep
  • Onder-boven

But as you can see. the value is ###, not included as a valid option

Attribute definition TAPPUNT/KEERKLEP is under the layer "A--L$8-hulplijnen" <-- currently locked

 

After unlocking and regen...

tada.JPG

TADA!!!

 

Drawing B

Although the same layer is locked but the value of the tag "TAPPUNT/KEERKLEP"  is not ###.... you know what I mean right?

 

We can however change the code to read the visibility state instead of the  attribute tag "TAPPUNT/KEERKLEP"

A simple quick unlock and regen will solve the issue though

 

Tell me how it goes..

 

 

0 Likes
Message 19 of 32

roland.r71
Collaborator
Collaborator

It has to do with the fields used for tappunt/keerklep

When looking at it with the enhanced attribute editor, the text shows up correctly for drawing B, but for drawing A it displays ####. On the Value line at the bottom, you can (still) see the correct value.

 

The lisp function however gets the same #### as a result when reading the attribute.

 

It has nothing to do with the drawing itself, as you can copy from B to A and it still works.

0 Likes
Message 20 of 32

pbejse
Mentor
Mentor

Here's the modified code that reads the visibility instead of the attribtues

 

(defun c:TP-change  (/  Your3digits data ss i en tapkeer  tagTo)
(vl-load-com)
(defun Your3digits (msg / int nstr)
 (while
	(and	
	    (progn
                (initget 6)
	                (setq int  (getint
	                                 (strcat "\n" msg ": ")))
	                )
                  (> int 999)	          
	          )
         (princ (strcat "\nInvalid value"))
	)
  	(setq nstr (itoa int))
        (strcat  (nth (strlen nstr)'(nil "00" "0" "")) nstr)
    )      
      
(setq data '(("Onder-boven" "NUMMER-ONDER-BOVEN")
             ("Keerklep" "NUMMER_KEERKLEP" )
	     ("Tappunt" "NUMMER_TAPPUNT" )
             )
      )     
      
(if (setq ss (ssget ":L"
                  '((0 . "insert") (66 . 1)
                    (2 . "`*U*,TAPPUNT_CZE"))))
   
       (repeat (setq i (sslength ss))
                  (setq en (ssname ss (setq i (1- i))))
                  (cond
                        ( (not
                              (and
                           	(Eq (getpropertyvalue en "BlockTableRecord/Name") "TAPPUNT_CZE")
				(setq tapkeer (getpropertyvalue  en "AcDbDynBlockPropertyVisibility1"))
                                (setq tagTo (Cadr (assoc tapkeer data)))
                         	)
                              )
                         )
                        ( (member tapkeer (mapcar 'car (Cdr data)))
			 	 (redraw en 3)
			 	 (setpropertyvalue en tagTo (Your3digits tagTo))
			 	(redraw en 4)
                                          )
                        ( t (print "N.v.t.: Tappunt onder/boven")
                         )
                        )
             )
           )
  (princ)
  )

HTH

 

0 Likes