Hi Wesley, thank you for the input! I used some other codes and got this working - full disclosure, a bit of Gemini in there too 🙂 I wish for it to search 3D sketches also, but have had a hard time with that... It will find Broken Projections in 2D sketches very well, but not in 3D. Would you have any input on this?
Thank you!
Sub Main()
Dim oDoc As Document = ThisApplication.ActiveDocument
If oDoc.DocumentType <> Inventor.DocumentTypeEnum.kPartDocumentObject Then
MessageBox.Show("Hey this code only runs with Part Documents!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
Dim oPDef As PartComponentDefinition = oDoc.ComponentDefinition
Dim brokenSketches As New System.Collections.ArrayList()
' Check 2D Sketches
For Each oSketch As PlanarSketch In oPDef.Sketches
Dim brokenProjectionFound As Boolean = False
For Each oSE As SketchEntity In oSketch.SketchEntities
Try
If oSE.Reference Then
If oSE.ReferencedEntity Is Nothing Then
brokenProjectionFound = True ' Found a broken projection
End If
End If
Catch
End Try
Next
If brokenProjectionFound Then
brokenSketches.Add(oSketch.Name & " (2D)") ' Add sketch name to the list with type
End If
Next
' Check 3D Sketches
For Each o3DSketch As Sketch3D In oPDef.Sketches3D
Dim brokenProjectionFound As Boolean = False
Try
Dim sketch3DEntitiesProperty As System.Reflection.PropertyInfo = o3DSketch.GetType().GetProperty("Sketch3DEntities")
If sketch3DEntitiesProperty IsNot Nothing Then
Dim sketch3DEntities As Object = sketch3DEntitiesProperty.GetValue(o3DSketch, Nothing)
If sketch3DEntities IsNot Nothing Then
For Each o3DEntity As Object In CType(sketch3DEntities, System.Collections.IEnumerable)
Try
If o3DEntity.GetType().Name = "Sketch3DReference" Then
Dim referencedEntityProperty As System.Reflection.PropertyInfo = o3DEntity.GetType().GetProperty("ReferencedEntity")
If referencedEntityProperty IsNot Nothing Then
Dim referencedEntity As Object = referencedEntityProperty.GetValue(o3DEntity, Nothing)
If referencedEntity Is Nothing Then
brokenProjectionFound = True
End If
End If
End If
Catch
End Try
Next
End If
End If
Catch
End Try
If brokenProjectionFound Then
brokenSketches.Add(o3DSketch.Name & " (3D)") ' Add 3D sketch name to the list with type
End If
Next
If brokenSketches.Count > 0 Then
Dim message As String = "The following sketches contain broken projections:" & vbCrLf
For Each sketchName As String In brokenSketches
message = message & "- " & sketchName & vbCrLf
Next
MessageBox.Show(message, "Broken Projections Found")
Else
MessageBox.Show("No broken projections found in any sketches.", "iLogic")
End If
End Sub