Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Folder Name into Text Field

  1. #1
    Member
    Join Date
    2012-04
    Location
    Redlands CA.
    Posts
    18
    Login to Give a bone
    0

    Default Folder Name into Text Field

    Im new to programming Lisp.
    Basically what i want todo is, add an field in Mtext that shows part of the directory name as a job number. But not the whole directory name. Example
    G:\745 The name of the Project\74534\drawings
    The above is the full directory name, what i want in the mtext field is 74534 not the rest of the folder name.
    G:\745 The name of the project <--- this part of the directory name varies in length.
    I do have this right now
    $(substr,$(getvar,DWGPREFIX),8,5)
    but the problem with this is that my prefix is at a set number of charactors. Our new folder layout varies now.
    \74534\drawings <--this part of the directory is fixed charactor length
    I did find this thread http://forums.augi.com/showthread.ph...der-name/page2
    But im having trouble trying to get them to work.
    I could use a quick step by step

    Any help would be great.

  2. #2
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: Folder Name into Text Field

    Welcome to AUGI!

    Give this a try:
    Code:
    (vl-load-com)
    (defun _FilePath->List (filePath / i folder folders)
      (setq filePath (vl-string-translate "\\" "/" filePath))
      (while (setq i (vl-string-search "/" filePath))
         (setq folders (cons (setq folder (substr filePath 1 i)) folders))
         (setq filePath (substr filePath (+ 2 i)))
         )
      (reverse folders)
      )
    Example:
    Code:
    _$ (_FilePath->List "G:\\745 The name of the Project\\74534\\drawings\\")
    ("G:" "745 The name of the Project" "74534" "drawings")
    _$
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  3. #3
    Member
    Join Date
    2012-04
    Location
    Redlands CA.
    Posts
    18
    Login to Give a bone
    0

    Default Re: Folder Name into Text Field

    Renderman,
    thanks for the reply,
    My next Noob question is. Do i copy paste the Code into a lisp file and load it through appload? and how do i use it? how would i use that in the Mtext field?
    Thanks again for the help

    Quote Originally Posted by RenderMan View Post
    Welcome to AUGI!

    Give this a try:
    Code:
    (vl-load-com)
    (defun _FilePath->List (filePath / i folder folders)
      (setq filePath (vl-string-translate "\\" "/" filePath))
      (while (setq i (vl-string-search "/" filePath))
         (setq folders (cons (setq folder (substr filePath 1 i)) folders))
         (setq filePath (substr filePath (+ 2 i)))
         )
      (reverse folders)
      )
    Example:
    Code:
    _$ (_FilePath->List "G:\\745 The name of the Project\\74534\\drawings\\")
    ("G:" "745 The name of the Project" "74534" "drawings")
    _$

  4. #4
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: Folder Name into Text Field

    Quote Originally Posted by janderson664783 View Post
    My next Noob question is. Do i copy paste the Code into a lisp file and load it through appload?
    I know some people still use appload, but I'm a fan of using Acad.lsp and AcadDoc.lsp instead.

    The former being loaded automagically once per session (by default), and the latter being loaded each time a drawing is opened in the editor. These are both user-defined files, meaning if they do not already exist within your Support File Search Path (SFSP), then simply open a new, empty text file and save it with a .LSP file extension. Save this/these files within your SFSP (near the top is recommended), et voila.

    Quote Originally Posted by janderson664783 View Post
    and how do i use it? how would i use that in the Mtext field?
    The code I posted is a sub-function which accepts a single argument, in this case a file path as String. There are numerous generic 'parser' sub-functions that will also accept a second argument, a delimiter as String, with which to use on multiple conditions. Since your situation is more specific, parsing a file path, I thought I'd just hard-code the delimeter, and account for both the double-backslash and single-forwardslash which are accepted in file paths as String.

    This can be used in any file path, and returns a list of Strings which represent each of the would be folders found within the file path argument. This code does not test that the file path is valid, and assumes that the user (you) will take care of this prior to passing the argument to this sub-function.

    Using this within a Field within an MText entity is a bit more complex, but should be relatively simple.

    ... Does this make (more?) sense to you now?
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  5. #5
    Member
    Join Date
    2012-04
    Location
    Redlands CA.
    Posts
    18
    Login to Give a bone
    0

    Default Re: Folder Name into Text Field

    RenderMan,
    It does make more sense, and i got it loaded using the AcadDoc.lsp and when i copy pasted at the command line:
    (_FilePath->List "G:\\745 The name of the Project\\74534\\drawings\\")
    It returned back to me:
    ("G:" "745 The name of the Project" "74534" "drawings")
    So i can see that is works, But now i need some help with the field in Mtext
    I have this currently in my field: $(substr,$(getvar,DWGPREFIX),8,5) / $(substr,$(getvar,DWGNAME),1,$(-,$(strlen,$(getvar,DWGNAME)),4))
    Which creatings my (job number / file name) (74534 / FD01-00)
    As noted before, \745 The name of the Project\ subfolder changed from a fixed count of charactors to variable count. so the first part of that getvar wont work (in RED). My end result is
    (74534 / FD01-00)

    Thanks again for all your help, i am learning alot very fast.

    Quote Originally Posted by RenderMan View Post
    I know some people still use appload, but I'm a fan of using Acad.lsp and AcadDoc.lsp instead.

    The former being loaded automagically once per session (by default), and the latter being loaded each time a drawing is opened in the editor. These are both user-defined files, meaning if they do not already exist within your Support File Search Path (SFSP), then simply open a new, empty text file and save it with a .LSP file extension. Save this/these files within your SFSP (near the top is recommended), et voila.



    The code I posted is a sub-function which accepts a single argument, in this case a file path as String. There are numerous generic 'parser' sub-functions that will also accept a second argument, a delimiter as String, with which to use on multiple conditions. Since your situation is more specific, parsing a file path, I thought I'd just hard-code the delimeter, and account for both the double-backslash and single-forwardslash which are accepted in file paths as String.

    This can be used in any file path, and returns a list of Strings which represent each of the would be folders found within the file path argument. This code does not test that the file path is valid, and assumes that the user (you) will take care of this prior to passing the argument to this sub-function.

    Using this within a Field within an MText entity is a bit more complex, but should be relatively simple.

    ... Does this make (more?) sense to you now?

  6. #6
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: Folder Name into Text Field

    The code you posted is Diesel, which is great for LT, but is lacking in many ways as compared to Visual LISP.

    Perhaps tomorrow I'll have some time during lunch to be of more help.

  7. #7
    Member
    Join Date
    2012-04
    Location
    Redlands CA.
    Posts
    18
    Login to Give a bone
    0

    Default Re: Folder Name into Text Field

    RenderMan,
    Thanks that would be great. I kind of figured it was lacking, so was looking into another way. We just had our cad meeting today and talked about the Projects folders label as exampled "G:\\745 The name of the Project\74534\drawings\" But told them i have to keep in mind that i have that Field in our title block, to aid in keeping the current file name and project number correct (74534 / FD01-00). As you would not believe how often people would forget to manual update that LOL.

    Thank you for your help, its always fun to learn new things.
    Sorry i didnt get back sooner, our office is usually close friday-sunday.

    James

  8. #8
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: Folder Name into Text Field

    Giving this more thought, if standardizing your directory structure is not an option, then incorporating LISP via AcadDoc.lsp is the only way I know of how to account for the inconsistent file paths.

    Perhaps rather than using LISP to brute-force update the Mtext's FieldCode String, it would just be simpler to add a Custom Properties to DWGPROPS (Custom tab), aptly named "JOB_NO". The 'filename' portion can be extracted via an existing Document Property in the Field selection box (more on that below).

    To create this Custom "JOB_NO" Property, simply use the DWGPROPS Command, Custom Tab, or use this code which checks for this property, and if it exists, updates it, if it doesn't it will create it:

    Code:
    (defun c:CheckEditProps (/ _FilePath->List _UpdateProperties oSummaryInfo)
    
      (defun _FilePath->List (filePath / i folder folders)
        (setq filePath (vl-string-translate "\\" "/" filePath))
        (while (setq i (vl-string-search "/" filePath))
          (setq folders (cons (setq folder (substr filePath 1 i)) folders))
          (setq filePath (substr filePath (+ 2 i)))
        )
        (reverse folders)
      )
    
      (defun _UpdateProperties (oSummaryInfo key value / val)
        (if (vl-catch-all-error-p
              (vl-catch-all-apply
                'vla-getcustombykey
                (list oSummaryInfo key 'val)
              )
            )
          (vla-addcustominfo oSummaryInfo key value)
          (vla-setcustombykey oSummaryInfo key value)
        )
      )
    
      (if (setq oSummaryInfo
                 (vla-get-summaryinfo
                   (vla-get-activedocument
                     (vlax-get-acad-object)
                   )
                 )
          )
        (_UpdateProperties
            oSummaryInfo
            "JOB_NO"
            (nth 2 (_FilePath->List (getvar 'dwgprefix)))
          )
      )
      (princ)
    )
    (c:CheckEditProps)
    Once you've created the Custom "JOB_NO" Property (manually, or via the code above), Change your Mtext to reference these two Fields:

    Code:
    %<\AcVar CustomDP.JOB_NO \f "%tc3">% / %<\AcVar Filename \f "%tc1%fn2">%
    The former will reference the Custom "JOB_NO" Property just created, and the latter will automagically reference the FileName (no file extension).

    Now, simply incorporate this code snippet into your AcadDoc.lsp file and each time you open a drawing, the "JOB_NO" Field will be updated. Also worthy of note, is that by incorporating this information into the DWGPROPS as a Custom Property, you can send your drawing out to an external client who does not have your AcadDoc.lsp, and this field will still be correct.

    Lemon squeezy.
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  9. #9
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,106
    Login to Give a bone
    0

    Default Re: Folder Name into Text Field

    The most common data storage method I use in LISP is CSV files...

    A general way to convert a path to a list of strings I use is...

    Peter


    Code:
    (defun CSVStringToList  (strText strChar / intPosition lstStrings)
     (while (setq intPosition (vl-string-search strChar strText 0))
      (setq lstStrings  (cons (substr strText 1 intPosition) lstStrings)
            strText     (substr strText (+ intPosition 1 (strlen strChar)))
      )
     )
     (if lstStrings
      (reverse (cons strText lstStrings))
      (list strText)
     )
    )
    (CSVStringToList (getvar "dwgprefix") "\\")
    AutomateCAD

  10. #10
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: Folder Name into Text Field

    Quote Originally Posted by peter View Post
    The most common data storage method I use in LISP is CSV files...
    Good point; "\t" is another good separator (as opposed to ",").


    Quote Originally Posted by peter View Post
    A general way to convert a path to a list of strings I use is...

    Code:
    (defun CSVStringToList  (strText strChar / intPosition lstStrings)
     (while (setq intPosition (vl-string-search strChar strText 0))
      (setq lstStrings  (cons (substr strText 1 intPosition) lstStrings)
            strText     (substr strText (+ intPosition 1 (strlen strChar)))
      )
     )
     (if lstStrings
      (reverse (cons strText lstStrings))
      (list strText)
     )
    )
    (CSVStringToList (getvar "dwgprefix") "\\")
    As I stated earlier in this thread, there are many parser sub-functions, but If I may offer some constructive criticism, Peter... The sub-function you shared yields (what I believe to be) an undesirable return value when:

    Code:
    (setq filePath (vl-string-translate "\\" "/" (getvar 'dwgprefix)))
    
    (CSVStringToList filePath "\\")
    The function I posted accounts for both forward, and back slashes, and returns Nil if an empty string is passed, whereas yours returns a list of the empty string argument (non-Nil) even though no valid input was passed. Not very useful in a test expression.

    Otherwise, these are very similar sub-functions.
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 2015-07-15, 06:38 PM
  2. Create folder Path from text in a .dcl text box
    By mark.hood695615 in forum AutoLISP
    Replies: 0
    Last Post: 2015-03-24, 06:58 PM
  3. 2012: inserting text into the text override field automatically?
    By MikeB6599 in forum AutoCAD General
    Replies: 28
    Last Post: 2014-12-23, 03:46 AM
  4. Field linking to a working folder
    By pilagan1 in forum AutoCAD General
    Replies: 1
    Last Post: 2012-10-05, 08:28 PM
  5. Replies: 4
    Last Post: 2006-04-25, 05:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •