Page 1 of 1

VBA Groups

Posted: Tue Mar 11, 2025 7:19 am
by richardss
I cannot get the group.appenditems to work. I have tried

Code: Select all

Sub testgroup()
    Dim icadApp As IntelliCAD.Application, icadDoc As IntelliCAD.Document
    Dim iCadLibrary As IntelliCAD.Library

    Set icadApp = GetObject(, "icad.application")
    Set icadDoc = icadApp.ActiveDocument
    Set iCadLibrary = icadApp.Library
 
    Dim myGroup As IntelliCAD.Group
    Set myGroup = icadDoc.Groups.Add("MyGroupName2")
    
'try one - creates an array of "entities" 
    ReDim ssobjs(0 To 8 ) As IntelliCAD.Entity
    For i = 990 To 998
        Set ssobjs(i - 990) = icadDoc.ModelSpace.Item(i)
    Next i
    myGroup.AppendItems ssobjs   ' FAILS HERE

'try two  - creates an array of variants
    ReDim ssobjsvar(0 To 8 ) As Variant
    For i = 990 To 998
        Set ssobjsvar(i - 990) = icadDoc.ModelSpace.Item(i)
    Next i
    myGroup.AppendItems ssobjsvar   'FAILS HERE
    
'try 3 - try single entity
    Dim myobj As IntelliCAD.Entity
    Set myobj = icadDoc.ModelSpace.Item(990)
    myGroup.AppendItems myobj    'FAILS HERE
    
'try 4 - try single item
    myGroup.AppendItems icadDoc.ModelSpace.Item(990)

'try 5 - draw and add line (have also tried by drawing 2 lines and adding each as csv, as an array etc
    dim line1 As Object
    Set line1 = icadDoc.ModelSpace.AddLine(ptLineStart, ptLine1End)   'points defined elsewhere - line is valid
    line1.Update
    myGroup.AppendItems line1   'FAILS HERE

end sub

I've run out of ideas - any one got any suggestions? thanks!!

Re: VBA Groups

Posted: Tue Mar 11, 2025 8:48 am
by QuanNguyen
Hi Richard,
Here's the simple code to add the entities in ModelSpace to a group.

Code: Select all

Sub Example_AppendItems()
    ' This example creates a group and appends the objects to the group.
    
    Dim groupColl As Groups
    Set groupColl = ActiveDocument.Groups
    
    ' Create a group named "TEST" in current drawing
    Dim testGroup As Group
    Set testGroup = groupColl.Add("TEST")
'    MsgBox "A new group called " & testGroup.Name & " has been added to the Groups collection.", vbInformation, "Groups Example"
    
    ' Iterate through the model space collection.
    ' Collect the objects found into an array of objects to be added to the group.
    ReDim appendObjs(0 To ActiveDocument.ModelSpace.Count - 1) As Entity
    Dim I As Integer
    For I = 0 To ActiveDocument.ModelSpace.Count - 1
        Set appendObjs(I) = ActiveDocument.ModelSpace.Item(I)
    Next
    
    ' Add the array of objects to the group
    testGroup.AppendItems appendObjs
End Sub

Re: VBA Groups

Posted: Tue Mar 11, 2025 11:06 pm
by richardss
Hi QuanNguyen, thanks so much for the fast and informative reply.

That code crashes IntelliCAD (completely - window disappears and Windows "send error report" shows up) at the testGroup.AppendItems appendObjs line

I have tested it on a second computer/install/license of IntelliCAD and seen the same.

Both versions tested are CMS IntelliCAD 11.1 64bit.

Many thanks, Richard

Re: VBA Groups

Posted: Wed Mar 12, 2025 8:01 am
by QuanNguyen
Hi Richard,
It works on CMS IntelliCAD 13.0.
Can you upgrade to?

Re: VBA Groups

Posted: Sun Jan 25, 2026 2:12 pm
by Spiros
richardss wrote:
Tue Mar 11, 2025 7:19 am
I cannot get the group.appenditems to work. I have tried

Code: Select all

Sub testgroup()
    Dim icadApp As IntelliCAD.Application, icadDoc As IntelliCAD.Document
    Dim iCadLibrary As IntelliCAD.Library

    Set icadApp = GetObject(, "icad.application")
    Set icadDoc = icadApp.ActiveDocument
    Set iCadLibrary = icadApp.Library
 
    Dim myGroup As IntelliCAD.Group
    Set myGroup = icadDoc.Groups.Add("MyGroupName2")
    
'try one - creates an array of "entities" 
    ReDim ssobjs(0 To 8 ) As IntelliCAD.Entity
    For i = 990 To 998
        Set ssobjs(i - 990) = icadDoc.ModelSpace.Item(i)
    Next i
    myGroup.AppendItems ssobjs   ' FAILS HERE

'try two  - creates an array of variants
    ReDim ssobjsvar(0 To 8 ) As Variant
    For i = 990 To 998
        Set ssobjsvar(i - 990) = icadDoc.ModelSpace.Item(i)
    Next i
    myGroup.AppendItems ssobjsvar   'FAILS HERE
    
'try 3 - try single entity
    Dim myobj As IntelliCAD.Entity
    Set myobj = icadDoc.ModelSpace.Item(990)
    myGroup.AppendItems myobj    'FAILS HERE
    
'try 4 - try single item
    myGroup.AppendItems icadDoc.ModelSpace.Item(990)

'try 5 - draw and add line (have also tried by drawing 2 lines and adding each as csv, as an array etc
    dim line1 As Object
    Set line1 = icadDoc.ModelSpace.AddLine(ptLineStart, ptLine1End)   'points defined elsewhere - line is valid
    line1.Update
    myGroup.AppendItems line1   'FAILS HERE

end sub

I've run out of ideas - any one got any suggestions? thanks!!
Hi richard
this is the only solution I could find

Code: Select all

Sub dummy(gr As IntelliCAD.group, icadDoc)
Dim OB(0) As IntelliCAD.Line
Dim apo As New IntelliCAD.point, se As New IntelliCAD.point

'  if there are no members the routine tries to add one
 '  An error occurs, it blocks with "on error resume  next"
  ' then error clearing and you can add anything"

If gr.count > 0 Then Exit Sub
se.X = 1
Set OB(0) = icadDoc.ModelSpace.Addline(apo, se)
OB(0).Update
On Error Resume Next
    gr.AppendItems OB
    If Err Then
        Err.Clear
        gr.AppendItems OB
    End If
    gr.Update
    gr.RemoveItems (OB)
    OB(0).delete
End Sub

Sub testgroup()
    Dim icadApp As IntelliCAD.Application, icadDoc As IntelliCAD.Document
    Dim iCadLibrary As IntelliCAD.Library
    Set icadApp = GetObject(, "icad.application")
    Set icadDoc = icadApp.ActiveDocument
    Set iCadLibrary = icadApp.Library
    Dim myGroup As IntelliCAD.group
    Set myGroup = icadDoc.Groups.Add("MyGroupName2")
    
    ' this is the only solution I could find"
    '  after the first error you can add items
    dummy myGroup, icadDoc


    ReDim ssobjs(0 To 8) As IntelliCAD.Entity
    For i = 990 To 998
        Set ssobjs(i - 990) = icadDoc.ModelSpace.Item(i)
    Next i
    myGroup.AppendItems ssobjs   ' no FAILS HERE

End Sub