Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
J-Camper
in reply to: ysbaodnj

@ysbaodnj,

 

Not sure what you will use this for, but I liked the challenge.  Here is a scalable option:

Sub Main
	
	Dim aDoc As AssemblyDocument = TryCast(ThisApplication.ActiveDocument, AssemblyDocument)
	If IsNothing(aDoc) Then Logger.Debug("Not Run In Assembly Document") : Exit Sub
	
	iLogicVb.Automation.LogControl.Level = LogLevel.Trace
	
	Dim FeatureTypesToCount As New List(Of ObjectTypeEnum)
	FeatureTypesToCount.AddRange({ObjectTypeEnum.kExtrudeFeatureObject, ObjectTypeEnum.kSweepFeatureObject, ObjectTypeEnum.kCombineFeatureObject })
	
	Dim WorkObjectsToCount As New List(Of String)
	WorkObjectsToCount.AddRange({"WorkPoint", "WorkAxis", "WorkPlane", "WorkSurface", "UCS" })
	
	Dim OtherObjectsToCount As New List(Of String)
	OtherObjectsToCount.AddRange({"Sketch", "Sketch3D" }) 	'Anything added needs manual programming in Function: "GetOtherObjectCount"
	
	Call CountAssemblyFeatures(aDoc, WorkObjectsToCount, OtherObjectsToCount, FeatureTypesToCount)
	
	For Each refDoc As Document In aDoc.ReferencedDocuments
		If aDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(refDoc).Count < 1 Then Continue For
		
		If refDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject
			Call CountAssemblyFeatures(refDoc, WorkObjectsToCount, OtherObjectsToCount, FeatureTypesToCount)
		Else If refDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject
			Call CountPartFeatures(refDoc, WorkObjectsToCount, OtherObjectsToCount, FeatureTypesToCount)
		End If
	Next
	
	Dim tempFileName As String = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), ".txt")
	iLogicVb.Automation.LogControl.SaveLogAs(tempFileName)
	
	Dim AllLogText As String = System.IO.File.ReadAllText(tempFileName)
	Dim MarkerString As String = ": >>---------------------------"
	Dim MarkerIndex As Integer = AllLogText.LastIndexOf(MarkerString) + MarkerString.Length+2
	System.IO.File.WriteAllText(tempFileName, AllLogText.Substring(MarkerIndex))
	
	Shell("notepad.exe " & tempFileName, vbNormalFocus)
	
End Sub

Sub CountPartFeatures(pDoc As PartDocument, WorkObjectsToCount As List(Of String), OtherObjectsToCount As List(Of String), FeatureTypesToCount As List(Of ObjectTypeEnum))
	Dim countingMap As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
	
	For Each s As String In WorkObjectsToCount
		countingMap.Add(s, GetWorkObjectCount(s, pDoc))
	Next
	
	For Each s As String In OtherObjectsToCount
		countingMap.Add(s, GetOtherObjectCount(s, pDoc))
	Next
	
	For Each ObjTypeEnum As ObjectTypeEnum In FeatureTypesToCount
		Dim StrippedName As String = Mid(CType(ObjTypeEnum, ObjectTypeEnum).ToString, 2, CType(ObjTypeEnum, ObjectTypeEnum).ToString.Length-7)
		countingMap.Add(StrippedName, pDoc.ComponentDefinition.Features.OfType(Of PartFeature).Where(Function(d) d.Type = ObjTypeEnum).Count)
	Next
	
	Call ExportResults(pDoc.DisplayName, countingMap)
End Sub

Sub CountAssemblyFeatures(aDoc As AssemblyDocument, WorkObjectsToCount As List(Of String), OtherObjectsToCount As List(Of String), FeatureTypesToCount As List(Of ObjectTypeEnum))
	Dim countingMap As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
	
	For Each s As String In WorkObjectsToCount
		countingMap.Add(s, GetWorkObjectCount(s, aDoc))
	Next
	
	For Each s As String In OtherObjectsToCount
		countingMap.Add(s, GetOtherObjectCount(s, aDoc))
	Next
	
	For Each ObjTypeEnum As ObjectTypeEnum In FeatureTypesToCount
		Dim StrippedName As String = Mid(CType(ObjTypeEnum, ObjectTypeEnum).ToString, 2, CType(ObjTypeEnum, ObjectTypeEnum).ToString.Length-7)
		countingMap.Add(StrippedName, aDoc.ComponentDefinition.Features.OfType(Of PartFeature).Where(Function(d) d.Type = ObjTypeEnum).Count)
	Next
	
	Call ExportResults(aDoc.DisplayName, countingMap)
End Sub

Sub ExportResults(DocDisplayName As String, countedMap As NameValueMap)
	
	Logger.Trace(DocDisplayName & " Feature Counts:")
	
	For i = 1 To countedMap.Count
		If countedMap.Value(countedMap.Name(i)) < 0 Then Continue For 'Exlcudes pre-built errors for hard call failures of non-existant objects
		If countedMap.Value(countedMap.Name(i)) < 1 Then Continue For 'Excludes lines for 0 results
		
		Logger.Trace("    " & countedMap.Name(i) & ": " & countedMap.Value(countedMap.Name(i)).ToString)
		
	Next
	
End Sub

Function GetWorkObjectCount(ObjectName As String, Doc As Document) As Integer
	
	Select Case ObjectName
		
	Case "WorkPoint"
		Return Doc.ComponentDefinition.WorkPoints.Count-1
	Case "WorkAxis"
		Return Doc.ComponentDefinition.WorkAxes.Count-3
	Case "WorkPlane"
		Return Doc.ComponentDefinition.WorkPlanes.Count-3
	Case "WorkSurface"
		Try
			Return Doc.ComponentDefinition.WorkSurfaces.Count
		Catch
			Return -1 'WorkSurfaceObject Doesn't exist for assemblies
		End Try
	Case "UCS"
		Return Doc.ComponentDefinition.UserCoordinateSystems.Count
	End Select
	
	Logger.Debug(ObjectName & " not setup in Function: ""GetWorkObjectCount""")
	Return -1
	
End Function

Function GetOtherObjectCount(ObjectName As String, Doc As Document) As Integer
	
	Select Case ObjectName
		
	Case "Sketch"
		Return Doc.ComponentDefinition.Sketches.Count
	Case "Sketch3D"
		Try
			Return Doc.ComponentDefinition.Sketches3D.Count
		Catch
			Return -1 'Sketch3DObject Doesn't exist for assemblies
		End Try
	End Select
	
	Logger.Debug(ObjectName & " not setup in Function: ""GetOtherObjectCount""")
	Return -1
	
End Function

 

Anything that is not a PartFeature, will need to be added to the "OtherObjectsToCount" list and manually programmed in the "GetOtherObjectCount" Function.  I included 2D and 3D sketches in that function for examples.  Part Features can be added by including the ObjectTypeEnum in the "FeatureTypesToCount" list.

 

The rule will display results in Logger and create a temp text file in the temp folder.