Additem Q

Additem Q

brian_adams
Collaborator Collaborator
1,651 Views
3 Replies
Message 1 of 4

Additem Q

brian_adams
Collaborator
Collaborator

 I know that I missed something.

but I can't understand what exactly....

I need to add only one Aecwall to selection set

I do selectAll and then filter it with  " if ... then" statment

 

I checked , the macro do finde the object I needed, but it can't add i to selection set

 first method i did

Set ssobjs(I) = ThisDrawing.ObjectIdToObject(ObjectID1)

second method i did

Set ssobjs(I) = ThisDrawing.ModelSpace.Item(I)

both don't works

what is wrong??  will be appreciate for help

________________________________________________

 

Sub test select()
Dim ent As AcadEntity
Dim ssetObj As AcadSelectionSet
Dim I As Integer
Dim G As Integer

For Each ent In ThisDrawing.ModelSpace

Set ssetObj = ThisDrawing.SelectionSets.Add("TEST_SELECTIONSET1715")
G = 0
ssetObj.Select acSelectionSetAll
G = ssetObj.Count
ObjectID1 = ent.ObjectID
'MsgBox ObjectID1
ReDim ssobjs(0 To G - 1) As AcadEntity
For I = 1 To G - 1
If ssetObj.Item(I).ObjectID = ObjectID1 And ssetObj.Item(I).ObjectName = "AecDbWall" Then
Set ssobjs(I) = ThisDrawing.ModelSpace.Item(I)
MsgBox "ok"

'MsgBox X
Else
'MsgBox ssetObj.Item(I).ObjectName & " " & ssetObj.Item(I).ObjectID
End If
Next I
'MsgBox ssobjs(0).ObjectID & " "
ssetObj.Clear
ssetObj.AddItems ssobjs

ThisDrawing.SendCommand "Select p " & vbCrLf
ThisDrawing.SendCommand "Materiallist" & vbCr & vbCr
MsgBox ssetObj.Count

ThisDrawing.SelectionSets("TEST_SELECTIONSET1715").Delete
Next
ssetObj.Clear
End Sub

 

0 Likes
Accepted solutions (1)
1,652 Views
3 Replies
Replies (3)
Message 2 of 4

brian_adams
Collaborator
Collaborator

that is problem part but why?

0 Likes
Message 3 of 4

norman.yuan
Mentor
Mentor
Accepted solution

Firstly, please post your code using the toolbar button "</>" so that the code would be displayed in read-able format.

 

Now, as for the error you get from code ssetObj.AddItems ssobjs, I think the problem is due to how you fill up the element in the AcadEntity array (ssobjs).

 

Here is the code snippet of inner loop where you fill up the AcadEntity array:

 

ReDim ssobjs(0 To G - 1) As AcadEntity
For I = 1 To G - 1
  If ssetObj.Item(I).ObjectID = ObjectID1 And ssetObj.Item(I).ObjectName = "AecDbWall" Then
    Set ssobjs(I) = ThisDrawing.ModelSpace.Item(I)
    MsgBox "ok"
  Else
    'MsgBox ssetObj.Item(I).ObjectName & " " & ssetObj.Item(I).ObjectID
  End If
Next I

 

As you can see, you declared the array ssobjs with given element count (the count of all select-able items in a drawing, assume,it is 100). Then in the "For... Next I" loop, you ONLY fill up the array partially because of the "If...Then..." statement. That is, you have an array with 100 element declared, but only fill up 1 element due to the "If...Then..." statement (the rest 99 element are left empty)! After the "For ... Next I" loop, your code tries to add this partially filled array to the selectionset, THUS, the error.

 

My understanding is that you need a selectionset with one item in it, so that you can pass the selection set to SendCommand().

 

The code can be modified/simplified this way:

 

Sub test select()

    Dim ent As AcadEntity
    Dim ssetObj As AcadSelectionSet
    Dim ssObjs() As AcadEntity
    Dim I As Integer
    Dim G As Integer

    For Each ent In ThisDrawing.ModelSpace
        '' You only need to to work for each "AecDbWall"
	If UCase(ent.ObjectName)="AECDBWALL" THEN

            ReDim ssObjs(0)
            Set ssobjs(0)=ent
            
            '' Create a selection set and select all, then clear it
            '' So that AutoCAD remembers "Previous" selection set
            Set ssetObj = ThisDrawing.SelectionSets.Add("TEST_SELECTIONSET1715") 
            ssetObj.Select acSelectionSetAll
            ssetobj.Clear

            ssetObj.AddItems ssobjs

'' These following SendCommand() calls would be problematic
'' If they work, it would be lucky :-( ThisDrawing.SendCommand "Select p " & vbCrLf ThisDrawing.SendCommand "Materiallist" & vbCr & vbCr ThisDrawing.SelectionSets("TEST_SELECTIONSET1715").Delete End If Next End Sub

HOWEVER, while this code I showed here would solve the issue with AcadSelectionSet.AddItems(), the entire process would likely not work because of calling SendCommand(0 in the "For..." loop: AutoCAD would/might execute the command called by SendCommand() asynchly, you may only get the last AecDbWall entity's information displayed, or no AecDbWall entity information displayed at all (because when the command "MaterialList" is sent to AutoCAD, the selection set ccreated by your code might have been deleted already.

 

I assume you are using AutoCAD Architecture. You should really use Archtecture's COM API to retrieve AecDbWall entity's information, instead of trying to get it from command line based on built-in command (well, I do not use AutoCAD Architecture, thus have no idea if there are COM APIs available that can be used in AutoCAD VBA. But I'd be very surprised if there is no COM API available, and if so, I'd not waste time in VBA at all).

 

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 4 of 4

brian_adams
Collaborator
Collaborator

sorry , next time will use  button "</>"

I am first  time on customasation vba  forum

You was right it was the only reason the array.

now it works , Thanks

VBA is the way i used to...so that why. 

0 Likes