Code Red > .NET

Setting Dynamic Block Values with VB.NET

(1/7) > >>

cmwade77:
Please bear with me here, I am very new to .NET and I am having an issue with setting Dynamic Block Properties, this is the code I have:


--- Code - vb.net: ---    Public Sub SetProperty(BlockRef As BlockReference, PropName As String, NewValue As Object)        ' Use a transaction when modifying block properties        Using trans As Transaction = db.TransactionManager.StartTransaction()            Dim Prop As DynamicBlockReferenceProperty = GetProperty(BlockRef, PropName)            If Not IsNothing(Prop) Then                If Not Prop.Value.Equals(NewValue) Then ' Check if the values are different                    Try                        ' Use Convert.ChangeType to convert NewValue to the property's data type                                                Prop.Value = Convert.ChangeType(NewValue, Prop.Value.GetType())                        ' Commit the transaction after setting the property                        trans.Commit()                        ed.WriteMessage(vbCrLf & NewValue.ToString & "  Property: " & Prop.PropertyName & " set successfully to value of: " & Prop.Value.ToString)                    Catch ex As System.Exception                        ' Handle conversion error or unsupported data type                        ' You can log an error or provide a custom error message                        Console.WriteLine("Error: " & ex.Message)                    End Try                End If            End If        End Using    End Sub     Public Function GetProperty(BlockRef As BlockReference, PropName As String) As DynamicBlockReferenceProperty        If BlockRef.IsDynamicBlock Then            For Each Prop As DynamicBlockReferenceProperty In BlockRef.DynamicBlockReferencePropertyCollection                If Not Prop.ReadOnly Then                    If Prop.PropertyName.ToLower = PropName.ToLower Then                        Return Prop                    End If                End If            Next        End If        Return Nothing    End Function
When this code sets the property, sometimes things will stretch, but as you can see on the command line message, it still thinks the property value is the original 40 and if you look at the image, even the grip is still showing at the original 40, this would be for the property DUCT_1 in the attached block.

I am sure I am missing something obvious here, but I just don't know what.

gile:

--- Quote from: cmwade77 on January 25, 2024, 12:37:12 PM ---I am very new to .NET

--- End quote ---
So don't go any further with VB and switch to C#. You'll find a lot more help and examples in C#, and the difference between the two languages should become even greater with the forthcoming move to .NET 8.0.

kdub_nz:
I was thinking of a way to say the same thing, then your reply popped up.

cmwade77:
I already know enough VB to make this workable, I know nothing with C#

Jeff H:
Try calling BlockTableRecord.UpdateAnonymousBlocks Method
Guessing the AnonymousBlockTableRecord for that BlockReference has not updated.

One way to test really quick in UI is to open it Block Editor and save it then see if it updates.

If you only have access to the Blockreference then make sure you use the BlockReference.DynamicBlockTableRecord Property to get its blocktable record

Navigation

[0] Message Index

[#] Next page

Go to full version