error code

error code

johan.degreef
Advisor Advisor
3,276 Views
41 Replies
Message 1 of 42

error code

johan.degreef
Advisor
Advisor

I get a lot of these error codes lately, with this code. Does anyone sees where it comes from?

 

System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.Document.SaveAs(String FileName, Boolean SaveCopyAs)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

 

 

Imports System.IO
GetInput :

Six_Digit = InputBox("Enter 6 Digit number", "iLogic", 000000)

If Six_Digit = "" Then
	Return
Else If Len(Six_Digit) <> "6" Then
MessageBox.Show("Input must be 6 digits", "ilogic")
GoTo GetInput

End If


Dim oDoc As Document
Dim sFilename As String

'hard code path
oLibrary_Folder = "C:\PTN_Part_Library\"


Dim oFilenames() As String
oFilenames = System.IO.Directory.GetFiles(oLibrary_Folder, _
"*.*", SearchOption.AllDirectories)

For Each oFilename As String In oFilenames
	If oFilename.Contains(Six_Digit) Then
		Dim oOptions As Inventor.NameValueMap
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		oDoc = ThisApplication.Documents.OpenWithOptions(oFilename, oOptions, False)
		sFilename = oFilename
		Exit For
	End If
Next

If sFilename = "" Then
	MessageBox.Show("No matching libary file found.", "iLogic")
	Return
End If

iCounter = 0

'path from current file
oActiveAssemblyfolder = ThisDoc.Path & "\"

'path from current project file ( *.ipj)
oProjectfolder = _
ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath & "\"

Dim oProject_Filenames() As String
oProject_Filenames = System.IO.Directory.GetFiles(oProjectfolder, _
"*.*", SearchOption.AllDirectories)

'count existing project files					  
For Each oFilename As String In oProject_Filenames
	If oFilename.Contains(Six_Digit) Then
		iCounter = iCounter + 1
	End If
Next

'increment counter
iCounter = iCounter + 1


'find the postion of the last backslash in the path
FNamePos = InStrRev(sFilename, "\", - 1)
'get the file name with the file extension
oName = Right(sFilename, Len(sFilename) - FNamePos)
'get the file name (without extension)
ShortName = Left(oName, Len(oName) - 4)
'get extension
oExt = Right(oName, 4)


If iCounter < 10 Then
	iCounter = "00" + CStr(iCounter)
ElseIf iCounter < 100 Then
	iCounter = "0" + CStr(iCounter)
Else
	iCounter = CStr(iCounter)
End If

oNewName = ShortName & "_" & iCounter

oPathandName = oActiveAssemblyfolder & oNewName & oExt

'save new document
oDoc.SaveAs(oPathandName, True)
oDoc.Close

'[ Place Component
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

' Create Matrix
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
Call oMatrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(50, 50, 50), True)


'insert new occurence
'Dim oOcc As ComponentOccurrence
'oOcc = oAsmCompDef.Occurrences.Add( _
'oPathandName, oMatrix)

ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent,oPathandName) 
ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyPlaceComponentCmd").Execute


'oOcc.Grounded = False
'ThisDoc.Document.SelectSet.Select(oOcc)

']

'MessageBox.Show("New file created: " & vbLf & oPathandName, "iLogic")

 

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
0 Likes
Accepted solutions (1)
3,277 Views
41 Replies
Replies (41)
Message 21 of 42

JhoelForshav
Mentor
Mentor

This is driving me crazy... where is the "85" part of the name coming from??

0 Likes
Message 22 of 42

johan.degreef
Advisor
Advisor

one of the last parts i inserted was PIPE_200213_085.ipt

Shall I send you my library?

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
0 Likes
Message 23 of 42

JhoelForshav
Mentor
Mentor

Ok....

So I see now that you only want three digits and I made it four. That is fixed in the code below.

What I cannot understand is why we don't get the "_" in the filename when we obviously add it!

Dim oNewName As String = ShortName & "_" & iCounter.ToString("000")

When I try it on my machine with random filepaths everything works as expected....

 

This code really should work 😞 At least it should add the "_"...

GetInput :

Six_Digit = InputBox("Enter 6 Digit number", "iLogic", 000000)

If Six_Digit = "" Then
	Return
