Difficult thing for me, Find the number in the file name!

Difficult thing for me, Find the number in the file name!

boyds86
Enthusiast Enthusiast
599 Views
6 Replies
Message 1 of 7

Difficult thing for me, Find the number in the file name!

boyds86
Enthusiast
Enthusiast

Hello,

 

I am trying to build a lisp as require but I can not, Please help me:


For example:


1-If the file name is J00823456-1-10 1398 Nam--> It will have 10 numbers appear in the dwg: J00823456-1,J00823456-2...J00823456-10
2-If the file name is J00823456-1,6,12,13 1398 Nam--> It will have 4 numbers appear in the dwg: J00823456-1,J00823456-6,J00823456-12,J00823456-16

3-If the file name is J00823456-1-10 1398 Nam (Ask 2,6)-->It will have 8 numbers appear in the dwg except J00823456-2,J00823456-8

 

The idea is pick the number from -....to first space. Then count the number.

After We get the numbers, we will compare in the drawing that how many Mtext in the current drawing (Mtext,layer 55) (ssget "_X" '((0 . "*TEXT") (1 . "J*") (8 . "55")))

If they match, do nothing, IF is is different, it will alert: it is wrong.

 

The attached file is image and CAD file!

Thank you!
--------------------------------

01.png

02.png

03.png

0 Likes
Accepted solutions (1)
600 Views
6 Replies
Replies (6)
Message 2 of 7

ВeekeeCZ
Consultant
Consultant

It's not too difficult, just a few vl-string functions all over again.

 

(vl-load-com)

(defun c:FilenameCheck ( / LM:str->lst n x f v l k e1 e2)

  (defun LM:str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
        (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
        (list str)))

  (setq n (vl-filename-base (getvar 'dwgname)))
  (if (and (or (setq x (vl-string-search "-" n))
	       (alert "Error: Wrong file to check. Dash check failed."))
	   (setq f (substr n 1 x))
	   (setq v (substr n (+ x 2)))
	   (or (setq x (vl-string-search " " v))
	       (alert "Error: Wrong file to check. Space check failed."))
	   (setq v (substr v 1 x))
	   (or (apply '= (vl-string->list (vl-string-translate "01234567891-," "             " v)))
	       (alert "Error: Wrong file to check. Version check failed."))
	   )
    (progn
      (setq l (LM:str->lst v ",")
	    l (mapcar '(lambda (x) (LM:str->lst x "-")) l)
	    l (mapcar '(lambda (x)
			 (if (= (length x) 2)
			   (repeat (- (atoi (cadr x)) (atoi (car x)) 1)
			     (setq x (cons (car x) (cons (itoa (1- (atoi (cadr x)))) (cdr x))))))
			 x)
		      l)
	    l (apply 'append l)
	    l (mapcar '(lambda (x) (strcat f "-" x)) l))))

  (if (wcmatch n "*(Ask*)")
    (if (and (setq x (vl-string-position (ascii "(") n 0 T))
	     (setq v (substr n (+ x 5)))
	     (setq v (vl-string-trim " )" v))
	     )
    (progn
      (setq k (LM:str->lst v ",")
	    k (mapcar '(lambda (x) (LM:str->lst x "-")) k)
	    k (mapcar '(lambda (x)
			 (if (= (length x) 2)
			   (repeat (- (atoi (cadr x)) (atoi (car x)) 1)
			     (setq x (cons (car x) (cons (itoa (1- (atoi (cadr x)))) (cdr x))))))
			 x)
		      k)
	    k (apply 'append k)
	    k (mapcar '(lambda (x) (strcat f "-" x)) k)))))
  (setq l (vl-remove-if '(lambda (x) (vl-position x k)) l))

  (setq l (mapcar '(lambda (x) (if (ssget "_X" (list '(0 . "*TEXT") '(1 . "J*") '(8 . "55") (cons 1 x)))
				 T
				 (not (setq e1 (cons x e1)))))
		  l))
  (setq k (mapcar '(lambda (x) (if (ssget "_X" (list '(0 . "*TEXT") '(1 . "J*") '(8 . "55") (cons 1 x)))
				 (not (setq e2 (cons x e2)))
				 T))
		  k))
  
  (if (and (apply 'and l)
	   (apply 'and k)
	   )
    (alert "It's ok")
    (alert "Not ok"))

  (if e1 (progn (princ "Error missing: ") (princ e1)))
  (if e2 (progn (princ "Error sparing: ") (princ e2)))
  
  (princ)
  )
 

 

0 Likes
Message 3 of 7

boyds86
Enthusiast
Enthusiast

Thank you very much but 4 more things I need is that:

1-If filename is: J00825654-1-3 1398 Nam--> Yourcurrent lisp:-->in the dwg J00825654-1 ,J00825654-2,J00825654-3,J00825654-4......It still says: It is OK. I want it say Not OK.

2-If filename is: J00825654-1-3 1398 Nam (Ask 2) --> Yourcurrent lisp:-->in the dwg J00825654-1 ,J00825654-3,J00825654-4......It still says: It is OK. I want it say Not OK.

3-J00825654-1-3 1398 Nam (Ask 2) Finish--> It does not works. Have text after (Ask..) it seem does not works!


4-J00825654-1-2 1398 Nam -->Yourcurrent lisp:-->in the dwg J00825654-1 only It still say: It is OK

The problem, first we count the numbers in the filenames and the numbers (J...) in the dwg, them compare them.


I am trying to fix your lisp to suitable for me but my knowleage have some limit......

0 Likes
Message 4 of 7

ВeekeeCZ
Consultant
Consultant
Accepted solution

Ok, I tried... I didn't test all the variants... you did not post the files.

 

Anyway, just to be clear. The posted code is an example of how such a thing could be done. Not going to play with every little possibility you can think of. That's up to YOU. 

 

(vl-load-com)

(defun c:FilenameCheck ( / LM:str->lst n x f v l k a m)
  
  (defun LM:str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
      (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
      (list str)))
  
  (setq n (vl-filename-base (getvar 'dwgname)))
  
  (if (and (or (setq x (vl-string-search "-" n))
	       (alert "Error: Wrong file to check. Dash check failed."))
	   (setq f (substr n 1 x))
	   (setq v (substr n (+ x 2)))
	   (or (setq x (vl-string-search " " v))
	       (alert "Error: Wrong file to check. Space check failed."))
	   (setq v (substr v 1 x))
	   (or (apply '= (vl-string->list (vl-string-translate "01234567891-," "             " v)))
	       (alert "Error: Wrong file to check. Version check failed."))
	   )
    (progn
      (setq l (LM:str->lst v ",")
	    l (mapcar '(lambda (x) (LM:str->lst x "-")) l)
	    l (mapcar '(lambda (x)
			 (if (= (length x) 2)
			   (repeat (- (atoi (cadr x)) (atoi (car x)) 1)
			     (setq x (cons (car x) (cons (itoa (1- (atoi (cadr x)))) (cdr x))))))
			 x)
		      l)
	    l (apply 'append l)
	    l (mapcar '(lambda (x) (strcat f "-" x)) l))))
  
  (if (wcmatch n "*(Ask*)")
    (setq x (vl-string-position (ascii "(") n 0 T)
	  v (substr n (+ 5 x) (- (vl-string-position (ascii ")") n 0 T) x 4))
	  v (vl-string-trim " " v)
	  k (LM:str->lst v ",")
	  k (mapcar '(lambda (x) (LM:str->lst x "-")) k)
	  k (mapcar '(lambda (x)
		       (if (= (length x) 2)
			 (repeat (- (atoi (cadr x)) (atoi (car x)) 1)
			   (setq x (cons (car x) (cons (itoa (1- (atoi (cadr x)))) (cdr x))))))
		       x)
		    k)
	  k (apply 'append k)
	  k (mapcar '(lambda (x) (strcat f "-" x)) k)
	  l (vl-remove-if '(lambda (x) (vl-position x k)) l)))
  
  (if (and (setq a (ssget "_X" (list '(0 . "*TEXT") '(8 . "55") (cons 1 "J*"))))
	   (setq p (mapcar '(lambda (x) (ssget "_X" (list '(0 . "*TEXT") '(8 . "55") (cons 1 x)))) l))
	   (apply 'and p) 						; catch if anything missing
	   (= (sslength a) (apply '+ (mapcar 'sslength p)))
	   )
    (alert "It's ok")
    (alert "Not ok"))
  
  (princ)
  )

 

 

0 Likes
Message 5 of 7

boyds86
Enthusiast
Enthusiast

This is the perfect version. Thank you very much! You are a genius! All best wish for you!

0 Likes
Message 6 of 7

pbejse
Mentor
Mentor

@boyds86 wrote:


After We get the numbers, we will compare in the drawing that how many Mtext in the current drawing


If the drawing name is J00823456-1-3 1398 Nam.dwg and the number of MTEXT found on the drawing is 6 numbers but two of each of this string J00823456-1,  J00823456-2 and J00823456-3, will that still be be True? what if 

2 of J00823456-1 but only one of each of these J00823456-2 and J00823456-3, making a total of 4 MTEXT , will that also be TRUE? of basically there should only be three MTEXT? which means only one of each is allowed.

 

 

 

0 Likes
Message 7 of 7

boyds86
Enthusiast
Enthusiast
Thank you for your concern but it exists the same number is OK to me!