Skip to content

Instantly share code, notes, and snippets.

@jeff123wang
Created April 6, 2022 15:18
Show Gist options
  • Save jeff123wang/1bab8a8e44c4589b49388bf304ed9485 to your computer and use it in GitHub Desktop.
Save jeff123wang/1bab8a8e44c4589b49388bf304ed9485 to your computer and use it in GitHub Desktop.
' this cat script will export search result to a txt file.
Sub CATMain()
Dim sPath
Dim i
Dim oSelection 'As Selection
Dim oSelectedElement 'As SelectedElement
Dim sElementName 'As String
Dim sElementPath 'As String
Dim sInput
Dim sType
Dim sName
Dim objDictionary
Dim key 'As Variant
Dim totalElementCount
Set objDictionary = CreateObject("Scripting.Dictionary")
sPath = CATIA.FileSelectionBox("Export Selection to .txt, append mode", "*.txt", CatFileSelectionModeSave)
If sPath = "" Then
MsgBox "Nothing is Selected, bye!", vbCritical
Exit Sub
End If
' get Parameters of root assembly.
Set prdAssembly = CATIA.ActiveDocument.Product
Set objAssemblyParams = prdAssembly.Parameters
Set oSelection = CATIA.ActiveDocument.Selection
oSelection.Clear
' edit this part if you want to do different search
sInput = InputBox("Enter search string:" & vbcrlf & _
"1.Name=*,all" & vbcrlf & _
"2.Other, enter later", "Test", "1")
if sInput = 1 then
sInput = "Name=*,all"
else
sInput = InputBox("Enter search string:", "Test", "name=*,all")
end if
oSelection.Search sInput
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(sPath, 8, 0)
' since it is append mode, need to tell who and when did this.
strUser = CreateObject("WScript.Network").UserName
sLine = strUser & "-" & Date & "-" & Time & vbCrLf
f.Write sLine
For i = 1 To oSelection.count
Set oSelectedElement = oSelection.Item(i).value
sElementPath = removeComma(objAssemblyParams.GetNameToUseInRelation(oSelectedElement))
sType = TypeName(oSelectedElement)
sName = removeComma(oSelectedElement.name)
sLine = sType + "," + sName + "," + sElementPath + vbCrLf
f.Write sLine
' add summary information to Dictionary
If objDictionary.Exists(sType) Then
objDictionary(sType) = objDictionary(sType) + 1
else
objDictionary(sType) = 1
end if
Next
f.write "Summary" & vbCrLf
' write summary infomation to file.
For Each key In objDictionary.Keys
f.Write key & "," & objDictionary(key) & vbCrLf
totalElementCount = totalElementCount + objDictionary(key)
Next
oSelection.Clear
f.write vbCrLf
f.Close
MsgBox totalElementCount & " Elements Exported!"
End Sub
' remove comma since comma is used in delimmiter.
function removeComma(str)
removeComma = replace(str,","," ")
end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment