Sub Assemblies - extract iProperties & Read a file's last modified date from a folder.

Sub Assemblies - extract iProperties & Read a file's last modified date from a folder.

andrew_canfield
Collaborator Collaborator
2,064 Views
11 Replies
Message 1 of 12

Sub Assemblies - extract iProperties & Read a file's last modified date from a folder.

andrew_canfield
Collaborator
Collaborator

I've been looking at some browser node examples thinking I could copy the browser name/filename & modified date of only the top level sub assemblies to user parameters within the assembly. [SubAssy_abc1_ModDate],[SubAssy_abc1_FilePath&Name]

 

Then swap out the sub assemblies with simplified parts.

 

When the assembly is next opened the date of the user parameter [SubAssy_abc1_ModDate],[SubAssy_abc1_FilePath&Name] is checked against the date in the folder (reading the last modified date from the filename saved in the user parameters) to warn if the simplified parts are older than the assemblies they replaced.

 

I hope this makes some sense - I've a hunch it can work, still digging through the forum.

 

Regards

 

Andrew

 

0 Likes
Accepted solutions (1)
2,065 Views
11 Replies
Replies (11)
Message 2 of 12

Ralf_Krieg
Advisor
Advisor

Hello

 

Add the first rule to the OnDocumentSave event in the iLogic triggers for assemblys.

 

Option Explicit On


Private Sub Main
	Dim oApp As Inventor.Application = ThisApplication
	Dim oAssDoc As AssemblyDocument= oApp.ActiveDocument 
	
	Dim oSubAssDoc As AssemblyDocument
	Dim oPartDoc As PartDocument
	Dim oOcc As ComponentOccurrence
	Dim iFileSaveCounter As Integer
	Dim sDate As String
	Dim oDate As Date
	Dim sFullFileName As String
	Dim sFileName As String
	Dim oParam As UserParameter
	
	For Each oOcc In oAssDoc.ComponentDefinition.Occurrences
	    If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
	        oSubAssDoc = oOcc.Definition.Document
	        iFileSaveCounter = oSubAssDoc.FileSaveCounter
	        sFullFileName = oSubAssDoc.FullFileName
	        sFileName=System.IO.Path.GetFileNameWithoutExtension(sFullFileName)
	        sDate =System.IO.File.GetLastWriteTime(sFullFileName).Ticks.ToString
			
			If iFileSaveCounter = 0 Then
				MsgBox(sFileName & " needs to be saved first. Skipping and continue next.", MsgBoxStyle.Critical, "iLogic")
			Else
				If CheckParamExist(oAssDoc, sFileName & "_FullPath") = False Then 
		            'Create new UserParameter
		            Call oAssDoc.ComponentDefinition.Parameters.UserParameters.AddByValue(sFileName & "_LastSaved", sDate, UnitsTypeEnum.kTextUnits )
					Call oAssDoc.ComponentDefinition.Parameters.UserParameters.AddByValue(sFileName & "_FullPath", sFullFileName, UnitsTypeEnum.kTextUnits )
		        Else
					'Update LastSaved UserParameter value?
					If Not oAssDoc.ComponentDefinition.Parameters.UserParameters.Item(sFileName & "_LastSaved").Value.ToString.Equals(sDate) Then
		            	oAssDoc.ComponentDefinition.Parameters.UserParameters.Item(sFileName & "_LastSaved").Value = sDate
					end if
		        End If
			End If	
		End If
	Next
End Sub

Private Function CheckParamExist(ByVal oAssDoc As AssemblyDocument, sFileName As String) As Boolean
    Dim oParam As UserParameter
    For Each oParam In oAssDoc.ComponentDefinition.Parameters.UserParameters
        If oParam.Name = sFileName Then
            Return True
        End If
    Next
End Function

 

Add the second rule to the OnOpenDocument event in the iLogic triggers for assemblys. 

 

Option Explicit On
Private Sub Main
	
	Dim oApp As Inventor.Application = ThisApplication
	Dim oAssDoc As AssemblyDocument = oApp.ActiveDocument 
	Dim sFullFileName As String
	Dim sParam As String
	Dim sDate As String
	Dim oDate As Long 'Date
	
	Dim oParam As UserParameter
	For Each oParam In oAssDoc.ComponentDefinition.Parameters.UserParameters
		If oParam.Name.EndsWith("_FullPath") Then
			sFullFileName = oParam.Value 
			oDate = System.IO.File.GetLastWriteTime(sFullFileName).Ticks 
			sParam = Replace(oParam.Name, "_FullPath", "_LastSaved")
			sDate = oAssDoc.ComponentDefinition.Parameters.UserParameters.Item(sParam).Value
			
			If oDate > CLng(sDate) Then
				MsgBox("Subassembly " & sFullFileName & " seems to be updated. Check replacement part.", MsgBoxStyle.Critical, "iLogic")
			End If
		End If
	Next
End Sub

 

There is only a minimum of prechecks. You will possible need to add additional. And you need to think about what to do with parameters for assemblies when the substitue part is removed. The code above doesn't care of that.


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 3 of 12

