How to split filename into iProperties with a format 'Part Number_A - Description' using iLogic

How to split filename into iProperties with a format 'Part Number_A - Description' using iLogic

HLendorf
Participant Participant
705 Views
6 Replies
Message 1 of 7

How to split filename into iProperties with a format 'Part Number_A - Description' using iLogic

HLendorf
Participant
Participant
Hello All,
 
I'm still quite a newbie at iLogic and looks like I have run into a problem trying to figure this out.
I would appreciate any help.
 
I have used the code below to successfully split the filename when saved - into 2 parts - for iProperties 1) Part Number and 2) Description.
Format is "Part Number - Description" with the space-dash-space, separating the two and this has worked well for me when testing.
 

 

Dim FileName, PartNumber, Description As String
Dim UnderPos As Long
FileName = ThisDoc.FileName(False)

'Search the "-" position in string
UnderPos = InStrRev(FileName, "-", - 1)
If UnderPos = 0 Then
MessageBox.Show("No dash character found in filename - please use format: 'DTR/DOC number - Description' with space-dash-space.  Rename filename in Vault to change Part number and/or Description.", "Rule for naming files")
Return
End If

PartNumber = Left(FileName, UnderPos - 2)
'MessageBox.Show(PartNumber, "Part Number")
iProperties.Value("Project", "Part Number") = PartNumber

Description = Left(FileName, Len(FileName))
Description = Right(Description, Len(Description) - (UnderPos+1))
'MessageBox.Show(Description, "Description")
iProperties.Value("Project", "Description") = Description

 

I place this as an iTrigger - "after save" just to avoid the first save Inventor does on new file. I did look at other code for this also, but really can't get around the "first save" that Inventor does, as "save as" dialog always appears for first save - but would be nice if this could also somehow be fixed, and run always as global trigger for ipt/iam and dwg files 😁 Where there is a will - there is a way.
 
So, I now have 2 main issues
-------------------------------
1) if I use more than one dash eg. "Part Number - Descrip-tion" for filename, it fails putting iProperty Part Name as "Part Number - Descrip" and Description as "tion"
 
2) I would like to optionally have the Revision number added as filename "Part Number_A - Description" - is there a way to do this, so if _A (or eg. _B) is part of the filename, iLogic will then add the value "A" to Rev. Number also.
 
To sum it up the format is:
Filename "Part Number_A - Description" to be saved into iProperties
1) ("Project", "Part Number") 
2) sometimes if _A/_B/_C etc. appear also ("Project", "Revison Number")
3) and ("Project", "Description")
and working for ipts/iams and dwgs 🤔
 
Further, I looked at some other code as below, but would be nice with a modified code to fix the 2 issues i have 😉
 

 

Dim oDoc As Document = ThisDoc.Document
If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject
Dim oFileName As String = ThisDoc.FileName(False)
Dim oFileNameSplit() As String = oFileName.Split("-")
iProperties.Value("Project", "Part Number") = oFileNameSplit(0) & "-" & oFileNameSplit(1) & "-" & "DRW" _
& "-" & oFileNameSplit(3) & "-" & oFileNameSplit(4).Split("_")(0)

iProperties.Value("Project", "Stock Number") = oFileName
iProperties.Value("Project", "Description") = oFileNameSplit(4).Split("_")(2)
iProperties.Value("Project", "Revision Number") = oFileNameSplit(4).Split("_")(1)
iProperties.Value("Project", "Project") = oFileNameSplit(0)
ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject
For Each opDoc As Document In oDoc.ReferencedDocuments
If opDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject
opDoc.PropertySets.Item("Design Tracking Properties").Item("Project").Value = iProperties.Value("Project", "Project")
End If
Next
End If

 

Hope someone can help 😉 
 
0 Likes
Accepted solutions (1)
706 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @HLendorf.  Here is a similar iLogic rule you could try out for that task.  I tried to include a lot of checking every step of the way, but read through this and see what you think.  There are a few situations in this where the rule will write a message to the iLogic Log window, then exit the rule, without changing any iProperties.  If you disagree with any of those, you can always comment them out, delete them, or change them as needed.  For instance, if after splitting the FileName by the " - " (space dash space) String, it results in either less than 2 parts, or more than 2 parts, let the user know about it, or if it doesn't even contain that String, then exit the routine, because it must not be formatted correctly.  And I was thinking that if the Description part is empty, then something may be wrong, and you may not want to continue processing that document either.

 

