Jump to content

Get Value of text string from a Block


TMORRIS

Recommended Posts

I have a family of Scale Bar blocks named 1bar, 2bar, 4bar, 8bar, 12bar, 16bar, etc

 

each block has a piece of similar text in it

i.e., 12" = 1'-0" , 6" = 1'-0" , 3" = 1'-0" , 1 1/2" = 1'-0" , etc

 

I need a little piece of code to get the value of that text string so that I can use it elsewhere in my lisp routine

 

Taking the lazy way out here and asking for help

 

TAM

Link to comment
Share on other sites

Is the text a nested text/mtext object, or is it an attribute?

 

It would also help if you could post a sample block.

Link to comment
Share on other sites

I should have said I want to "setq" the whole piece of text

 

it is only a piece of text (not mtext or attribute)

Link to comment
Share on other sites

This can be done using AutoLISP, or Visual LISP (not sure which you're more proficient with?)

 

You're going to have to step through the block's nested objects, and when you find a match for the object you're looking for, extract the TextString Property.

 

Example:

 

 
;; <-- Step through
 (if (= "AcDbText" (vla-get-objectname [i][color=blue]<Object>[/color][/i]))
   (setq TextString (vla-get-textstring [i][color=#0000ff]<Object>[/color][/i])))

Link to comment
Share on other sites

Its far easier if the text is an attribute (takes less code), as you can interface with the Block Reference rather than needing iterate through the objects in the block definition.

 

I posted something similar recently - perhaps search this forum for ApplyToBlockObjects.

 

Otherwise, if you decide to go the attribute route, search my site for 'attribute functions'.

 

I hope this helps.

 

Lee

Link to comment
Share on other sites

Can you not use a wild card here? (instead of stepping through the text in the block)

 

I know that the text I am looking for always ends in =1'-0"

 

can I somehow use (*=1'-0") to get it?

 

like using (*bar) to get the block?

Link to comment
Share on other sites

I need a little piece of code to get the value of that text string so that I can use it elsewhere in my lisp routine

 

You're going to have to step through the block's nested objects, and when you find a match for the object you're looking for, extract the TextString Property.

 

Example:

 

 
[color=red];; <-- Step through
[/color][color=blue]  (if (= "AcDbText" (vla-get-objectname <Object>))
   (setq TextString (vla-get-textstring <Object>)))
[/color]

 

Is there another piece of code that comes before this?

 

a piece that gets the block?

 

Yes... that 'piece' can be found/written in your LISP routine :wink:.

 

If you'd like further assistance, you'll have to post your code (and a sample block?) :), as I left my telepathy hat at home today :(.

Link to comment
Share on other sites

Can you not use a wild card here? (instead of stepping through the text in the block)

 

I know that the text I am looking for always ends in =1'-0"

 

can I somehow use (*=1'-0") to get it?

 

like using (*bar) to get the block?

 

Wildcards can be used for slection set (ssget) filters, however, you're looking for text inside of a block definition (nested entity), and not a stand-alone text object.

 

Does this make sense?

 

I know there is a nested option for ssget (:N), but am not adept at using it for my tasks. Perhaps someone else may know better, and can correct me where I may be mistaken.

Link to comment
Share on other sites

I have attached the .dwg file of the block

 

I've also put in the code that I'm working on

although - I think it will just confuse matters

 

 

the code I need (I'm asking for / I'm working on) will:

 

1) get the scale bar block (= "*bar") or ((0 . "INSERT)(2 . "*BAR"))

 

2) get the string of the specific text entity in the block (that equal to "*=1'-0*)

or ((0 . "TEXT")(1 . "*=1'-0*")

 

3) shorten the string to how I want it to read

 

4) use the shortened text entity to update an attribute in the titleblock block

 

I can do 1, 3 and 4

I don't know how to make the relationship between 1 and 2

 

TAM

 

 

 

 
(progn
;;SET VARIABLES
(setvar "cmdecho" 0)
(setvar "attdia" 0)
(setvar "attreq" 0)
(setvar "filedia" 0)
(command "ucsicon" "off")
(command "style" "standard" "simplex" "" "" "22.5" "" "" "")
;
;
;
;
;
;DEFINED FUNCTION - CHOOSES STUFF FROM HYDRO PULLDOWN MENU PICKS AND SETS VARIABLES
(defun tbupdate ()
   (setq b1 (ssname sset1 0))
     (setq e1 (entnext b1) e2 (entnext e1) e3 (entnext e2)
           e4 (entnext e3) e5 (entnext e4) e6 (entnext e5)
           e7 (entnext e6) e8 (entnext e7) e9 (entnext e8)
           e10 (entnext e9) e11 (entnext e10) e12 (entnext e11)
           e13 (entnext e12) e14 (entnext e13) e15 (entnext e14)
           e16 (entnext e15) e17 (entnext e16) e18 (entnext e17)
           e19 (entnext e18) ep1 (entnext e19) ep2 (entnext ep1)
      )
;
;
   (setq fg (strlen (getvar "dwgname")))
   (setq fg2 (- fg 4))
   (setq ofile (substr (getvar "dwgname") 1 fg2))
;
;
   (setq nfile (entget e1))
      (setq nfile (subst (cons 1 ofile)(assoc 1 nfile)nfile))
      (entmod nfile)
   (setq nnam (entget e2))
      (setq nnam (subst (cons 1 nam)(assoc 1 nnam)nnam))
      (entmod nnam)
   (setq nmcode (entget ep2))
      (setq nmcode (subst (cons 1 mcode)(assoc 1 nmcode)nmcode))
      (entmod nmcode)
   (setq ndamloc (entget e7))
      (setq ndamloc (subst (cons 1 damloc)(assoc 1 ndamloc)ndamloc))
      (entmod ndamloc)
      (command "regen")
);defun
;
;
;;START VISUAL LISP
(vl-load-com)
(vla-startundomark (vla-Get-ActiveDocument (vlax-Get-Acad-Object)))
;
;
;;SET AUTOLISP SYMBOL ASSIGNMENTS
(setq asize "A")
(setq bsize "B")
(setq csize "C")
(setq dsize "D")
(setq esize "E")
(setq style "HYDRO-DEC-DIM")
(setq p1 "0,0")
;
;
;;FREEZE UNNEEDED LAYERS
(command "layer" "s" "scale" "f" "*" "")
;
;
;;FIND SOLIDWORKS SCALE-OBJECT
(setq ss1 (ssget "_X" '((8 . "SCALE")(0 . "MTEXT")(1 . "SCALE:*"))))
;
;
;;CHANGE SCALE-OBJECT TO REPLACE UNNEEDED DATA WITH NOTHING AND GET REMAINING DATA
(setq patt "SCALE: 1:")
(setq newpatt "")
(setq count -1)
 (while (< (setq count (1+ count))(sslength ss1))
   (setq ent1 (entget (ssname ss1 count))
         obj1 (vlax-ename->vla-object (cdr (car ent1)))
         str1 (vla-get-textstring obj1))
   (while (setq match? (vl-string-search patt str1))
   (setq str1 (vl-string-subst newpatt patt str1))
   (vla-put-textstring obj1 str1)
   );while
 );while
;
;
;;GET VALUE OF SCALE-OBJECT DATA
(setq scale str1)
;
;
;;DELETE SCALE-OBJECT
(vla-delete obj1)
;
;
;;FIND SOLIDWORKS TITLEBLOCK SIZE-OBJECT
(setq ss2 (ssget "_X" '((8 . "SCALE")(0 . "MTEXT"))))
(setq count2 -1)
(setq count2 (1+ count2))
(sslength ss2)
(setq ent2 (entget (ssname ss2 count2)))
(setq obj2 (vlax-ename->vla-object (cdr (car ent2))))
;
;
;;GET VALUE OF TB SIZE-OBJECT
(setq str2 (vla-get-textstring obj2))
;
;
;;DELETE TB SIZE-OBJECT
(vla-delete obj2)
;
;
;;MATCH VALUE OF TB SIZE-OBJECT TO CHOOSE AUTOCAD TITLEBLOCK SIZE AND SET POINT FOR SCALE-BAR INSERT
(if (eq asize str2)
(setq tb "HEDTBA" p5 (* (read str1) 3) p6 (* (read str1) 3.25)))
(if (eq bsize str2)
(setq tb "HEDTBB" p5 (* (read str1) 4.75) p6 (* (read str1) 0.5)))
(if (eq csize str2)
(setq tb "HEDTBC" p5 (* (read str1) 10.75) p6 (* (read str1) 0.5)))
(if (eq dsize str2)
(setq tb "HEDTBD" p5 (* (read str1) 21.75) p6 (* (read str1) 0.5) x1 (* (read str1) 25.5)))
(if (eq esize str2)
(setq tb "HEDTBE" p5 (* (read str1) 27.25) p6 (* (read str1) 0.5)))
;
;
;
;
;
;
;;INSERT, DELETE AND PURGE AUTOCAD TEMPLATE BLOCK TO GET DIMENSION STYLE
(command "insert" "template" "0,0" "" "" "")
(command "erase" "l" "")
(command "purge" "b" "template" "n")
;
;
;;THAW ALL LAYERS, SET LAYER 0 TO CURRENT AND ADD LAYER BASE
(command "layer" "t" "*" "s" "0" "n" "BASE" "")
;
;
;;CHANGE ALL SOLIDWORKS ENTITIES TO BASE LAYER
(command "change" "all" "" "p" "la" "BASE" "")
;
;
;;PURGE ALL UNUSED LAYERS
(command "purge" "la" "" "n")
;
;
;;SET "HYDRO-DIM-DEC" AS CURRENT DIMSTYLE, CHANGE DIMSCALE TO RETRIEVED SCALE VALUE & SAVE THE CHANGE TO "HYDRO-DIM-DEC" 
(setq activedoc(vlax-get-property(vlax-get-acad-object)'activedocument))
(vlax-put-property activedoc 'ActiveDimStyle (vla-item(vla-get-dimstyles activedoc)style))
(setvar "dimscale" (read scale))
(command "dimstyle" "s" "HYDRO-DEC-DIM" "y")
;
;
;;SCALE SOLIDWORKS ENTITIES PER RETRIEVED SCALE VALUE
(command "scale" "all" "" "0,0" scale)
;
;
;;CHANGE ALL SOLIDWORKS DIMESIONS TO HYDRO DIMSTYLE
(command "dimstyle" "a" "all" "")
(command "zoom" "a" "zoom" "e" "zoom" "a")
;
;
;;MOVE SOLIDWORKS ENTITIES TO BASE POINT
(setq p2 (getvar "extmin"))
(setq p3 (list (* 0.5 (read scale))(* 0.5 (read scale))))
(command "move" "all" "" p2 p3)
;
;
;;PURGE UNUSED DIMENSION STYLES
(command "purge" "d" "" "n")
;
;
;;PURGE UNUSED TEXT STYLES
(command "purge" "st" "" "n")
;
;
;;SET OTHER AUTOCAD VARIABLES PER RETRIEVED SCALE VALUE
(setvar "ltscale" (read scale))
(setvar "dimscale" (read scale))
(setvar "textsize" (* 0.125 (read scale)))
;
;
;;INSERT SCALEBAR PER RETRIEVED SCALE VALUE
(SETQ bar (strcat str1 "BAR"))
(command "insert" bar (list p5 p6) 1 1 0)
;
;
;;PURGE
(command "purge" "a" "" "n")
;
;
;;INSERT THE MADE IN SOLIDWORKS BLOCK
(setq Y1 0)
(command "insert" "madeinsw" (list x1 y1) scale scale 0)
;
;
;;INSERT TITLEBLOCK PER RETRIEVED SCALE VALUE
(command "insert" tb "0,0" (read scale) "" "")
;
;
;;GETS HYDRO TITLEBLOCK AND PREPARES FOR THE DEFINED FUNCTION TO RUN
(setq sset1 (ssget "x" (list (cons 2 "hedtb*"))))
;
;
;;SET DWG LIMITS
(command "zoom" "a")
(setq p4 (getvar "extmax"))
(command "limits" p1 p4)
(command "zoom" "a")
;
;
;;RUNS THE DEFINED FUNCTION
(tbupdate)
;
;
;SET BASE LAYER TO CURRENT
(command "layer" "s" "base" "")
;
;
);progn
(setvar "cmdecho" 1)
(setvar "attdia" 1)
(setvar "attreq" 1)
(setvar "filedia" 1)
(setvar "lunits" 2)
(command "ddatte" "l")

24bar.dwg

Link to comment
Share on other sites

2) get the string of the specific text entity in the block (that equal to "*=1'-0*)

or ((0 . "TEXT")(1 . "*=1'-0*")

 

You cannot use ssget to select subentities, you will need to step through the block definition, as stated earlier.

Link to comment
Share on other sites

Verbose example:

([color=BLUE]defun[/color] _GetBlockTextIf ( blockcollection blockcondition btextcondition [color=BLUE]/[/color] _getitemincollectionif blockdef btextobj )

 ([color=BLUE]defun[/color] _GetItemInCollectionIf ( collection condition )
   ([color=BLUE]if[/color]
     ([color=BLUE]vl-catch-all-error-p[/color]
       ([color=BLUE]vl-catch-all-apply[/color]
         ([color=BLUE]function[/color]
           ([color=BLUE]lambda[/color] [color=BLUE]nil[/color]
             ([color=BLUE]vlax-for[/color] item collection
               ([color=BLUE]if[/color] (condition item) ([color=BLUE]progn[/color] ([color=BLUE]setq[/color] result item) ([color=BLUE]exit[/color])))
             )
           )
         )
       )
     )
     result
   )
 )

 ([color=BLUE]setq[/color] blockcondition ([color=BLUE]eval[/color] blockcondition)
       btextcondition ([color=BLUE]eval[/color] btextcondition)
 )

 ([color=BLUE]if[/color]
   ([color=BLUE]and[/color]
     ([color=BLUE]setq[/color] blockdef (_GetItemInCollectionIf blockcollection blockcondition))
     ([color=BLUE]setq[/color] btextobj (_GetItemInCollectionIf blockdef btextcondition))
   )
   ([color=BLUE]vla-get-textstring[/color] btextobj)      
 )
)



([color=BLUE]defun[/color] c:test ( [color=BLUE]/[/color] acdoc acblk ) ([color=BLUE]vl-load-com[/color])

 ([color=BLUE]setq[/color] acdoc ([color=BLUE]vla-get-activedocument[/color] ([color=BLUE]vlax-get-acad-object[/color]))
       acblk ([color=BLUE]vla-get-blocks[/color] acdoc)
 )

 (_GetBlockTextIf acblk
   '([color=BLUE]lambda[/color] ( x ) ([color=BLUE]wcmatch[/color] ([color=BLUE]strcase[/color] ([color=BLUE]vla-get-name[/color] x)) [color=MAROON]"*-BAR"[/color]))
   '([color=BLUE]lambda[/color] ( x ) ([color=BLUE]and[/color] ([color=BLUE]wcmatch[/color] ([color=BLUE]vla-get-objectname[/color] x) [color=MAROON]"AcDb*Text"[/color]) ([color=BLUE]wcmatch[/color] ([color=BLUE]vla-get-textstring[/color] x) [color=MAROON]"*=1'-0*"[/color])))
 )
)

Link to comment
Share on other sites

Sounds like someone needs to make an attributed block.

 

... I had a sneaking suspicion that my Hero would show up. :lol::P

 

captain-obvious.jpg

Link to comment
Share on other sites

That's just sad - :cry:

I have 35,000 drawings for two Hydro Electric Dams

maybe 1/5 of those have one of these predefined blocks in them

if I change the block

I have to update all those drawings (with the proper scale bar)

 

For this exercise (updating a solidworks drawing to Our AutoCAD standards)

I'd rather just explode the scale block

erase all the pieces

and then reinsert it

 

or

shove some code in there that sets every scale possibility

to get the piece of text I want :lol:

Link to comment
Share on other sites

I have 35,000 drawings for two Hydro Electric Dams

maybe 1/5 of those have one of these predefined blocks in them

if I change the block

I have to update all those drawings (with the proper scale bar)

...

I'd rather just explode the scale block

erase all the pieces

and then reinsert it

 

or

shove some code in there that sets every scale possibility

to get the piece of text I want

 

I can't think of a reason why ObjectDBX would be unable to replace the current block (non-dynamic, non-attributed), with a new block (dynamic, attributed)... provided some thorough planning, and conditional filtering in your code, that is.

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...