Reorder Attributes values

Reorder Attributes values

C.Utzinger
Collaborator Collaborator
3,576 Views
53 Replies
Message 1 of 54

Reorder Attributes values

C.Utzinger
Collaborator
Collaborator

Hi

 

I have the attached block with one Attribute.

 

I'm looking for a Lisp code to reoder the given values. Like:

 

EG-001, EG-002, EG-003, EG-003, EG-007, EG-010 ---> EG-001, EG-002, EG-003, EG-004, EG-005, EG-006.

 

I tried some codes i found in other Forums, but nothing worked.

 

Please help...

0 Likes
Accepted solutions (2)
3,577 Views
53 Replies
Replies (53)
Message 2 of 54

pbejse
Mentor
Mentor

@C.Utzinger wrote:

 

I'm looking for a Lisp code to reoder the given values. Like:

 

EG-001, EG-002, EG-003, EG-003, EG-007, EG-010 ---> EG-001, EG-002, EG-003, EG-004, EG-005, EG-006.

 

 

Please help...


Re-order in what way? You mean from a selection? 

 

0 Likes
Message 3 of 54

C.Utzinger
Collaborator
Collaborator

This block is just a Point with a number, and you can have this point 100 times in a drawing.

 

But sometimes you Need to erase some Points and then you don´t have an contiued order.

 

Order you have:  EG-001, EG-002, EG-003, EG-003, EG-007, EG-010

Order you want: EG-001, EG-002, EG-003, EG-004, EG-005, EG-006.

 

Thank you

0 Likes
Message 4 of 54

pbejse
Mentor
Mentor

@C.Utzinger wrote:

This block is just a Point with a number, and you can have this point 100 times in a drawing.

 

 

Thank you


Easy. one question though , are you wanting the user to be prompted for selection? and just to be sure, will it always start with 001? waht if the missing numbers are from 001 to 003? should the new order starts with 004? I've was asked to consider this condition once before is why i'm asking.

 

 

 

 

0 Likes
Message 5 of 54

C.Utzinger
Collaborator
Collaborator

I don't understand exaclty your last question.

 

But yes, the numbers should always start with 001.

 

And prompt for selection also would be good.

 

Thank you...

0 Likes
Message 6 of 54

pbejse
Mentor
Mentor

@C.Utzinger wrote:

I don't understand exaclty your last question.

 Thank you...


Ok, i will explain, say there are 4 sheets., the first sheet shows items from 1 to 250, second sheet 250 to 312 and so on. Say for example on sheet 2 , 265, 298 are missing and 305 has a duplicate, If you are running a program that always reset the lowest number to one , sheet 2 will end up with 1 to 62.Hence the question.

 

Do you want to be prompted for the lowest number?

 

0 Likes
Message 7 of 54

C.Utzinger
Collaborator
Collaborator

Ohh ok.

 

Yes that would be great!

 

 

Thank you in advance!

0 Likes
Message 8 of 54

pbejse
Mentor
Mentor

@C.Utzinger wrote:

 

Yes that would be great!



Ok, last question Cree-G, is this specific to "SPI-Datenextraktionspunkt-CM" block with "01" Tag name for attribute?

 

0 Likes
Message 9 of 54

C.Utzinger
Collaborator
Collaborator

Yes sprecific only for that block.

 

Thank you

0 Likes
Message 10 of 54

pbejse
Mentor
Mentor

@C.Utzinger wrote:

Yes sprecific only for that block.

 



Specific for that block. "SPI-Datenextraktionspunkt-CM"

Only one attribute TAG.

Values are in this format XXX-### (alpha characters - numeric value)

User prompt selection

 

 

A lisp code skills are a bit rusty nowadays. 

 

 

