Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Extract a portion of a filename

16 REPLIES 16
Reply
Message 1 of 17
shawn.p.phillips
586 Views, 16 Replies

Extract a portion of a filename

In a script file I want to check to see if a portion of the current files name contains "-FLS-".  If it does not then do command A, if it does then do not do command A.  Below is what I've started with but not sure where to go from here.  I suspect I need a string statement but I don't understand how to use them.  Clearly I am a beginner here.

 

(defun c:get-dwgname ( )
    (if (= (getvar 'dwgtitled) 1)
        (vl-filename-base (getvar 'dwgname))
    )
)

16 REPLIES 16
Message 2 of 17
hmsilva
in reply to: shawn.p.phillips

Try

(defun c:get-dwgname ( )
    (if (and (= (getvar 'dwgtitled) 1)
             (wcmatch (vl-filename-base (getvar 'dwgname)) "*-FLS-*")
             );; and
      ;; do command A
    )
)

 

 

Henrique

EESignature

Message 3 of 17
shawn.p.phillips
in reply to: hmsilva

forgive my ignorance but what do you mean by ";;and" and ";;do command A"?  You lost me there.  The rest makes sense.

Message 4 of 17
hmsilva
in reply to: shawn.p.phillips


@shawn.p.phillips wrote:

...what do you mean by ";;and" and ";;do command A"?  ...


The ;;xxx
are just comments to try to make easier the code understanding, in this case, signaling the end of the 'and' function, and the place where you can add your command A...
Henrique

EESignature

Message 5 of 17

Ok, I've gone way off the deep end here.  I am trying to run this script and getting an error:

 

;;;CHANGE HATCH TO COLOR 254
(defun c:hatsel (/ sset)
    (ssget "X" (list (cons 0 "HATCH")))
    (setq sset (ssget "P"))
    (sssetfirst sset sset)
    (princ)
)
;;;Settings for Fire Life Safety
;;;
(defun c:FLSSET ( )
    (Princ "Hi Shawn")
)
;;;Check Filename to proceed
;;;
(defun C:iddwg ( )
    (cond ((= (vl-string-search "-FLS-" (getvar 'dwgname))
         (command "FLSSET))
         (t (command "HATSEL"))
    )
    (princ)
)

 

Error is shown below:

Command: (defun C:iddwg ( )
(_>     (cond ((= (vl-string-search "-FLS-" (getvar 'dwgname))
((((_>          (command "FLSSET))
((((("_>          (t (command "HATSEL"))
((((("_>     )
((((("_>     (princ)
((((("_> )
((((("_> TILEMODE 0
((((("_> ZOOM ALL
((((("_> FILEDIA 1
((((("_> Undo E

Message 6 of 17
hmsilva
in reply to: shawn.p.phillips

Change your 'iddwg' function to

(defun C:iddwg ()
  (cond ((vl-string-search "-FLS-" (getvar 'dwgname))
         (c:FLSSET)
        )
        (t (c:HATSEL))
  )
  (princ)
)
:: or
(defun C:iddwg ()
  (if (vl-string-search "-FLS-" (getvar 'dwgname))
    (c:FLSSET)
    (c:HATSEL)
  )
  (princ)
)

 

Henrique

EESignature

Message 7 of 17
shawn.p.phillips
in reply to: hmsilva

That worked for the most part but the HATSEL routine isn't initiating when the filename doesn't have the "-FLS-" in it.  No errors, just not running the routine.

Message 8 of 17
hmsilva
in reply to: shawn.p.phillips

It should

 

try

(defun c:hatsel (/ sset)
  (if (setq sset (ssget "X" (list (cons 0 "HATCH"))))
    (sssetfirst nil sset)
  )
  (princ)
)

 

Henrique

EESignature

Message 9 of 17
shawn.p.phillips
in reply to: hmsilva

It worked before.  I forgot to add the command line telling it what to do with the hatch once selected.  Working smoothly now.  Thanks so much for your help.

Message 10 of 17
hmsilva
in reply to: shawn.p.phillips

You're welcome, Shawn
Glad I could help

Henrique

EESignature

Message 11 of 17
shawn.p.phillips
in reply to: hmsilva

ok, so one more caveat, how do I add another command when the filename does NOT contain FLS?  So in addition to HATSEL I want to run BASICSET as well.  Actually run BASICSET then HATSEL in that order.

Message 12 of 17
hmsilva
in reply to: shawn.p.phillips


@shawn.p.phillips wrote:

ok, so one more caveat, how do I add another command when the filename does NOT contain FLS?  So in addition to HATSEL I want to run BASICSET as well.  Actually run BASICSET then HATSEL in that order.


If 'BASICSET' function is defined as a command (with 'c:' in name), perhaps something like this

 

(defun c:iddwg ()
  (if (vl-string-search "-FLS-" (getvar 'dwgname))
    (c:FLSSET)
    (progn
      (c:BASICSET)
      (c:HATSEL)
    )
  )
  (princ)
)

 

Henrique

EESignature

Message 13 of 17
shawn.p.phillips
in reply to: hmsilva

thanks again

Message 14 of 17
Lee_Mac
in reply to: shawn.p.phillips

shawn.p.phillips wrote:

ok, so one more caveat, how do I add another command when the filename does NOT contain FLS?  So in addition to HATSEL I want to run BASICSET as well.  Actually run BASICSET then HATSEL in that order.

 

Try something like this:

 

(defun c:iddwg nil
  (if (wcmatch (getvar 'dwgname) "*-FLS-*")
      (c:FLSSET)
      (progn
          (c:BASICSET)
          (c:HATSEL)
      )
  )
  (princ)
)

 

The progn function will simply evaluate each of the supplied arguments, returning the value returned by the last evaluated argument; the progn expression can then be supplied as a single argument to the if function to enable multiple expressions to be evaluated as part of the 'then' or 'else' arguments.

 

Lee

Message 15 of 17
Lee_Mac
in reply to: Lee_Mac

Looks like I was too slow! - You're too quick Henrique Smiley Wink

Message 16 of 17
hmsilva
in reply to: Lee_Mac


@Lee_Mac wrote:

Looks like I was too slow! - You're too quick Henrique Smiley Wink


Faster than you...once in a lifetime... Smiley Very Happy

 

Cheers

Henrique

EESignature

Message 17 of 17
doni49
in reply to: hmsilva

Shawn,

 

You've been given some great advice.  I do have a question for you though.

 

Your code makes calls to three other autolisp functions.  Are those functions ALWAYS called from within a lisp routine?  Or do you want them to be able to be called from the command line?

 

If you have no need for having them called from the command line, then I'd remove the c-colon from their definitions and calls.

 

So instead of having the following definition and subsequent function call:

 

(defun c:basicset().....................

 

(c:basicset)

 

 

I'd have the following:

 

(defun basicset().........................

 

(basicset)



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost