Author Topic: Parcing a filename into a block  (Read 8278 times)

0 Members and 2 Guests are viewing this topic.

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Parcing a filename into a block
« on: October 20, 2014, 09:30:39 PM »
I have several cad files that follow the same naming convention for instance.  It would be amazing if I could automate this. Outside of the sheet set manager.   

currently the files are named in this manor.

Unit - discipline - type - system -  DWG# - sheet

example
C1-X-41-CFS-170001-7

And I have a title block that has attributed fields.  (see image) Is there a way to use formulas to trim off the numbers leading or behind specific sections of the file name so I could populate the attributes automatically. 
the hard part is any one of the sections can vary in length.  the first C1 could be F45 and the 41 could easily be a 1 and the last number could be any-ware from 1 to 5 char.

So I'm looking for a way to parse the file name into it's sections using the delimiter "-" and counting over.. i.e.   if I'm looking for the sheet number  I'm looking for the text after the 4th "-" but before the 5th "-"

I've used trim in excel, but I don't know the full list of functions for AutoCAD fields.  and i don't know how to formulate the counting.

Each block attribute would have a different formula catering to which piece of info it would be taking from the file name.

Any Ideas?

I am very  trainable....   (forgive my spelling)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Parcing a filename into a block
« Reply #1 on: October 21, 2014, 06:34:39 AM »
If the hyphens in the filename were instead commas, then I believe this would be possible to achieve using DIESEL expressions, otherwise, I don't see a way to achieve this using only Field expressions.

