iLogic check parts against filenames, modify if promted

iLogic check parts against filenames, modify if promted

josh.linares-stanley
Enthusiast Enthusiast
843 Views
10 Replies
Message 1 of 11

iLogic check parts against filenames, modify if promted

josh.linares-stanley
Enthusiast
Enthusiast

Hi,

Id like the following code to traverse through a top level assemblies parts & sub-assemblies to identify any that have a mismatched part number/filename and prompt the user to modify. 

If a matching part number/filename is not desired, it adds a custom iProperty to identify that for future. 

I have spliced together the following from a few sources,  It only appears to work on the top level assembly file instead of the parts/assemblies within. Please could someone point out the flaw?

 

Any help is appreciated, thanks in advance.

Sub Main
    ' Get the active assembly. 
	Dim oAssyDoc As AssemblyDocument
	oAssyDoc = ThisApplication.ActiveDocument 
	
    ' Call the function
    Call TraverseAssembly(oAssyDoc.ComponentDefinition.Occurrences)
End Sub 
 
Sub TraverseAssembly(oOccs As ComponentOccurrences)
    ' Iterate through all of the occurrence in this collection
	Dim oOcc As ComponentOccurrence
	
	For Each oOcc In oOccs 
		
		oDoc = ThisDoc.FileName(False)
PartNumber = iProperties.Value("Project", "Part Number")

If oDoc = PartNumber Then
	Return
Else
	Try 
		oProp = iProperties.Value("Custom", "NotFileName")
		Return
	Return
	Catch
		i = MessageBox.Show("Part number does not match filename. Would you like to match?","Action",MessageBoxButtons.YesNo,MessageBoxIcon.Hand ,MessageBoxDefaultButton.Button1)
		If i = MsgBoxResult.Yes Then
			iProperties.Value("Project", "Part Number") = oDoc
		ElseIf i = MsgBoxResult.No Then
			iProperties.Value("Custom", "NotFileName") = "No"
		End If
	End Try
End If

		' Recursively call sub if needed
		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
			Call TraverseAssembly(oOcc.SubOccurrences)
		End If
	Next
End Sub

 

0 Likes
Accepted solutions (1)
844 Views
10 Replies
Replies (10)
Message 2 of 11

Cadkunde.nl
Collaborator
Collaborator

If you use this ilogic snippet

 

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

it retrieves the part number of the document you run the rule in.

 

This is a useful link to bookmark for iproperties:

https://modthemachine.typepad.com/my_weblog/2010/02/accessing-iproperties.html

Dim occdoc as document = oOcc.definition.document

occdoc.PropertySets.Item("Design Tracking Properties").Item("Part Number")

 

0 Likes
Message 3 of 11

Cadkunde.nl
Collaborator
Collaborator
Dim strFileName As String = System.IO.Path.GetFileNameWithoutExtension(occdoc.FullFileName)

 This is how you retrieve the filename of the occurrence to compare with partnumber

0 Likes
Message 4 of 11

josh.linares-stanley
Enthusiast
Enthusiast

Hi, thanks for the response. Still getting my head around iLogic so please bare with. 

I understand what you mean with it getting the active file information instead of the files within...

 

I added those snippets to where I assumed they'd need to be but get an error Object reference not set to an instance of an object. on line 13.

 

Thanks

 

Sub Main
    ' Get the active assembly. 
	Dim oAssyDoc As AssemblyDocument
	oAssyDoc = ThisApplication.ActiveDocument 
	
    ' Call the function
    Call TraverseAssembly(oAssyDoc.ComponentDefinition.Occurrences)
End Sub 
 
Sub TraverseAssembly(oOccs As ComponentOccurrences)
    ' Iterate through all of the occurrence in this collection
	Dim oOcc As ComponentOccurrence
	Dim oCcdoc As Document = oOcc.Definition.Document
	Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(oCcdoc.FullFileName)
	
	For Each oOcc In oOccs 
		PartNumber = oCcdoc.PropertySets.Item("Design Tracking Properties").Item("Part Number")
		If oFileName = PartNumber Then
	Return
Else
	Try 
		oProp = iProperties.Value("Custom", "NotFileName")
		Return
	Return
	Catch
		i = MessageBox.Show("Part number does not match filename. Would you like to match?","Action",MessageBoxButtons.YesNo,MessageBoxIcon.Hand ,MessageBoxDefaultButton.Button1)
		If i = MsgBoxResult.Yes Then
			iProperties.Value("Project", "Part Number") = oFileName
		ElseIf i = MsgBoxResult.No Then
			iProperties.Value("Custom", "NotFileName") = "No"
		End If
	End Try