Else If Len(Six_Digit) <> "6" Then
MessageBox.Show("Input must be 6 digits", "ilogic")
GoTo GetInput

End If



Dim oDoc As Document
Dim sFilename As String

'hard code path
oLibrary_Folder = "C:\PTN_Part_Library\"


Dim oFilenames() As String
oFilenames = System.IO.Directory.GetFiles(oLibrary_Folder, _
"*.*", SearchOption.AllDirectories)

For Each oFilename As String In oFilenames
	If oFilename.Contains(Six_Digit) Then
		Dim oOptions As Inventor.NameValueMap
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		oDoc = ThisApplication.Documents.OpenWithOptions(oFilename, oOptions, False)
		sFilename = oFilename
		Exit For
	End If
Next

If sFilename = "" Then
	MessageBox.Show("No matching libary file found.", "iLogic")
	Return
End If


Dim iCounter As Integer = 0

'path from current file
Dim oActiveAssemblyfolder As String = ThisDoc.Path & "\"

'path from current project file ( *.ipj)
Dim oProjectfolder As String = _
ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath & "\"

Dim oProject_Filenames() As String
oProject_Filenames = System.IO.Directory.GetFiles(oProjectfolder, _
"*.*", SearchOption.AllDirectories)

'count existing project files					  
For Each oFilename As String In oProject_Filenames
	If oFilename.Contains(Six_Digit) Then
		Dim oFileNameSplit As String() = System.IO.Path.GetFileNameWithoutExtension(oFilename).Split("_")
		If CInt(oFileNameSplit(oFileNameSplit.Length - 1)) > iCounter Then iCounter = CInt(oFileNameSplit(oFileNameSplit.Length - 1))
	End If
Next

iCounter += 1



'find the postion of the last backslash in the path
Dim FNamePos As Integer = InStrRev(sFilename, "\", -1)
'get the file name with the file extension
Dim oName As String = Right(sFilename, Len(sFilename) -FNamePos)
'get the file name (without extension)
Dim ShortName As String = Left(oName, Len(oName) -4)
'get extension
Dim oExt As String = Right(oName, 4)



Dim oNewName As String = ShortName & "_" & iCounter.ToString("000")

Dim oPathandName As String = oActiveAssemblyfolder & oNewName & oExt



MsgBox(oPathandName)
'save new document
oDoc.SaveAs(oPathandName, True)
oDoc.Close



'[ Place Component
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition



' Create Matrix
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
Call oMatrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(50, 50, 50), True)



'insert new occurence
'Dim oOcc As ComponentOccurrence
'oOcc = oAsmCompDef.Occurrences.Add( _
'oPathandName, oMatrix)

ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oPathandName)
ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyPlaceComponentCmd").Execute


'oOcc.Grounded = False
'ThisDoc.Document.SelectSet.Select(oOcc)

']

'MessageBox.Show("New file created: " & vbLf & oPathandName, "iLogic")

 

Message 24 of 42

johan.degreef
Advisor
Advisor

@JhoelForshav 

Maybe my bad because of the 85.... there was a part in my folders ..._850002.ipt I deleted both and now it seems to work OK. I will test a few days and let you know if all work.

 

Million times thanks for helpingme out here!

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
0 Likes
Message 25 of 42

JhoelForshav
Mentor
Mentor
Accepted solution

@johan.degreef 

That sounds like the problem😆

Hopefully it'll work as expected from now on 🙂

Message 26 of 42

johan.degreef
Advisor
Advisor

@JhoelForshav 

Another issue popped up.

When I create a mirror in my assembly, the filenames get _MIR behind.

PIPE_200211_010_MIR.ipt

 

when this occurs I get the error below when trying to enter another 200211 pipe

 

System.InvalidCastException: De conversie van tekenreeks MIR1 naar type Integer is ongeldig. ---> System.FormatException: Input string was not in a correct format.
at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)
--- End of inner exception stack trace ---
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
0 Likes
Message 27 of 42

JhoelForshav
Mentor
Mentor

@johan.degreef 

I havn't tried the code myself but I think this should work then 🙂

 

GetInput :

Six_Digit = InputBox("Enter 6 Digit number", "iLogic", 000000)

If Six_Digit = "" Then
	Return
