For example, a drawing file name = ABCDE12345-111-222.idw/dwg.
How to get the prefix "ABCDE" from the file name in iLogic?
Thanks in advance.
Solved! Go to Solution.
Solved by Curtis_Waguespack. Go to Solution.
Solved by Andrii_Humeniuk. Go to Solution.
Hi @BeKirra . If in your case the prefix is the first 5 characters, then try this code:
Dim oDoc As Document = ThisDoc.Document
Dim sPrefix As String = IO.Path.GetFileNameWithoutExtension(oDoc.FullDocumentName).Substring(0, 5)
MessageBox.Show(sPrefix)
If your name has a different number of letters, you can use this code (it finds the first number):
Public Sub Main()
Dim oDoc As Document = ThisDoc.Document
Dim sName As String = IO.Path.GetFileNameWithoutExtension(oDoc.FullDocumentName)
Dim i As Integer = GetFirstNumber(sName)
MessageBox.Show(sName.Substring(0, i))
End Sub
Private Function GetFirstNumber(ByVal sName As String) As Integer
For i As Integer = 0 To sName.Length - 1
If IsNumeric(sName.Chars(i)) Then Return i
Next i
Return sName.Length
End Function
Andrii Humeniuk - Leading design engineer
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
@Andrii_Humeniuk wrote:Hi @BeKirra . If in your case the prefix is the first 5 characters, then try this code:
Sorry, I should give more details.
The number of characters in the drawing name varies through our sales, projects and company standard drawings.
However, the naming format is the same.
"Letters + Numbers" (e.g. LETTERS1234567)
It would definitely help if iLogic code can detect and get all characters before numbers.
Thanks. 🙂
Maybe something like this example.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
sName = "ABCDE12345-111-222.idw" sArray = Split(sName, "-") 'split string using dash sPrefix = sArray(0) 'get first string in array 'assemble string using only letters in string Dim LeffterPrefix As New System.Text.StringBuilder For Each chr As Char In sPrefix If Char.IsLetter(Chr) Then LeffterPrefix.Append(Chr) End If Next 'convert stringbuilder to string sPrefix = LeffterPrefix.ToString MsgBox(sPrefix,, sName)
Thanks to @Andrii_Humeniuk
I insert your 2nd code to mine as a module. but it doesn't work.
The message is about the "Public sub" function.
It may be caused by the variable name conflict.
I need to dig into it.
It works when saving your code as an file.
Also thanks to @Curtis_Waguespack
Your code works straightforward.
Can't find what you're looking for? Ask the community or share your knowledge.