Author Topic: Looking for a script to add file name to specific block and attribute.  (Read 2609 times)

0 Members and 1 Guest are viewing this topic.

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Ladies and Gentlemen,
I'm looking for a LSP to add the text of the file name to a specific block and a specific attribute of that block.

If anyone has a simple LSP routine to do that.. I'd love to see it.

Any help is appreciated.
I am very  trainable....   (forgive my spelling)

ronjonp

  • Needs a day job
  • Posts: 7524
Re: Looking for a script to add file name to specific block and attribute.
« Reply #1 on: January 28, 2015, 04:21:59 PM »
If it's the name of the drawing you could use a field: %<\AcVar Filename \f "%fn2">%

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Re: Looking for a script to add file name to specific block and attribute.
« Reply #2 on: January 28, 2015, 04:30:39 PM »
No can do Ronjonp,

Municipality will not accept fields in attributed blocks.  So it has to be text in the attribute.

So the LSP has to read the file name.. and in essance.. type it into the attribute of the block named in the LSP.

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

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Looking for a script to add file name to specific block and attribute.
« Reply #3 on: January 28, 2015, 05:36:19 PM »
Do it as a field (wait - there's more!).  Before returning it, convert the field to simple text either manually or by code.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Re: Looking for a script to add file name to specific block and attribute.
« Reply #4 on: January 28, 2015, 07:17:38 PM »
I have three different title blocks I'm trying to populate.. I have LSP routines for 11x17 sheets that break up the file name based on the "-" in the name..
Example... 

Dave-Neu-Dwg.dwg
fills in block attributes
at1 = DAVE
at2 = NEU
at3 = DWG

I have a similar LSP for the 24x36 title blocks

but for drawings we get from the vendor that are TIFF's  we have to use there drawing number block

CLIENT DWG. NO.: <atrribute>

All the previous LSP enter text into the attributes.  I.E. being complient to our CLIENT's CAD standards.. I need the third to

I am recording a macro that will hopefully run all three.. so I can use the same command to update all the title blocks

I.E. Record-->
CLIENT1  (runs LSP for 11x17)
CLIENT2  (runs LSP for 24x36)
CLIENT3  (runs LSP for TIFFS)
end record... name recording CLIENT

I can open all the dwgs  and type.. CLIENT <ENTER> QSAVE <ENTER> CLOSE <ENTER>

(I'd make a SCR and run it in SCRIPTPRO)


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

ChrisCarlson

  • Guest
Re: Looking for a script to add file name to specific block and attribute.
« Reply #5 on: January 29, 2015, 07:22:10 AM »
What happens if the .TIFF filename changes? Is the title block required to update, or only fill in once?

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Re: Looking for a script to add file name to specific block and attribute.
« Reply #6 on: January 29, 2015, 09:38:57 AM »
I embed the tiff, using Raster tools.  So there is just the file name to contend with.
The title block needs to update just at the time of running the script. 

I've worked on ~2 dozen projects with this client, and once we set the name on existing dwgs (TIFFs) they don't tend to change.  However, we have to generate names in the clients database for the new DWGs.  We don't have a method to delete.. so we have a our own temporary naming system.  Once we get past the Constructability Review.  We enter the DWG names into their database, and finalize the names of the CAD files. 
« Last Edit: January 29, 2015, 09:42:07 AM by DIW_CADtech »
I am very  trainable....   (forgive my spelling)

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Re: Looking for a script to add file name to specific block and attribute.
« Reply #7 on: January 30, 2015, 02:27:07 AM »
Found one by Lee Mac

(defun c:UPDTNAM (/ ss eLst dNme aEnt aEntLst)
(vl-load-com)
(if (setq ss (ssget "X" (list (cons 0 "INSERT") (cons 2 "TITLEBLOCK") (cons 66 1)
(if (getvar "CTAB")
(cons 410 (getvar "CTAB"))
(cons 67 (- 1 (getvar "TILEMODE")))))))
(progn
(setq eLst (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
dNme (vl-filename-base (getvar "DWGNAME")))
(foreach e eLst
(setq aEnt (entnext e))
(while (not (eq "SEQEND" (cdadr (setq aEntLst (entget aEnt)))))
(if (= "DWGNAM" (cdr (assoc 2 aEntLst)))
(entmod (subst (cons 1 dNme)(assoc 1 aEntLst) aEntLst)))
(setq aEnt (entnext aEnt)))))
(princ "\n<!> No Title Blocks Found <!> "))
(command "_regenall")
(princ))

If you want to modify for your own purposes..

UPDTNAM calls the LSP from the command line
TITLEBLOCK is the name of the block
DWGNAM is the attribute

Thanks Again Lee Mac!!!
I am very  trainable....   (forgive my spelling)

Lee Mac

  • Seagull
  • Posts: 12899
  • London, England
Re: Looking for a script to add file name to specific block and attribute.
« Reply #8 on: January 30, 2015, 09:28:56 AM »
Wow - that's some old code!

Here's an updated version which conveniently defines the various parameters at the top of the code, and should also work with dynamic blocks:

Code: [Select]
(defun c:dwgatt ( / blk idx obj sel str tag )
    (setq blk "TITLEBLOCK" ;; Block name (case-insensitive)
          tag "DWGNAM"     ;; Attribute tag (case-insensitive)
          str (vl-filename-base (getvar 'dwgname)) ;; Attribute value
    )
    (if
        (setq sel
            (ssget "_X"
                (list '(0 . "INSERT") '(66 . 1) (cons 2 (strcat "`*U*," blk))
                    (if (= 1 (getvar 'cvport))
                        (cons 410 (getvar 'ctab))
                       '(410 . "Model")
                    )
                )
            )
        )
        (repeat (setq idx (sslength sel))
            (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx)))))
            (if (= (strcase blk) (strcase (vlax-get-property obj (if (vlax-property-available-p obj 'effectivename) 'effectivename 'name))))
                (foreach att (vlax-invoke obj 'getattributes)
                    (if (= (strcase tag) (strcase (vla-get-tagstring att)))
                        (vla-put-textstring att str)
                    )
                )
            )
        )
        (princ (strcat "\nNo \"" blk "\" blocks found in the current layout."))
    )
    (princ)
)
(vl-load-com) (princ)

(the above is untested)

Happy to help DIW! :-)

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Re: Looking for a script to add file name to specific block and attribute.
« Reply #9 on: January 30, 2015, 05:39:05 PM »
I'm getting a submittal out.. the old code works, but I'm more than happy to "Refresh" it. with the above...  I will test on Monday.
I am very  trainable....   (forgive my spelling)

DIW_CADtech

  • Bull Frog
  • Posts: 368
  • Push limits, embrace success, & discard failure.
Re: Looking for a script to add file name to specific block and attribute.
« Reply #10 on: February 02, 2015, 07:43:45 PM »
The above code works great.. only addition I made was to add a

(command "_regenall")
above the
(princ))

Works great.
Thanks Lee Mac

"Donate to Lee Mac"
google AutoCAD Lee Mac  you'll find the page..   He's a man on a mission.

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

Lee Mac

  • Seagull
  • Posts: 12899
  • London, England
Re: Looking for a script to add file name to specific block and attribute.
« Reply #11 on: February 03, 2015, 04:04:12 AM »
Many thanks indeed DIW, I'm glad the code works well.  :-)