Else If Len(Six_Digit) <> "6" Then
MessageBox.Show("Input must be 6 digits", "ilogic")
GoTo GetInput

End If



Dim oDoc As Document
Dim sFilename As String

'hard code path
oLibrary_Folder = "C:\PTN_Part_Library\"


Dim oFilenames() As String
oFilenames = System.IO.Directory.GetFiles(oLibrary_Folder, _
"*.*", SearchOption.AllDirectories)

For Each oFilename As String In oFilenames
	If oFilename.Contains(Six_Digit) Then
		Dim oOptions As Inventor.NameValueMap
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		oDoc = ThisApplication.Documents.OpenWithOptions(oFilename, oOptions, False)
		sFilename = oFilename
		Exit For
	End If
Next

If sFilename = "" Then
	MessageBox.Show("No matching libary file found.", "iLogic")
	Return
End If


Dim iCounter As Integer = 0

'path from current file
Dim oActiveAssemblyfolder As String = ThisDoc.Path & "\"

'path from current project file ( *.ipj)
Dim oProjectfolder As String = _
ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath & "\"

Dim oProject_Filenames() As String
oProject_Filenames = System.IO.Directory.GetFiles(oProjectfolder, _
"*.*", SearchOption.AllDirectories)

'count existing project files					  
For Each oFilename As String In oProject_Filenames
	If oFilename.Contains(Six_Digit) Then
		Dim oFileNameSplit As String() = System.IO.Path.GetFileNameWithoutExtension(oFilename).Split("_")
		Dim oNumber As Integer
		If oFileNameSplit(oFileNameSplit.Length - 1) = "MIR" Then
			oNumber = CInt(oFileNameSplit(oFileNameSplit.Length - 2))
		Else
			oNumber = CInt(oFileNameSplit(oFileNameSplit.Length - 1))
		End If
		If oNumber > iCounter Then iCounter = oNumber
	End If
Next

iCounter += 1



'find the postion of the last backslash in the path
Dim FNamePos As Integer = InStrRev(sFilename, "\", -1)
'get the file name with the file extension
Dim oName As String = Right(sFilename, Len(sFilename) -FNamePos)
'get the file name (without extension)
Dim ShortName As String = Left(oName, Len(oName) -4)
'get extension
Dim oExt As String = Right(oName, 4)



Dim oNewName As String = ShortName & "_" & iCounter.ToString("000")

Dim oPathandName As String = oActiveAssemblyfolder & oNewName & oExt



MsgBox(oPathandName)
'save new document
oDoc.SaveAs(oPathandName, True)
oDoc.Close



'[ Place Component
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition



' Create Matrix
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
Call oMatrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(50, 50, 50), True)



'insert new occurence
'Dim oOcc As ComponentOccurrence
'oOcc = oAsmCompDef.Occurrences.Add( _
'oPathandName, oMatrix)

ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oPathandName)
ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyPlaceComponentCmd").Execute


'oOcc.Grounded = False
'ThisDoc.Document.SelectSet.Select(oOcc)

']

'MessageBox.Show("New file created: " & vbLf & oPathandName, "iLogic")
0 Likes
Message 28 of 42

JhoelForshav
Mentor
Mentor

Maybe change:

If oFileNameSplit(oFileNameSplit.Length - 1) = "MIR" Then

To

If oFileNameSplit(oFileNameSplit.Length - 1).Contains("MIR") Then

Because in your error message it seems like it's "_MIR1" and not "_MIR"...

Message 29 of 42

johan.degreef
Advisor
Advisor

@JhoelForshav 

Thank you It works,

but can't we better say if length of "oFilename is different from 15 characters" (PIPE_200211_000.ipt), because now it is MIR, but next time it could be something else. Now it is after, but next time it may be before "PIPE_200211_000"

 

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
0 Likes
Message 30 of 42

JhoelForshav
Mentor
Mentor

Like this? 🙂

GetInput :

Six_Digit = InputBox("Enter 6 Digit number", "iLogic", 000000)

If Six_Digit = "" Then
	Return
Else If Len(Six_Digit) <> "6" Then
MessageBox.Show("Input must be 6 digits", "ilogic")
GoTo GetInput

End If



Dim oDoc As Document
Dim sFilename As String

