I used a similar approach to change view settings in the past, but this time I want to get the hatching info on each view and it seems to require inquiring the face of the model.
I noticed that the loop works until the sheet changes. If I have not open the sheet it returns error 91 as GetFaceHatches seems to fail, If I open the sheet and run again it works until the next sheet and so on...probably the hatch information inside the sheet require to load some more data?? I am testing it agains a very big ddrawing with 100+ views over 4+ sheets
I tried my usual loop and a loop from the cadcoder.com and some sample code from the API help below, but probably I have to switch sheet to make it work?
Code: Select all
Option Explicit
' Creating variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Creating variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Creating variable for Solidworks Drawing
Dim swDrawing As SldWorks.DrawingDoc
' Creating variable for Solidworks View
Dim swView As SldWorks.View
' Creating variable for Solidworks Sheet
Dim swSheet As SldWorks.Sheet
' Program to Loop All Sheets & Views in Drawing
Sub main()
' Setting Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document variable to currently opened document
Set swDoc = swApp.ActiveDoc
' Check if Solidworks document is opened or not
If swDoc Is Nothing Then
MsgBox "Solidworks document is not opened."
Exit Sub
End If
' Set Solidworks Drawing document variable
Set swDrawing = swDoc
' Variable for Sheet names
Dim vSheetName As Variant
' Get the sheets in the drawing document
vSheetName = swDrawing.GetSheetNames
' Variable for Sheet Index
Dim sheetIndex As Integer
' Loop through sheet names in drawing
For sheetIndex = 0 To UBound(vSheetName)
' Set Solidworks Sheet variable
Set swSheet = swDrawing.Sheet(vSheetName(sheetIndex))
' Check if we failed to get sheet
If swSheet Is Nothing Then
MsgBox "Failed to get drawing sheet."
Exit Sub
End If
' Print current sheet name
Debug.Print "Active Sheet Name: " & vSheetName(sheetIndex)
' Variable for drawing views
Dim views As Variant
' Get all views in this sheet
views = swSheet.GetViews
' Variable for drawing view
Dim vView As Variant
' Loop all drawing views
For Each vView In views
' Set Solidworks view variable
Set swView = vView
' Check if we get the view
If swView Is Nothing Then
MsgBox "Failed to get current view."
Exit Sub
End If
' Print View's Name
Debug.Print "View Name: " & swView.Name
Dim vFaceHatch As Variant
Dim swFaceHatch As SldWorks.FaceHatch
Dim swFace As SldWorks.Face2
Dim Debugtext As String
vFaceHatch = swView.GetFaceHatches
If Not IsEmpty(vFaceHatch) Then
Dim k As Integer
For k = 0 To UBound(vFaceHatch)
Set swFaceHatch = vFaceHatch(k)
Set swFace = swFaceHatch.Face
' Cannot select a face because a face is in model
swFace.Select2 True, 0
' Get sketch hatch data
Debug.Print " Pattern = " + swFaceHatch.Pattern
Debug.Print " SolidFill = " & swFaceHatch.SolidFill
Debug.Print " Hatchtype = " & swFaceHatch.HatchType
Next k
End If
Next vView
Next sheetIndex
End Sub