Looping views inside sheets and retrieve hatches info

Programming and macros
User avatar
mp3-250
Posts: 630
Joined: Tue Sep 28, 2021 4:09 am
Answers: 20
Location: Japan
x 695
x 346

Looping views inside sheets and retrieve hatches info

Unread post by mp3-250 »

I am concept testing a macro that loops on every view inside every sheet of a drawing.
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

User avatar
gupta9665
Posts: 405
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 426
x 445

Re: Looping views inside sheets and retrieve hatches info

Unread post by gupta9665 »

If I remove the codes to get the hatch information, the loop works OK.

And you should not need to select the face of the view but should be able to get the hatch information from the view. Check this example https://help.solidworks.com/2022/englis ... ple_vb.htm
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
mp3-250
Posts: 630
Joined: Tue Sep 28, 2021 4:09 am
Answers: 20
Location: Japan
x 695
x 346

Re: Looping views inside sheets and retrieve hatches info

Unread post by mp3-250 »

initially I used another loop I use in drawings and the result is the same, so i think the problem is ... getting the hatch data?
about face selection it was in the example inside the help (needless to say those examples come often with errors or quite unclear or confusing) , probably due to separate the count for face based and sketch based hatch which I have deleted from my code.
User avatar
gupta9665
Posts: 405
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 426
x 445

Re: Looping views inside sheets and retrieve hatches info

Unread post by gupta9665 »

Can you share one dummy set of files to debug the macro?
Deepak Gupta
SOLIDWORKS Consultant/Blogger
Post Reply