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

new type of script/lisp for attribute block request

21 REPLIES 21
Reply
Message 1 of 22
RocksterB
1474 Views, 21 Replies

new type of script/lisp for attribute block request

Yes, we've seen many request for a script or lisp to change attribute data in a block. Hopefully, my request will be different.

 

I often get titleblock files from various client, with many attributes. I'd like is a routine that will allow me to click on an attribute block and it gathers ALL of the attribute names and current value into a file. I could then modify the values in the file and use it to script the values back into the files attribute block. Can I get a hand with this?

21 REPLIES 21
Message 2 of 22
pbejse
in reply to: RocksterB

I think this would be an easy one.

 

So gather all info from a titleblock on the current drawing, save it to a file, open the external file, modify items, run a code to  write the values on the titleblock on the sane drawing (current)?

or write the values from the edited external file to a different drawing using a script?

 

Tell me,

Not every titleblock has the same name?

If they are,  these titleblocks have the same number of attributes and tag names?

Are there more than one titleblock per drawing? (other companies still do that)

 

 

 

 

Message 3 of 22
RocksterB
in reply to: pbejse

The routine should be able to work with any named titleblock attribute block. The routine should be able to capture the name of the block and add/append it to the routines generated file. There is only one ttitleblock attribute file per drawing. It would also be nice if the resulting file was formatted so it could be run on the other DWG file via a batch program. Not necessary, but nice.

Message 4 of 22
pbejse
in reply to: RocksterB


@Anonymous wrote:

The routine should be able to work with any named titleblock attribute block. The routine should be able to capture the name of the block and add/append it to the routines generated file. There is only one ttitleblock attribute file per drawing. It would also be nice if the resulting file was formatted so it could be run on the other DWG file via a batch program. Not necessary, but nice.


Hmmmmn... ponder ..ponder.... Only way we can get away with this if there are similar tags or a pattern for comparison.

 

Say we have a drawing with a titleblock named "Arch-D"  and you said  capture the name of the block and add/append it to the routines generated file. ALL data on the titleblock and save.

 

Then open another drawing (script or something else) WRITE the data on this drawing which happens to have a titleblock with a different name? is this correct?

 

are there any similar attribute tags on these titleblocks? like i've said, we need a pattern/list for  comparison?

 

Tell you what , post a sample drawing here, that would make it easier for us to determine the proper approach.

 

I still think its easy as long as I understand the parameters perfectly.

 

 

 

 

 

Message 5 of 22
RocksterB
in reply to: pbejse

Sorry for the confusion, it was early in the morning.

 

Then open another drawing (script or something else) WRITE the data on this drawing which happens to have a titleblock with a different name? is this correct?

 

The attribute titleblock name would be the same for client's project "A" and project "B". So, its consistant for life of a particular client project, but of course different for another client.


Are there any similar attribute tags on these titleblocks? like i've said, we need a pattern/list for  comparison?

 

As I mentioned above, the pattern and attribute tags are consistant for each client's titleblock.

 

As requested, I've supplied a sample attribute block. This is a typical size one, not a big attribute block.

 

Thanks for you assistance with this.

 

 

Message 6 of 22
wkiernan
in reply to: RocksterB

Suppose either the source block or the attribute block has two or more attributes with the same name? Imagine the source drawing has a block called COMPANY_TITLEBLOCK with attributes PROJECT_NAME, PROJECT_NUMBER, DATE, FILE_NAME and DRAFTSMAN, and suppose the target drawing has a block called COMPANY_TITLEBLOCK with attributes PROJECT_NUMBER, FILE_NAME, PROJECT_NAME, DRAFTSMAN and CLIENT_NAME (note the order has changed).  It's pretty straightforward to extract the values of the attributes in an INSERT of that block COMPANY_TITLEBLOCK from the source drawing and write them to the corresponding attributes of all the INSERTs of the block called COMPANY_TITLEBLOCK in the target drawing.  But suppose the source block has multiple attributes all with the same name - say the source block has attributes PROJECT_NAME, PROJECT_NUMBER, DATE, REVISION, REVIUSION, REVISION and REVISION, and the target block is identical.  How do you want to deal with that?

 

 

Message 7 of 22
pbejse
in reply to: RocksterB

Reading your first post again made me understand what you want to achieve.

 

Correct me if i'm wrong, what you need is a program that reads the data on a titleblock (generic), write it to a file.

open the newly created file, modify it, then write it back to the same drawing after modification?  and not necessarily another drawing? and all this for mulitple drawings

 

3 drawing on a list, fire up the routine to read all three, grab data, save to file (1 file for all three)

Do your modification on the file. go back to cad. fire up a routine that will read the file and modify the drawings

 

BTW:Titleblocks on layout tab?

 

If it so, i'll start writing it for you

 

 

 

 

 

Message 8 of 22
pbejse
in reply to: pbejse

Here's a draft,

Single drawing

Prompt for flename

Prompt for single selection

 

command: Writetest

 