'hard code path
oLibrary_Folder = "C:\PTN_Part_Library\"


Dim oFilenames() As String
oFilenames = System.IO.Directory.GetFiles(oLibrary_Folder, _
"*.*", SearchOption.AllDirectories)

For Each oFilename As String In oFilenames
	Dim oFileNameNoExt As String = System.IO.Path.GetFileNameWithoutExtension(oFilename)
	If oFileNameNoExt.Contains(Six_Digit) AndAlso oFileNameNoExt.Length = 15 Then
		Dim oOptions As Inventor.NameValueMap
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		oDoc = ThisApplication.Documents.OpenWithOptions(oFilename, oOptions, False)
		sFilename = oFilename
		Exit For
	End If
Next

If sFilename = "" Then
	MessageBox.Show("No matching libary file found.", "iLogic")
	Return
End If


Dim iCounter As Integer = 0

'path from current file
Dim oActiveAssemblyfolder As String = ThisDoc.Path & "\"

'path from current project file ( *.ipj)
Dim oProjectfolder As String = _
ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath & "\"

Dim oProject_Filenames() As String
oProject_Filenames = System.IO.Directory.GetFiles(oProjectfolder, _
"*.*", SearchOption.AllDirectories)

'count existing project files					  
For Each oFilename As String In oProject_Filenames
	Dim oFileNameNoExt As String = System.IO.Path.GetFileNameWithoutExtension(oFilename)
	If oFileNameNoExt.Contains(Six_Digit) AndAlso oFileNameNoExt.Length = 15 Then
		Dim oFileNameSplit As String() = oFileNameNoExt.Split("_")
		Dim oNumber As Integer = CInt(oFileNameSplit(oFileNameSplit.Length - 1))
		If oNumber > iCounter Then iCounter = oNumber
	End If
Next

iCounter += 1



'find the postion of the last backslash in the path
Dim FNamePos As Integer = InStrRev(sFilename, "\", -1)
'get the file name with the file extension
Dim oName As String = Right(sFilename, Len(sFilename) -FNamePos)
'get the file name (without extension)
Dim ShortName As String = Left(oName, Len(oName) -4)
'get extension
Dim oExt As String = Right(oName, 4)



Dim oNewName As String = ShortName & "_" & iCounter.ToString("000")

Dim oPathandName As String = oActiveAssemblyfolder & oNewName & oExt



MsgBox(oPathandName)
'save new document
oDoc.SaveAs(oPathandName, True)
oDoc.Close



'[ Place Component
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition



' Create Matrix
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
Call oMatrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(50, 50, 50), True)



'insert new occurence
'Dim oOcc As ComponentOccurrence
'oOcc = oAsmCompDef.Occurrences.Add( _
'oPathandName, oMatrix)

ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oPathandName)
ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyPlaceComponentCmd").Execute


'oOcc.Grounded = False
'ThisDoc.Document.SelectSet.Select(oOcc)

']

'MessageBox.Show("New file created: " & vbLf & oPathandName, "iLogic")
0 Likes
Message 31 of 42

johan.degreef
Advisor
Advisor

I think so. it will grab more potential errors, no?

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
0 Likes
Message 32 of 42

JhoelForshav
Mentor
Mentor

Yes. You wouldn't get any error with the previous code though if the numbering was the last part of the filename. But if the last part of the filename wouldn't be the number and also not contain "MIR" it would throw an error.

0 Likes
Message 33 of 42

johan.degreef
Advisor
Advisor

@JhoelForshav 

It seems to work quiet well - many thanks.

Would you be so kind to add a line so that the new part's iproperty "Part Number" = variable "Shortname" while saving it?

ex. Partnumber = PIPE_200213

 

Imports System.IO
GetInput :

Six_Digit = InputBox("Enter 6 Digit number", "iLogic", 000000)

If Six_Digit = "" Then
	Return
Else If Len(Six_Digit) <> "6" Then
MessageBox.Show("Input must be 6 digits", "ilogic")
GoTo GetInput

End If



Dim oDoc As Document
Dim sFilename As String

'hard code path
oLibrary_Folder = "C:\PTN_Part_Library\"


Dim oFilenames() As String
oFilenames = System.IO.Directory.GetFiles(oLibrary_Folder, _
"*.*", SearchOption.AllDirectories)

