If 3 last characters in filename match a name, populate custom iproperty in all parts from assembly.

If 3 last characters in filename match a name, populate custom iproperty in all parts from assembly.

tom.van.schaeybroeck
Participant Participant
315 Views
7 Replies
Message 1 of 8

If 3 last characters in filename match a name, populate custom iproperty in all parts from assembly.

tom.van.schaeybroeck
Participant
Participant

Hello,

 

I'm trying to make a rule in a top level assembly. This rule should check all part filenames in the assembly. If the part filenames end in "ADV", it should write "ADV" in a new custom iproperty called "Type" in the parts that contain the "ADV".

 

I tried piecing together different bits of code I found from similar posts on this forum, but I just can't get it to work. I'm not really into coding, so my code is probably bad in many ways.

 

Here is what I have:

 

'define the active assembly
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument 

Dim oFullFileName As String
Dim oDoc As Inventor.Document

For Each oDoc In oAsmDoc.AllReferencedDocuments
	
	oFullFileName = oDoc.FullFileName
	'find the postion of the last backslash in the path
	FNamePos = InStrRev(oFullFileName, "\", -1)   
	'get the file name with the file extension
	Name = Right(oFullFileName, Len(oFullFileName) - FNamePos)
	'get the file name (without extension)
	ShortName = Left(Name, Len(Name) -4)
	Type = Right(ShortName, 3)
	
	If Type = "ADV" Then
		iProperties.Value("Custom", "Type") = "ADV"

	Else 
		iProperties.Value("Custom", "Type") = ""
		
	End If

Next

 

Any help would be appreciated.

 

Regards,

Tom

Autodesk Inventor Professional 2022
0 Likes
Accepted solutions (1)
316 Views
7 Replies
Replies (7)
Message 2 of 8

JelteDeJong
Mentor
Mentor

try something like this:

Dim oAsmDoc As AssemblyDocument = ThisDoc.Document

For Each doc As Document In oAsmDoc.AllReferencedDocuments

    Dim fullFileName = doc.FullFileName
    Dim fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fullFileName)


    Dim propSet = doc.PropertySets.Item("Inventor User Defined Properties")

    Dim propValue = ""
    If (fileNameWithoutExtension.ToUpper().EndsWith("ADV")) Then
        propValue = "ADV"
    End If

    Try
        propSet.Item("Type").Value = propValue
    Catch ex As Exception
        propSet.Add(propValue, "Type")
    End Try
Next

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 8

tom.van.schaeybroeck
Participant
Participant

Hello Jelte,

 

Thank you for taking the time to help me out. Your code adds "ADV" to the top level assembly as custom iProperty, which I don't want. I only want "ADV" added as a custom iproperty to the parts that contain "ADV" as the end of their file name.

 

Regards,

Tom

Autodesk Inventor Professional 2022
0 Likes
Message 4 of 8

JelteDeJong
Mentor
Mentor

I have added a part document check

Dim oAsmDoc As AssemblyDocument = ThisDoc.Document

For Each doc As Document In oAsmDoc.AllReferencedDocuments
    If (doc.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then Continue For

    Dim fullFileName = doc.FullFileName
    Dim fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fullFileName)


    Dim propSet = doc.PropertySets.Item("Inventor User Defined Properties")

    Dim propValue = ""
    If (fileNameWithoutExtension.ToUpper().EndsWith("ADV")) Then
        propValue = "ADV"
    End If

    Try
        propSet.Item("Type").Value = propValue
    Catch ex As Exception
        propSet.Add(propValue, "Type")
    End Try
Next

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 5 of 8

WCrihfield
Mentor
Mentor

Which version/year of Inventor are you using?  If you are using 2022 or later, do any of the components have any custom ModelStates.  Are any of the components from the Content Center or other read only library type area?  If so, you may not be able to edit those.  Do you want the code to attempt to process any components that are suppressed?

Oops.  Posted before seeing above responses.  Disregard this post.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 8

tom.van.schaeybroeck
Participant
Participant

@JelteDeJong 

 

The check works in preventing to populate the top level assembly. It adds a custom iProperty named "Type" to all parts in the assembly now, which is good. However it does still not add "ADV" to the parts with "ADV" at the end of their filename.

 

@WCrihfield 

 

1) Inventor Professional 2021

2) The rule does not have to apply to content center parts or read only libs

3) Components that are suppressed don't have to be processed 

Autodesk Inventor Professional 2022
0 Likes
Message 7 of 8

JelteDeJong
Mentor
Mentor
Accepted solution

It works for me. I changed the code a bit to do some logging. It writes to the log files names that does not end on ADV. maybe you can share a file name that does not work.

Dim oAsmDoc As AssemblyDocument = ThisDoc.Document

For Each doc As Document In oAsmDoc.AllReferencedDocuments
    If (doc.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then Continue For

    Dim fullFileName = doc.FullFileName
    Dim fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fullFileName)

    Dim propSet = doc.PropertySets.Item("Inventor User Defined Properties")

    Dim propValue = ""
    If (fileNameWithoutExtension.ToUpper().EndsWith("ADV")) Then
        propValue = "ADV"
        Logger.Info(String.Format("----- '{0}' ------ Done", fileNameWithoutExtension))
    Else
        Logger.Info(String.Format("'{0}' does not end with ADV", fullFileName))
    End If

    Try
        propSet.Item("Type").Value = propValue
    Catch ex As Exception
        propSet.Add(propValue, "Type")
    End Try
Next

' Show logger
Dim loggerWindow = ThisApplication.UserInterfaceManager.
    DockableWindows.
    Cast(Of DockableWindow).
    Where(Function(d) d.InternalName.Equals("ilogic.logwindow")).
    First()
loggerWindow.Visible = True

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 8 of 8

tom.van.schaeybroeck
Participant
Participant

Jelte,

 

It works indeed. I did not realise it was case-sensitive. I managed to fix it with your guidance.

 

Thank you for your help.

 

Regards,

Tom

Autodesk Inventor Professional 2022
0 Likes