Author Topic: Getting block name to string  (Read 2663 times)

0 Members and 1 Guest are viewing this topic.

Dommy2Hotty

  • Swamp Rat
  • Posts: 1127
Getting block name to string
« on: June 16, 2008, 11:32:24 AM »
Continuing from THIS thread.

I'm using the following code from a shortcut menu to get the selected entity's info.
Code: [Select]
^C^C(setq ent (entget (ssname (nth 1 (ssgetfirst)) 0)))

((-1 . <Entity
name: 7e7a68e0>) (0 . "INSERT") (330 . <Entity name: 7e7a66b8>) (5 . "1374")
(100 . "AcDbEntity") (67 . 1) (410 . "24x36") (8 . "0") (100 .
"AcDbBlockReference") (66 . 1) (2 . "Titleblock-Sheet_Info-24x36") (10 0.0 0.0
0.0) (41 . 1.0) (42 . 1.0) (43 . 1.0) (50 . 0.0) (70 . 0) (71 . 0) (44 . 0.0)
(45 . 0.0) (210 0.0 0.0 1.0))

Then, I set the block name to 'bname'
Code: [Select]
Command: (setq bname (assoc 2 ent))
(2 . "Titleblock-Sheet_Info-24x36")

Then, I want to strip the (2 . "  and the ") from the block name, BUT, I get the following error because I'm guessing 'bname' isn't a string:
Code: [Select]
Command: (setq bname (subst 5 (strlen bname) bname))
; error: bad argument type: stringp (2 . "Titleblock-Sheet_Info-24x36")

Any help?

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Getting block name to string
« Reply #1 on: June 16, 2008, 11:41:31 AM »
Try:

(setq bname (cdr (assoc 2 ent)))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Dommy2Hotty

  • Swamp Rat
  • Posts: 1127
Re: Getting block name to string
« Reply #2 on: June 16, 2008, 11:42:38 AM »
Try:

(setq bname (cdr (assoc 2 ent)))

I do believe that will do the trick!  Simple, effective, to the point...me like!

Thank you!!!

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Getting block name to string
« Reply #3 on: June 16, 2008, 11:43:17 AM »
You're welcome :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

deegeecees

  • Guest
Re: Getting block name to string
« Reply #4 on: June 16, 2008, 11:44:50 AM »
I think you need an assoc in there...

Beat to the punch again.  :-)

Dommy2Hotty

  • Swamp Rat
  • Posts: 1127
Re: Getting block name to string
« Reply #5 on: June 16, 2008, 11:51:07 AM »
I think you need an assoc in there...

Beat to the punch again.  :-)

I missed the cdr, BUT, I do need more assistance:

Can I strip the block name from the first character to the first - ?

Code: [Select]
(setq bname (cdr (assoc 2 ent)))
"Titleblock-Sheet_Info-24x36"

(setq btype (substr 1 "TO FIRST HYPHEN" bname)
"Titleblock"

deegeecees

  • Guest
Re: Getting block name to string
« Reply #6 on: June 16, 2008, 11:57:32 AM »
'wcmatch' will do that for you if you have constant titleblock names. Combine that with a 'cond' statement for multiples. Wait, I may be barking up the wrong tree. It's been a while so I may get beat again here...

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Getting block name to string
« Reply #7 on: June 16, 2008, 12:01:28 PM »
Something like this? (substr "Titleblock-Sheet_Info-24x36" 12)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Getting block name to string
« Reply #8 on: June 16, 2008, 12:01:56 PM »
wcmatch will tell you if a pattern is in a string, only T or nil.  If you want to find the position use 'vl-string-search'.  This will tell you the position of the string within the string, note that this starts at 0 where substr starts at one.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Dommy2Hotty

  • Swamp Rat
  • Posts: 1127
Re: Getting block name to string
« Reply #9 on: June 16, 2008, 12:02:31 PM »
Little background:

Block names here are in the following format:

Titleblock-Sheet_Info-24x36

Titleblock = Block Type / Sub-Folder in blocks directory on server
Sheet_Info = Block Description
24x36 = Size

Another example:
Site-Handicap_Ramp.dwg

Site = Block Type / Sub-Folder in blocks directory on server
Handicap_Ramp = Block Description

Dommy2Hotty

  • Swamp Rat
  • Posts: 1127
Re: Getting block name to string
« Reply #10 on: June 16, 2008, 12:06:02 PM »
I'm using all of this to be a dynamic block update:

Select block, right click, select "Update Block..." from shortcut menu.

Code will determine the block name, block type (used for setting the path), then insert the block from the server, cancel the insert, and update the attributes:

-insert
Titleblock-Sheet_Info-24x36=Z:/Main/Blocks/Titleblock/Titleblock-Sheet_Info-24x36.dwg
(command)
attsync
name
bname

Dommy2Hotty

  • Swamp Rat
  • Posts: 1127
Re: Getting block name to string
« Reply #11 on: June 16, 2008, 12:16:31 PM »
Got it!!!  Thank you so much everyone...final code to follow in a little bit!
Code: [Select]
Command: (setq bname (cdr (assoc 2 ent)))
"Titleblock-Sheet_Info-24x36"

Command: (vl-string-search "-" bname)
10

Command: (setq btypepos (vl-string-search "-" bname))
10

Command: (setq bname (substr bname 1 btypepos))
"Titleblock"

Dommy2Hotty

  • Swamp Rat
  • Posts: 1127
Re: Getting block name to string
« Reply #12 on: June 16, 2008, 12:27:40 PM »
Dirty code so far, but does exactly what I want it to!  Thanks again!!!

Code: [Select]
(setq ent (entget (ssname (nth 1 (ssgetfirst)) 0)))
(setq bpath "Z:/Main/Blocks/")
(setq blockname (cdr (assoc 2 ent)))
(setq btypepos (vl-string-search "-" blockname))
(setq bname (substr blockname 1 btypepos))
(setq bname (strcat blockname "=" bpath bname "/" blockname ".dwg"))
(command "-insert" bname)
(command)
(command "attsync" "name" blockname)

Dommy2Hotty

  • Swamp Rat
  • Posts: 1127
Re: Getting block name to string
« Reply #13 on: June 16, 2008, 12:50:48 PM »
Pretty much the final code:

Invoked from right click context menu:
Code: [Select]
^C^C(setq ent (entget (ssname (nth 1 (ssgetfirst)) 0)));(load "Z:/Main/AutoLISP/Block-Update_From_Server.lsp");Block-Update_From_Server;
LISP:
Code: [Select]
(defun C:Block-Update_From_Server (/ blockname btypepos bname)

  (setq bpath "Z:/Main/Blocks/"
blockname (cdr (assoc 2 ent))
btypepos (vl-string-search "-" blockname)
bname (substr blockname 1 btypepos)
bname (strcat blockname "=" bpath bname "/" blockname ".dwg")
);setq

  (command "-insert" bname); insert block from server
  (command); end insert before insertion
  (command "attsync" "name" blockname); update attributes

  );defun