Jump to content
katto01

VBA - information on selection

Recommended Posts

katto01

Hello,

 

 

Q1: How do I get the number of entities in a selection using VBA?

Q2: What is the command/method to find the type of each entity in a selection using VBA?

 

 

Thank you

Share this post


Link to post
Share on other sites
RICVBA

A1: use .Count method of SelectionSet object

A2: you could use typeOf() like:

If typeOf acEnt Is AcadLine then

...

and the likes where acEnt is some AcadEntity type variable you're looping through SelectionSet with

Share this post


Link to post
Share on other sites
Grrr

Heres my example-attempt:

 

Sub test() 'define a named function
 
 'Filter for circles - I don't know how to implement this type of filtering:
 'Dim gpCode(0) As Integer
 'Dim dataValue(0) As Variant
 'gpCode(0) = 0
 'dataValue(0) = "Circle"  
 
 Dim SS As AcadSelectionSet 'Declare "SS" variable as local
 Set SS = ThisDrawing.SelectionSets.Add("NewSS") 'create a new selection set object [include it in the "SelectionSets" collection]

 SS.SelectOnScreen 'select the objects to change (invoke the "SelectOnScreen" method)

 For Each Entity In SS 'iterate over the selection set
  
   If Entity.ObjectName = "AcDbCircle" Then 'Filter for circles by objectname
     Entity.color = 1 'change entity's colour
   End If
     
 Next 'process the next entity
 MsgBox SS.Count 'alert the total selected amount of objects
 SS.Delete 'delete the selection set object [exclude it from the "SelectionSets" collection]
End Sub

 

I'm just starting to learn about VBA, so any inputs are appreciated.

Share this post


Link to post
Share on other sites
BIGAL

Grr this may be usefull

 

Dim FilterDXFCode(0) As Integer
Dim FilterDXFVal(0) As Variant

FilterDXFCode(0) = 0
FilterDXFVal(0) = "INSERT"
'FilterDXFCode(1) = 2
'FilterDXFVal(1) = "SCHEDTEXT"

Set SS = ThisDrawing.SelectionSets.Add("pit1sel")
SS.Select acSelectionSetAll, , , FilterDXFCode, FilterDXFVal

for circle
FilterDXFVal(0) = "CIRCLE"

lisp (0 . "Insert")

Share this post


Link to post
Share on other sites
Grrr

Thanks BIGAL!

 

This second shot worked:

Sub test() 'define a named function
 
 Dim FilterDXFCode(0) As Integer
 Dim FilterDXFVal(0) As Variant

 FilterDXFCode(0) = 0
 FilterDXFVal(0) = "CIRCLE"

 Dim SS As AcadSelectionSet 'Declare "SS" variable as local
 Set SS = ThisDrawing.SelectionSets.Add("NewSS") 'create a new selection set object [include it in the "SelectionSets" collection]

 'SS.Select acSelectionSetWindow , , , FilterDXFCode, FilterDXFVal '<<< BIGAL's example 
 SS.SelectOnScreen FilterDXFCode, FilterDXFVal 'select the objects to change (invoke the "SelectOnScreen" method) and include filters
  
 For Each Entity In SS 'iterate over the selection set

     Entity.color = 1 'change entity's colour
     
 Next 'process the next entity
 MsgBox SS.Count 'alert the total selected amount of objects
 SS.Delete 'delete the selection set object [exclude it from the "SelectionSets" collection]
End Sub

 

It seems hard for me to get use with VBA, from lisp (since the lack of syntaxes).

First Impressions are that VBA and Visual Lisp (not Vanilla) are related, you have to, create a Selection Set Object, invoke a method and iterate thru it - instead of a single ssget function, that would be enough in vanilla lisp (altho still requires iteration).

 

With Visual Lisp it would wook like this:

(
 (lambda ( / SS )
   (vla-SelectOnScreen
     (setq SS (vla-Add (vla-get-SelectionSets (vla-get-ActiveDocument (vlax-get-acad-object))) "NewSS"))
     (vlax-safearray-fill (vlax-make-safearray vlax-vbinteger '(0 . 0)) '(0))
     (vlax-safearray-fill (vlax-make-safearray vlax-vbvariant '(0 . 0)) '("CIRCLE"))
   )
   (vlax-for o SS (vla-put-Color o 1))
   (vla-Delete SS)
 )
)

 

P.S. Sorry for late reply - I'm busy with other kind of work (computerless).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×