Synchronizing parameters through nested blocks using VBA

Synchronizing parameters through nested blocks using VBA

InfoLEC
Explorer Explorer
209 Views
0 Replies
Message 1 of 1

Synchronizing parameters through nested blocks using VBA

InfoLEC
Explorer
Explorer
Greetings to all,
 
Here is simplified example of what I am trying to achieve so we don't get confused with all the details. I know this example doesn't need this sort complex macro, but keep in mind it will proove usefull to me.
 
I am trying to create a parametric drawing in which I define user parameters at the drawing level. 
-Length
-Width
-Diameter
 
My drawing contains a block "rectangle". "rectangle" contains the following entities:
-A rectangle
-The block "circle"
It also contains the following user parameters:
-Length
-Width
-Diameter
 
The block "circle" contains the following entity:
-A circle
It also contains the following user parameter:
-Diameter
 
It want to program a VBA macro that propagates the drawing level parameter values to all nested blocks so I don't have to manually change the values of the parameters in each block.
 
I can propagate a dimensional variable using the following code but am unable to propagate a user variable. Does someone know how to access a drawing level user variable's value using VBA? I can access the user variables contained in a block but not those contained at the drawing level.
 
 
Sub sync_block_param()
 
 ' Check if drawing is opened
    If ThisDrawing Is Nothing Then
        MsgBox "no drawing opened", vbExclamation
        Exit Sub
    End If
 
    Dim blkRef As Variant
    Dim param As AcadDynamicBlockReferenceProperty
    Dim blkParams As Variant
    Dim obj As Variant
    Dim i As Integer
    Dim a As AcadBlockReference
 
    'find all dimensional constraints in modelspace
    For Each obj In ThisDrawing.ModelSpace
        ' Is this object a dimensional constraint
        If Left(obj.ObjectName, 4) = "AcDb" And Right(obj.ObjectName, 9) = "Dimension" Then
            ' Store dimensional constraint
            Dim ConstrName As String
            Dim ConstrValue As Variant
            ConstrName = obj.DimConstrName
            ConstrValue = obj.DimConstrValue
 
            ' Loop through blocks of modelspace
            For Each blkRef In ThisDrawing.ModelSpace
                
                If blkRef.ObjectName = "AcDbBlockReference" Then
    'Récupère les paramètres utilisateur du bloc
                    blkParams = blkRef.GetDynamicBlockProperties
 
                    'Loop through user parameters of the block
                    For i = LBound(blkParams) To UBound(blkParams)
                        Set param = blkParams(i)
                        ' Copy value if parameters have the same name
                        If param.PropertyName = ConstrName Then
                            If IsNumeric(Replace(ConstrValue, ",", ".")) Then
                                param.value = CDbl(Replace(ConstrValue, ",", "."))
                            ElseIf IsNumeric(Replace(ConstrValue, ".", ",")) Then
                                param.value = CDbl(Replace(ConstrValue, ".", ","))
                            End If
                        End If
                    Next i
                End If
            Next blkRef
        End If
    Next obj
 
 
End Sub
 
Thanks in advance
John (unable to sing in since autodesk page constantly redirects to homepage)
0 Likes
210 Views
0 Replies
Replies (0)