VBA Basic --> Intermediate
Posted: Mon Sep 10, 2001 3:10 pm
Now we have covered the Basics of VBA we can start working on multiple drawing entity objects and to do this we use selection sets.
Selection sets are temporary collection of drawing entity objects that we can then manipulate.
In VBA we can select the entities using different selection modes which can be automatic or user interfaced.
There are two steps required to create a selection set:
Step 1
Add a selection set name to the drawing database.
Step 2
Populate the selection set with the drawing entity objects
Once we have the entity objects in the selection set we can loop through and change the properties of each entity.
Open a new drawing and draw some lines and circles in a new drawing then open the VBA IDE.
Insert a module in the new drawing and add the following code:
Start Code………………………………….
Sub SSExample()
' dimension the variables
Dim objSS As SelectionSet
Dim ColNum As Integer
Dim I As Integer
Dim objEnt As Entity
' add the selection set name to the drawing database
Set objSS = ThisDocument.SelectionSets.Add("SetName")
' populate the selection set with all the drawing entity objects
objSS.Select vicSelectionSetAll
' highlight all the entities in the selection set
objSS.Highlight True
ColNum = InputBox("Input a number from 1 to 256", "CMS Tutorial")
' trap any user input errors and default to 1
If ColNum > 256 Or ColNum < 1 Then ColNum = 1
' iterate through the selection set and change the color of each entity
For I = 1 To objSS.Count
Set objEnt = objSS(I)
objEnt.Color = ColNum
objEnt.Update
Next I
MsgBox "There are " & objSS.Count & " entities in the Selection Set", vbInformation, "CMS Tutorial"
End Sub
End Code ………………………………….
Try this out by placing the cursor in the subroutine then select F5.
All the drawing entity objects will highlight and an input box will ask for a color number. Input a number and select enter, all the entities will change color. Next a message box will advise how many entities are in the drawing selection set.
Well that was very easy!
What if we only wanted to change the lines and not the circles?
There are three ways we can do this:
First Way
Filter the selection set so only lines are included.
( best way )
Second Way
Get the user to select the lines and place them in the selection set.
( user may not get all lines )
Third Way
As we loop through the selection set entities, check the entity type and only change the line colors. ( slower in execution than First Way )
To filter entity objects in a selection set we use the DXF group codes as the selection criteria.
What are DXF group codes?
These are the Drawing File Interchange names and numbers that represent each drawing entity in text. All entity names are represented by a number 0 and a name, so a line is represented as (0 .“Line”)
To find out what any group codes for any entity type (entget(car(entsel))) at the command prompt – press enter - select an entity and press F2 to see the prompt history window.
You will see something like:
Select entity: ((-1 . <Entity name: d15e20> ) (0 . "LINE") (5 . "1CC") (67 . 0) (8 . "0") (62 . 56) (6 . "ByLayer") (48 . 1.00000) (60 . 0) (39 . 0.000000) (10 15873.7 10692.9 0.000000) (11 7030.93 10692.9 0.000000) (210 0.000000 0.000000 1.00000))
: '_PMTHIST
You can see the (0 . “LINE”) now type the (entget(car(entsel))) and select a circle and you will see (0 . “CIRCLE”) with other group codes.
To filter our selection set we need to add:
Dim DXFNumber(0) As Integer
Dim DXFName(0) As Variant
' set the filter options
DXFNumber(0) = 0
DXFName(0) = "LINE"
Then add additional options to the Select method as below:
' populate the selection set with all the drawing entity objects
objSS.Select vicSelectionSetAll, , , DXFNumber, DXFName
Here is the complete code
Start Code ………………………………….
Sub SSFExample()
' dimension the variables
Dim objSS As SelectionSet
Dim ColNum As Integer
Dim I As Integer
Dim objEnt As Entity
Dim DXFNumber(0) As Integer
Dim DXFName(0) As Variant
' set the filter options
DXFNumber(0) = 0
DXFName(0) = "LINE"
' add the selection set name to the drawing database
Set objSS = ThisDocument.SelectionSets.Add("SetName")
' populate the selection set with all the drawing entity objects
objSS.Select vicSelectionSetAll, , , DXFNumber, DXFName
' highlight all the entities in the selection set
objSS.Highlight True
ColNum = InputBox("Input a number from 1 to 256", "CMS Tutorial")
' trap any user input errors and default to 1
If ColNum > 256 Or ColNum < 1 Then ColNum = 1
' iterate through the selection set and change the color of each entity
For I = 1 To objSS.Count
Set objEnt = objSS(I)
objEnt.Color = ColNum
objEnt.Update
Next I
MsgBox "There are " & objSS.Count & " entities in the Selection Set", vbInformation, "CMS Tutorial"
End Sub
End Code ………………………………….
Our subroutine now only highlights the lines and alters their color.
Now alter DXFName(0) = "LINE" to DXFName(0) = "CIRCLE" and see only the circles change.
Using VBA we have control over all the drawing entity objects and can alter their properties such as layer and color also we can use filters to only select the entities we want to work on.
Have you ever received a CAD file that was produced by an inexperienced user and had to clean it up to use it?
Do you work on drawings that others produce and have to clean them out of excessive entities?
Next we will put our selection set knowledge to good use and I will show you some additional filtering to help cleanup drawings that other CAD people produced or even tidy up our own drawings.
------------------
Regards
John Finlay
Don't want to post a question - email me direct on john@acecad.com.au
Selection sets are temporary collection of drawing entity objects that we can then manipulate.
In VBA we can select the entities using different selection modes which can be automatic or user interfaced.
There are two steps required to create a selection set:
Step 1
Add a selection set name to the drawing database.
Step 2
Populate the selection set with the drawing entity objects
Once we have the entity objects in the selection set we can loop through and change the properties of each entity.
Open a new drawing and draw some lines and circles in a new drawing then open the VBA IDE.
Insert a module in the new drawing and add the following code:
Start Code………………………………….
Sub SSExample()
' dimension the variables
Dim objSS As SelectionSet
Dim ColNum As Integer
Dim I As Integer
Dim objEnt As Entity
' add the selection set name to the drawing database
Set objSS = ThisDocument.SelectionSets.Add("SetName")
' populate the selection set with all the drawing entity objects
objSS.Select vicSelectionSetAll
' highlight all the entities in the selection set
objSS.Highlight True
ColNum = InputBox("Input a number from 1 to 256", "CMS Tutorial")
' trap any user input errors and default to 1
If ColNum > 256 Or ColNum < 1 Then ColNum = 1
' iterate through the selection set and change the color of each entity
For I = 1 To objSS.Count
Set objEnt = objSS(I)
objEnt.Color = ColNum
objEnt.Update
Next I
MsgBox "There are " & objSS.Count & " entities in the Selection Set", vbInformation, "CMS Tutorial"
End Sub
End Code ………………………………….
Try this out by placing the cursor in the subroutine then select F5.
All the drawing entity objects will highlight and an input box will ask for a color number. Input a number and select enter, all the entities will change color. Next a message box will advise how many entities are in the drawing selection set.
Well that was very easy!
What if we only wanted to change the lines and not the circles?
There are three ways we can do this:
First Way
Filter the selection set so only lines are included.
( best way )
Second Way
Get the user to select the lines and place them in the selection set.
( user may not get all lines )
Third Way
As we loop through the selection set entities, check the entity type and only change the line colors. ( slower in execution than First Way )
To filter entity objects in a selection set we use the DXF group codes as the selection criteria.
What are DXF group codes?
These are the Drawing File Interchange names and numbers that represent each drawing entity in text. All entity names are represented by a number 0 and a name, so a line is represented as (0 .“Line”)
To find out what any group codes for any entity type (entget(car(entsel))) at the command prompt – press enter - select an entity and press F2 to see the prompt history window.
You will see something like:
Select entity: ((-1 . <Entity name: d15e20> ) (0 . "LINE") (5 . "1CC") (67 . 0) (8 . "0") (62 . 56) (6 . "ByLayer") (48 . 1.00000) (60 . 0) (39 . 0.000000) (10 15873.7 10692.9 0.000000) (11 7030.93 10692.9 0.000000) (210 0.000000 0.000000 1.00000))
: '_PMTHIST
You can see the (0 . “LINE”) now type the (entget(car(entsel))) and select a circle and you will see (0 . “CIRCLE”) with other group codes.
To filter our selection set we need to add:
Dim DXFNumber(0) As Integer
Dim DXFName(0) As Variant
' set the filter options
DXFNumber(0) = 0
DXFName(0) = "LINE"
Then add additional options to the Select method as below:
' populate the selection set with all the drawing entity objects
objSS.Select vicSelectionSetAll, , , DXFNumber, DXFName
Here is the complete code
Start Code ………………………………….
Sub SSFExample()
' dimension the variables
Dim objSS As SelectionSet
Dim ColNum As Integer
Dim I As Integer
Dim objEnt As Entity
Dim DXFNumber(0) As Integer
Dim DXFName(0) As Variant
' set the filter options
DXFNumber(0) = 0
DXFName(0) = "LINE"
' add the selection set name to the drawing database
Set objSS = ThisDocument.SelectionSets.Add("SetName")
' populate the selection set with all the drawing entity objects
objSS.Select vicSelectionSetAll, , , DXFNumber, DXFName
' highlight all the entities in the selection set
objSS.Highlight True
ColNum = InputBox("Input a number from 1 to 256", "CMS Tutorial")
' trap any user input errors and default to 1
If ColNum > 256 Or ColNum < 1 Then ColNum = 1
' iterate through the selection set and change the color of each entity
For I = 1 To objSS.Count
Set objEnt = objSS(I)
objEnt.Color = ColNum
objEnt.Update
Next I
MsgBox "There are " & objSS.Count & " entities in the Selection Set", vbInformation, "CMS Tutorial"
End Sub
End Code ………………………………….
Our subroutine now only highlights the lines and alters their color.
Now alter DXFName(0) = "LINE" to DXFName(0) = "CIRCLE" and see only the circles change.
Using VBA we have control over all the drawing entity objects and can alter their properties such as layer and color also we can use filters to only select the entities we want to work on.
Have you ever received a CAD file that was produced by an inexperienced user and had to clean it up to use it?
Do you work on drawings that others produce and have to clean them out of excessive entities?
Next we will put our selection set knowledge to good use and I will show you some additional filtering to help cleanup drawings that other CAD people produced or even tidy up our own drawings.
------------------
Regards
John Finlay
Don't want to post a question - email me direct on john@acecad.com.au