For Each oFilename As String In oFilenames
	Dim oFileNameNoExt As String = System.IO.Path.GetFileNameWithoutExtension(oFilename)
	If oFileNameNoExt.Contains(Six_Digit) AndAlso oFileNameNoExt.Length = 11 Then
		Dim oOptions As Inventor.NameValueMap
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		oDoc = ThisApplication.Documents.OpenWithOptions(oFilename, oOptions, False)
		sFilename = oFilename
		Exit For
	End If
Next

If sFilename = "" Then
	MessageBox.Show("No matching libary file found.", "iLogic")
	Return
End If


Dim iCounter As Integer = 0

'path from current file
Dim oActiveAssemblyfolder As String = ThisDoc.Path & "\"

'path from current project file ( *.ipj)
Dim oProjectfolder As String = _
ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath & "\"

Dim oProject_Filenames() As String
oProject_Filenames = System.IO.Directory.GetFiles(oProjectfolder, _
"*.*", SearchOption.AllDirectories)

'count existing project files					  
For Each oFilename As String In oProject_Filenames
	Dim oFileNameNoExt As String = System.IO.Path.GetFileNameWithoutExtension(oFilename)
	If oFileNameNoExt.Contains(Six_Digit) AndAlso oFileNameNoExt.Length = 15 Then
		Dim oFileNameSplit As String() = oFileNameNoExt.Split("_")
		Dim oNumber As Integer = CInt(oFileNameSplit(oFileNameSplit.Length - 1))
		If oNumber > iCounter Then iCounter = oNumber
	End If
Next

iCounter += 1



'find the postion of the last backslash in the path
Dim FNamePos As Integer = InStrRev(sFilename, "\", -1)
'get the file name with the file extension
Dim oName As String = Right(sFilename, Len(sFilename) -FNamePos)
'get the file name (without extension)
Dim ShortName As String = Left(oName, Len(oName) -4)
'get extension
Dim oExt As String = Right(oName, 4)


Dim oNewName As String = ShortName & "_" & iCounter.ToString("000")

Dim oPathandName As String = oActiveAssemblyfolder & oNewName & oExt

MsgBox(oPathandName)
'save new document
oDoc.SaveAs(oPathandName, True)
oDoc.Close

'[ Place Component
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition



' Create Matrix
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
Call oMatrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(50, 50, 50), True)



'insert new occurence
'Dim oOcc As ComponentOccurrence
'oOcc = oAsmCompDef.Occurrences.Add( _
'oPathandName, oMatrix)

ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oPathandName)
ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyPlaceComponentCmd").Execute


'oOcc.Grounded = False
'ThisDoc.Document.SelectSet.Select(oOcc)

']

'MessageBox.Show("New file created: " & vbLf & oPathandName, "iLogic")

 

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
Message 34 of 42

JhoelForshav
Mentor
Mentor

Sure 🙂

GetInput :

Six_Digit = InputBox("Enter 6 Digit number", "iLogic", 000000)

If Six_Digit = "" Then
	Return
Else If Len(Six_Digit) <> "6" Then
MessageBox.Show("Input must be 6 digits", "ilogic")
GoTo GetInput

End If



Dim oDoc As Document
Dim sFilename As String

'hard code path
oLibrary_Folder = "C:\PTN_Part_Library\"


Dim oFilenames() As String
oFilenames = System.IO.Directory.GetFiles(oLibrary_Folder, _
"*.*", SearchOption.AllDirectories)

For Each oFilename As String In oFilenames
	Dim oFileNameNoExt As String = System.IO.Path.GetFileNameWithoutExtension(oFilename)
	If oFileNameNoExt.Contains(Six_Digit) AndAlso oFileNameNoExt.Length = 11 Then
		Dim oOptions As Inventor.NameValueMap
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		oDoc = ThisApplication.Documents.OpenWithOptions(oFilename, oOptions, False)
		sFilename = oFilename
		Exit For
	End If
Next

If sFilename = "" Then
	MessageBox.Show("No matching libary file found.", "iLogic")
	Return
End If


Dim iCounter As Integer = 0

'path from current file
Dim oActiveAssemblyfolder As String = ThisDoc.Path & "\"