Sub Main
	Dim oDoc As Document = ThisDoc.Document
	Dim sFileName As String = ThisDoc.FileName(False)
	Dim sSeparator As String = " - "
	If sFileName.Contains(sSeparator) = False Then
		Logger.Warn("The following FileName does not include the '" & sSeparator & "' phrase:" _
		& vbCrLf & sFileName)
		Exit Sub
	End If
	Dim oFileNameSections() As String = sFileName.Split(sSeparator)
	Dim sPN, sRev, sDesc As String
	If oFileNameSections.Length < 2 Then
		Logger.Warn("Splitting the following FileName resulted in less than 2 parts:" _
		& vbCrLf & sFileName)
		Exit Sub
	ElseIf oFileNameSections.Length > 2 Then
		Logger.Warn("Splitting the following FileName resulted in more than 2 parts:" _
		& vbCrLf & sFileName)
		Exit Sub
	End If
	'we now know that the split resulted in exactly 2 parts
	'first part should be the Part Number, second part should be the Description
	'however, the Part Number portion may also contain the Revision Number specification
	sPN = oFileNameSections.First
	sDesc = oFileNameSections.Last
	If sDesc = "" Then
		Logger.Warn("After splitting the following FileName, the second part was empty:" _
		& vbCrLf & sFileName)
		Exit Sub
	Else
		oDoc.PropertySets.Item(3).Item("Description").Value = sDesc
	End If
	'now check if the Part Number part ends with an underscore (_), followed by a single character
	'if found, that single character should be the Revision Number
	If sPN.Contains("_") Then
		Dim sPNSections() As String = sPN.Split("_")
		If sPNSections.Length = 2 Then
			sPN = sPNSections.First 'reset Part Number to first part of this split
			sRev = sPNSections.Last 'Revision Number should be last part of this split
		End If
	End If
	oDoc.PropertySets.Item(3).Item("Part Number").Value = sPN
	If sRev <> "" Then 'if empty, do not overwrite Revision Number with empty value
		oDoc.PropertySets.Item(1).Item("Revision Number").Value = sRev
	End If
	If oDoc.RequiresUpdate Then oDoc.Update2(True)
	'If oDoc.Dirty Then oDoc.Save2(False)
End Sub

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7

HLendorf
Participant
Participant

@WCrihfield Thanks you so much for looking at this to get me further 👍 

I managed to get some more code into iLogic, to try find out what is going on 😊

 

I'm not too familier with the Doc.PropertySets.Item(3).Item("Part Number").Value - so I did add the following into the code iProperties.Value("Project", "Part Number") = sPN - to force value being pushed into the iProperty itself.

Not sure if that is correct, but still learning/investigating commands. Same for the command "Logger.Warn" i threw in a couple of "MessageBox.Show" to see.

 

Seems line 4 is giving me problems (a bit like other script i used), but the " - " always get interpreted as "more than 2" dashes. Also, with a filename as eg. "PartNumber - Description - 7" this will just put everything into the Part Number, and not split up into 2.

 

Code with just "-" in line 4 yields the correct result now - to get every value into the iProperties (sPN, sRev and sDesc), but now the issue is you get added space in front of iProperty for the description, just as I assume there is an added space after the iProperty for Part Number.

 

Maybe there is an easy fix?

 

Hope you can help a bit more 😊

 

Code that is partially working - except for the space-dash-space:

 

 

 

Sub Main
	Dim oDoc As Document = ThisDoc.Document
	Dim sFileName As String = ThisDoc.FileName(False)
	Dim sSeparator As String = "-"
	If sFileName.Contains(sSeparator) = False Then
		Logger.Warn("The following FileName does not include the '" & sSeparator & "' phrase:" & vbCrLf & sFileName)
		MessageBox.Show("No dash character found - please enter filename as 'DTR/DOC number - Description' with space-dash-space", "ILogic")
		Exit Sub
	End If
	Dim oFileNameSections() As String = sFileName.Split(sSeparator)
	Dim sPN, sRev, sDesc As String
	If oFileNameSections.Length < 2 Then
		Logger.Warn("Splitting the following FileName resulted in less than 2 parts:" & vbCrLf & sFileName)
		MessageBox.Show("less than 2 parts", "ILogic")
		Exit Sub
	ElseIf oFileNameSections.Length > 2 Then
		Logger.Warn("Splitting the following FileName resulted in more than 2 parts:" & vbCrLf & sFileName)
		MessageBox.Show("more than 2 parts", "ILogic")
		Exit Sub
	End If
	'we now know that the split resulted in exactly 2 parts
	'first part should be the Part Number, second part should be the Description
	'however, the Part Number portion may also contain the Revision Number specification
	sPN = oFileNameSections.First
	sDesc = oFileNameSections.Last
	If sDesc = "" Then
		Logger.Warn("After splitting the following FileName, the second part was empty:" & vbCrLf & sFileName)
		Exit Sub
	Else
		oDoc.PropertySets.Item(3).Item("Description").Value = sDesc
		iProperties.Value("Project", "Description") = sDesc
	End If
	'now check if the Part Number part ends with an underscore (_), followed by a single character
	'if found, that single character should be the Revision Number
	If sPN.Contains("_") Then
		Dim sPNSections() As String = sPN.Split("_")
		If sPNSections.Length = 2 Then
			sPN = sPNSections.First 'reset Part Number to first part of this split
			sRev = sPNSections.Last 'Revision Number should be last part of this split
		End If
	End If
	oDoc.PropertySets.Item(3).Item("Part Number").Value = sPN
	iProperties.Value("Project", "Part Number") = sPN
	If sRev <> "" Then 'if empty, do not overwrite Revision Number with empty value
		oDoc.PropertySets.Item(1).Item("Revision Number").Value = sRev
		iProperties.Value("Project", "Revision Number") = sRev
	End If
	If oDoc.RequiresUpdate Then oDoc.Update2(True)
	'If oDoc.Dirty Then oDoc.Save2(False)
