Increment Attributes inserting block again

Increment Attributes inserting block again

C.Utzinger
Collaborator Collaborator
4,271 Views
63 Replies
Message 1 of 64

Increment Attributes inserting block again

C.Utzinger
Collaborator
Collaborator

HI

 

First of all: Happy New Year!!!

 

OK! I wrote this little Code, but i want to use Chars with numbers. For example EG001... EG002... EG003

 

How can i do that...????

 

 

(defun C:<Test5 ( / Pnummer)
	(princ "\nEinfügepunkt angeben: ")
	(command "_-insert" "SPI-Datenextraktionspunkt-CM" pause "1" "1" "" (setq Pnummer (getint "\nPunktnummer angeben (01): ")) "")
	(repeat 1000
	(setq Pnummer (+ Pnummer 1))
	(command "_-insert" "SPI-Datenextraktionspunkt-CM" pause "1" "1" "" Pnummer ""))	
	(prin1)
) ; end of defun
0 Likes
4,272 Views
63 Replies
Replies (63)
Message 41 of 64

C.Utzinger
Collaborator
Collaborator

HI

 

Sorry but this code crashes after a first use. You cannot execute it again. At the command line Shows up: stringp nil.

 

Thanks

0 Likes
Message 42 of 64

ВeekeeCZ
Consultant
Consultant

Try this one.

 

(vl-load-com)

