LISP Routine That Writes Filename to Attribute

LISP Routine That Writes Filename to Attribute

joelmNKB6B
Enthusiast Enthusiast
1,156 Views
14 Replies
Message 1 of 15

LISP Routine That Writes Filename to Attribute

joelmNKB6B
Enthusiast
Enthusiast

I have a block that is on every drawing, named “OFIMP_1_0”. I’m looking for a LISP routine that writes the name of the file, minus the extension, to the value for attribute with tag “ole_link” within that block once every time the drawing is opened.

 

I’ve searched high and low but can’t find a solution to this problem. Some solutions came close, but I was unable to adapt them to meet my specific situation - It seems like it would be pretty straight forward for someone more adept as LISP than me.

 

So essentially, each time I open the file, the value for attribute tag “ole_link” in the block “OFIMP_1_0” should be equal to the drawing name, minus the .dwg extension.

0 Likes
Accepted solutions (2)
1,157 Views
14 Replies
Replies (14)
Message 2 of 15

ronjonp
Mentor
Mentor

Use a field: %<\AcVar Filename \f "%fn2">%

Quick code to populate the blocks:

 

(defun c:foo (/ s)
  (if (setq s (ssget "_X" '((0 . "INSERT") (2 . "OFIMP_2_0"))))
    (foreach e (mapcar 'cadr (ssnamex s))
      (setpropertyvalue e "OLE_LINK" "%<\\AcVar Filename \\f \"%fn2\">%")
    )
  )
  (princ)
)

 

 

0 Likes
Message 3 of 15

joelmNKB6B
Enthusiast
Enthusiast

@ronjonp:

 

I had no idea I would get help so quickly, this is fantastic!

 

I tried the code and it didn't error out, but nothing happened to the attribute. I don't think that this is related to it not working, but I should have been a little more specific in what I needed - it actually needs to change the value to "C5.06!R1C1:R37C5" (assuming the dwg file is named "C5.06.dwg"). I also should add that I accounted for if it is case-sensitive and made sure everything matched exactly, and made sure the block names matched as well. Here is the edited code made to match the attached picture:

 

(defun c:foo (/ s)
(if (setq s (ssget "_X" '((0 . "INSERT") (2 . "OFIMP_5_0"))))
(foreach e (mapcar 'cadr (ssnamex s))
(setpropertyvalue e "ole_link" "%<\\AcVar Filename \\f \"%fn2\">%")
)
)
(princ)
)

 

Thanks again for the quick reply!

0 Likes
Message 4 of 15

ronjonp
Mentor
Mentor

@joelmNKB6B Post a sample drawing.

0 Likes
Message 5 of 15

joelmNKB6B
Enthusiast
Enthusiast

@ronjonp 

 

Here's a sample file, the Block is "OFIMP_2_0".

 

Thanks!

 

0 Likes
Message 6 of 15

ronjonp
Mentor
Mentor

That's the problem. In your first post the block name is "OFIMP_1_0" but in this drawing it's "OFIMP_2_0"

 

Change the filter at the top of the code to match the correct blockname: (2 . "OFIMP_2_0")

ronjonp_1-1652281663737.png

 

*Edit:


@joelmNKB6B wrote:

I should have been a little more specific in what I needed - it actually needs to change the value to "C5.06!R1C1:R37C5" 

 


Try this:

 

(defun c:foo (/ s)
  (if (setq s (ssget "_X" '((0 . "INSERT") (2 . "OFIMP_2_0"))))
    (foreach e (mapcar 'cadr (ssnamex s))
      (setpropertyvalue e "OLE_LINK" (strcat (vl-filename-base (getvar 'dwgname)) "!R1C1:R37C5"))
    )
  )
  (princ)
)

 

 

0 Likes
Message 7 of 15

joelmNKB6B
Enthusiast
Enthusiast

Sorry for the slapped-together file - when I originally tested the code, I accounted for the block name and was still unsuccessful. It doesn't seem to be doing anything when I look at the command history. I'm loading the LISP in the CUI/ACAD/LISP files/Filename_To_Attribute.LSP and just assuming it will run when the file is opened. Perhaps it is user error?

LISP.PNG

0 Likes
Message 8 of 15

ronjonp
Mentor
Mentor
Accepted solution

@joelmNKB6B 

Are you running the code ? 'FOO'

 

This is what I get from the latest code that was posted.

ronjonp_0-1652282745364.png

 

Also, if these blocks all vary in name by a number the filter can be updated like so to account for 0-99

(2 . "OFIMP_#_0,OFIMP_##_0")

 

0 Likes
Message 9 of 15

joelmNKB6B
Enthusiast
Enthusiast

Got it to work upon opening - I renamed the file to "acaddoc.lsp" and removed the first and last lines of code. Now it works perfectly. Thanks, couldn't have done it without you!

 

(if (setq s (ssget "_X" '((0 . "INSERT") (2 . "OFIMP_2_0"))))
(foreach e (mapcar 'cadr (ssnamex s))
(setpropertyvalue e "OLE_LINK" (strcat (vl-filename-base (getvar 'dwgname)) "!R1C1:R37C5"))
)
)
(princ)

0 Likes
Message 10 of 15

ronjonp
Mentor
Mentor

@joelmNKB6B wrote:

Got it to work upon opening - I renamed the file to "acaddoc.lsp" and removed the first and last lines of code. Now it works perfectly. Thanks, couldn't have done it without you!

 

(if (setq s (ssget "_X" '((0 . "INSERT") (2 . "OFIMP_2_0"))))
(foreach e (mapcar 'cadr (ssnamex s))
(setpropertyvalue e "OLE_LINK" (strcat (vl-filename-base (getvar 'dwgname)) "!R1C1:R37C5"))
)
)
(princ)


Glad you got it to work :). You can also automatically run the code by simply adding (c:foo) to the end.

ronjonp_0-1652284684410.png

 

0 Likes
Message 11 of 15

joelmNKB6B
Enthusiast
Enthusiast

@ronjonp 

 

It's mostly been working great, but sometimes the attribute value changes to XXXX upon opening the file, and also it's very odd but the background to the text is opaque. I have tried using multiple blocks, and changing the properties of the text but nothing works. When I edit the attribute manually, the background goes back to clear...any insight to this?

 

Opaque Attribute.PNG

0 Likes
Message 12 of 15

ronjonp
Mentor
Mentor
Accepted solution

@joelmNKB6B 

It looks like you're using the code HERE. That puts a FIELD in the attribute which is denoted by the grey background when FIELDDISPLAY is set to 1 ( it does not plot ). If you see #### try a regen to fix it assuming FIELDEVAL is set correctly.

0 Likes
Message 13 of 15

joelmNKB6B
Enthusiast
Enthusiast
Setting fieldsdisplay = 0 solved this problem. Since I don't use fields this was the easy way out.

Thanks!
0 Likes
Message 14 of 15

ronjonp
Mentor
Mentor

But if you use my code then you are using fields? They are nice because if you change the name of the drawing they will automatically update. 

0 Likes
Message 15 of 15

joelmNKB6B
Enthusiast
Enthusiast

@ronjonp 

 

Wow, fields are amazing! Very powerful and intuitive - I will be using these in the future!

0 Likes