andrew_canfield
Collaborator
Collaborator

Thankyou - this confirms what I'm looking for isn't too far out!

Slight issue with an exception warning which starts here (I think - Ln 31 appears but not Ln33):

Call.PNG

I'm checking user parameters in the assembly & not seeing any being added - still learning.

 

Regards

 

Andrew

0 Likes
Message 4 of 12

Ralf_Krieg
Advisor
Advisor

Hello

 

Can you Copy'n Paste the detailed error message?

Which Inventor version are you running?


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 5 of 12

andrew_canfield
Collaborator
Collaborator

Hello.

Currently using 2018 but migrating soon.

The error returns:

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.UserParameters.AddByValue(String Name, Object Value, Object UnitsSpecifier)
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Regards

 

Andrew

0 Likes
Message 6 of 12

Ralf_Krieg
Advisor
Advisor

Hello

 

I think the sFileName variable is empty which should not be. Can you try the code below and check the iLogic protocol window if error occurs? Maybe you need to switch the protocol level in iLogic rule editor to Debug or Trace to see the output.

 

Option Explicit On


Private Sub Main
	Dim oApp As Inventor.Application = ThisApplication
	Dim oAssDoc As AssemblyDocument= oApp.ActiveDocument 
	
	Dim oSubAssDoc As AssemblyDocument
	Dim oPartDoc As PartDocument
	Dim oOcc As ComponentOccurrence
	Dim iFileSaveCounter As Integer
	Dim sDate As String
	Dim oDate As Date
	Dim sFullFileName As String
	Dim sFileName As String
	Dim oParam As UserParameter
	
	For Each oOcc In oAssDoc.ComponentDefinition.Occurrences
	    If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
	        oSubAssDoc = DirectCast(oOcc.Definition.Document,AssemblyDocument)
	        iFileSaveCounter = oSubAssDoc.FileSaveCounter
	        sFullFileName = oSubAssDoc.FullFileName
	        sFileName = System.IO.Path.GetFileNameWithoutExtension(sFullFileName)
	        sDate =System.IO.File.GetLastWriteTime(sFullFileName).Ticks.ToString
			
			Logger.Debug("DisplayName:  " & oSubAssDoc.DisplayName)
			Logger.Debug("iFileSaveCounter:  " & iFileSaveCounter)
			Logger.Debug("sFullFileName:  " & sFullFileName)
			Logger.Debug("sFileName:  " & sFileName)
			Logger.Debug("sDate:  " & sDate)

			If sFileName = String.Empty Then
				MsgBox("Filename of SubAssembly (Displayname in Model browser)" & oSubAssDoc.DisplayName & " not found.")
			Else
				If iFileSaveCounter = 0 Then
					MsgBox(sFileName & " needs to be saved first. Skipping and continue next.", MsgBoxStyle.Critical, "iLogic")
				Else
					If CheckParamExist(oAssDoc, sFileName & "_FullPath") = False Then 
			            'Create new UserParameter
			            Call oAssDoc.ComponentDefinition.Parameters.UserParameters.AddByValue(sFileName & "_LastSaved", sDate, UnitsTypeEnum.kTextUnits )
						Call oAssDoc.ComponentDefinition.Parameters.UserParameters.AddByValue(sFileName & "_FullPath", sFullFileName, UnitsTypeEnum.kTextUnits )
			        Else
						'Update UserParameter value?
						If Not oAssDoc.ComponentDefinition.Parameters.UserParameters.Item(sFileName & "_LastSaved").Value.ToString.Equals(sDate) Then
			            	oAssDoc.ComponentDefinition.Parameters.UserParameters.Item(sFileName & "_LastSaved").Value = sDate
						End If
			        End If
				End If	
			End If
		End If
	Next
End Sub

Private Function CheckParamExist(ByVal oAssDoc As AssemblyDocument, sFileName As String) As Boolean
    Dim oParam As UserParameter
    For Each oParam In oAssDoc.ComponentDefinition.Parameters.UserParameters
        If oParam.Name = sFileName Then
            Return True
        End If
    Next
End Function

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 7 of 12

andrew_canfield
Collaborator
Collaborator

Warning on Line 61 : Function 'CheckParamExist' doesn't return a value on all code paths. Are you missing a 'Return' statement?

 


Error on Line 26 : 'Logger' is not declared. It may be inaccessible due to its protection level.
Error on Line 27 : 'Logger' is not declared. It may be inaccessible due to its protection level.
Error on Line 28 : 'Logger' is not declared. It may be inaccessible due to its protection level.
Error on Line 29 : 'Logger' is not declared. It may be inaccessible due to its protection level.
Error on Line 30 : 'Logger' is not declared. It may be inaccessible due to its protection level.

0 Likes
Message 8 of 12

Ralf_Krieg
Advisor
Advisor

Hello

 

Sorry, seems Logger was implemented in a later version than 2018. I've modified this part to display a messagebox and added a return value for the function.

 

