iLogic to Trim a File Name to Populate iProperties

iLogic to Trim a File Name to Populate iProperties

tony
Enthusiast Enthusiast
4,681 Views
18 Replies
Message 1 of 19

iLogic to Trim a File Name to Populate iProperties

tony
Enthusiast
Enthusiast

Hi,

Please assist with ilogic code;

 

I use automated filename numbering but would like to trim parts of the filename to populate the part number,rev letter and description iproperties after saving the part file. An example file name: "AD-Bracket gusset-123456.A" Part number iproperty = AD-123456.A, Rev iproperty = A and Description iproperty = Bracket gusset.

 

Notes; the start of the filename "AD" will always be 2 charactors only. The end of the filename Rev will always be 1 letter and the Partnumber will always be 11 charactors. The description text will vary in length. 

 

Only update Description IF the field is empty.

 

 

Thankyou 

0 Likes
Accepted solutions (2)
4,682 Views
18 Replies
Replies (18)
Message 2 of 19

Mike.Wohletz
Collaborator
Collaborator
Accepted solution

I have not checked to see the the iProperties are null but that is something that you could easily add. This was created with some functions that use some different methods that you can modify to make the results you are looking for.

thanks

Mike W.

 

  Sub Main
  Dim ThisDocument As PartDocument = ThisApplication.ActiveEditDocument
         
'we need to get the file name of the current document.
	Dim FileName As String =  System.IO.Path.GetFileNameWithoutExtension(ThisDocument.FullDocumentName)
Dim PartNumber As String = GetPartNumber(FileName)
Dim Desc as String = GetDesc(FileName)
Dim Rev As String = GetRevision(FileName)
'use the below for testing to see what we are getting back
'MsgBox(PartNumber & vbLf & Desc & vbLf & Rev)

' we will see if all the variables are valid and set the properties if they are
If Not PartNumber Is Nothing Then
iProperties.Value("Project", "Part Number") = PartNumber
End If

If Not Desc Is Nothing Then
iProperties.Value("Project", "Description") = Desc
End If

If Not Rev Is Nothing Then'
iProperties.Value("Project", "Revision Number") = Rev
End If




End Sub

  	
	   Public Function GetPartNumber(ByVal FileName As String) As String
        'we will look to see the we have 2 - marks and split the string out
        'we could do this by counting characters also if needed
        If CountCharacter(FileName, "-") = 2  Then
            Dim S() As String = FileName.split("-")
			If S(2).contains(".")Then
			'if we found the . for the revision we will trim that from the string
			S(2) = Left(S(2), InStr(S(2), ".")-1)
			End If
			Return S(0) & S(2)
			Else
			Return Nothing	
        End If
    End Function
	
		   Public Function GetDesc(ByVal FileName As String) As String
        'we will look to see the we have 2 - marks and split the string out
        'we could do this by counting characters also if needed
		'Same as GetPartNumber but we will pull out S(1) for this function
        If CountCharacter(FileName, "-") = 2  Then
            Dim S() As String = FileName.split("-")
					Return S(1)
			Else
			Return Nothing	
        End If
    End Function
	
		   Public Function GetRevision(ByVal FileName As String) As String
        'we will look to see the we have 2 - marks and split the string out
        'we could do this by counting characters also if needed
		'Same as GetPartNumber but we will pull out the right part of S(2) for this function
        If CountCharacter(FileName, "-") = 2  Then
            Dim S() As String = FileName.split("-")
			If S(2).contains(".")Then
			'if we found the . for the revision we will trim that from the string
			
			S(2) = S(2).Remove(0, S(2).IndexOf(".")+1)
			
			
			Else
			Return Nothing
			End If
			Return  S(2)
			Else
			Return Nothing	
        End If
    End Function


	Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer
  Return Len(value) - Len(Replace(value, ch, ""))
End Function
	

 

 

Message 3 of 19

tony
Enthusiast
Enthusiast

 

Mike,

This is excellent work. I’m sure there will be many who will take advantage of this time saving iprop code. 