End Sub

 

 

 

0 Likes
Message 4 of 7

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @HLendorf,

 

See the example code below.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

 

 

 

Dim oDoc As Document = ThisDoc.Document
If oDoc.FileSaveCounter = 0 Then Exit Sub 'catch file that has not been saved yet

Dim oFileName As String = ThisDoc.FileName(False)

Dim oFileNameSplit() As String = oFileName.Split("-")
Name = oFileNameSplit(0).TrimEnd(" ") 'remove space at end

Desc = Replace(oFileName, oFileNameSplit(0), "") 'remove Name from string to result in description
If Desc.Contains("-") Then Desc = Replace(Desc, "-", "") 'removes extra dashes
Desc = Desc.TrimStart(" ") 'remove space at front

If Name.Contains("_") Then
	Dim oFileNameSplit2() As String = Name.Split("_")
	Name = oFileNameSplit2(0)
	Rev = oFileNameSplit2(1)
End If

If Rev = "" Then
	Stock = Name & " - " & Desc
Else
	Stock = Name & "_" & Rev & " - " & Desc
End If

iProperties.Value("Project", "Part Number") = Name
iProperties.Value("Project", "Revision Number") = Rev
iProperties.Value("Project", "Description") = Desc
iProperties.Value("Project", "Project") = Name
iProperties.Value("Project", "Stock Number") = Stock

 

 

 

 

Results for filename = 1234_A - Something Some-thing Something.ipt , with extra dash

Curtis_Waguespack_0-1697639445066.png

 

Results for filename = 1234_A - Something Something Something.ipt , with Revision

Curtis_Waguespack_1-1697639492361.png

 

Results for filename = 1234 - Something Something Something.ipt , without revision

Curtis_Waguespack_0-1697640189598.png

 

 

 

 

 

EESignature

Message 5 of 7

WCrihfield
Mentor
Mentor

Hi @HLendorf.  The difference between the following two lines of code in your example code:

oDoc.PropertySets.Item(3).Item("Part Number").Value = sPN

and

iProperties.Value("Project", "Part Number") = sPN

 is that the first one is using Inventor's API (Application Programming Interface), and accesses the property directly though the API Object Model, as designed, starting from the Document object that the properties are stored within.  The second line of code will sometimes work just the same, but it lacks a document specification (which document the property is stored in).  The iProperties.Value method is from the iLogic API, and has a couple options (Link) for attempting to specify which document to work with, but those options are not very well documented, making it less desirable in some situations.  The iLogic method requires less code, and is therefore faster to type in, but may not always be the best option.  Under every Document object is a PropertySets property, which returns a PropertySets type object.  There are typically always at least 4 PropertySets, but there can be more.  The first 3 PropertySet objects in the collection contain all of the standard Property objects (also called iProperties), and fourth set is for custom properties.  That fourth set (the custom one) is always there, but is empty (contains no properties by default), but we can add some to it.  And when we check the checkbox in the Parameters dialog box to export a Parameter object, that automatically creates a custom property in that fourth PropertySet, with the same name and same value as the Parameter.  I have attached a PDF I made that charts out all of the common iProperties, what PropertySets they belong to, what their names are, what Index positions they are at, and additional information about each one that I was able to gather over the past several years.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 7

HLendorf
Participant
Participant

Many thanks @Curtis_Waguespack - this worked so nicely.

Very clean - and exactly what was needed 🤗

Message 7 of 7

HLendorf
Participant
Participant

Thank you @WCrihfield - this is so useful, I have the PDF pinned to my desktop now 👍 

Also, this thread pinned to my favorites bar with all the great references 😁

0 Likes