'path from current project file ( *.ipj)
Dim oProjectfolder As String = _
ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath & "\"

Dim oProject_Filenames() As String
oProject_Filenames = System.IO.Directory.GetFiles(oProjectfolder, _
"*.*", SearchOption.AllDirectories)

'count existing project files					  
For Each oFilename As String In oProject_Filenames
	Dim oFileNameNoExt As String = System.IO.Path.GetFileNameWithoutExtension(oFilename)
	If oFileNameNoExt.Contains(Six_Digit) AndAlso oFileNameNoExt.Length = 15 Then
		Dim oFileNameSplit As String() = oFileNameNoExt.Split("_")
		Dim oNumber As Integer = CInt(oFileNameSplit(oFileNameSplit.Length - 1))
		If oNumber > iCounter Then iCounter = oNumber
	End If
Next

iCounter += 1



'find the postion of the last backslash in the path
Dim FNamePos As Integer = InStrRev(sFilename, "\", -1)
'get the file name with the file extension
Dim oName As String = Right(sFilename, Len(sFilename) -FNamePos)
'get the file name (without extension)
Dim ShortName As String = Left(oName, Len(oName) -4)
'get extension
Dim oExt As String = Right(oName, 4)


Dim oNewName As String = ShortName & "_" & iCounter.ToString("000")

Dim oPathandName As String = oActiveAssemblyfolder & oNewName & oExt

oDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = ShortName
'save new document
oDoc.SaveAs(oPathandName, True)
oDoc.Close

'[ Place Component
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition



' Create Matrix
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
Call oMatrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(50, 50, 50), True)



'insert new occurence
'Dim oOcc As ComponentOccurrence
'oOcc = oAsmCompDef.Occurrences.Add( _
'oPathandName, oMatrix)

ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oPathandName)
ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyPlaceComponentCmd").Execute


'oOcc.Grounded = False
'ThisDoc.Document.SelectSet.Select(oOcc)

']

'MessageBox.Show("New file created: " & vbLf & oPathandName, "iLogic")
Message 35 of 42

johan.degreef
Advisor
Advisor

Great Thank you!

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
0 Likes
Message 36 of 42

johan.degreef
Advisor
Advisor

@JhoelForshav 

You want to know what woke me up this morning? the idea we could make the code much easier and also more performant? You already helped me big time, so no worries I it is not possible in your time.

 

What if we would skip the whole icounter code to look for parts already in the projectfolder and add 1 to the icounter to become a unique name?

 

Just append date and time to the library part and done.

PIPE_200213.ipt would than become PIPE_200213_20200828141136.ipt

syntax

PIPE_200213_<currentyear><currentmonth><currentday><current24hour><currentminute><currentsecond>

 

and done. Uniqueness garantied, without looping through all the parts in my project, no?

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
0 Likes
Message 37 of 42

JhoelForshav
Mentor
Mentor

I think this will be enough then 🙂

 

GetInput :

Six_Digit = InputBox("Enter 6 Digit number", "iLogic", 000000)

If Six_Digit = "" Then
	Return
Else If Len(Six_Digit) <> "6" Then
MessageBox.Show("Input must be 6 digits", "ilogic")
GoTo GetInput

End If



Dim oDoc As Document
Dim sFilename As String

'hard code path
oLibrary_Folder = "C:\PTN_Part_Library\"


Dim oFilenames() As String
oFilenames = System.IO.Directory.GetFiles(oLibrary_Folder, _
"*.*", SearchOption.AllDirectories)

For Each oFilename As String In oFilenames
	Dim oFileNameNoExt As String = System.IO.Path.GetFileNameWithoutExtension(oFilename)
	If oFileNameNoExt.Contains(Six_Digit) AndAlso oFileNameNoExt.Length = 11 Then
		Dim oOptions As Inventor.NameValueMap
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		oDoc = ThisApplication.Documents.OpenWithOptions(oFilename, oOptions, False)
		sFilename = oFilename
		Exit For
	End If
Next

If sFilename = "" Then
	MessageBox.Show("No matching libary file found.", "iLogic")
	Return
End If


Dim iCounter As Integer = 0

'path from current file
Dim oActiveAssemblyfolder As String = ThisDoc.Path & "\"


