+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Full Member
    Using
    AutoCAD 2012
    Join Date
    Apr 2015
    Posts
    37

    Default VBA dynamic blocks edit

    Registered forum members do not see this ad.

    Hi, iv got the below vba in progress, i copied it from somewhere online but cannot remember where to give credit

    Anyway iv got it running from a userform but it will only update the last block inserted. Eventually i want to use it to control 5+ dynamic block, also with visibility states but im at a loss after trying multiple things over the last few hours. Can someone point me in the right direction please?

    Code:
    Public Sub block_dyn()
    
    
    Dim objBlock As AcadBlockReference
    8
    Dim FilterType(0) As Integer
    Dim FilterData(0) As Variant
    Dim p1(0 To 2) As Double
    Dim p2(0 To 2) As Double
    Dim ssetObj As AcadSelectionSet
                    
    On Error Resume Next
    ThisDrawing.SelectionSets.Item("SS1").Delete
    Set ssetObj = ThisDrawing.SelectionSets.Add("SS1")
    
    FilterType(0) = 0
    FilterData(0) = ""
    ssetObj.Select acSelectionSetAll, p1, p2, FilterType, FilterData
    
    For Each Item In ssetObj
        If objBlock.EffectiveName = "Box" Then
            Set objBlock = ssetObj.Item(n)
            Call dyn_prop(objBlock, "Width", TextBox3.Value)
            Call dyn_prop(objBlock, "Height", TextBox4.Value)
            
        End If
            
        If objBlock.EffectiveName = "Rect" Then
            Call dyn_prop(objBlock, "Thickness", TextBox3.Value)
            Call dyn_prop(objBlock, "Depth", TextBox4.Value)
        End If
    Next
    
    End Sub
    
    Public Sub dyn_prop(objBlock As AcadBlockReference, name_of_property As String, value_of_property As Double)
    
    Dim dyn_properties() As AcadDynamicBlockReferenceProperty
    Dim var_atts As Variant
    
    var_atts = objBlock.GetDynamicBlockProperties
    
        For i = LBound(var_atts) To UBound(var_atts)
            If var_atts(i).PropertyName = name_of_property Then
                var_atts(i).Value = value_of_property
                ThisDrawing.SendCommand "_regen" & vbCr
            End If
        Next
    
    End Sub
    Thanks in advance
    ~D

  2. #2
    Luminous Being
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    9,725

    Default

    My $0.05 I just manage to do VBA stuff your selection set is looking at all of the dwg.

    Code:
    FilterDXFCode(0) = 0
    FilterDXFVal(0) = "INSERT"
    'FilterDXFCode(1) = 2
    'FilterDXFVal(1) = "SCHEDTEXT"
    Set SS = ThisDrawing.SelectionSets.Add("pit1sel")
    SS.Select acSelectionSetAll, , , FilterDXFCode, FilterDXFVal
     
     
    ' also if you pick one block then something like this
    attribs = SS.Item(Cntr).GetAttributes
     
         If attribs(0).TextString = pitname Then
           pt1 = ThisDrawing.Utility.GetPoint(, " pick first point")
           txtx1 = CStr(FormatNumber(pt1(0), 3))
           TXTY1 = CStr(FormatNumber(pt1(1), 3))
     
            attribs(1).TextString = txtx1
            attribs(2).TextString = TXTY1
     
            attribs(1).Update
            attribs(2).Update
    '        ThisDrawing.Application.Update
    A man who never made mistakes never made anything

  3. #3
    Full Member
    Using
    AutoCAD 2012
    Join Date
    Apr 2015
    Posts
    37

    Default

    so i got the vba changing multiple blocks in the drawing with the code below.

    my next part is to change the visibility state but i cannot find any info anywhere on doing this.

    My aim is to create a userform with tickboxes which will set the visibility state
    Any ideas?

    Code:
    Public Sub block_dyn()
    
    
    Dim objBlock As AcadBlockReference
    8
    Dim FilterType(0) As Integer
    Dim FilterData(0) As Variant
    Dim p1(0 To 2) As Double
    Dim p2(0 To 2) As Double
    Dim ssetObj As AcadSelectionSet
                    
    On Error Resume Next
    ThisDrawing.SelectionSets.Item("SS1").Delete
    Set ssetObj = ThisDrawing.SelectionSets.Add("SS1")
    
    FilterType(0) = 0
    FilterData(0) = "INSERT"
    ssetObj.Select acSelectionSetAll, p1, p2, FilterType, FilterData
    
    Dim dyn_properties() As AcadDynamicBlockReferenceProperty
    Dim var_atts As Variant
    Dim n As Integer
    n = 0
    'For Each Item In ssetObj
    While n < ssetObj.Count
        Set objBlock = ssetObj.Item(n)
        If objBlock.EffectiveName = "Box" Then
            'Set objBlock = ssetObj.Item(n)
            Call dyn_prop(objBlock, "Width", 100)
            Call dyn_prop(objBlock, "Height", 100)
            
        End If
            
        If objBlock.EffectiveName = "Rect" Then
            Call dyn_prop(objBlock, "Thickness", 150)
            Call dyn_prop(objBlock, "Height", 150)
        End If
        n = n + 1
    Wend
    'Next
        'ThisDrawing.SendCommand "_regen" & vbCr
    
    End Sub
    
    Public Sub dyn_prop(objBlock As AcadBlockReference, name_of_property As String, value_of_property As Double)
    
    Dim dyn_properties() As AcadDynamicBlockReferenceProperty
    Dim var_atts As Variant
    
    var_atts = objBlock.GetDynamicBlockProperties
    
        For i = LBound(var_atts) To UBound(var_atts)
            If var_atts(i).PropertyName = name_of_property Then
                var_atts(i).Value = value_of_property
                ThisDrawing.SendCommand "_regen" & vbCr
            End If
        Next
    
    End Sub

  4. #4
    Forum Newbie
    Computer Details
    rexxitall's Computer Details
    Operating System:
    Windoze 7
    Computer:
    Homebrewed
    Motherboard:
    Asus server mainboard
    CPU:
    Intel core i7
    RAM:
    64 GB
    Graphics:
    3. nvidea quaddro 4000
    Primary Storage:
    Solid state
    Secondary Storage:
    Seagate 4 TB
    Monitor:
    2x Siemens widescreen
    Discipline
    Civil
    rexxitall's Discipline Details
    Occupation
    construction engineer
    Discipline
    Civil
    Details
    Mainly i work on bridges, power plants, factorys, railway systems
    Using
    AutoCAD 2007
    Join Date
    Feb 2015
    Location
    Gerrmany / Finland
    Posts
    4

    Default visibility

    Code:
    those routines are known as working.
    to set a parameter just give its value as text string
    so e.g. the visibility state name
    
    instead of sentcommand try
    thisdrawing.renegen all
    
    best regards
    
    Function block_set_parameter(blockref As AcadBlockReference, ByVal NAME As String, VALUE As Variant, Optional ucaseName As Boolean = True) As Boolean
        block_set_parameter = False
        If blockref Is Nothing Then Exit Function
        Dim DynProp As AcadDynamicBlockReferenceProperty
        Dim PROPNAME As String
        Dim Variable As Variant
        Dim temp As String
        Dim BV As Variant
        If ucaseName Then NAME = UCase(NAME)
        Variable = blockref.GetDynamicBlockProperties
        For K = LBound(Variable) To UBound(Variable)
            Set DynProp = Variable(K)
            PROPNAME = DynProp.PropertyName
            If ucaseName Then PROPNAME = UCase(PROPNAME)
            'Check for variable and when found ask for input
            If PROPNAME = NAME Then
                If DynProp.ReadOnly = False Then
                    BV = DynProp.VALUE
                    On Error Resume Next
                    err.Clear
                    If DynProp.VALUE <> VALUE Then
                        DynProp.VALUE = VALUE
                    End If
                    If err.number = 0 Then block_set_parameter = True
                    On Error GoTo 0
                    err.Clear
                    Exit Function
                End If
            End If
        Next
        On Error GoTo 0
    End Function
    
    Function block_get_parameter(ByRef v As Variant, blockref As AcadBlockReference, ByVal NAME As String, Optional ucaseName As Boolean = True) As Boolean
    
        Dim DynProp As AcadDynamicBlockReferenceProperty
        Dim Variable As Variant
        Dim PROPNAME As String
        Dim temp As String
        If ucaseName Then NAME = UCase(NAME)
        block_get_parameter = False
        If blockref Is Nothing Then Exit Function
        If blockref.IsDynamicBlock Then
            Variable = blockref.GetDynamicBlockProperties
    
            For K = LBound(Variable) To UBound(Variable)
                Set DynProp = Variable(K)
                PROPNAME = DynProp.PropertyName
                If ucaseName Then PROPNAME = UCase(PROPNAME)
                If PROPNAME = NAME Then
                    block_get_parameter = True
                    v = DynProp.VALUE
                    Exit For
                End If
            Next
        End If
    End Function
    Last edited by rexxitall; 15th Apr 2015 at 10:13 pm.

  5. #5
    Super Moderator SLW210's Avatar
    Computer Details
    SLW210's Computer Details
    Operating System:
    Windows 7 PRO 64-bit
    Computer:
    IBM Lenovo
    Motherboard:
    ACPI x64
    CPU:
    Pentium(R) i5 4570 @ 3.2GHz
    RAM:
    8 GB RAM
    Graphics:
    Nvidia Quadro 600 1GB
    Primary Storage:
    300 GB
    Secondary Storage:
    650GB
    Monitor:
    2x ThinkVision 24"
    Discipline
    Multi-disciplinary
    SLW210's Discipline Details
    Occupation
    Design Draftsman
    Discipline
    Multi-disciplinary
    Details
    Mostly do drafting related to manufacturing. From doing site layouts with proposed updates, additions and renovations to be budgeted and submitted for bid, to updating and changing existing drawings to reflect maintenance and repair/revision work done on site.
    Using
    AutoCAD 2011
    Join Date
    May 2007
    Location
    South Florida, USA
    Posts
    11,725

    Default

    Please read the Code Posting Guidelines and edit your post to include the Code in Code Tags.
    “A narrow mind and a fat head invariably come on the same person” Zig Zigler



  6. #6
    Forum Newbie
    Computer Details
    rexxitall's Computer Details
    Operating System:
    Windoze 7
    Computer:
    Homebrewed
    Motherboard:
    Asus server mainboard
    CPU:
    Intel core i7
    RAM:
    64 GB
    Graphics:
    3. nvidea quaddro 4000
    Primary Storage:
    Solid state
    Secondary Storage:
    Seagate 4 TB
    Monitor:
    2x Siemens widescreen
    Discipline
    Civil
    rexxitall's Discipline Details
    Occupation
    construction engineer
    Discipline
    Civil
    Details
    Mainly i work on bridges, power plants, factorys, railway systems
    Using
    AutoCAD 2007
    Join Date
    Feb 2015
    Location
    Gerrmany / Finland
    Posts
    4

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by SLW210 View Post
    Please read the Code Posting Guidelines and edit your post to include the Code in Code Tags.
    Happy now ?

Similar Threads

  1. Dynamic Blocks, Attributes w/ Visibility States, and Attribute Edit
    By rayg11757 in forum AutoCAD 2D Drafting, Object Properties & Interface
    Replies: 5
    Last Post: 7th Dec 2015, 03:51 pm
  2. How to Edit Dynamic Blocks
    By Tahir Qayyum in forum AutoCAD Drawing Management & Output
    Replies: 8
    Last Post: 28th Oct 2014, 11:40 am
  3. Insert dynamic block from file with multiple dynamic blocks
    By BUrBaKy in forum AutoCAD Drawing Management & Output
    Replies: 17
    Last Post: 14th Nov 2012, 11:38 pm
  4. Edit Dynamic Block using VBA
    By TheBrain in forum AutoLISP, Visual LISP & DCL
    Replies: 0
    Last Post: 1st Oct 2009, 09:22 am
  5. Can I edit BlockPlaceholder in dynamic blocks?
    By Lindsay in forum AutoCAD Drawing Management & Output
    Replies: 0
    Last Post: 8th Jul 2008, 06:00 pm

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts