PDA

View Full Version : Dynamic Blocks a nest of trouble



artisteroi
2007-08-30, 07:41 PM
Hey new forum layout, kewl.

There are hundreds of posts pertaining to nested blocks. most saying "DON'T DO IT!" but there are a few that say "I DID IT!"

I wan't to be an 'I did it'. I am trying to assemble a vba to handle the dynamic properties of of a nested dynamic block. I have assembled a bit of code from the scraps I have found here form those who have triumphed (or said they did anyway).

I have not been able to get any of it to work properly. Most people have been saying things like iterate thru the table record and that sort of thing but I cant get it.

So I am here asking for help from those who are wiser than I.
Attached is a simple block with a visiblity state, nested inside it is another simple block with a visibility state.

Any help will be appreciated and if I can get the code to work properly after recieving help I will share the code over at dynamic block sharing forum with a link form here to there so anyone in the future that is trying to do this will be able to find actual working code-fully commented so they can modify it to there own needs. Because a lot of people have asked for this and I am willing to increase the knowlege base of our community.

Capt. Computer Crasher
2007-09-06, 09:20 PM
This is a portion of code from a program I wrote to search drawings for certain block attributes weather in a block or NESTED block and replace the attribute according to an Excel list. Actually using VBA to change nested blocks is very simple. just keep an eye on your if-then statements because some can get very nested (for every if must have an end if). This example only goes one deep in nesting, but you could set one up to go infinite deep until no more are found. Hope this helps. this was written in 2007.


If Not oBLK.IsXRef Then
'Check to make sure the block has attributes
If oBLKREF.HasAttributes Then
'If it has attributes then get attributes
varAttributes = oBLKREF.GetAttributes
For v = LBound(varAttributes) To UBound(varAttributes)
' Check for target tag string if found give new target value
If varAttributes(v).TagString = "ACCOUNT" Then
varAttributes(v).textString = NstrAccount
D = D + 1
End If 'ACCOUNT value end if
If varAttributes(v).TagString = "CLASS" Then
varAttributes(v).textString = NstrClass
D = D + 1
End If 'CLASS value end if
Next 'Next V

Else
'If it has NO ATTRIBUTES then check for nested blocks

For Each oENT In oBLK
If TypeOf oENT Is AcadBlockReference Then
Set oBLKREF1 = oENT
If oBLKREF1.HasAttributes Then
'If it has attributes then get attributes
varAttributes = oBLKREF1.GetAttributes
For v = LBound(varAttributes) To UBound(varAttributes)
' Check for target tag string if found give new target value
If varAttributes(v).TagString = "ACCOUNT" Then
varAttributes(v).textString = NstrAccount
D = D + 1
End If 'ACCOUNT value end if
If varAttributes(v).TagString = "CLASS" Then
varAttributes(v).textString = NstrClass
D = D + 1
End If 'CLASS value end if
Next 'Next V
End If 'Oblkref has no attributes end if
End If 'oENT is Acadreference
Next 'Next oENT
End If 'Oblkref has attributes end if

artisteroi
2007-09-07, 06:11 PM
This is a portion of code from a program I wrote to search drawings for certain block attributes weather in a block or NESTED block and replace the attribute according to an Excel list. Actually using VBA to change nested blocks is very simple. just keep an eye on your if-then statements because some can get very nested (for every if must have an end if). This example only goes one deep in nesting, but you could set one up to go infinite deep until no more are found. Hope this helps. this was written in 2007.



Hey Thanks for the code but I think I might be to thick to do this. I have been tring all day to get this to work but am still stumped. I added an attribute to make the block compliant with your code. It seems that what is happening is the code is finding the nested block but instead of iterating through the nested item it seems to jump back to the parent block. Which of course has no attributes in it so it just ends the sub at that point. This is very frustrating for me. Can you help any more, maybe show me the specific block of code that reads the nested items so I will be able to modify it for the block names in mine.

Capt. Computer Crasher
2007-09-07, 06:56 PM
Actually the second part of the code shown deals with the nested part.
Lets back-up and tell me exactly what 'you have to start with" and your "end goal" will be.

Are you looking for a nested block that you know the name of or are you looking for any nested block? what are you going to do with it when you find it? change attributes? change color? change layer? give me more you can

The code I gave you was looking for unknown named blocks with a specific attribute tag name.

