Linking a FIELD to a Block Count

Linking a FIELD to a Block Count

seedstudiolandscapedesign
Enthusiast Enthusiast
14,743 Views
39 Replies
Message 1 of 40

Linking a FIELD to a Block Count

seedstudiolandscapedesign
Enthusiast
Enthusiast

Is it possible to display an autoupdating block count by using a FIELD in a TABLE?  

 

I've looked but I can't seem to find the information, any input is appreciated.

0 Likes
Accepted solutions (2)
14,744 Views
39 Replies
Replies (39)
Message 2 of 40

maxim_k
Consultant
Consultant
Hi Steve,

There is no direct way in plain AutoCAD and especially in AutoCAD for Mac. If you have full AutoCAD version (not LT) you can use workaround with small AitoLISP routine and DIESEL expression in the Field.

Are you using AutoCAD LT?
If so, than it seems to be impossible to accomplish this task, even with workaround I mentioned above.

Maxim

Do you find the posts helpful? "LIKE" these posts!
Have your question been answered successfully? Click 'ACCEPT SOLUTION' button.


Maxim Kanaev
Architect
MARSS

MacACAD | Linkedin

Etiquette and Ground Rules of Autodesk Community
0 Likes
Message 3 of 40

seedstudiolandscapedesign
Enthusiast
Enthusiast

Hi Maxim,

 

I'm on the full version.  Would the routine go in the Field Expression box area of the FIELD command popup?

 

My experience is very limited with coding.  What is the difference between DIESEL and AutoLISP and which would be better in this scenario?

0 Likes
Message 4 of 40

maxim_k
Consultant
Consultant
You cannot use AutoLISP expressions in fields, but you can use DIESEL expressions.
The idea is to assign a number of occurrences of the block to variable and show the value of this variable in the FIELD using DIESEL expression.

Look at this discussion:

https://www.theswamp.org/index.php?topic=15926.0

Here is another discussion:

http://www.arch-pub.com/DIESEL-Expression-to-return-occurances-of-blocks_10221422.html

Maxim

Do you find the posts helpful? "LIKE" these posts!
Have your question been answered successfully? Click 'ACCEPT SOLUTION' button.


Maxim Kanaev
Architect
MARSS

MacACAD | Linkedin

Etiquette and Ground Rules of Autodesk Community
Message 5 of 40

seedstudiolandscapedesign
Enthusiast
Enthusiast

Hi Maxim,

 

Thanks for directing me to these resources, they appear to do exactly what I need.  

 

Unfortunately I am a total newbie to AutoLISP and was unable to get Mark's routine over at theswamp.org you referenced here to work.  I'll repost my edited version and hopefully you wouldn't mind pointing out where I went wrong.

 

 

Retracing my steps:

 

 

1) Modified the code to count multiple blocks and saved as BLOCK_COUNT.lsp, here's my modified version (I renamed the test blocks 1 2 and 3:

 