If you are open to using LISP, the following program should perform as required (change the block name & tags at the top of the code to suit):

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / blk idx lst obj sel tag val )
  2.     (setq blk  "YourBlockName"
  3.           tag '("UNIT" "DISC" "TYPE" "SYS" "NUMBER" "SHEET")
  4.           blk  (strcase blk)
  5.           tag  (mapcar 'strcase tag)
  6.     )
  7.     (if (setq sel (ssget "_X" (list '(0 . "INSERT") '(66 . 1) (cons 2 (strcat "`*U*," blk)))))
  8.         (progn
  9.             (setq lst (mapcar 'cons tag (LM:str->lst (vl-filename-base (getvar 'dwgname)) "-")))
  10.             (repeat (setq idx (sslength sel))
  11.                 (if (= blk (strcase (LM:blockname (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx))))))))
  12.                     (foreach att (vlax-invoke obj 'getattributes)
  13.                         (if (setq val (cdr (assoc (strcase (vla-get-tagstring att)) lst)))
  14.                             (vla-put-textstring att val)
  15.                         )
  16.                     )
  17.                 )
  18.             )
  19.         )
  20.     )
  21.     (princ)
  22. )
  23.  
  24. ;; Block Name  -  Lee Mac
  25. ;; Returns the true (effective) name of a supplied block reference
  26.                        
  27. (defun LM:blockname ( obj )
  28.     (if (vlax-property-available-p obj 'effectivename)
  29.         (defun LM:blockname ( obj ) (vla-get-effectivename obj))
  30.         (defun LM:blockname ( obj ) (vla-get-name obj))
  31.     )
  32.     (LM:blockname obj)
  33. )
  34.  
  35. ;; String to List  -  Lee Mac
  36. ;; Separates a string using a given delimiter
  37. ;; str - [str] string to process
  38. ;; del - [str] delimiter by which to separate the string
  39.  
  40. (defun LM:str->lst ( str del / pos )
  41.     (if (setq pos (vl-string-search del str))
  42.         (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
  43.         (list str)
  44.     )
  45. )
  46.  

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Re: Parcing a filename into a block
« Reply #2 on: October 21, 2014, 01:04:58 PM »
Mr. Lee,

First,  Thank you for all the assitance you have provided for all my other projects.. I visit your site OFTEN, and am more than mildly impressed with your work.

That being said,

The hope of this, is to minimize us opening and closing of files when/if the dwg's name is changed by the engineer.  I.E.  Instead of opening 60 DWG's to perform the change, we just change the number in Windows Explorer.  As well as for use in our template drawing, so this can be automated on newly created DWG's.  This would be especially handy when copying drawings from older project to newer projects and renaiming.  So, I would definitly prefere some form of Field Expression, and or DIESEL code. With the use of the LSP it will still require opening the drawing and performing a function, and the lisp would have to be installed on ever CAD technicians machine, both here and in KC.  This could be problematic to coordinate/educate CAD technicians.  The Field Expresson and Diesel code would be much cleaner operationally across the board.  Although, from the results of my searching, seemingly much more difficult.

Do you think this is impossible with Field Expressions or DIESEL .. or even Rtext?

Please advise.
I am very  trainable....   (forgive my spelling)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Parcing a filename into a block
« Reply #3 on: October 21, 2014, 02:03:41 PM »
First,  Thank you for all the assitance you have provided for all my other projects.. I visit your site OFTEN, and am more than mildly impressed with your work.

Thank you DIW, I appreciate your gratitude & compliments  :-)

The hope of this, is to minimize us opening and closing of files when/if the dwg's name is changed by the engineer.  I.E.  Instead of opening 60 DWG's to perform the change, we just change the number in Windows Explorer.  As well as for use in our template drawing, so this can be automated on newly created DWG's.  This would be especially handy when copying drawings from older project to newer projects and renaiming.  So, I would definitly prefere some form of Field Expression, and or DIESEL code. With the use of the LSP it will still require opening the drawing and performing a function, and the lisp would have to be installed on ever CAD technicians machine, both here and in KC.  This could be problematic to coordinate/educate CAD technicians.  The Field Expresson and Diesel code would be much cleaner operationally across the board.  Although, from the results of my searching, seemingly much more difficult.

Do you think this is impossible with Field Expressions or DIESEL .. or even Rtext?

Regarding the possibility of using Fields (which includes DIESEL):

  • If the hyphens in the filename were commas -> yes.
or
  • If the sections of the filename were of fixed length -> yes.

Otherwise, I would say no, since DIESEL does not offer a means of reporting the position of a character in a string.

Regarding the LISP solution:

With the use of the LSP it will still require opening the drawing and performing a function

Firstly, note that a solution using fields would still require you to open each drawing in order to update the attribute fields (that is, the field will only be 'recalculated' when the drawing is next opened).

However, a LISP solution would operate in an identical manner - you would simply include the above code in an acaddoc.lsp saved to a network support path and the code would automatically run on drawing startup to update the necessary attribute values - there would be no user input required, only an initial setup.

Lee

ChrisCarlson

  • Guest
Re: Parcing a filename into a block
« Reply #4 on: October 21, 2014, 03:51:23 PM »
You could do a combination of field attributes with Lee's routine and set the field name under LispVariable. Either way the drawing needs to be opened to updated as mentioned above.

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Re: Parcing a filename into a block
« Reply #5 on: October 21, 2014, 03:58:05 PM »
Chris,
I'm not sure I follow you !00% .. could you elaborate?
I am very  trainable....   (forgive my spelling)

ChrisCarlson

  • Guest
Re: Parcing a filename into a block
« Reply #6 on: October 21, 2014, 03:58:30 PM »
I think diesel expression could also look like this (assuming the same format is kept)

Unit-    $(substr,$(getvar,DWGNAME),1,2)
Discipline-    $(substr,$(getvar,DWGNAME),4,4)
Type-  $(substr,$(getvar,DWGNAME),6,7)
system-  $(substr,$(getvar,DWGNAME),9,11)
DWG#- $(substr,$(getvar,DWGNAME),13,18)
Sheet- $(substr,$(getvar,DWGNAME),20,20)


DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Re: Parcing a filename into a block
« Reply #7 on: October 21, 2014, 03:59:18 PM »
Lee,

Thanks for the LISP.  I will still pursue this for a bit... I'm depressed by your responce.  Things do have limitations.
I am very  trainable....   (forgive my spelling)

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Re: Parcing a filename into a block
« Reply #8 on: October 21, 2014, 04:04:18 PM »
Chris,

Since each of these do not have a FIXED length.. I don't think your DIESEL will work.. This type of DIESEL is what I've come across in my search.. but sometimes the Unit will be 3 char.. most of the time 2 and sometimes 1.. and the sheet could vary wildly.. from 1-10 to 4000 to 123.A.. so if I'm understanding your code, the fixed locations of the DIESEL code won't work...
need something in () to tell the DIESEL where to start.. by somehow locating the "-".

in EXCEL there is a FIND("X",#)

But I don't know how to do that in DIESEL.
 
I am very  trainable....   (forgive my spelling)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Parcing a filename into a block
« Reply #9 on: October 21, 2014, 06:55:56 PM »
You could do a combination of field attributes with Lee's routine and set the field name under LispVariable. Either way the drawing needs to be opened to updated as mentioned above.

I would advise against this, since if the drawing is opened on a machine in which the global variable is not defined, the field will show blank. Furthermore, I see no benefit of using the program to set a variable for the field to reference, when the program could just set the attribute value directly - in both cases, the program would need to be re-evaluated should the drawing filename change.

Lee
« Last Edit: October 21, 2014, 06:59:50 PM by Lee Mac »

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Re: Parcing a filename into a block
« Reply #10 on: October 21, 2014, 06:57:36 PM »
Thank you for the insite.
I am very  trainable....   (forgive my spelling)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Parcing a filename into a block
« Reply #11 on: October 21, 2014, 07:06:24 PM »
With the sections not being of fixed length, the only DIESEL option is to test a range of characters for each section, e.g. for the first section, accounting for 1-3 characters would result in the following DIESEL code:

Code: [Select]
$(if,$(eq,-,$(substr,$(getvar,dwgname),2,1)),$(substr,$(getvar,dwgname),1,1),$(if,$(eq,-,$(substr,$(getvar,dwgname),3,1)),$(substr,$(getvar,dwgname),1,2),$(if,$(eq,-,$(substr,$(getvar,dwgname),4,1)),$(substr,$(getvar,dwgname),1,3))))

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Re: Parcing a filename into a block
« Reply #12 on: October 21, 2014, 07:09:47 PM »
Well that worked!  Now how do do the next one..   
( forgive me I don't read code well... but I'm trying )
I am very  trainable....   (forgive my spelling)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Parcing a filename into a block
« Reply #13 on: October 22, 2014, 05:43:05 AM »
Well that worked!  Now how do do the next one..   
( forgive me I don't read code well... but I'm trying )

Note that this is by no means a feasible solution - the above posted code only accounts for the first section being 1-3 characters in length, and the code will grow exponentially for every subsequent section since the positions of each hyphen character will be dependent on the length of the previous sections, which the code does not 'know'. You would likely hit the maximum nesting level for DIESEL expressions before you reach the last section.

To give you an idea of what I'm talking about: for the first section, the above code checks whether the second character in the filename is a hyphen - if so, it will return the first character - if not, it will check whether the third character is a hyphen - if so, it will return the first & second characters - if not, it will check whether the fourth character is a hyphen... However, for subsequent sections, the code would need to perform this process for each of the previous hyphens even before testing for the position of the required hyphen terminating the current section - and this still involves 'guessing' the maximum possible length of a section, else the code would be infinitely long.

The LISP solution is your best option.

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Re: Parcing a filename into a block
« Reply #14 on: October 22, 2014, 02:09:32 PM »
Lee,
I like the Lisp, but I can not adjust the acaddoc.lsp. I do not have rights.  I can use it on my machine. But again, what we're looking for here is a turnkey item we can plant it right into the template.  I have three thoughts...

One,
I know you were saying that it would grow exponentially.. from the first to the last...   but could it be done in a mannor that the first three  were worked from the left side.. and the last three worked from the right?  I'm guessing we would have to get the full count of characters first to know possition  position1=(string lengnth - 1)  postion2(string length -2)etc....   

Two,
I know .. since we don't know the full possible lenth of the sheet.. I.e.. 1-10 or 4000  or 4000.A   We could just assume a max of 4 and if it's beyond that. it will just require manual edditing..  numbers larger than 4 char for the sheet will be rare.  and we can't cover every possible situation.  but... 4, 3, 2, and 1 for lenths are common.  If I can cover 95% of the possibilities.. than I still have something worth while.

unit .. up to 3 char
disc 1 char
type 2 char

syst 3 char
Number 5-7 char (I know this one's going to be a *****)
sheet 1-4 char

I don't know what the max lenth of DIESEL code is but I'm pretty sure it's pretty long.. I think beyond 255 char. 

Knowing the above information.. Is this HARD.. or Impossible?

We will be working with this client for the next 2-3 years.. 50 dwg's per project 4 projects per month x 36 months...   
Hard I can live with.. Impossible.. ouch..

I don't know how to get past the first -...

3rd Thought
could the name be converted to a string and the "-" be converted to "," would that make the DIESEL code easier?


P.S.   I'm stubern this way... I'm good at beating dead horses.
I am very  trainable....   (forgive my spelling)