Message 1 of 18
Select all hatches on given layer and move them to new layer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am trying to select all entities for each layer that has "4-Hatch" string in it's name and move them to a new layer called "4-Hatch". One layer at a time. Problem is that SelectionSet does not get any Hatch entity. Also, to move hatch to a new layer, is 'oObject.Layer = "4-Hatch"' enough or not?
Sub AddLayer(sLayerName As String, oLayerColor As AcColor, sLayerLineType As String)
Dim oNewLayer As AcadLayer
'On Error Resume Next
Set oNewLayer = ThisDrawing.Layers.Add(sLayerName)
oNewLayer.color = oLayerColor
oNewLayer.Linetype = sLayerLineType
End Sub
Public Function SelectAllByLayer(sLayer As String, sSetName As String) As AcadSelectionSet
Dim oSelSet As AcadSelectionSet
Dim intCode(0 To 1) As Integer
Dim varData(0 To 1) As Variant
intCode(0) = 8
varData(0) = sLayer
'intCode(1) = 0
'varData(1) = "INSERT"
On Error Resume Next
Err.Clear
Set oSelSet = ThisDrawing.SelectionSets.Item(sSetName)
If Err.Number > 0 Then
oSelSet.Clear
Err.Clear
Else
Set oSelSet = ThisDrawing.SelectionSets.Add(sSetName)
End If
oSelSet.Select acSelectionSetAll, , , intCode, varData
Set SelectAllByLayer = oSelSet
End Function
Public Function GetSelectionAsArray(oSS As AcadSelectionSet)
Dim lEnt As Long
ReDim oObjects(0 To oSS.count - 1) As AcadEntity
For lEnt = 0 To oSS.count - 1
Set oObjects(lEnt) = oSS.Item(lEnt)
Next lEnt
GetSelectionAsArray = oObjects
End Function
Public Sub MoveAllToLayer(sLayerName As String, sActDrw As String)
'LINETYPES: ByLayer, ByBlock, Center, CONTINUOUS, DASHED, DOT, HIDDEN, PHANTOM
'COLORS: acBlue, acByBlock, acByLayer, acCyan, acGreen, acMagenta, acRed, acWhite, acYellow
'sActDrw: PRT, ASM
Dim oObject As Variant
Dim oColor As AcColor
Dim sLineType As String
Dim oLayer As AcadLayer
Dim oSelSet As AcadSelectionSet
'Dim oSelArray() As AcadObject 'Is this properly declared variable?
Select Case sLayerName
Case "4-Hatch"
Debug.Print "4-Hatch"
oColor = 8
sLineType = "CONTINUOUS"
Case Else
Debug.Print "Not correct layer name.: & sLayerName"
Exit Sub
End Select
On Error Resume Next
ThisDrawing.ActiveLayer = ThisDrawing.Layers(sLayerName)
If Error > 0 Then
Call AddLayer(sLayerName, oColor, sLineType)
End If
On Error GoTo 0
For Each oLayer In ThisDrawing.Layers
' If drawing is assembly do not work on any top assembly layer
If sActDrw = "ASM" Then
If Left(ThisDrawing.Name, 14) = Left(oLayer.Name, 14) Then
Debug.Print oLayer.Name
GoTo ContinueLoop:
End If
End If
' Check if given layer name is within layer name
If InStr(1, oLayer.Name, UCase(sLayerName)) > 0 Then
Set oSelSet = SelectAllByLayer(oLayer.Name, oLayer.Name)
'If oSelSet.count > 0 Then
' oSelArray = GetSelectionAsArray(oSelSet)
'End If
If oSelSet.count > 0 Then
For Each oObject In GetSelectionAsArray(oSelSet) 'oSelArray
oObject.Layer = sLayerName
Next
End If
' Delete selection set - not needed anymore
oSelSet.Delete
End If
ContinueLoop:
Next
End Sub
Public Sub TestRun()
Call MoveAllToLayer("4-Hatch", "ASM")
ThisDrawing.PurgeAll
End Sub
--Moderator edit: change code format to VB.