(defun c:recount (/ block_list cnt env_name)

(setq block_list
(list
(cons '2 "1")
(cons '2 "2")
(cons '2 "3")
)
)


(foreach i block_list
(if
(setq ss (ssget "X" (list '(0 . "INSERT") i)))
(progn
(setq
cnt (itoa (sslength ss))
env_name (strcat "BLOCK_COUNT_" (cdr i))
)
(setenv env_name cnt)
(princ (strcat "\nSet " env_name " to " cnt))
)
)
)
(princ)
)

 

 

2) used the APPLOAD command to load the routine named BLOCK_COUNT.lsp

 

 

3) inserted three fields into my drawing using the DieselExpression type with the following expressions: 

 

$(getenv, BLOCK_COUNT_1)

$(getenv, BLOCK_COUNT_2)

$(getenv, BLOCK_COUNT_3)

 

 

4) ran the command UPDATEFIELD

 

 

... and nothing happened.  

 

Any help would be greatly appreciated 🙂

 

0 Likes
Message 6 of 40

maxim_k
Consultant
Consultant
Accepted solution

Hi Steve,

 

Everything works fine on my side. Verify that the blocks in your drawing have the names "1", "2" and "3", as the routine counts blocks only with these names.

 

I just added one line to the AutoLISP routine (REGEN command) in order to refresh fields values within the routine, so you don't need to use UPDATEFIELD command for each field after recounting blocks.

 

(defun c:recount (/ block_list cnt env_name)

(setq block_list
 (list
 (cons '2 "1")
 (cons '2 "2")
 (cons '2 "3")
 )
 )


 (foreach i block_list
 (if
 (setq ss (ssget "X" (list '(0 . "INSERT") i)))
 (progn
 (setq 
 cnt (itoa (sslength ss))
 env_name (strcat "BLOCK_COUNT_" (cdr i)) 
 )
 (setenv env_name cnt)
 (princ (strcat "\nSet " env_name " to " cnt))
 )
 )
 )
 (command "_REGEN") ;_refreshes field values
 (princ)
 )

Maxim


Do you find the posts helpful? "LIKE" these posts!
Have your question been answered successfully? Click 'ACCEPT SOLUTION' button.


Maxim Kanaev
Architect
MARSS

MacACAD | Linkedin

Etiquette and Ground Rules of Autodesk Community
Message 7 of 40

seedstudiolandscapedesign
Enthusiast
Enthusiast

Hi Maxim,

 

I've tried your updated routine and attempted to update the FIELDs again but I still cannot get a number output.  I only see "- - - -" in the FIELD, both when I insert it and after updating.

 

Are you using the routine in AutoCAD for Mac?

0 Likes
Message 8 of 40

maxim_k
Consultant
Consultant
Yes, it works in AutoCAD for Mac - I have tested it in 2016 version.
Can you attach here the drawing file (with blocks and inserted fields) where you tested this routine?

Maxim

Do you find the posts helpful? "LIKE" these posts!
Have your question been answered successfully? Click 'ACCEPT SOLUTION' button.


Maxim Kanaev
Architect
MARSS

MacACAD | Linkedin

Etiquette and Ground Rules of Autodesk Community
0 Likes
Message 9 of 40

seedstudiolandscapedesign
Enthusiast
Enthusiast

Sure, see attached.

0 Likes
Message 10 of 40

maxim_k
Consultant
Consultant

Try this:

 

at the command prompt type:

(setenv "BLOCK_COUNT_1" "10")

than hit Return.

 

Now type:

(getenv "BLOCK_COUNT_1")

and hit Return.

 

The output in the command window should be:

 

Command: (setenv "BLOCK_COUNT_1" "10")
"10"
Command: (getenv "BLOCK_COUNT_1")
"10"
Command:

What do you see on your side?

 

Maxim


Do you find the posts helpful? "LIKE" these posts!
Have your question been answered successfully? Click 'ACCEPT SOLUTION' button.


Maxim Kanaev
Architect
MARSS

MacACAD | Linkedin

Etiquette and Ground Rules of Autodesk Community
0 Likes
Message 11 of 40

seedstudiolandscapedesign
Enthusiast
Enthusiast

Yes, I see:

 

Command: (setenv "BLOCK_COUNT_1" "10")
"10"
Command: (getenv "BLOCK_COUNT_1")
"10"

0 Likes
Message 12 of 40

maxim_k
Consultant
Consultant

 

Than I cannot understand why fields on your side are not updated.

As I wrote before, on my side everything works in your drawing:

 

 

Double check DIESEL expressions in the fields:

 

2016-07-05_23-11-02.png

 

Maxim


Do you find the posts helpful? "LIKE" these posts!
Have your question been answered successfully? Click 'ACCEPT SOLUTION' button.


Maxim Kanaev
Architect
MARSS

MacACAD | Linkedin

Etiquette and Ground Rules of Autodesk Community
0 Likes
Message 13 of 40

seedstudiolandscapedesign
Enthusiast
Enthusiast

Thanks for the video, when I type RECOUNT the FIELDs do indeed update correctly.  

 

I was using the commands UPDATEFIELD and REGEN, why wouldn't these commands also update the count?  Should I update my FIELDEVAL setting (currently at 31)?

0 Likes
Message 14 of 40

maxim_k
Consultant
Consultant
RECOUNT calls AutoLISP routine which calculates block instances, keeps the number of the blocks in the AutoCAD preferences file (environment variables) and then updates the fields according to the calculations.
REGEN and UPDATEFIELD knows nothing about the blocks in the drawing, so the cannot change fields according to the number of blocks.
When you load AutoLISP routine, it doesn't run immediately, you need to call it with RECOUNT command each time you need to recalculate the number of blocks.

Maxim

Do you find the posts helpful? "LIKE" these posts!
Have your question been answered successfully? Click 'ACCEPT SOLUTION' button.


Maxim Kanaev
Architect
MARSS

MacACAD | Linkedin

Etiquette and Ground Rules of Autodesk Community
0 Likes
Message 15 of 40

seedstudiolandscapedesign
Enthusiast
Enthusiast

Thanks for clarifying, this little nugget of code will make my workflow quite a bit smoother.

 

Thank you Maxim for all your help 🙂

0 Likes
Message 16 of 40

seedstudiolandscapedesign
Enthusiast
Enthusiast

Hi Maxim,

 

One more question, is there any way to modify the routine to show "0" if no blocks of a particular name are found?  Currently if you erase all of say block 3 and RECOUNT, the FIELD will show the previous count total instead of updating to "0".

0 Likes
Message 17 of 40

maxim_k
Consultant
Consultant
Accepted solution

Hi Steve,

 

Try this:

 

(defun c:recount (/ block_list cnt env_name)
(setq block_list
(list
(cons '2 "1")
(cons '2 "2")
(cons '2 "3")
)
)
(foreach i block_list
(if
(setq ss (ssget "X" (list '(0 . "INSERT") i)))

(progn
(setq
cnt (itoa (sslength ss))
env_name (strcat "BLOCK_COUNT_" (cdr i))
)
(setenv env_name cnt)
(princ (strcat "\nSet " env_name " to " cnt))
)

(progn
(setq
cnt "0"
env_name (strcat "BLOCK_COUNT_" (cdr i))
)
(setenv env_name cnt)
(princ (strcat "\nSet " env_name " to " cnt))
)

)
)
(command "_REGEN") ;_refreshes field values
(princ)
)

 

Maxim


Do you find the posts helpful? "LIKE" these posts!
Have your question been answered successfully? Click 'ACCEPT SOLUTION' button.


Maxim Kanaev
Architect
MARSS

MacACAD | Linkedin

Etiquette and Ground Rules of Autodesk Community
Message 18 of 40

seedstudiolandscapedesign
Enthusiast
Enthusiast

Works like a charm.  I can't thank you enough Maxim, cheers 🙂

0 Likes
Message 19 of 40

Anonymous
Not applicable

Hello maxim_k!

 

I need to count the blocks through an attribute called LUM, how can I do it?

 

I have the blocks like this:

Name: 03.6572.40 (The manufacturer code, FLOS in this case)
Attribute "LUM": I rename the field with L1, L2, L3...

 

Thank you very much!

0 Likes
Message 20 of 40

Anonymous
Not applicable

Use data extraction method to update block count

after updating blocks update data table


@seedstudiolandscapedesign wrote:

Is it possible to display an autoupdating block count by using a FIELD in a TABLE?  

 

I've looked but I can't seem to find the information, any input is appreciated.


 

0 Likes