artisteroi
2007-09-07, 07:15 PM
What I have is mostly just an experiment form that selects the block in the cad drawing, checks for a visibility state and then updates it. It then is supposed to check for a nested block in this new visibility, check for the nested block dynamic properties and modify them. Not having much luck. I know the name of all the blocks and dynamic props and have them hard coded into the code, but I keep running up against the generic block names that ACAD uses, or maybe I am just an idiot. The block attachment on the first post is the one I started with but I modified it so the nested block had different parameter types and included an attribute to try your code. Still couldn't get it.

I'll attach the new block and my code set



'set object block from the active layout of current drawing
Set oblock = ThisDrawing.ActiveLayout.Block '(ACTBLOCK_NAME)
'For iCount = 0 To oblock.Count - 1
'set the object entity
Set oEntity = oblock.Item(iCount)
'test the type of entity
If TypeOf oEntity Is AcadBlockReference Then
'set it for refernce editing
Set oBlockRef = oEntity
'select the block by name
If UCase(oBlockRef.Name) = UCase(ACTBLOCK_NAME) Then
'test for dynamic properties
If oBlockRef.IsDynamicBlock Then
'list dynamic properties
oProps = oBlockRef.GetDynamicBlockProperties
For I = 0 To UBound(oProps)
'set dynamic properties for editing
Set oDynamicblkProp = oProps(I)

'change the visiblity
If oDynamicblkProp.PropertyName = "Visibility" Then oDynamicblkProp.Value = "Fancy"
bCount = bCount + 1
'End If

'UPDATE_NEST

Next
End If
Set INBlock = ThisDrawing.ActiveLayout.Block '(NESTBLOCK_NAME)
'For inCount = 0 To INBlock.Count - 1
'set the object entity
Set NESTEDEntity = INBlock.Item(inCount)
'test the type of entity
If TypeOf NESTEDEntity Is AcadBlockReference Then
'set it for refernce editing
Set INBlockRef = NESTEDEntity
'select the block by name
If UCase(INBlockRef.Name) = UCase(NESTBLOCK_NAME) Then
'test for dynamic properties
If INBlockRef.IsDynamicBlock Then
'list dynamic properties
INProps = INBlockRef.GetDynamicBlockProperties
For L = 0 To UBound(INProps)
'set dynamic properties for editing
Set INDynamicblkProp = INProps(L)
'change the visiblity of nested block
If INDynamicblkProp.PropertyName = "Visibility" Then INDynamicblkProp.Value = "Circle"
bCount = bCount + 1
'End If
Next
End If
End If
End If
' Next inCount
End If
End If

Capt. Computer Crasher
2007-09-10, 09:34 PM
Take a look at the attached file. the macro should be embedded in it. run macro it should work . For some reason, the nested attributes had to be changed FIRST then the Visibility
changed. it run through all objects in drawing then deals only with the blocks.

artisteroi
2007-09-11, 04:00 PM
Take a look at the attached file. the macro should be embedded in it. run macro it should work . For some reason, the nested attributes had to be changed FIRST then the Visibility
changed. it run through all objects in drawing then deals only with the blocks.


Excelent! This works really well, I am going to fine tune it so there is a form for the attribute settings and other adjustments. And if I can I will set a filter so you can chose an individual item instead of every thing in model space. I'll work on it this weekend if I get a chance.

Thanks for all the help:beer: Cheers!

Capt. Computer Crasher
2007-09-11, 04:12 PM
Excelent! This works really well, I am going to fine tune it so there is a form for the attribute settings and other adjustments. And if I can I will set a filter so you can chose an individual item instead of every thing in model space. I'll work on it this weekend if I get a chance.

Thanks for all the help:beer: Cheers!

Change it the way you want I just wanted to get it working. Of course you need some input interfacing and change the selection. I just like selection sets. Let me know if you need more help. :beer:

artisteroi
2007-10-11, 12:09 PM
Have run into a bit of trouble with it. It would appear that cad sort of "chains" the blocks together. so if you have 3 blocks all with the same name, and then change the dynamic props of a nested item in only 1 of those blocks, the other 2 are updated as well. Even if you do it manually from the block editor. There is no way to filter one block from another. However, if you have 3 blocks that are identical in every way except the name, the chain is broken. So I am going to attempt to write a routine that will increment the name at insert. Sort of like "Paste as Block" only insteads of the generic acad identifier, it will use the original name and stringcat an incremental number to the end of it. Wish me luck :?