A couple of items to make this code tick all the boxes (if possible):

The part number to include the delimiter AD-123456

Allow description edit (changes back to file name description on subsequent saves)

Everything else works perfectly!

Cheers Tony

0 Likes
Message 4 of 19

tony
Enthusiast
Enthusiast
Hi Mike,



Hopefully a minor bug, error during testing;



Part was added to an assembly and then changes were made in the "project"
iprop page to other non-linked fields (stock number, project etc.) and then
"Apply." the error below appears on saving the Assembly .



If the part is opened explicitly and saved part updates with no error.



Error in rule: Rule2 iprop, in document: AH-1234567891234567891-00190.B.ipt



Unable to cast COM object of type 'System.__ComObject' to interface type
'Inventor.PartDocument'. This operation failed because the QueryInterface
call on the COM component for the interface with IID
'{29F0D463-C114-11D2-B77F-0060B0F159EF}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE)).



System.InvalidCastException: Unable to cast COM object of type
'System.__ComObject' to interface type 'Inventor.PartDocument'. This
operation failed because the QueryInterface call on the COM component for
the interface with IID '{29F0D463-C114-11D2-B77F-0060B0F159EF}' failed due
to the following error: No such interface supported (Exception from HRESULT:
0x80004002 (E_NOINTERFACE)).

at LmiRuleScript.Main()

at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)

at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)



Regards

Tony
0 Likes
Message 5 of 19

Mike.Wohletz
Collaborator
Collaborator
Accepted solution

For the error if you change the first line from:

Dim ThisDocument As PartDocument = ThisApplication.ActiveEditDocument

to:

Dim ThisDocument As Document = ThisApplication.ActiveEditDocument

I think the error will be handled as the active edit item was not a type part i would assume. 

 

To add the - to the part number part the below updated function should do that part. 

 

	   Public Function GetPartNumber(ByVal FileName As String) As String
        'we will look to see the we have 2 - marks and split the string out
        'we could do this by counting characters also if needed
        If CountCharacter(FileName, "-") = 2  Then
            Dim S() As String = FileName.split("-")
			If S(2).contains(".")Then
			'if we found the . for the revision we will trim that from the string
			S(2) = Left(S(2), InStr(S(2), ".")-1)
			End If
			Return S(0) & "-" & S(2)
			Else
			Return Nothing	
        End If
    End Function

 

Message 6 of 19

tony
Enthusiast
Enthusiast

Hi Mike

Great work. Thank you for your help with this.

Regards

Tony Bates








0 Likes
Message 7 of 19

tony
Enthusiast
Enthusiast

Hi,

Idealy the iprop Descrption could be edited after the initial save;please assist with code for one of the following,

Description only updates if the description field is empty?

Code only runs if the file name has not been set?

Thankyou.

0 Likes
Message 8 of 19

Mike.Wohletz
Collaborator
Collaborator

How can you only do this when the file name has not been set when the information used to do all this is something that is extracted from the file name? I think that what you are looking for is that this only run once and as soon as the Description is not empty it does not run again. below is a sample modification to Sub Main that should do this. 

 

  Sub Main
  Dim ThisDocument As PartDocument = ThisApplication.ActiveEditDocument
         
'we need to get the file name of the current document.
	Dim FileName As String =  System.IO.Path.GetFileNameWithoutExtension(ThisDocument.FullDocumentName)
Dim PartNumber As String = GetPartNumber(FileName)
Dim Desc as String = GetDesc(FileName)
Dim Rev As String = GetRevision(FileName)
'use the below for testing to see what we are getting back
'MsgBox(PartNumber & vbLf & Desc & vbLf & Rev)

'we will check to see if the description is blank
if iProperties.Value("Project", "Description") = "" then

' we will see if all the variables are valid and set the properties if they are
If Not PartNumber Is Nothing Then
iProperties.Value("Project", "Part Number") = PartNumber
End If

If Not Desc Is Nothing Then
iProperties.Value("Project", "Description") = Desc
End If

If Not Rev Is Nothing Then'
iProperties.Value("Project", "Revision Number") = Rev
End If

