copy/ extract string from filename

copy/ extract string from filename

Anonymous
Not applicable
1,654 Views
9 Replies
Message 1 of 10

copy/ extract string from filename

Anonymous
Not applicable

Hello, everyone,

After self-educating lisp and consulating online for a while, I still couldn't find solutions for my questions; refer to attached plan

 

I want to extract certain text strings from file name and put it in the attributes. For example, the file name is “11175 #5-Station Paris_AR-03 Plans 2150 Nord.dwg” and

  1. if file name contain #1, #2,#3 #4, #5, the attribute “PLANNO” shall be 8001-AR-01(or 02,03,04 05), the last 2 numbers corresponding to the one after #.
  2. if file name contain #6, #7,#8, the attribute “PLANNO” shall be 9001-AR-06(or 07,08).

I have learned a lot from the people and appreciate your constant help.

 

one another question: how did you get the knowledge on Autolisp, self-educating or in school?

 

Thanks,

0 Likes
Accepted solutions (1)
1,655 Views
9 Replies
Replies (9)
Message 2 of 10

john.uhden
Mentor
Mentor

First of all, I learned a lot by decrypting DCA encrypted AutoLisp files (forerunner of Softdesk and Land Desktop and now Civil 3D).  Then it was mostly trial and error.  Then, after I was getting pretty comfortable, I joined this forum back in the Compuserve days when guys like Terry Dotson, Owen Wengerd, Steve Johnson, Bill Townsend, Stephan Koster, and of course Tony Tanzillo were helping out.  We had a lot of fun attempting to build functions that worked faster than than the next guy's.

 

As to your file names, might a name actually contain "#1, #2,#3 #4, #5" or just #1, or #2, or #3, etc?  Does the number of digits exceed 2, as in #10 or #13?

If multiple numbers, are they always separated by commas?

John F. Uhden

Message 3 of 10

pbejse
Mentor
Mentor

Does the number of digits exceed 2, as in #10 or #13?


Good question.

 

Will the value representing  8001 or 9001 change based on a range? or is just these two?

 

 

Message 4 of 10

Anonymous
Not applicable
Cool, good to know the legendaries.
Each file name has #1, #2.... respectively. The number will exceed 2
digits till 14.
Thx, John
0 Likes
Message 5 of 10

Anonymous
Not applicable
Cool, good to know the legendaries.
Each file name has #1, #2.... respectively. The number will exceed 2
digits till 14.
Thx, John
0 Likes
Message 6 of 10

pbejse
Mentor
Mentor

AR from 8001-AR-01 is based from #5-Station Paris_AR-03 Plans 2150 Nord.dwg ?  it varies depending on the filename? say IR

 

Message 7 of 10

pbejse
Mentor
Mentor
(defun c:demo  (/
                drawingName
                discipline
                prefixCode
                theAttributeBlock
                nameToSetPLANNO)

;;;			pBe July 201			lll
;;;							;;;
      (if
            (and
                  (= (getvar 'Dwgtitled) 1)
                  (setq drawingName (getvar 'Dwgname))
                  (setq p (vl-string-position 35 drawingName))
                  (> (setq numberAfterSymbol
                                (atoi (substr drawingName
                                              (+ 2 p))))
                     0)
                  (setq discipline
                             (vl-some
                                   '(lambda (d)
                                          (if (vl-string-search
                                                    d
                                                    drawingName)
                                                (substr d 2)))
                                   '("_AR-" "_ST-" "_IR-" "_PL-")))
                  (setq prefixCode
                             (cond
                                   ((<= 1 numberAfterSymbol 5) "8001-")
                                   ((<= 6 numberAfterSymbol 14) "9001-")
                                   )
                        )
                  (setq theAttributeBlock
                             (ssget "_X"
                                    '((0 . "INSERT")
                                      (410 . "~Model")
                                      (66 . 1)
                                      (2 . "FR_ATTR-T*T"))))
                  )
                 (progn
                       (setq nameToSetPLANNO
                                  (strcat prefixCode
                                          discipline
                                          (nth (strlen (setq sn   (itoa numberAfterSymbol)))
                                               '(nil "0" ""))
                                          sn))

                       (repeat (setq i (sslength theAttributeBlock))
                             (vl-some
                                   '(lambda (atb)
                                          (if   (equal
                                                      (vla-get-tagstring
                                                            atb)
                                                      "PLANNO")
                                                     (vla-put-textstring
                                                           atb
                                                           nameToSetPLANNO)))
                                   (vlax-invoke
                                         (vlax-ename->vla-object
                                               (ssname
                                                     theAttributeBlock
                                                     (setq i (1- i))))
                                         'GetATtributes)
                                   )
                             )
                       )
                 )
      (princ)
      )

HTH

Message 8 of 10

Anonymous
Not applicable

the four digits will vary acoording the range; 10-14 will be 10001, and #8 for 1101

thanks

0 Likes
Message 9 of 10

Anonymous
Not applicable

AR will remain. 

0 Likes
Message 10 of 10

pbejse
Mentor
Mentor
Accepted solution

@Anonymous wrote:

the four digits will vary acoording the range; 10-14 will be 10001, and #8 for 1101

thanks


 

From this

(setq prefixCode
     (cond
           ((<= 1 numberAfterSymbol 5) "8001-")
           ((<= 6 numberAfterSymbol 14) "9001-")
           )
)

To

(setq prefixCode
     (cond
           ((=  8  numberAfterSymbol) 	"1101-"	)  
           ((<= 1  numberAfterSymbol 5) "8001-"	)
	   ((<= 6  numberAfterSymbol 9) "9001-"	)
           ((<= 10  numberAfterSymbol 14) "10001-") 
           )
)

 


@Anonymous wrote:

AR will remain. 


Remove

(setq discipline
     (vl-some
           '(lambda (d)
                  (if (vl-string-search
                            d
                            drawingName)
                        (substr d 2)))
           '("_AR-" "_ST-" "_IR-" "_PL-")))

And from this

(setq nameToSetPLANNO
          (strcat prefixCode
                  discipline
                  (nth (strlen (setq sn   (itoa numberAfterSymbol)))
                       '(nil "0" ""))
                  sn))   

To

 

(setq nameToSetPLANNO
          (strcat prefixCode
                  "AR-"
                  (nth (strlen (setq sn   (itoa numberAfterSymbol)))
                       '(nil "0" ""))
                  sn))

HTH

0 Likes