Option Explicit On


Private Sub Main
	Dim oApp As Inventor.Application = ThisApplication
	Dim oAssDoc As AssemblyDocument= oApp.ActiveDocument 
	
	Dim oSubAssDoc As AssemblyDocument
	Dim oPartDoc As PartDocument
	Dim oOcc As ComponentOccurrence
	Dim iFileSaveCounter As Integer
	Dim sDate As String
	Dim oDate As Date
	Dim sFullFileName As String
	Dim sFileName As String
	Dim oParam As UserParameter
	
	For Each oOcc In oAssDoc.ComponentDefinition.Occurrences
	    If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
	        oSubAssDoc = DirectCast(oOcc.Definition.Document,AssemblyDocument)
	        iFileSaveCounter = oSubAssDoc.FileSaveCounter
	        sFullFileName = oSubAssDoc.FullFileName
	        sFileName = System.IO.Path.GetFileNameWithoutExtension(sFullFileName)
	        sDate =System.IO.File.GetLastWriteTime(sFullFileName).Ticks.ToString
			
			MsgBox("sFullFileName:  " & sFullFileName & vbCrLf & _
					"sFileName:  " & sFileName & vbCrLf & _
					"DisplayName:  " & oSubAssDoc.DisplayName & vbCrLf & _
					"iFileSaveCounter:  " & iFileSaveCounter & vbCrLf & _
					"sDate:  " & sDate, MsgBoxStyle.Information, "iLogic")

			If sFileName = String.Empty Then
				MsgBox("Filename of SubAssembly (Displayname in Model browser)" & oSubAssDoc.DisplayName & " not found.")
			Else
				If iFileSaveCounter = 0 Then
					MsgBox(sFileName & " needs to be saved first. Skipping and continue next.", MsgBoxStyle.Critical, "iLogic")
				Else
					If CheckParamExist(oAssDoc, sFileName & "_FullPath") = False Then 
			            'Create new UserParameter
			            Call oAssDoc.ComponentDefinition.Parameters.UserParameters.AddByValue(sFileName & "_LastSaved", sDate, UnitsTypeEnum.kTextUnits )
						Call oAssDoc.ComponentDefinition.Parameters.UserParameters.AddByValue(sFileName & "_FullPath", sFullFileName, UnitsTypeEnum.kTextUnits )
			        Else
						'Update UserParameter value?
						If Not oAssDoc.ComponentDefinition.Parameters.UserParameters.Item(sFileName & "_LastSaved").Value.ToString.Equals(sDate) Then
			            	oAssDoc.ComponentDefinition.Parameters.UserParameters.Item(sFileName & "_LastSaved").Value = sDate
						End If
			        End If
				End If	
			End If
		End If
	Next
End Sub

Private Function CheckParamExist(ByVal oAssDoc As AssemblyDocument, sFileName As String) As Boolean
    Dim oParam As UserParameter
    For Each oParam In oAssDoc.ComponentDefinition.Parameters.UserParameters
        If oParam.Name = sFileName Then
            Return True
        End If
    Next
	
	Return False
End Function

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 9 of 12

WCrihfield
Mentor
Mentor

Disregard this post.  Other post at same time stated the same.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 12

andrew_canfield
Collaborator
Collaborator

Please , no need for any apologies, this is great help..i'm still seeing a problem & think it's these lines: 

	 'Call oAssDoc.ComponentDefinition.Parameters.UserParameters.AddByValue(sFileName & "_LastSaved", sDate, UnitsTypeEnum.kTextUnits )
	'Call oAssDoc.ComponentDefinition.Parameters.UserParameters.AddByValue(sFileName & "_FullPath", sFullFileName, UnitsTypeEnum.kTextUnits )

 It runs when I comment them out showing the file information in the message box - guess the lines above write the custom parameter?

When active this is what the message box returns:

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.UserParameters.AddByValue(String Name, Object Value, Object UnitsSpecifier)
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Regards

 

Andrew

 

0 Likes
Message 11 of 12

Ralf_Krieg
Advisor
Advisor
Accepted solution

Hello

 

Are all the variable values showed in the messagebox filled and correct? If all values seems to be correct, can you try with this little snippet if creating text parameters with fixed name and value is possible?

 

Private Sub Main
	Dim oApp As Inventor.Application = ThisApplication
	Dim oAssDoc As AssemblyDocument = oApp.ActiveDocument 

    'Create new UserParameter
	Call oAssDoc.ComponentDefinition.Parameters.UserParameters.AddByValue("sFileName_LastSaved", "sDate", UnitsTypeEnum.kTextUnits )
	Call oAssDoc.ComponentDefinition.Parameters.UserParameters.AddByValue("sFileName_FullPath", "sFullFileName", UnitsTypeEnum.kTextUnits)
End Sub

R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 12 of 12

andrew_canfield
Collaborator
Collaborator
Hello Ralf
Removing the "" around sDate & sFullFileName has worked .
Thanks

Regards

Andrew

0 Likes