End If


End Sub
0 Likes
Message 9 of 19

tony
Enthusiast
Enthusiast

Hi Mike,

Excellent work and appreciate your time with this.

Regards

Tony

0 Likes
Message 10 of 19

tony
Enthusiast
Enthusiast

Hi Mike,

Please look at attached doc. This error is when saving a part (that was originally saved with the ilogic code) in an assembly environment.

Hope you can make sense of it?

Regards

Tony

 

0 Likes
Message 11 of 19

Mike.Wohletz
Collaborator
Collaborator

I am going to guess that this document was getting edited while in the context of an assembly document (double click on the part in the assembly to edit the part)? if that is the case it was not writen to handle this type of edit so i can see where the error would happen as it possibly is trying to assign the assembly object as a part and that would cause an error. 

0 Likes
Message 12 of 19

tony
Enthusiast
Enthusiast

Hi Mike,

Yes you are correct.

Editing a part in the Assy environment will either trigger the reported error or populate the parts iprop fields with the assembly file name and description. I.e every part may end up with the same part number?

Any suggestions?

Thanks

Tony

0 Likes
Message 13 of 19

Mike.Wohletz
Collaborator
Collaborator
The error that you posted was already solved in an earlier post by changing from part document to just document in the variable type. As for why the error is happening is it possible that you are using this as an external rule or is it in the parts?
0 Likes
Message 14 of 19

tony
Enthusiast
Enthusiast

Hi Mike,

The only rule setup is "After Save Document". The error is not consistant but i do run other software that is triggered by "save".

Maybe on opening a new or existing part file a message box is triggered "Update iproperties" "Yes/NO" and if yes will run the code making this independant of the save trigger?

Thankyou

Tony

0 Likes
Message 15 of 19

tony
Enthusiast
Enthusiast

Hi Mike

Our ERP software runs script to sync all parts and drawings in the current assy doc during save. I’ve experimented with several event trigger types but all generate an error while editing in the context of an assembly document or during ERP synchronisation. I’ve tried a message box without success. Can you tweak the code to allow edit in an open assy doc or think of a work around?

Thankyou.

0 Likes
Message 16 of 19

tony
Enthusiast
Enthusiast

Hi,

The logic attached automatically trims a parts file name to prefill the part number, description and revision fields on the parts iproperties page.

Currently, the code only writes to these iproperties fields if they are empty. 

Please assist with modifying the code to overwrite the revision field if it is already populated.

Thankyou.

0 Likes
Message 17 of 19

Mike.Wohletz
Collaborator
Collaborator

you just need to remove or comment out the if statement as shown below. 

 

'this will update the revision number if it is empty or nothing
If Not Rev Is Nothing Then'
iProperties.Value("Project", "Revision Number") = Rev
End If

'to update the revision all the time either remove the if Not line and End If or
'comment them out as shown below.
'If Not Rev Is Nothing Then'
iProperties.Value("Project", "Revision Number") = Rev
'End If
0 Likes
Message 18 of 19

tony
Enthusiast
Enthusiast

Hi,

Not quite there. I'm using an App "Rename" that allows updates to a parts file name in an assembly. I use it to update the file Rev letter. I've discovered manually clearing the project iprop part number, description, and Rev code 1st and then firing the "trim a file name" code works well. I've found some of your previously published code to Null all iprop fields but not individual project properties as I need to retain the custom iprop. Any suggestions?

Thankyou.

 

 

0 Likes
Message 19 of 19

Anonymous
Not applicable

@Mike.Wohletz 

 

Hello Mike. Sorry to bother you, but i want to ask for your help:

I have similar problem and i have tried to describe it here: https://forums.autodesk.com/t5/inventor-forum/ilogic-file-name-to-populate-iproperties/m-p/9518224/h...

 

In addition of that i need the first characters from XXXXX-YYY-ZZZ-WWWWW-VVV_U_Name to be put under Project Iproperties. Hope you can help me, because i have no idea to code anything:)

0 Likes