Capture and use elements of a file name.

Capture and use elements of a file name.

Anonymous
Not applicable
1,917 Views
5 Replies
Message 1 of 6

Capture and use elements of a file name.

Anonymous
Not applicable

I have several lisp routines that I want to create. Most of which are pretty simple which is good since I'm not a great programmer. Most of all of these require a similar element that I am having trouble with. I'm looking for a way to capture a segment of a file name and place it into a string of text to create a new file name.

 

 

Let me try and describe my sequence, hopefully it makes sense.

 

I'm in a drawing called 001-Xref.dwg, I want lisp to read and store just the first three letters of that file name ("001" As this changes per project). secondly I want to run the wblock command, select all and save the file name be 001-Site.dwg.  

 

Hopefully this makes sense, just in case I will try a second description. save and store the first three letters of the current file name, use that stored data and add to the rest of a standard file name. 

 

Please ask any questions you may have.

 

 

0 Likes
1,918 Views
5 Replies
Replies (5)
Message 2 of 6

Ranjit_Singh
Advisor
Advisor

How you call the first 3 letters will depend on your LISP code. Use the below to extract first 3 letters of drawing name

 

(substr (getvar 'dwgname) 1 3)
0 Likes
Message 3 of 6

Anonymous
Not applicable

Thank you,that's part of it. Now how to add it back in to create a new file name with the same 3 letter prefix. 

0 Likes
Message 4 of 6

Ranjit_Singh
Advisor
Advisor
(setq filename (getvar 'dwgname))
(command "._saveas" "" (strcat (substr filename 1 3) "-Site" (vl-filename-extension filename)))
There are several existing codes for what you are trying to accomplish. Do a search in the community forums
0 Likes
Message 5 of 6

Anonymous
Not applicable

How's this?

 

(defun C:SAVE_AS_SITE()
 (setq oldcmddia (getvar "cmddia"))

 (setvar "cmddia" 0)
 (setq new_dwg_name (getvar "dwgname") )

 (while (< (strlen new_dwg_name) 7)
  (setq new_dwg_name (strcat "0" new_dwg_name))
 )

 (setq new_dwg_name (substr new_dwg_name 1 3))
 (setq new_dwg_name (strcat (getvar "dwgprefix") new_dwg_name "-site.dwg"))
 (if (findfile new_dwg_name)
  (progn
   (princ (strcat "\nFile " new_dwg_name " already exists. "))
   (initget "Yes No")
   (if (= "Yes" (getkword "\nOverwrite? Yes, <No>: "))
    (progn
     (command "-wblock" new_dwg_name "y" "*")
     (while (= 1 (getvar "cmdactive"))
      (command "n")
     )
    )
    (princ "\nCancelled. ")
   )
  )
  (progn
   (command "-wblock" new_dwg_name "*")
   (while (= 1 (getvar "cmdactive"))
    (command "n")
   )
  )
 )
 (setvar "cmddia" oldcmddia)
 (prin1)
)

 

The reason I temporarily reset the system variable "cmddia" to 0 is because if you have AutoCAD Map it will pop up a dialog box asking

 

Include Autodesk Map information in the export?

 

and AutoLISP can't click the buttons on that dialog box.  Whereas if cmddia is set to 0, rather than getting a dialog box you get another prompt at the AutoCAD command line, and AutoLISP can respond to that prompt.

 

Now look at a couple more system variables, dwgprefix and dwgname.  If your drawing is named

 

I:\PROJECT\SUR\15005600SS\DWG\WKS2017,DWG

 

then the dwgname system variable is

 

WKS2017.DWG

 

and the dwgprefix system variable is

 

I:\PROJECT\SUR\15005600SS\DWG\

 

You want the first three characters of the name of your current drawing for the prefix for the output file. The first thing that comes to mind is to just use

 

(substr (getvar "dwgname") 1 3)

 

But suppose the name of your drawing has only one or two characters, e.g. SP.DWG or A.DWG? That's what the first while loop does; it adds the character "0" (that's a zero, not a capital "oh") to the beginning of new_dwg_name until it is at least seven characters long, including the ".dwg" part. So if your drawing was named SP.DWG then NEW_DWG_NAME becomes

 

0SP.dwg

 

and if your drawing was named A.DWG then NEW_DWG_NAME becomes

 

00A.dwg

 

Now that the part before the period is at least three characters long, chop it down to the first three characters with

 

(setq new_dwg_name (substr new_dwg_name 1 3))

 

Now build the output file name.  Add together the path of the original drawing, the three letter prefix stored as NEW_DWG_NAME, and the string "-SITE.DWG".  If the original drawing was

 

I:\PROJECT\SUR\15005600SS\DWG\WKS2017,DWG

 

then (setq new_dwg_name (strcat (getvar "dwgprefix") new_dwg_name "-site.dwg")) returns

 

I:\PROJECT\SUR\15005600SS\DWG\WKS-site.dwg

 

Next, use the (findfile) function to see if there's already a file by that name.  If there is, then this program asks you if you want the -WBLOCK command to overwrite it, and if you say "yes" then it runs the -WBLOCK command, with the following prompts:

 

Command: -WBLOCK

Enter name of output file:  i:\project\sur\15005600ss\dwg\wks-site.dwg
I:\project\sur\15005600ss\dwg\wks-site.dwg already exists, do you want to replace it? [Yes/No] <N>: y
Enter name of existing block or [= (block=output file)/* (whole drawing)] <define new drawing>: *

But if the there's not already a file with the name stored in new_dwg_name it runs the -wblock command without that second prompt:

 

Command: -WBLOCK

Enter name of output file:  i:\project\sur\15005600ss\dwg\wks-site.dwg

Enter name of existing block or [= (block=output file)/* (whole drawing)] <define new drawing>: *

 

Now there's another while loop.  What's happening there is that if you are running AutoCAD Map you will get that "Include Autodesk Map information in the export?" prompt at the command line, to which you want to answer "No".  Since you're still in the middle of the -WBLOCK command the system variable cmdactive will be 1, and the loop will issue an "n" to the command line.  This will complete the -WBLOCK command, at which point cmdactive will be 0 and you'll jump out of the loop.  If, however, you aren't running AutoCAD Map, the -WBLOCK command will have already terminated, cmdactive will be 0, and the loop will be bypassed.

 

Finally, reset the cmddia system variable to its original values and exit.

 

It's possible that -WBLOCK will have failed, however.  If you don't have write permission in the target directory, or the disc is full, or if you are trying to overwrite a read-only file, then you'll see

 

Error writing/closing file.

 

at the command line.

Message 6 of 6

Anonymous
Not applicable

Thank you very much this worked perfectly

0 Likes