(defun c:WriteTest  (/ Objects fl BlockName)
  (vl-load-com)
  (cond
    ((and
       (setq Objects (car (entsel "\nSelect TitleBlock")))
       (setq Objects (vlax-ename->vla-object Objects))
       (setq fl (getfiled "Output File" "" "csv" 1))
       (eq (vla-get-objectname Objects) "AcDbBlockReference")
       (eq (vla-get-HasAttributes Objects) :vlax-true)
       (setq BlockName (vla-get-effectivename Objects))
       (setq fl (open fl "a"))
       (write-line BlockName fl)
       (mapcar
         (function
           (lambda (i)
             (write-line
               (strcat
                 (vla-get-tagstring i)
                 ","
                 (vla-get-textstring i))
               fl)))
         (vlax-invoke Objects 'GetAttributes)))
     (close fl)))
  (princ))

Open the saved CSV file and modify.......

 

Then

command: Edittest

Prompt forfilename

Prompt for single selection

 

(defun  c:EditTest  (/ fl BlockName AttVal AttribList ValToWrite)
  (vl-load-com)
  (if (and
        (setq fl (getfiled "Output File" "" "csv" 0))
        (setq fl (open fl "r")))
    (progn
      (setq BlockName (vl-string-right-trim "," (read-line fl)))
      (while (setq AttVal (read-line fl))
        (setq
          AttribList
           (cons
             (list
               (substr
                 AttVal
                 1
                 (vl-string-search "," AttVal))
               (vl-string-right-trim
                 ","
                 (substr
                   AttVal
                   (+ 2
                      (vl-string-search "," AttVal)))))
             AttribList)))
      (close fl)))
  (cond
    ((and
       (setq Objects (car (entsel "\nSelect TitleBlock")))
       (setq Objects (vlax-ename->vla-object Objects))
       (eq (vla-get-objectname Objects) "AcDbBlockReference")
       (eq (vla-get-HasAttributes Objects) :vlax-true)
       (equal BlockName (vla-get-effectivename Objects))
       (mapcar
         (function
           (lambda (j)
             (if (setq
                   ValToWrite
                    (assoc
                      (vla-get-tagstring j)
                      AttribList))
               (vla-put-textstring j (cadr ValToWrite)))))
         (vlax-invoke Objects 'GetAttributes)))))
  (princ))

 

Try this for now, after you give your comments, we'll write it in such a way that there'll be no need for filename and selection prompt, so you can run it thru script of ODBX

 

 

Message 9 of 22
RocksterB
in reply to: pbejse

I'll have to look at this when I'm in the office this afternoon.

 

Yes, the titleblock attribute block is on paperspace. This routine would all me to modifying number of sheet files with the client's titleblock attribute block on paperspace. I should have the ability to run the resulting file via a batch script on all sheet files in the project, thus modifying the attribute block with the same name.

 

Hmmm..., maybe the routine should account for text field attibutes, too.

 

Thus, this routine can be run on any named project's attribute file, no matter how many attributes are in the original attribute file.


Wkiernan,  my situation does not have multiple attributes with the same name. I can't imagine why a attribute would?

Message 10 of 22
RocksterB
in reply to: RocksterB

I just ran the routine and was very pleased with the results. It did just what was initially requested.

 

Now, for a little fine tuning.

  1. As you mentioned, remove the request for selecting the block. This is unnecessary, since the routine will update the same attribute block name used to gather the attribute block data.
  2. Make for provisions for text "Field" attributes.
  3. Have the routine apply the same name of the attribute block to the generated .CSV file. This will keep the names tied together.
  4. Make provisions for the routine to be run using a batch routine program.

Great start! Thank you in advance!

 

Message 11 of 22
RocksterB
in reply to: RocksterB

Line #1 should have read, "As you mentioned, remove the request for selecting the block to edit when applying the edits to the attribute block or series of sheet files." Not necessary sine the block being editted is the same name.

Message 12 of 22
pbejse
in reply to: RocksterB

Been away for 3 days, alright I'll take a look and see what we can come up

 

Cheers

 

Message 13 of 22
pbejse
in reply to: RocksterB


@Anonymous wrote:

Make for provisions for text "Field" attributes. 


For this item

 

What is the reference for the field value?  I guess it would depend on where you reference the value for that field.

How do you proposed we do this?

 

 

 

 

 

 

 

Message 14 of 22
RocksterB
in reply to: RocksterB

I've thought about your question and decided it's not appropriate to have Fields as part of this routine. Fields are placeholder for text to be entered from other methods, SheetSet Manager for example. So, ignore I mentioned this. But, we do add Field attributes to the titleblock file. The two that come to mind are the user's login name as Drawn_by and the sheet number as the actual file name minus the file's extension.

 

So with that, feel free to continue your development of the routine. 

Message 15 of 22
pbejse
in reply to: RocksterB

Randy,

 

I need your thoughts on this, On your first post you want this routine to run on a script, I would suggest you save data file on separate files on one folder

Data Filename as drawing filename but with CSV extension

Would you want to run the scirpt only on writing the values back to the drawing? or both ways?

 

If you can guarantee that ther's only one source Block Attribute and one layout tab. i think we can also eliminate selection of source attrib for writing the data.

 

I assume you have  experience with scripts, so i'll leave that writing of that to you.

ODBX pehaps?

 

 

 

 

Message 16 of 22
RocksterB
in reply to: pbejse

Yes, I want to run the routine using a script. For example, all of the discipline sheet files are within the discipline's folders, i.e.. ...\CAD\A\Sheet, ...\CAD\P\Sheet, etc. All of the sheet files contain the same named titleblock attribute block. So, your routine will be run via a batch program such as ScriptPro or AutoM8. I am not familiar with ODBX. I use a simple AutoCAD related batch process drawing program. Hopefully, when I use the program to call your routine there won't be any problem.

 

The routine should take on the name of the attribute block with the CDV extension, not the file name. The name of the sheet file where the original block resides in irrelevant. The name of the attribute block in pertinent.

 

I assume that once the routine gathers the data from the attribute file, any captured line can be mark to ignore applying changes, REM'd out or removed entirely? For example, initially, the routine would grab the items such as sheet number and name from the attribute block. Theses attributes are unique to each sheet and will be manually edited by the CAD users. Any problem with this?

Message 17 of 22
pbejse
in reply to: RocksterB


@Anonymous wrote:

 

The routine should take on the name of the attribute block with the CDV extension, not the file name. The name of the sheet file where the original block resides in irrelevant. The name of the attribute block in pertinent.

 


CDV? or CSV?

 

Alright, i'll post the code later today

 

cheers

 

 

Message 18 of 22
pbejse
in reply to: pbejse

Try this

 

Script for writing Data

_.open "D:\randi\folder1\sheet1.dwg" (WriteToCSV) _.close
_.open "D:\randi\folder1\sheet2.dwg" (WriteToCSV) _.close

 

 

Script for Extracting Data and editing drawing:

_.open "D:\randi\folder1\sheet1.dwg" (ReadEditFronCSV) _.save  _Y _.close
_.open "D:\randi\folder1\sheet2.dwg" (ReadEditFronCSV) _.save  _Y _.close

 

 

Note: both Lsp routines should be pre-loaded

 

See attached File

 

Message 19 of 22
RocksterB
in reply to: RocksterB

1. The first routine, WriteToCSV.lsp does not run via a batch script. This one is manual load and use. Only the second routine that applies the changes will run on multiple files via a batch scripting  program.

2. Instruct and allow the user to click the titleblock's attribute block on the sheet file. The CSV should include the name of the titleblock attribute block chosen by the user. Depending on the client's project, this name will vary.

3. Once the ReadToCSV completes it's task, the user needs to be informed the first step is complete and location of the CSV file. Print this at the command line, "Attribute data gathering complete! Edit the CSV file located in same folder as current file."

4. Something need to tell the ReadEditFronCSV to ignore overwriting certain attribute values. For ex., the sheet number and sheet name are unique to each sheet file and parts will not be handled by values coming from the CSV file. Could placing a code like "NA" in the adjacent third column cell restrict the routine from overwriting the sheet file's attribute value.

5. Could the first line in the CSV file say "Edit column "B" cells only!" If we don't tell them, they might edit the wrong cells and really messing things up. If this and line 3 above are possible, we tell "When adding NA in column 3 cells, the row edits will be ignored."

6. When saving the CSV file, I'm getting warnings about "Filename".CSV may not be compatible with CSV (Comma delimited). See below attachment. Maybe this is an EXCEL version thing?

Message 20 of 22
pbejse
in reply to: RocksterB

1. The first routine, WriteToCSV.lsp does not run via a batch script.This one is manual load and use.

 

Then you can revert to the first test code (WriteTest)

 

2. Instruct and allow the user to click the titleblock's attribute block on the sheet file. The CSV should include the name of the titleblock attribute block chosen by the user. Depending on the client's project, this name will vary.

 

I believe files created by WriteToCSV.lsp routine already includes the block name (first line)
or are you pertaining to include the blockname as part of the CSV filename?


3. Once the ReadToCSV completes it's task, the user needs to be informed the first step is complete and location of the CSV file. Print this at the command line, "Attribute data gathering complete! Edit the CSV file located in same folder as current file."

 

ReadToCSV? or WriteToCSV ?
Yes
Add this line to (WriteTest) code before the (princ)) at the end
(princ "Attribute data gathering complete! Edit the CSV file located in same folder as current file.")


4. Something need to tell the ReadEditFronCSV to ignore overwriting certain attribute values. For ex., the sheet number and sheet name are unique to each sheet file and parts will not be handled by values coming from the CSV file. Could placing a code like "NA" in the adjacent third column cell  restrict the routine from overwriting the sheet file's attribute value.

 

Yes it can be done. test the variable AttribList for length if value is 3 the skip this TAG

5. Could the first line in the CSV file say "Edit column "B" cells only!" If we don't tell them,
they might edit the wrong cells and really messing things up. If this and line 3 above are possible

 

Yes its possible


Do you have a handle on this now? or you need assistance?

Dont worry  about the message. its an excel thing. i would think you can adjust excel's setting to suppress the alert

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

Post to forums  

Autodesk Design & Make Report

”Boost