(defun c:demo ( / _split ss i e attVal_collection)
;;;	pBe July 2007  ;;;
(setq _split (lambda (a)
		(setq p (vl-string-position 45 a nil t))
	       (if p  (list (substr a 1 (1+ p)) (substr a (+  2 p))) a)))
			
(and (setq ss (ssget "_:L" '((0 . "INSERT")(66 . 1)(2 . "SPI-Datenextraktionspunkt-CM"))))
     (repeat (setq i (sslength ss))
	  (setq e (vlax-ename->vla-object  (ssname ss (setq i (1- i)))))
	  (setq attVal_collection (cons (car (mapcar (function (lambda (at)
			(list ( _split (vla-get-textstring at)) at )))
	                      (vlax-invoke e 'Getattributes))
	  	) attVal_collection))
	  )
    	(setq attVal_collection 				 
	       (vl-sort
		 (vl-remove-if-not '(lambda (x) (and (listp (Car x))
						    (numberp (read (cadar x))))) attVal_collection)
					 '(lambda (a b) (< (distof (cadar a))
							   (distof (cadar b))))))
	(setq startNum
	  (cond
	    ((getint  (strcat "\nEnter Start Number <"
	          (itoa (setq startNum (atoi (cadar (car attVal_collection)))))
	          ">: " )))
	    ( startNum )
	  )
	)
	    (Foreach itm attVal_collection
	      (vla-put-textstring (Cadr itm)
	      (strcat (Caar itm) (nth (strlen (itoa startNum)) '(nil "00" "0" "")) (itoa startNum)) )
	      (setq startNum (1+ startNum))
	    )
	)
  )

 

Command: DEMO
Select objects: Specify opposite corner: 5 found ( or user can type ALL )
Select objects:
Enter Start Number <1>:  ( Default value will the the lowest number from the selection)

 

HTH

 

 

 

0 Likes
Message 11 of 54

C.Utzinger
Collaborator
Collaborator

This is a very good start.

 

The only Problem now is that ther could be different fomat:

 

#

##

###

####

X-#

X-##

X-###

X-####

XX-#

XX-##

XX-###

XX-####

XXX-#

XXX-##

XXX-###

XXX-####

 

and perhaps

 

XXXX-#

XXXX-##

XXXX-###

XXXX-####

 

Sorry if this is a bigger Problem :(...

 

Please help and Kind regards...

0 Likes
Message 12 of 54

pbejse
Mentor
Mentor

@C.Utzinger wrote:

This is a very good start.

The only Problem now is that ther could be different fomat:....

 

 ....

Sorry if this is a bigger Problem :(...

 

Please help and Kind regards...


Its not that big a deal to modify the code to consider all other formats.AND we're not going to let a little thing like that stop us 🙂

 

Try and replace the the _split line to:

 

(setq _split (lambda (a)
		(setq p (vl-some '(lambda (j)
			  (if (setq j (vl-string-position j a nil t)) j))'( 45 59))
				 )
	       (if p  (list (substr a 1 (1+ p)) (substr a (+  2 p))) a)))

 

Try it and tell me how it goes

 

pBe

 

I just realized what you meant by others, I will tweak it a bit more and add a prompt for the number format 1 / 01 / 001 etc..

 

We may not need the prompt though, Answer me this, if one value format is 001 i assume the rest are the same?

 

Also, i saw that you can write a code yourself, try to come up with a solution, If all else fails, we can always continue ...

Message 13 of 54

pbejse
Mentor
Mentor

pbejse wrote:
 .... Also, i saw that you can write a code yourself, try to come up with a solution, If all else fails, we can always continue ...

Nothing yet from your end Cree-G?

 

 

0 Likes
Message 14 of 54

C.Utzinger
Collaborator
Collaborator

Sorry

 

it was a short and intense familyweekend, and at night i was working on another code :).

 

The Video looks great...

 

 

A soon I have a little time i will look at it!

 

Kind regards

0 Likes
Message 15 of 54

pbejse
Mentor
Mentor

@C.Utzinger wrote:

.... it was a short and intense familyweekend,....

 

The Video looks great...

 ........A soon I have a little time i will look at it!

 


Nothing to apologies about Cree-g

 

Not the reply i was expecting. but anyway. Here's the code [ attached ]

 

Command: FGP <---- FillTheGap

 

HTH

 

 

 

0 Likes
Message 16 of 54

C.Utzinger
Collaborator
Collaborator

WOW

 

Thank you very much, and again sorry.

 

I just tested it and i'm really sorry i forgot to give you all the possibilities :(:(:(...

 

It could also be:

 

X#

X##

X###

X####

XX#

XX##

XX###

XX####

XXX#

XXX##

XXX###

XXX####

 

In a hurry i missed this ones, sorry!!!!

 

Thanks again

0 Likes
Message 17 of 54

pbejse
Mentor
Mentor

@C.Utzinger wrote:

 

 

I just tested it and i'm really sorry i forgot to give you all the possibilities :(:(:(...

 

It could also be: X# ..... XXX#### 


Really Cree-G? Another format?  Really?!! 😄

 

 

No worries,  but I'ts a good thing though, I found a bug on the previous code. I change the _split sub to account for the additional format

 

 

 

(defun c:FTG (/ _split _padZero ss i e attVal_collection Startnum
                Low_Num High_Num  Preifxformat)
              
;;;			Fill The Gap			;;;
;;;			pBe July 2007  			;;;
;;;							;;;
      
(defun _padZero (num md)
(setq num (strcat "0" num))
      (while (< (strlen num) md)
		(setq num (strcat "0" num))
	)
      )
      
(defun _split (lst i / numbers)
	(while (< 46  (setq n (nth (setq i (1- i)) lst)) 58)
			(setq numbers (cons n  numbers)))
      	(if (and numbers
                 (setq str (vl-list->string lst)))
		 (list (substr str 1 (1+ i)) (vl-list->string numbers))))
      	
(and (setq ss (ssget "_:L"
                     '((0 . "INSERT")
                       (66 . 1)
                       (2 . "SPI-Datenextraktionspunkt-CM"))))

     (repeat (setq i (sslength ss))
	  (setq e (vlax-ename->vla-object  (ssname ss (setq i (1- i)))))
          (setq attval (car (mapcar (function (lambda (at / num)                         
					 (list ( _split  (vl-string->list
						(setq str (vla-get-textstring at))) (strlen str)) at )))
                                  	 (vlax-invoke e 'Getattributes))))
           
	  	(if (car attval) (setq attVal_collection (cons attval  attVal_collection)))
           	attVal_collection
	  )
    	(setq attVal_collection 				 
	       (vl-sort attVal_collection
					 '(lambda (a b) (< (distof (cadar a))
							   (distof (cadar b))))))

	(setq Low_Num 	(cadar (car attVal_collection)))
        (setq High_Num  (cadar (last attVal_collection)))
	(setq Preifxformat (if (< (strlen Low_Num) (strlen High_Num))
                                       	 0 (strlen Low_Num)))
     
	(setq startNum
	  (cond
	    ((getint  (strcat "\nEnter Start Number <"
	          (itoa (setq startNum (atoi Low_Num)))
	          ">: " )))
	    ( startNum )
	  )
	)
	    (Foreach itm attVal_collection
	      (vla-put-textstring (Cadr itm)
	      (strcat (Caar itm)
                    (if (zerop  Preifxformat) (itoa startNum)
                    	(_padZero (itoa startNum) Preifxformat ))))
	      (setq startNum (1+ startNum))
	    )
	)
  )

 

HTH

 

0 Likes
Message 18 of 54

C.Utzinger
Collaborator
Collaborator

SOOOORRRRYYYY!!!!

 

The code works for less than 10 points.

 

If there are more, then it crashes with: stringp Nil

 

Could you fix this please?

 

 

Kind regards

0 Likes
Message 19 of 54

pbejse
Mentor
Mentor

Not on my end. post a sample drawing

 

0 Likes
Message 20 of 54

C.Utzinger
Collaborator
Collaborator

Sorry I take so much time from you...

 

Thanks again!

0 Likes