Jump to content

Use file name to find scale in csv file to set as variable - lisp


KLipski

Recommended Posts

Hi all,

I have a lot of drawings for which I need to insert title blocks for.

Each drawing is at a different scale so I have created a csv file, the first column being the file name and the second column being the scale.

I wish to look for the current file name in the first column and then once I have that, return that file name's scale setting it as the variable 'scale'.

I have a command script that will call the variable 'scale' when inserting the block.

Any help would be much appreciated.

 

Link to comment
Share on other sites

If you use layouts  each dwg would be at real scale as the layout view would be scaled, the title block being in the layout is at 1:1 scale, so no factor required.

  • Like 2
Link to comment
Share on other sites

Unfortunately, I am not creating the drawings in AutoCAD, but I am converting them from .dgn to .dwg and I'm doing this for hundreds of documents.

I'm also doing this in the model space as I have to unlock and delete layers in order to insert the title blocks, which I'm doing in the command script as well.

Edited by KLipski
Link to comment
Share on other sites

You can use my Read CSV function for this task - here is a quick example:

(defun c:test ( / csv lst scl )
    (cond
        (   (not (setq csv (getfiled "Select CSV File" "" "csv" 16)))
            (princ "\n*Cancel*")
        )
        (   (not (setq lst (mapcar '(lambda ( x ) (cons (strcase (car x)) (cadr x))) (LM:readcsv csv))))
            (princ "\nUnable to read CSV file or CSV file is empty.")
        )
        (   (not (setq scl (cdr (assoc (strcase (vl-filename-base (getvar 'dwgname))) lst))))
            (princ (strcat "\n" (vl-filename-base (getvar 'dwgname)) " not found in 1st column of " (vl-filename-base csv) ".csv."))
        )
        (   (princ (strcat "\nFound scale: " scl)))
    )
    (princ)
)

 

  • Like 1
Link to comment
Share on other sites

Thank Lee, I'm new to lisp and your information is always great.

I am having two issues though:

What would I have to change to automatically automatically set the csv file? I am trying to use your "Update Title Block" code to do this, but I'm unsure how.

Also, when I try to call scale in my command script, it's not working. I have inserted my whole command script down below.

-layer
unlock
"Title Block"

-laydel
n
Title Block

y
(load "C:\\Project\\ReadCSV-V1-3_KL.lsp")
(c:test)
-insert
"C:\Project\TitleBlock.dwg"
0,0
!scl
!scl
0
(load "C:\\Project\\UpdateTitleblockV1-9_KL.lsp")

Thank you for your help so far.

Link to comment
Share on other sites

You're welcome @KLipski :thumbsup:

 

Since you are calling the variable within an AutoCAD Script as opposed to performing all operations within the AutoLISP program, the variable will need to be global rather than local; therefore, it may be better to write the program as a function:

(defun getscalevalue ( csv / lst )
    (if (and (setq csv (findfile csv))
             (setq lst (mapcar '(lambda ( x ) (cons (strcase (car x)) (cadr x))) (LM:readcsv csv)))
        )
        (cdr (assoc (strcase (vl-filename-base (getvar 'dwgname))) lst))
    )
)

And then call the above with:

(setq scl (getscalevalue "C:\\YourCSVFile.csv"))

Though, since you are using a Script, you would not be able to test whether the value of scl is valid before using it.

Link to comment
Share on other sites

4 minutes ago, Lee Mac said:

Though, since you are using a Script, you would not be able to test whether the value of scl is valid before using it.

 

As such, it would likely be cleaner to perform the block insertion as part of the function, e.g.:

(defun inserttitleblock ( dwg csv / att lst )
    (if
        (and
            (setq dwg (findfile dwg))
            (setq csv (findfile csv))
            (setq lst (mapcar '(lambda ( x ) (cons (strcase (car x)) (cadr x))) (LM:readcsv csv)))
            (setq scl (cdr (assoc (strcase (vl-filename-base (getvar 'dwgname))) lst)))
        )
        (progn
            (setq att (getvar 'attreq))
            (setvar 'attreq 0)
            (vl-cmdf "_.-insert" dwg "_S" scl "_R" "0" "_non" "0,0,0")
            (setvar 'attreq att)
        )
    )
)

You may then call the above with the block filepath & CSV filepath:

(inserttitleblock "C:/Project/TitleBlock.dwg" "C:/YourCSVFile.csv")

 

  • Thanks 1
Link to comment
Share on other sites

@Lee Mac Thank you so much for that, I've added the function inserttitleblock to the end of the Read CSV.

I've been playing around with it for a bit but cannot seem to figure out the cause of an error I keep getting.

When I run the command (inserttitleblock "C:/Project/TitleBlock.dwg" "C:/YourCSVFile.csv"), I keep getting a return of nil.

I believe it might be the scale as when I do it by hand, inserting the title block drawing to the current drawing, that is when the nil occurs.

Link to comment
Share on other sites

4 hours ago, KLipski said:

When I run the command (inserttitleblock "C:/Project/TitleBlock.dwg" "C:/YourCSVFile.csv"), I keep getting a return of nil.

I believe it might be the scale as when I do it by hand, inserting the title block drawing to the current drawing, that is when the nil occurs.

I've attach an example of the csv file used.

I noticed that after running the code, the columns merge, and I believe that this may be causing the nil result as there is no longer a second column to look in.

Would this assumption be correct?

 

Edit: All variables are returned as nil so I think I am wrong in my assumption

 

ExampleCSV.csv

Edited by KLipski
Link to comment
Share on other sites

Ok, I think I have actually figured out the issue, I shouldn't have had .dwg in the file name in the csv.

I will be continuing to test, but I believe the issue has been resolved (fingers crossed).

Thank you @Lee Mac for all your help, I really appreciate it.

Link to comment
Share on other sites

11 hours ago, KLipski said:

Ok, I think I have actually figured out the issue, I shouldn't have had .dwg in the file name in the csv.

I will be continuing to test, but I believe the issue has been resolved (fingers crossed).

Thank you @Lee Mac for all your help, I really appreciate it.

 

You're welcome @KLipski - I'm glad you were able to resolve the issue in my absence.

 

The sample function I have posted will simply return nil if anything is not as it should be - you may want to have the function write a description to an error log when something isn't right (since you won't be able to observe any error messages at the command-line during the Script operation).

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...