'find the postion of the last backslash in the path
Dim FNamePos As Integer = InStrRev(sFilename, "\", -1)
'get the file name with the file extension
Dim oName As String = Right(sFilename, Len(sFilename) -FNamePos)
'get the file name (without extension)
Dim ShortName As String = Left(oName, Len(oName) -4)
'get extension
Dim oExt As String = Right(oName, 4)


Dim oNewName As String = ShortName & "_" & Now.Date.ToString

Dim oPathandName As String = oActiveAssemblyfolder & oNewName & oExt

oDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = ShortName
'save new document
oDoc.SaveAs(oPathandName, True)
oDoc.Close

'[ Place Component
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition



' Create Matrix
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
Call oMatrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(50, 50, 50), True)



'insert new occurence
'Dim oOcc As ComponentOccurrence
'oOcc = oAsmCompDef.Occurrences.Add( _
'oPathandName, oMatrix)

ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oPathandName)
ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyPlaceComponentCmd").Execute


'oOcc.Grounded = False
'ThisDoc.Document.SelectSet.Select(oOcc)

']

'MessageBox.Show("New file created: " & vbLf & oPathandName, "iLogic")
0 Likes
Message 38 of 42

johan.degreef
Advisor
Advisor

On my side I get error

 

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._DocumentClass.SaveAs(String FileName, Boolean SaveCopyAs)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
0 Likes
Message 39 of 42

JhoelForshav
Mentor
Mentor

How about now?

 

GetInput :

Six_Digit = InputBox("Enter 6 Digit number", "iLogic", 000000)

If Six_Digit = "" Then
	Return
Else If Len(Six_Digit) <> "6" Then
MessageBox.Show("Input must be 6 digits", "ilogic")
GoTo GetInput

End If



Dim oDoc As Document
Dim sFilename As String

'hard code path
oLibrary_Folder = "C:\PTN_Part_Library\"


Dim oFilenames() As String
oFilenames = System.IO.Directory.GetFiles(oLibrary_Folder, _
"*.*", SearchOption.AllDirectories)

For Each oFilename As String In oFilenames
	Dim oFileNameNoExt As String = System.IO.Path.GetFileNameWithoutExtension(oFilename)
	If oFileNameNoExt.Contains(Six_Digit) AndAlso oFileNameNoExt.Length = 11 Then
		Dim oOptions As Inventor.NameValueMap
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		oDoc = ThisApplication.Documents.OpenWithOptions(oFilename, oOptions, False)
		sFilename = oFilename
		Exit For
	End If
Next

If sFilename = "" Then
	MessageBox.Show("No matching libary file found.", "iLogic")
	Return
End If

'path from current file
Dim oActiveAssemblyfolder As String = ThisDoc.Path & "\"




'find the postion of the last backslash in the path
Dim FNamePos As Integer = InStrRev(sFilename, "\", -1)
'get the file name with the file extension
Dim oName As String = Right(sFilename, Len(sFilename) -FNamePos)
'get the file name (without extension)
Dim ShortName As String = Left(oName, Len(oName) -4)
'get extension
Dim oExt As String = Right(oName, 4)


Dim oNewName As String = ShortName & "_" & Microsoft.VisualBasic.DateAndTime.Now.Date.ToString("yyyyMMddHHmmss")

Dim oPathandName As String = oActiveAssemblyfolder & oNewName & oExt

oDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = ShortName
'save new document
oDoc.SaveAs(oPathandName, True)
oDoc.Close

'[ Place Component
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition



' Create Matrix
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
Call oMatrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(50, 50, 50), True)



'insert new occurence
'Dim oOcc As ComponentOccurrence
'oOcc = oAsmCompDef.Occurrences.Add( _
'oPathandName, oMatrix)

ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oPathandName)
ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyPlaceComponentCmd").Execute


'oOcc.Grounded = False
'ThisDoc.Document.SelectSet.Select(oOcc)

']

'MessageBox.Show("New file created: " & vbLf & oPathandName, "iLogic")
0 Likes
Message 40 of 42

johan.degreef
Advisor
Advisor

@JhoelForshav 

It works, but the result is PIPE_200213_20200828000000.ipt

Date is OK, but there's something wrong with the time

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
0 Likes