(defun c:<Koordpunkt ( / *error* lay cmd :MaxAttValue :AddLeadingZeros txt enl enlast)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg)))
    (setvar 'CLAYER lay)
    (princ "\nLetzte Punktnummer gespeichert!")
    (terpri)
    (princ))
  
  (defun :MaxAttValue (pre / mxm i en em val att)
    (setq mxm 0)
    (if (= pre "") (setq pre ";"))
    (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "SPI-Datenextraktionspunkt-CM") (cons 410 (getvar 'CTAB)))))
      (repeat (setq i (sslength ss))
        (setq en (ssname ss (setq i (1- i))))
        (while (/= (cdr (assoc 0 (setq em (entget (setq en (entnext en)))))) "SEQEND")
          (and (eq (cdr (assoc 0 em)) "ATTRIB")
               (setq att (cdr (assoc 1 (reverse em))))
               (vl-string-search pre att)
               (setq val (substr att (+ 1 (vl-string-search pre att) (strlen pre))))
               (setq mxm (max mxm (atoi val)))
               ))))
    mxm)
  
  (defun :AddLeadingZeros (a d / b) ;add zeros to 'd' many digits  ;a string
    (strcat (substr "000000000" 1 (if (>= d (setq b (strlen (itoa (fix (atof a)))))) (- d b) 0)) a))
  
  
  ; ------------------------------------------------------------------------------------------------------
  
  (setq lay (getvar 'CLAYER))
  
  (command "_.-LAYER" "_m" "-I-Koordinatenpunkte" "_co" "3" "-I-Koordinatenpunkte" "")
  
  (setq oATTREQ (getvar 'ATTREQ)
        oATTDIA (getvar 'ATTDIA))
  (setvar 'ATTREQ 1)
  (setvar 'ATTDIA 0)
  
  (or *<Koordpunkt-g*
      (setq *<Koordpunkt-g* ""))
  (initget 128)
  (setq *<Koordpunkt-g* (strcase (cond ((getkword (strcat "\nGeschoss angeben (zB. \"1U-\", \"EG-\" oder \"\" für Ohne) <" (if (= "" *<Koordpunkt-g*) "Ohne" *<Koordpunkt-g*) ">: ")))
                                       (*<Koordpunkt-g*)))
        *<Koordpunkt-g* (if (= "\"\"" *<Koordpunkt-g*)
                          ""
                          *<Koordpunkt-g*))
  
  
  (setq *<Koordpunkt-i* (1+ (:MaxAttValue *<Koordpunkt-g*))
        *<Koordpunkt-i* (cond ((getint (strcat "\nPunktnummer angeben <" (itoa *<Koordpunkt-i*) ">: ")))
                              (*<Koordpunkt-i*)))
  
  (setq enlast (entlast))
  
  (while (progn
           (initget "Undo")
           (setq pnt (getpoint (strcat "\nEinfügepunkt angeben für '" (setq txt (strcat *<Koordpunkt-g* (:AddLeadingZeros (itoa *<Koordpunkt-i*) 1))) "' [Undo] <exit>: "))))
    (if (= pnt "Undo")
      (progn
        (if (equal enlast (entlast))
          (alert "Alle eingefügten Punkte gelöscht!")
          (progn
            (setq enl (entlast))
            (command "_.UNDO" 1)
            (if (not (equal enl (entlast)))
              (setq *<Koordpunkt-i* (1- *<Koordpunkt-i*))))))
      (progn
        (command "_.-INSERT" "SPI-Datenextraktionspunkt-CM" "_none" pnt "1" "1" "" txt "")
        (setq *<Koordpunkt-i* (1+ *<Koordpunkt-i*)))))
  ) ; end of defun
0 Likes
Message 43 of 64

C.Utzinger
Collaborator
Collaborator

BeekeeCZ wrote:

 

BTW your formatted MTEXT attribute give me a little hard time - so don't change the format of text inside of MTEXT - you may loos a max value recognition (especially with no prefix). The format is a part of a string value, see "\\W0.7000;EG001" Actual value is bold.

 

___________________________

 

 

HI BeekeeCZ

 

I have fallen in the trap...

 

Long story for explain why i have to Change the Format :(..., but i had to!

 

Could you give me a Little help, please?

 

I have also an other post were i have the same Problem with another part of the main code...

 

 

Kind regards.

 

 

0 Likes
Message 44 of 64

C.Utzinger
Collaborator
Collaborator

I just fixed this function and now it works. Is that correct??? I just eliminate  (if (= pre "") (setq pre ";")) !

 

  (defun :MaxAttValue (pre / mxm i en em val att)
    (setq mxm 0)
    (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "SPI-Koordinatenpunkt") (cons 410 (getvar 'CTAB)))))
      (repeat (setq i (sslength ss))
        (setq en (ssname ss (setq i (1- i))))
        (while (/= (cdr (assoc 0 (setq em (entget (setq en (entnext en)))))) "SEQEND")
          (and (eq (cdr (assoc 0 em)) "ATTRIB")
               (setq att (cdr (assoc 1 (reverse em))))
               (vl-string-search pre att)
               (setq val (substr att (+ 1 (vl-string-search pre att) (strlen pre))))
               (setq mxm (max mxm (atoi val)))
               ))))
    mxm)

 

Kind regards

0 Likes
Message 45 of 64

ВeekeeCZ
Consultant
Consultant

Hi Christian, good... 

 

yes, it will work but for new blocks only. If you want to keep the functionality for the both old and new blocks, you need something smarter. The following suggestion will cover it, but again, old and new block only (no other additional formatting). I think it should be enought for you... but all-covering solution would be use some StripMtext function... 

 

Anyway, I've also added the leading-zeros recognition... I guess you may find it useful. Now it is automated, but you might uncomment user prompt if you find it necessary.

 

Most changes is bold.

 

(vl-load-com)

(defun c:<Koordpunkt ( / *error* lay cmd :MaxAttValue :AddLeadingZeros txt enl enlast)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg)))
    (setvar 'CLAYER lay)
    (princ "\nLetzte Punktnummer gespeichert!")
    (terpri)
    (princ))
  
  (defun :MaxAttValue (pre / mxm i en em val att frm)
    (setq mxm "0")
    (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "SPI-Koordinatenpunkt") (cons 410 (getvar 'CTAB)))))
      (repeat (setq i (sslength ss))
        (setq en (ssname ss (setq i (1- i))))
        (while (/= (cdr (assoc 0 (setq em (entget (setq en (entnext en)))))) "SEQEND")
          (and (eq (cdr (assoc 0 em)) "ATTRIB")
               (setq att (cdr (assoc 1 (reverse em))))
               (setq frm (cond ((vl-string-search pre att))
			       ((vl-string-search ";" att))))
               (setq val (substr att (+ 1 frm (strlen pre))))
	       (> (atoi val) (atoi mxm))
               (setq mxm val)
               ))))
    mxm)
  
  (defun :AddLeadingZeros (a d / b) ;add zeros to 'd' many digits  ;a string
    (strcat (substr "000000000" 1 (if (>= d (setq b (strlen (itoa (fix (atof a)))))) (- d b) 0)) a))
  
  
  ; ------------------------------------------------------------------------------------------------------
  
  (setq lay (getvar 'CLAYER))
  
  (command "_.-LAYER" "_m" "-I-Koordinatenpunkte" "_co" "3" "-I-Koordinatenpunkte" "")
  
  (setq oATTREQ (getvar 'ATTREQ)
        oATTDIA (getvar 'ATTDIA))
  (setvar 'ATTREQ 1)
  (setvar 'ATTDIA 0)
  
  (or *<Koordpunkt-g*
      (setq *<Koordpunkt-g* ""))
  (initget 128)
  (setq *<Koordpunkt-g* (strcase (cond ((getkword (strcat "\nGeschoss angeben (zB. \"1U-\", \"EG-\" oder \"\" für Ohne) <" (if (= "" *<Koordpunkt-g*) "Ohne" *<Koordpunkt-g*) ">: ")))
                                       (*<Koordpunkt-g*)))
        *<Koordpunkt-g* (if (= "\"\"" *<Koordpunkt-g*)
                          ""
                          *<Koordpunkt-g*))
  
  
  (setq *<Koordpunkt-i* (1+ (atoi (setq *<Koordpunkt-z* (:MaxAttValue *<Koordpunkt-g*))))
	*<Koordpunkt-z* (strlen *<Koordpunkt-z*)
        *<Koordpunkt-i* (cond ((getint (strcat "\nPunktnummer angeben <" (:AddLeadingZeros (itoa *<Koordpunkt-i*) *<Koordpunkt-z*) ">: ")))
                              (*<Koordpunkt-i*)))

  ;(setq *<Koordpunkt-z* (cond ((getint (strcat "\nFill up with leading zeros up to <" (itoa *<Koordpunkt-z*) "> place: "))) (*<Koordpunkt-z*))) ; uncomment this line to ask the user

    
  (setq enlast (entlast))
  
  (while (progn
           (initget "Undo")
           (setq pnt (getpoint (strcat "\nEinfügepunkt angeben für '" (setq txt (strcat *<Koordpunkt-g* (:AddLeadingZeros (itoa *<Koordpunkt-i*) *<Koordpunkt-z*))) "' [Undo] <exit>: "))))
    (if (= pnt "Undo")
      (progn
        (if (equal enlast (entlast))
          (alert "Alle eingefügten Punkte gelöscht!")
          (progn
            (setq enl (entlast))
            (command "_.UNDO" 1)
            (if (not (equal enl (entlast)))
              (setq *<Koordpunkt-i* (1- *<Koordpunkt-i*))))))
      (progn
        (command "_.-INSERT" "SPI-Koordinatenpunkt" "_none" pnt "1" "1" "" txt "") ;"SPI-Datenextraktionspunkt-CM"
        (setq *<Koordpunkt-i* (1+ *<Koordpunkt-i*)))))
  ) ; end of defun
0 Likes
Message 46 of 64

C.Utzinger
Collaborator
Collaborator

Hi BeekeeCZ

 

I will try it later.

 

The leading-Zeros y use it with fixed user option in the dcl menu.

 

Thank you a lot!!!!!!!!!!!!!!!!!!!!!

 

 

Kind regards

0 Likes
Message 47 of 64

C.Utzinger
Collaborator
Collaborator

HI again!

 

So i have tested it.

 

The :MaxAttValue  works fine with the new block (textstyle) but not with the old one.

 

Sometimes it dont starts with number 1 when you introduce a new string.

 

Like:

 

First you have: EG1, EG2 ... EG7

Then with EGR it starts with EGR8

And with a third one, also it starts with OGW8.

 

It's not so much a Problem, i don´t want the user to use the old one. But if it is an easy fix then it would be great.

 

 

Thank you

0 Likes
Message 48 of 64

ВeekeeCZ
Consultant
Consultant

Ohh man, it's super easy! You gotta do that by yourself!

 

(defun :MaxAttValue (pre / mxm i en em val att frm)
    (setq mxm "0")
    (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "SPI-Koordinatenpunkt") (cons 410 (getvar 'CTAB)))))
      (repeat (setq i (sslength ss))
        (setq en (ssname ss (setq i (1- i))))
        (while (/= (cdr (assoc 0 (setq em (entget (setq en (entnext en)))))) "SEQEND")
          (and (eq (cdr (assoc 0 em)) "ATTRIB")
               (setq att (cdr (assoc 1 (reverse em))))

               ; confirmation if the att value is in the range given by user.... basicly it does:
               (setq frm (cond ((vl-string-search pre att)) 	; if *prefix* then T (new, no formatting)
			       ((vl-string-search ";" att)))) 	; if *;* then T (old, formatted "\\W0.7000;acual_value")
               							; ... T in this case also means the position in the string where searched pattern is
               ; -----------------------------------------------------------
	
               
               (setq val (substr att (+ 1 frm (strlen pre))))
	       (> (atoi val) (atoi mxm))
               (setq mxm val)
               ))))
    mxm)



;; You need something stronger than just test the existence of the prefix in the attvalue

;; no formatting
(wcmatch att (strcat pre "#*"))

;; old format (very simply)
(wcmatch att (strcat "*;" pre "#*"))

;; both
(wcmatch att (strcat pre "#*" "," "*;" pre "#*"))

;; both, more obvious form
(or (wcmatch att (strcat pre "#*"))
    (wcmatch att (strcat "*;" pre "#*")))

;; then get the substring
(setq val (substr att (+ 1 (vl-string-search pre att) (strlen pre))))
Message 49 of 64

C.Utzinger
Collaborator
Collaborator

HI

 

HARD TIME! But I have a code and it seems to work well.

 

Is that correct?

 

 

  (defun :MaxAttValue (pre / mxm i en em val att frm)
    (setq mxm "0")
    (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "SPI-Datenextraktionspunkt-CM") (cons 410 (getvar 'CTAB)))))
      (repeat (setq i (sslength ss))
        (setq en (ssname ss (setq i (1- i))))
        (while (/= (cdr (assoc 0 (setq em (entget (setq en (entnext en)))))) "SEQEND")
          (and (eq (cdr (assoc 0 em)) "ATTRIB")
               (setq att (cdr (assoc 1 (reverse em))))
               (setq frm (cond ((vl-string-search pre att))
			       ((vl-string-search (strcat ";" pre) att))))
               (setq pre (if (and (= pre "")(vl-string-search ";" att)) ";" pre))
               (setq val (substr att (+ 1 frm (strlen pre))))
	       (> (atoi val) (atoi mxm))
               (setq mxm val)
               ))))
    mxm)

 

Best regards

0 Likes
Message 50 of 64

ВeekeeCZ
Consultant
Consultant

Well, I suggested to use this code:

 

(defun :MaxAttValue (pre / mxm i en em val att frm)
    (setq mxm "0")
    (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "SPI-Koordinatenpunkt") (cons 410 (getvar 'CTAB)))))
      (repeat (setq i (sslength ss))
        (setq en (ssname ss (setq i (1- i))))
        (while (/= (cdr (assoc 0 (setq em (entget (setq en (entnext en)))))) "SEQEND")
          (and (eq (cdr (assoc 0 em)) "ATTRIB")
               (setq att (cdr (assoc 1 (reverse em))))
               (or (wcmatch att (strcat pre "#*"))
                   (wcmatch att (strcat "*;" pre "#*")))
               (setq val (substr att (+ 1 (vl-string-search pre att) (strlen pre))))
	       (> (atoi val) (atoi mxm))
               (setq mxm val)
               ))))
    mxm)

Not sure if your version will work...I guess you won't see the difference between EG1 and EGR8 ...but test it and you'll see. 

 

 

0 Likes
Message 51 of 64

C.Utzinger
Collaborator
Collaborator

HI

 

Sorry it doesn't work, I don't know why!!!!

 

Just the old Format, when you use only numbers, then it Fails.

  

 

Kind regards

0 Likes
Message 52 of 64

ВeekeeCZ
Consultant
Consultant

Hmm, must be something wrong on your side. The following code works on your test file from the post #43. Make sure that block names are correct.

 

(vl-load-com)

(defun c:<Koordpunkt ( / *error* lay cmd :MaxAttValue :AddLeadingZeros txt enl enlast)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg)))
    (setvar 'CLAYER lay)
    (princ "\nLetzte Punktnummer gespeichert!")
    (terpri)
    (princ))


  (defun :MaxAttValue (pre / mxm i en em val att)
    (setq mxm "0")
    (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "SPI-Koordinatenpunkt") (cons 410 (getvar 'CTAB)))))
      (repeat (setq i (sslength ss))
        (setq en (ssname ss (setq i (1- i))))
        (while (/= (cdr (assoc 0 (setq em (entget (setq en (entnext en)))))) "SEQEND")
          (and (eq (cdr (assoc 0 em)) "ATTRIB")
               (setq att (cdr (assoc 1 (reverse em))))
               (or (wcmatch att (strcat pre "#*"))
                   (wcmatch att (strcat "*;" pre "#*")))
               (setq val (substr att (+ 1 (vl-string-search pre att) (strlen pre))))
	       (> (atoi val) (atoi mxm))
               (setq mxm val)
               ))))
    mxm)
  
;;;  (defun :MaxAttValue (pre / mxm i en em val att frm)
;;;    (setq mxm "0")
;;;    (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "SPI-Koordinatenpunkte") (cons 410 (getvar 'CTAB)))))
;;;      (repeat (setq i (sslength ss))
;;;        (setq en (ssname ss (setq i (1- i))))
;;;        (while (/= (cdr (assoc 0 (setq em (entget (setq en (entnext en)))))) "SEQEND")
;;;          (and (eq (cdr (assoc 0 em)) "ATTRIB")
;;;               (setq att (cdr (assoc 1 (reverse em))))
;;;               (setq frm (cond ((vl-string-search pre att))
;;;			       ((vl-string-search ";" att))))
;;;               (setq val (substr att (+ 1 frm (strlen pre))))
;;;	       (> (atoi val) (atoi mxm))
;;;               (setq mxm val)
;;;               ))))
;;;    mxm)
  
  (defun :AddLeadingZeros (a d / b) ;add zeros to 'd' many digits  ;a string
    (strcat (substr "000000000" 1 (if (>= d (setq b (strlen (itoa (fix (atof a)))))) (- d b) 0)) a))
  
  
  ; ------------------------------------------------------------------------------------------------------
  
  (setq lay (getvar 'CLAYER))
  
  (command "_.-LAYER" "_m" "-I-Koordinatenpunkte" "_co" "3" "-I-Koordinatenpunkte" "")
  
  (setq oATTREQ (getvar 'ATTREQ)
        oATTDIA (getvar 'ATTDIA))
  (setvar 'ATTREQ 1)
  (setvar 'ATTDIA 0)
  
  (or *<Koordpunkt-g*
      (setq *<Koordpunkt-g* ""))
  (initget 128)
  (setq *<Koordpunkt-g* (strcase (cond ((getkword (strcat "\nGeschoss angeben (zB. \"1U-\", \"EG-\" oder \"\" für Ohne) <" (if (= "" *<Koordpunkt-g*) "Ohne" *<Koordpunkt-g*) ">: ")))
                                       (*<Koordpunkt-g*)))
        *<Koordpunkt-g* (if (= "\"\"" *<Koordpunkt-g*)
                          ""
                          *<Koordpunkt-g*))
  
  
  (setq *<Koordpunkt-i* (1+ (atoi (setq *<Koordpunkt-z* (:MaxAttValue *<Koordpunkt-g*))))
	*<Koordpunkt-z* (strlen *<Koordpunkt-z*)
        *<Koordpunkt-i* (cond ((getint (strcat "\nPunktnummer angeben <" (:AddLeadingZeros (itoa *<Koordpunkt-i*) *<Koordpunkt-z*) ">: ")))
                              (*<Koordpunkt-i*)))

  ;(setq *<Koordpunkt-z* (cond ((getint (strcat "\nFill up with leading zeros up to <" (itoa *<Koordpunkt-z*) "> place: "))) (*<Koordpunkt-z*))) ; uncomment this line to ask the user

    
  (setq enlast (entlast))
  
  (while (progn
           (initget "Undo")
           (setq pnt (getpoint (strcat "\nEinfügepunkt angeben für '" (setq txt (strcat *<Koordpunkt-g* (:AddLeadingZeros (itoa *<Koordpunkt-i*) *<Koordpunkt-z*))) "' [Undo] <exit>: "))))
    (if (= pnt "Undo")
      (progn
        (if (equal enlast (entlast))
          (alert "Alle eingefügten Punkte gelöscht!")
          (progn
            (setq enl (entlast))
            (command "_.UNDO" 1)
            (if (not (equal enl (entlast)))
              (setq *<Koordpunkt-i* (1- *<Koordpunkt-i*))))))
      (progn
        (command "_.-INSERT" "SPI-Koordinatenpunkt" "_none" pnt "1" "1" "" txt "") ;"SPI-Datenextraktionspunkt-CM"
        (setq *<Koordpunkt-i* (1+ *<Koordpunkt-i*)))))
  ) ; end of defun
0 Likes
Message 53 of 64

C.Utzinger
Collaborator
Collaborator

OK!

 

The filename i tried to change it once, but i get back to the old one "SPI-Datenextraktionspunkt-CM".

 

 

Actual code with the attached dwg, it doesn't work :(...     Just when you have only numbers.

 

(vl-load-com)

(defun c:<Koordpunkt ( / *error* lay cmd :MaxAttValue :AddLeadingZeros txt enl enlast)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg)))
    (setvar 'CLAYER lay)
    (princ "\nLetzte Punktnummer gespeichert!")
    (terpri)
    (princ))


  (defun :MaxAttValue (pre / mxm i en em val att)
    (setq mxm "0")
    (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "SPI-Datenextraktionspunkt-CM") (cons 410 (getvar 'CTAB)))))
      (repeat (setq i (sslength ss))
        (setq en (ssname ss (setq i (1- i))))
        (while (/= (cdr (assoc 0 (setq em (entget (setq en (entnext en)))))) "SEQEND")
          (and (eq (cdr (assoc 0 em)) "ATTRIB")
               (setq att (cdr (assoc 1 (reverse em))))
               (or (wcmatch att (strcat pre "#*"))
                   (wcmatch att (strcat "*;" pre "#*")))
               (setq val (substr att (+ 1 (vl-string-search pre att) (strlen pre))))
	       (> (atoi val) (atoi mxm))
               (setq mxm val)
               ))))
    mxm)
  

  (defun :AddLeadingZeros (a d / b) ;add zeros to 'd' many digits  ;a string
    (strcat (substr "000000000" 1 (if (>= d (setq b (strlen (itoa (fix (atof a)))))) (- d b) 0)) a))
  
  
  ; ------------------------------------------------------------------------------------------------------
  
  (setq lay (getvar 'CLAYER))
  
  (command "_.-LAYER" "_m" "-I-Koordinatenpunkte" "_co" "3" "-I-Koordinatenpunkte" "")
  
  (setq oATTREQ (getvar 'ATTREQ)
        oATTDIA (getvar 'ATTDIA))
  (setvar 'ATTREQ 1)
  (setvar 'ATTDIA 0)
  
  (or *<Koordpunkt-g*
      (setq *<Koordpunkt-g* ""))
  (initget 128)
  (setq *<Koordpunkt-g* (strcase (cond ((getkword (strcat "\nGeschoss angeben (zB. \"1U-\", \"EG-\" oder \"\" für Ohne) <" (if (= "" *<Koordpunkt-g*) "Ohne" *<Koordpunkt-g*) ">: ")))
                                       (*<Koordpunkt-g*)))
        *<Koordpunkt-g* (if (= "\"\"" *<Koordpunkt-g*)
                          ""
                          *<Koordpunkt-g*))
  
  
  (setq *<Koordpunkt-i* (1+ (atoi (setq *<Koordpunkt-z* (:MaxAttValue *<Koordpunkt-g*))))
	*<Koordpunkt-z* (strlen *<Koordpunkt-z*)
        *<Koordpunkt-i* (cond ((getint (strcat "\nPunktnummer angeben <" (:AddLeadingZeros (itoa *<Koordpunkt-i*) *<Koordpunkt-z*) ">: ")))
                              (*<Koordpunkt-i*)))

  (setq *<Koordpunkt-z* (cond ((getint (strcat "\nFill up with leading zeros up to <" (itoa *<Koordpunkt-z*) "> place: "))) (*<Koordpunkt-z*))) ; uncomment this line to ask the user

    
  (setq enlast (entlast))
  
  (while (progn
           (initget "Undo")
           (setq pnt (getpoint (strcat "\nEinfügepunkt angeben für '" (setq txt (strcat *<Koordpunkt-g* (:AddLeadingZeros (itoa *<Koordpunkt-i*) *<Koordpunkt-z*))) "' [Undo] <exit>: "))))
    (if (= pnt "Undo")
      (progn
        (if (equal enlast (entlast))
          (alert "Alle eingefügten Punkte gelöscht!")
          (progn
            (setq enl (entlast))
            (command "_.UNDO" 1)
            (if (not (equal enl (entlast)))
              (setq *<Koordpunkt-i* (1- *<Koordpunkt-i*))))))
      (progn
        (command "_.-INSERT" "SPI-Datenextraktionspunkt-CM" "_none" pnt "1" "1" "" txt "")
        (setq *<Koordpunkt-i* (1+ *<Koordpunkt-i*)))))
  ) ; end of defun

I'm sorry Smiley Frustrated

 

0 Likes
Message 54 of 64

ВeekeeCZ
Consultant
Consultant

You're right! The old format with numbers only did not work. My bad, sorry. Try and test this one.

 

(defun c:<Koordpunkt ( / *error* lay cmd :MaxAttValue :AddLeadingZeros txt enl enlast)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg)))
    (setvar 'CLAYER lay)
    (princ "\nLetzte Punktnummer gespeichert!")
    (terpri)
    (princ))


  (defun :MaxAttValue (pre / mxm i en em val att)
    (setq mxm "0")
    (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "SPI-Datenextraktionspunkt-CM") (cons 410 (getvar 'CTAB)))))
      (repeat (setq i (sslength ss))
        (setq en (ssname ss (setq i (1- i))))
        (while (/= (cdr (assoc 0 (setq em (entget (setq en (entnext en)))))) "SEQEND")
          (and (eq (cdr (assoc 0 em)) "ATTRIB")
               (setq att (cdr (assoc 1 (reverse em))))
               (setq val (cond ((wcmatch att (strcat pre "#*"))  				; no format 
				(substr att (+ 1 (vl-string-search pre att) (strlen pre))))
			       ((wcmatch att (strcat "*;" pre "#*"))				; old format "\\W0.7000;value"
				(substr att (+ 2 (vl-string-search (strcat ";" pre) att) (strlen pre))))))
	       (> (atoi val) (atoi mxm))
               (setq mxm val)
               ))))
    mxm)
  

  (defun :AddLeadingZeros (a d / b) ;add zeros to 'd' many digits  ;a string
    (strcat (substr "000000000" 1 (if (>= d (setq b (strlen (itoa (fix (atof a)))))) (- d b) 0)) a))
  
  
  ; ------------------------------------------------------------------------------------------------------
  
  (setq lay (getvar 'CLAYER))
  
  (command "_.-LAYER" "_m" "-I-Koordinatenpunkte" "_co" "3" "-I-Koordinatenpunkte" "")
  
  (setq oATTREQ (getvar 'ATTREQ)
        oATTDIA (getvar 'ATTDIA))
  (setvar 'ATTREQ 1)
  (setvar 'ATTDIA 0)
  
  (or *<Koordpunkt-g*
      (setq *<Koordpunkt-g* ""))
  (initget 128)
  (setq *<Koordpunkt-g* (strcase (cond ((getkword (strcat "\nGeschoss angeben (zB. \"1U-\", \"EG-\" oder \"\" für Ohne) <" (if (= "" *<Koordpunkt-g*) "Ohne" *<Koordpunkt-g*) ">: ")))
                                       (*<Koordpunkt-g*)))
        *<Koordpunkt-g* (if (= "\"\"" *<Koordpunkt-g*)
                          ""
                          *<Koordpunkt-g*))
  
  
  (setq *<Koordpunkt-i* (1+ (atoi (setq *<Koordpunkt-z* (:MaxAttValue *<Koordpunkt-g*))))
	*<Koordpunkt-z* (strlen *<Koordpunkt-z*)
        *<Koordpunkt-i* (cond ((getint (strcat "\nPunktnummer angeben <" (:AddLeadingZeros (itoa *<Koordpunkt-i*) *<Koordpunkt-z*) ">: ")))
                              (*<Koordpunkt-i*)))

  (setq *<Koordpunkt-z* (cond ((getint (strcat "\nFill up with leading zeros up to <" (itoa *<Koordpunkt-z*) "> place: "))) (*<Koordpunkt-z*))) ; uncomment this line to ask the user

    
  (setq enlast (entlast))
  
  (while (progn
           (initget "Undo")
           (setq pnt (getpoint (strcat "\nEinfügepunkt angeben für '" (setq txt (strcat *<Koordpunkt-g* (:AddLeadingZeros (itoa *<Koordpunkt-i*) *<Koordpunkt-z*))) "' [Undo] <exit>: "))))
    (if (= pnt "Undo")
      (progn
        (if (equal enlast (entlast))
          (alert "Alle eingefügten Punkte gelöscht!")
          (progn
            (setq enl (entlast))
            (command "_.UNDO" 1)
            (if (not (equal enl (entlast)))
              (setq *<Koordpunkt-i* (1- *<Koordpunkt-i*))))))
      (progn
        (command "_.-INSERT" "SPI-Datenextraktionspunkt-CM" "_none" pnt "1" "1" "" txt "")
        (setq *<Koordpunkt-i* (1+ *<Koordpunkt-i*)))))
  ) ; end of defun
Message 55 of 64

C.Utzinger
Collaborator
Collaborator

HI

 

Thank you a lot again!

 

Now it works.

 

 

Kind regards

 

Christian

0 Likes
Message 56 of 64

C.Utzinger
Collaborator
Collaborator

 

Sorry I have to bother you again whith this s***t.

 

I just saw when i hae a value like <5OG-001> and i the wnat to introduce values only numbers like 001, then it starts with 006, the value from 5OG.

 

 

I wish i could fix it by my self Smiley Frustrated

 

Thanks in advance...

 

 

Kind regards

 

0 Likes
Message 57 of 64

ВeekeeCZ
Consultant
Consultant

Ok, there is the easy task for you! Forget all about how we get the value now. Dead end.

 

Write a loop. Cycle the chars from the end of str one by one, UNTIL is a num, then keep it. Have fun!

Message 58 of 64

C.Utzinger
Collaborator
Collaborator

I give it a last Chance!

 

I think now it works!

 

  (defun :MaxAttValue (pre / mxm i en em val att)
    (setq mxm "0")
    (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "SPI-Datenextraktionspunkt-CM") (cons 410 (getvar 'CTAB)))))
      (repeat (setq i (sslength ss))
        (setq en (ssname ss (setq i (1- i))))
        (while (/= (cdr (assoc 0 (setq em (entget (setq en (entnext en)))))) "SEQEND")
          (and (eq (cdr (assoc 0 em)) "ATTRIB")
               (setq att (cdr (assoc 1 (reverse em))))
               (setq val (cond ((and (= pre "")(= (wcmatch att "*@*") nil)) att) ; for new format and values: 1, 01, 001, 0001...
                               ((and (= pre "")(wcmatch att (strcat "*;" pre "#*"))) ; for old format and values: 1, 01, 001, 0001...
                                (setq val (substr att (+ 2 (vl-string-search ";" att))))
                                (if (wcmatch val "*@*") nil val))
                               ((and (/= pre "")(wcmatch att (strcat pre "#*"))) ; for new format and values: EG-1, EG-01, EG-001, EG-0001...
				(substr att (+ 1 (vl-string-search pre att) (strlen pre))))
			       ((and (/= pre "")(wcmatch att (strcat "*;" pre "#*"))) for old format and values: EG-1, EG-01, EG-001, Eg-0001...			
				(substr att (+ 2 (vl-string-search (strcat ";" pre) att) (strlen pre))))))
	       (> (atoi val) (atoi mxm))
               (setq mxm val)
               ))))

    mxm)

 

 

 

Kind regards and have a nice Weekend!

 

Christian

Message 59 of 64

ВeekeeCZ
Consultant
Consultant

Nice exercise, but no future here!

 

Try this.. just quick one with not much of testing.

 

(defun c:<Koordpunkt ( / *error* lay cmd :AttMaxValue :AttMaxValues :getNumFromRight :AddLeadingZeros txt enl enlast)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg)))
    (setvar 'CLAYER lay)
    (princ "\nLetzte Punktnummer gespeichert!")
    (terpri)
    (princ))


  (defun :getNumFromRight (str / idx val char)
    (setq idx (strlen str)
          val "")
    (while (and (> idx 0)
                (setq char (substr str idx 1))
                (wcmatch char "#"))
      (setq val (strcat char val)
            idx (1- idx)))
    val)

  
  (defun :AttMaxValues (/ i en em att val num lst)
    (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "SPI-Datenextraktionspunkt-CM") (cons 410 (getvar 'CTAB)))))
      (repeat (setq i (sslength ss))
        (setq en (ssname ss (setq i (1- i))))
        (while (/= (cdr (assoc 0 (setq em (entget (setq en (entnext en)))))) "SEQEND")
          (and (eq (cdr (assoc 0 em)) "ATTRIB")
               (setq att (cdr (assoc 1 (reverse em))))
               (setq pre (vl-string-right-trim "0123456789" att))
               (setq num (:getNumFromRight att))
               (setq lst (if (setq a (assoc pre lst))
                           (subst (cons pre (if (> (atoi num) (atoi (cdr a)))
                                              num
                                              (cdr a)))
                                  a
                                  lst)
                           (cons (cons pre num) lst)))))))
    (setq lst (vl-sort lst '(lambda (x y) (< (car x) (car y))))))

  (defun :AttMaxValue (pre / )
    (if (setq a (assoc pre (:AttMaxValues)))
      (cdr a)
      "0"))  

  (defun :AddLeadingZeros (a d / b) ;add zeros to 'd' many digits  ;a string
    (strcat (substr "000000000" 1 (if (>= d (setq b (strlen (itoa (fix (atof a)))))) (- d b) 0)) a))
  
  
  ; ------------------------------------------------------------------------------------------------------
  
  (setq lay (getvar 'CLAYER))
  
  (command "_.-LAYER" "_m" "-I-Koordinatenpunkte" "_co" "3" "-I-Koordinatenpunkte" "")
  
  (setq oATTREQ (getvar 'ATTREQ)
        oATTDIA (getvar 'ATTDIA))
  (setvar 'ATTREQ 1)
  (setvar 'ATTDIA 0)
  
  (or *<Koordpunkt-g*
      (setq *<Koordpunkt-g* ""))

  (setq amvs (:AttMaxValues))
  (princ (strcat "\nMAX: " (apply 'strcat (mapcar '(lambda (x) (strcat (car x) (cdr x) " # ")) amvs))))
  
  (initget 128)
  (setq *<Koordpunkt-g* (strcase (cond ((getkword (strcat "\nGeschoss angeben (zB. \"1U-\", \"EG-\" oder \"\" für Ohne) <" (if (= "" *<Koordpunkt-g*) "Ohne" *<Koordpunkt-g*) ">: ")))
                                       (*<Koordpunkt-g*)))
        *<Koordpunkt-g* (if (= "\"\"" *<Koordpunkt-g*)
                          ""
                          *<Koordpunkt-g*))
  
  
  (setq *<Koordpunkt-i* (1+ (atoi (setq *<Koordpunkt-z* (:AttMaxValue *<Koordpunkt-g*))))
	*<Koordpunkt-z* (strlen *<Koordpunkt-z*)
        *<Koordpunkt-i* (cond ((getint (strcat "\nPunktnummer angeben <" (:AddLeadingZeros (itoa *<Koordpunkt-i*) *<Koordpunkt-z*) ">: ")))
                              (*<Koordpunkt-i*)))

  (setq *<Koordpunkt-z* (cond ((getint (strcat "\nFill up with leading zeros up to <" (itoa *<Koordpunkt-z*) "> place: "))) (*<Koordpunkt-z*))) ; uncomment this line to ask the user

    
  (setq enlast (entlast))
  
  (while (progn
           (initget "Undo")
           (setq pnt (getpoint (strcat "\nEinfügepunkt angeben für '" (setq txt (strcat *<Koordpunkt-g* (:AddLeadingZeros (itoa *<Koordpunkt-i*) *<Koordpunkt-z*))) "' [Undo] <exit>: "))))
    (if (= pnt "Undo")
      (progn
        (if (equal enlast (entlast))
          (alert "Alle eingefügten Punkte gelöscht!")
          (progn
            (setq enl (entlast))
            (command "_.UNDO" 1)
            (if (not (equal enl (entlast)))
              (setq *<Koordpunkt-i* (1- *<Koordpunkt-i*))))))
      (progn
        (command "_.-INSERT" "SPI-Datenextraktionspunkt-CM" "_none" pnt "1" "1" "" txt "")
        (setq *<Koordpunkt-i* (1+ *<Koordpunkt-i*)))))
  ) ; end of defun
Message 60 of 64

ВeekeeCZ
Consultant
Consultant
Just realized that I didn't add the old format support. But it's easy for you to fix, isn't it? 😉