End If

		' Recursively call sub if needed
		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
			Call TraverseAssembly(oOcc.SubOccurrences)
		End If
	Next
End Sub

 

0 Likes
Message 5 of 11

Cadkunde.nl
Collaborator
Collaborator
Sub Main()
Dim oAssyDoc As AssemblyDocument = ThisApplication.ActiveDocument
TraverseAssembly(oAssyDoc.ComponentDefinition.Occurrences)
End Sub

Sub TraverseAssembly(oOccs As ComponentOccurrences)
	Dim oCcdoc As Document
	Dim strFileName As String
	Dim PartNumber As String

	For Each oOcc As ComponentOccurrence In oOccs
		oCcdoc = oOcc.Definition.Document
		strFileName = System.IO.Path.GetFileNameWithoutExtension(oCcdoc.FullFileName)
		PartNumber = oCcdoc.PropertySets.Item("Design Tracking Properties").Item("Part Number")
		If Not strFileName = PartNumber Then
			Try
				oProp = oCcdoc.PropertySets.Item("Inventor User Defined Propertiess").Item("NotFileName")
			Catch
				i = MessageBox.Show("Part number does not match filename. Would you like to match?", "Action", MessageBoxButtons.YesNo, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
				If i = MsgBoxResult.Yes Then
					oCcdoc.PropertySets.Item("Design Tracking Properties").Item("Part Number") = strFileName
				ElseIf i = MsgBoxResult.No Then
					oCcdoc.PropertySets.Item("Inventor User Defined Propertiess").Item("NotFileName") = "No"
				End If
			End Try
		End If

		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
			TraverseAssembly(oOcc.SubOccurrences)
		End If
	Next
End Sub
0 Likes
Message 6 of 11

Cadkunde.nl
Collaborator
Collaborator

You can only use the ilogic snippet iproperties.value for the document you run the rule.

In your situation you need to completely forget that snippet.

 

I think the part in try catch is still wrong.

 

Normally you try to get the value. If you get an error, the iproperty does not exist and you have to create it.

I didn't see code to add the iproperty (but perhaps that snippet you cant use automated that part too)

 

You can do that with:

oCcdoc.PropertySets.Item("Inventor User Defined Properties").Add(Value, ipropertyname)
0 Likes
Message 7 of 11

josh.linares-stanley
Enthusiast
Enthusiast

It tries to work but states that the Propertysets are read only values when it attempts to overwrite them. I tried to find a fix to toggle that off but no luck yet.

 

Thanks

0 Likes
Message 8 of 11

Cadkunde.nl
Collaborator
Collaborator
Accepted solution

oh my bad, you need to add .value like this for all the iproperties

 

oCcdoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").value

 

0 Likes
Message 9 of 11

josh.linares-stanley
Enthusiast
Enthusiast

Excellent, the addition .value allows it to work seemlessly! Thanks a lot for you help

0 Likes
Message 10 of 11

WCrihfield
Mentor
Mentor

@josh.linares-stanley , @Cadkunde.nl 

Hi guys.  I usually prefer to use the API way to access iProperties too, but just to clarify... that iProperties.Value() iLogic snippet can be used on more than just the main document that the rule is running in.  However, it can only access documents that are in the 'AllReferencedDocuments' scope of 'the document that the rule is running in'.  And to clarify what 'AllReferencedDocuments' means, that will include every document at every level of an assembly type document, even those in sub assemblies which are within other sub assemblies.  But when you need it to work with a document other than the main one that the rule was started from, you need to specify which one it should target, one of two main traditional ways.  If you want to access the iProperties of an assembly component (ComponentOccurrence) in an assembly, you need to specify the ComponentOccurrence.Name value as the first of three input values, like this:  iPropeties.Value(oOcc.Name, "PropertySetName", "PropertyName").  If you want to access the iProperties of another document directly, then you need to specify that document's file name (without path, but with file extension) as the first of three input values, similar to this:  iProperties.Value(oDocFileName, "PropertySetName", "PropertyName").

 

It can also automatically create new custom iProperties for you, if you attempt to set a value to a custom iProperty that does not exist yet, which is pretty convenient.  It will not automatically create a custom iProperty though if you are just trying to check the current value of one that does not exist yet...it will just throw an error.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 11 of 11

Cadkunde.nl
Collaborator
Collaborator

You are right, I forgot about it.

There is a iproperties.value (Assy use) snippet.

0 Likes