Autocad VBA Selection Sets - Frfly

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

21.05.

2018 Autocad VBA Select on Sets | frfly

Us ng Move, Copy w th
AcadSelect onSet
Posted on February 10, 2018 by frfly

Select on Sets n Autocad VBA do not conta n methods to Move, Copy, Rotate, Scale, M rror
etc. The programmer has to make a For-Each loop to terate through the Select on Set and
apply the method to each nd v dual ent ty one at a t me. There are qu te a few steps between
mak ng the select on set, populat ng t, then loop ng through ts members and actually do ng
someth ng to them. Break ng down these steps nto small re-usable sub-procedures s the way
to go.

Other than how to use them, Select on Sets have pretty good documentat on not too hard to
understand. The f rst sub-procedure makes a new clean Select on Set n the draw ng. If t
already ex sts t deletes t. Its called w th a parameter name.

1 Sub addss(strname As String)


2 'adds a clean selection set
3 Dim sset As AcadSelectionSet
4 On Error Resume Next
5
6 Set sset = acadDoc.SelectionSets
7 sset.Delete
8 Set sset = acadDoc.SelectionSets
9
10 If sset Is Nothing Then
11 MsgBox "unable to add " & strnam
12 End If
13 End Sub

In a recent program I made two k nds of select on, ALL and W ndow, so I made two funct on
procedures to handle th s task and return an AcadSelect onSet object. These funct ons call
addss above. The way these work are documented and eas ly found w th VBA
ACADSelect onSets. Th nk of th s as the second layer of abstract on, a funct on to make and
return a set accord ng to your select on method.

1 Function sset_all() As AcadSelection


2 'returns a selection set ALL
3 addss ("All_Entities")
4 Set sset_all = acadDoc.Selection
5 sset_all.Select acSelectionSetA
6 End Function
7
8 Function sset_win(x1 As Double, y1 A
9 'returns a selection set with window
10 Dim pt1(0 To 2) As Double
11 Dim pt2(0 To 2) As Double
12
13 Call initpt(pt1, x1, y1, 0)
14 Call initpt(pt2, x2, y2, 0)
15
16 'items not visible do not select
17 acadApp.Update
18 acadApp.ZoomAll
https://frfly.wordpress.com/category/autocad-vba-select on-sets/ 1/4
21.05.2018 Autocad VBA Select on Sets | frfly
19
20 addss ("Win_Entities")
21 Set sset_win = acadDoc.Selection
22 sset_win.Select acSelectionSetWi
23 End Function
24
25 Sub initpt(ByRef ptn() As Double, va
26 ptn(0) = val1: ptn(1) = val2: ptn(2)
27 End Sub

n tpt s a l ttle helper I made. You don’t need the ByRef keyword, I put t n there to rem nd that
arrays always pass by reference. It would not work otherw se.

Now we have, when properly called, a select on set of our choos ng. We want to make sub-
procedures for MOVE, COPY, ROTATE, MIRROR and SCALE wh ch accept a select on set as
an argument and whatever other bas c parameters requ red, such as a d splacement for
MOVE. Th s s the th rd level of abstract on. We are call ng these methods w th a select on set
prev ously selected by whatever method.

Erase s the s mplest case. t doesnt requ re a loop.

1 Sub erase_ss(sset As AcadSelectionSet


2 sset.Erase 'erases the autocad ent
3 sset.Delete 'deletes the selectio
4 Set sset = Nothing
5 'call by reference will set t
6 End Sub

now f nally here s the code to MOVE a select on set us ng a loop to go thru the set for each
tem. It takes the set tself as argument and the d splacement n x and y. You can select tems
w th e ther of the subs above by w ndow or all or your program w th Cross ng or F lters. Delet ng
the select on set at the end of the rout ne s opt onal and may not always be des red, say f you
wanted to move and rotate.

1 Sub move_ss(sset As AcadSelectionSet


2 'x1 y1 is the displacement
3 Dim objent As AcadEntity
4 Dim pt0(0 To 2) As Double
5 Dim pt1(0 To 2) As Double
6
7 Call initpt(pt0, 0, 0, 0)
8 Call initpt(pt1, x1, y1, 0)
9
10 If 0 <> sset.Count Then
11 For Each objent In sset
12 objent.Move pt0, pt1
13 Next
14 End If
15
16 sset.Delete 'deletes the selecti
17 Set sset = Nothing
18 'call by reference will set
19 End Sub

At th s po nt these sub-procedures st ll do not change per program requ rements. They are
bas c tools.
https://frfly.wordpress.com/category/autocad-vba-select on-sets/ 2/4
21.05.2018 Autocad VBA Select on Sets | frfly
Here are m rror and scale (I have not needed COPY yet).

1 Sub mirror_ss(sset As AcadSelectionS


2 'deletes old copy, pt1 pt2 are axi
3 Dim objent As AcadEntity
4 Dim objent_mirrored As AcadEntit
5
6 If 0 <> sset.Count Then
7 For Each objent In sset
8 Set objent_mirrored = objent.Mir
9 objent.Delete
10 Next
11 End If
12 sset.Delete
13 Set sset = Nothing
14 End Sub
15
16 Sub scale_ss(sset As AcadSelectionS
17 'x1 y1 is the scale from point, sc i
18 Dim objent As AcadEntity
19 Dim pt0(0 To 2) As Double
20 Call initpt(pt0, x1, y1, 0)
21
22 If 0 <> sset.Count Then
23 For Each objent In sset
24 objent.ScaleEntity pt0, sc
25 Next
26 End If
27
28 acadApp.Update
29 acadApp.ZoomAll
30
31 sset.Delete 'deletes the selecti
32 Set sset = Nothing
33 'call by reference will set
34 End Sub

Now to some extent, we have h dden the loops, we don’t have to dupl cate them, and we can
call them w th s mple programs. Here are some upper level call ng programs. You w ll need to
wr te your own accord ng to the method and select on you want to use.

1 Sub erase_all()
2 Dim sset As AcadSelectionSet
3 Set sset = sset_all
4 Call erase_ss(sset)
5 End Sub
6
7 Sub move_all(x1 As Double, y1 As Dou
8 Dim sset As AcadSelectionSet
9 Set sset = sset_all
10 Call move_ss(sset, x1, y1)
11 End Sub
12
13 Sub scale_w(x1 As Double, y1 As Doub
14 'scales from pt 0,0
15 Dim sset As AcadSelectionSet
16 Set sset = sset_win(x1, y1, x2,
17 Call scale_ss(sset, 0, 0, sc)
18 End Sub
19
https://frfly.wordpress.com/category/autocad-vba-select on-sets/ 3/4
21.05.2018 Autocad VBA Select on Sets | frfly
20 Sub mirror_all(pt1() As Double, pt2(
21 Dim sset As AcadSelectionSet
22 Set sset = sset_all
23 Call mirror_ss(sset, pt1, pt2)
24 End Sub

those get called rather s mply by your top level, where you can see what you are do ng w thout
hav ng to d ve n to the deta ls.

1 Call erase_all
2 Call move_all (0,20)
3 Call scale_w(-1, -1, 12, 9.5, sc)
4 Call mirror_all(vt1, vt4)

when you draw w th xy data start ng at the or g n, th s s how you move and pos t on the p ece
onto your draw ng assembly or border.

https://frfly.wordpress.com/category/autocad-vba-select on-sets/ 4/4

You might also like