Jump to content

[VBA AUTOCAD]Create a loop with variable selectionset


lubracali

Recommended Posts

I start from a situation like this

line.jpg

 

I have to create a loop for each iteration allows me to select segments of a group(in the picture I have 5 groups, marked in red), so I have 5 "boxes/containers"(in this case, can be any number) that list each segment of the corresponding group.

 

I tried to use

Dim objSset(J) As AcadSelectionSet

Dim J as integer

 

or

Dim objSset(J) As Collection

Dim J as integer

 

but I always got the error "Constant expression required".

 

What can I use to run something like this?

Private Sub cmdGruppi_Click()

Dim j As Integer
Dim objSset(j) As AcadSelectionSet

answer = vbYes
j = 1

Do

Name = j

Set objSset(j) = ThisDrawing.SelectionSets.Add(Name)
MsgBox "Choose all the segments in a group", vbOKOnly, "Group"
Me.Hide
objSset(j).SelectOnScreen


answer = MsgBox("Another group?", vbYesNo, "New group")
If answer = vbYes Then
MsgBox "yes", vbOKOnly, "yes"
ElseIf answer = vbNo Then
MsgBox "no", vbOKOnly, "no"
End If

j = j + 1
Loop Until answer = vbNo


End Sub

Is there any container for objects that can be used?

Link to comment
Share on other sites

  • 1 month later...

I've just had a quick look at your post so I might have missed something, but what about adding each group of lines to an AutoCAD Group?

 

Dim oSSetGroup As AcadGroup

Set oSSetGroup = ThisDrawing.Groups.Add("SG") 'SG = group name as string
oSSetGroup.name  = "GroupName"

Dim AddItem(0) As AcadEntity
Set AddItem(0) = SSet(X1)                         'SSet(X1) are items from your selection set

oSSetGroup.AppendItems AddItem       'AddItem is the name of the AcadEntity in previous step

 

Then you can just call your group using the group name... e.g.

(command "copy" GroupName "")

Link to comment
Share on other sites

An error will be thrown unless either the array size is set explicitly:

Dim objSset(4) As AcadSelectionSet

 

Or set up as a Dynamic array:

Dim objSset() As AcadSelectionSet

 

If a dynamic array is used (and it looks like that is the direction you are headed) then it would have to be “ReDim”ed as needed.

See this link for the procedure.

 

http://msdn.microsoft.com/en-us/library/aa140074(v=office.10).aspx

Edited by SEANT
Link to comment
Share on other sites

SEANT - In the code discussed in the other thread (here) I ended up using "ReDim Array (variable) as XXXXX" and it worked fine. I didn't need to Dim it as a dynamic array to start with... Is this OK practice?

Link to comment
Share on other sites

Variables are the usual method for working with dynamic arrays. It is just that the variable has to be assigned an integer value prior to Dim”ming” or Redim”ing”.

 

Looking back on the OP, the first two code snippets posted didn’t assign a value type until after the array was declared. The routine in the code tags, though, did declare; Dim j As Integer.

 

That may actually work okay due to the fact that Integer value types are automatically initialized to 0. The massage given at the error("Constant expression required") leads me to believe that dimming the array without a constant (or empty braces for a dynamic array) was the cause.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...