Error: Argument Length Must be Greater than or Equal to Zero

Error: Argument Length Must be Greater than or Equal to Zero

aurel_e
Collaborator Collaborator
3,397 Views
9 Replies
Message 1 of 10

Error: Argument Length Must be Greater than or Equal to Zero

aurel_e
Collaborator
Collaborator

Hi all,

 

I have the Ilogic rule below that is working fine in my PC but not in my colleague's one (the same Inventor 2021.3.3 version). I think it is because different versions of Visual Basics or something similar.

I wonder if someone can help me to change the rule to avoid it. The rule is used to add a prefix to the Part numbers of all the components of the assembly.

Sub Main ()

RUsure = MessageBox.Show("This will add the LFE No Prefix to the Part Numbers" _
& vbLf & "" _ 
&         vbLf & "Are you sure you want to do it?", "ADD LFE NO PREFIX ",MessageBoxButtons.YesNo)

If RUsure = vbNo Then
Return
Else
End If

' Get the active assembly.
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
' Get the assembly component definition.
Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
' Get all of the leaf occurrences of the assembly.
Dim oLeafOccs As ComponentOccurrencesEnumerator = oAsmDef.Occurrences.AllLeafOccurrences
ThisApplication.SilentOperation = True
' Iterate through the occurrences and print the name.
Dim sPrefix As String
sPrefix = InputBox ("LFE NO:","Set PN surfix", "######")
Dim oOcc As ComponentOccurrence
For Each oOcc In oLeafOccs
Dim oDoc As PartDocument 


'find colon SeparatorPosistion
Dim SeparatorPos As Long
SeparatorPos = InStrRev(oOcc.Name, ":",-1)

'define part number prefix   

'set part number for each component
iProperties.Value(oOcc.Name, "Project", "Part Number") = "LFE"&sPrefix & " - " & Left(oOcc.Name,SeparatorPos -1)

Next
End Sub

 

These are the messages I am getting:

 

2022-01-12 08_15_23-2022-01-12 08_04_41-.png ‎- Photos.png2022-01-12 08_14_22-2022-01-12 08_04_18-Autodesk Inventor Professional 2021.png ‎- Photos.png

0 Likes
Accepted solutions (1)
3,398 Views
9 Replies
Replies (9)
Message 2 of 10

fidel.makatiaD5W7V
Alumni
Alumni

Hi @aurel_e have you tried to update the VBA version of your colleagues PC? 



Fidel Makatia
Developer Advocate

href=https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=GUID-0BD48573-7193-4285-87B7-6727555D053E rel= "noopener noreferrer">Inventor 2022 Documentation |
0 Likes
Message 3 of 10

aurel_e
Collaborator
Collaborator

Hi @fidel.makatiaD5W7V 

Thanks for replying.

I don't really know how to do it.

And it looks we have got the same version: 1069

0 Likes
Message 4 of 10

fidel.makatiaD5W7V
Alumni
Alumni

HI @aurel_e are you using VBA editor or Visual studio?



Fidel Makatia
Developer Advocate

href=https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=GUID-0BD48573-7193-4285-87B7-6727555D053E rel= "noopener noreferrer">Inventor 2022 Documentation |
0 Likes
Message 5 of 10

aurel_e
Collaborator
Collaborator
I have got Visual Studio installed in my PC.
The VBA I think comes with Inventor.
For what I understand, the Ilogic don't use them. Correct?
0 Likes
Message 6 of 10

fidel.makatiaD5W7V
Alumni
Alumni

yes sorry @aurel_e I now understand your question. I thought you are using VBA editor or VB.NET in Visual studio. Getting back . Let me run the code in my inventor. Sorry for that 



Fidel Makatia
Developer Advocate

href=https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=GUID-0BD48573-7193-4285-87B7-6727555D053E rel= "noopener noreferrer">Inventor 2022 Documentation |
0 Likes
Message 7 of 10

WCrihfield
Mentor
Mentor

Hi @aurel_e.  I could tell you were using iLogic, because if you were using VBA you would have to use the keyword 'Set' when assigning values to variables, and for the most part can't assign a value to a variable in the same line as creating the variable.  And not using straight VB.NET, because you are using the iProperties.Value() line, which is defined within the iLogic AddIn.

 

It looks to me like this line:

SeparatorPos = InStrRev(oOcc.Name, ":",-1)

must be either returning a value of zero or -1, which would cause a problem in this bit of code, near the end of your loop code:

Left(oOcc.Name,SeparatorPos -1)

...because you are using that variable within as the 'Length' (number of characters), then subtracting 1 from its numerical value, as the number of characters to return from the oOcc.Name.  That is what is causing the error.

 

So, I'm thinking that maybe there is a component somewhere in that loop who's name does not contain that ":" character.  If that were so, it seems to me like that could be causing this problem to occur.

Another way to get the first part of a String which may contain that ":" character, that may be less complicated would be something like this:

oName = oOcc.Name.Split(":")(0)

What that does is split the name up into possibly multiple Strings (as an Array of String), then gets the first Item (a String) in that array.  But that too might fail, if that character is not found within the name, so you might need to use something like a Try...Catch block of code to try betting just the first part of the name that way, and if that fails, then in the Catch part, just get the name normally, without trying to filter anything out.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 10

aurel_e
Collaborator
Collaborator

Hi @WCrihfield 

 

I am trying to understand what you are suggesting, but I don't know much about the Ilogic.

I think you are right. There was a part without ":" I still can't understand why on my Pc it was ignoring that and on the other Pc was stopping. 

Below is my code. It is working on my Pc as it is. I can't access the other Pc for the moment. 

I don't know how to use that Try...Catch...End try though.

Also I can see the line 

oName = oOcc.Name.Split(":")(0)

is splitting the name in 2 parts. I'm curious: what tells it to pick the first (before the ":") and not the second part (after:) of the split? What does that (0) mean?

 

Thanks.

 

 

 

Sub Main ()

RUsure = MessageBox.Show("This will add the LFE No Prefix to the Part Numbers" _
& vbLf & "" _ 
&         vbLf & "Are you sure you want to do it?", "ADD LFE NO PREFIX ",MessageBoxButtons.YesNo)

If RUsure = vbNo Then
Return
Else
End If

Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument

Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition

Dim oLeafOccs As ComponentOccurrencesEnumerator = oAsmDef.Occurrences.AllLeafOccurrences
ThisApplication.SilentOperation = True

Dim sPrefix As String
sPrefix = InputBox ("LFE NO:","Set PN surfix", "######")
Dim oOcc As ComponentOccurrence
For Each oOcc In oLeafOccs
Dim oDoc As PartDocument 

oName = oOcc.Name.Split(":")(0)

iProperties.Value(oOcc.Name, "Project", "Part Number") = "LFE"&sPrefix & " - " & oName

Next
End Sub

 

 

 

0 Likes
Message 9 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Hi @aurel_e.  Sorry for the delay.  I'm fairly busy today.  Below is your code that I have added a Try...Catch statement into.  On the next line, right after the 'Try' keyword, you can try to do something that you expect might fail sometimes.  Then on the next line after the 'Catch' keyword, is where you can put some alternate code to execute, and that code will only be ran if the code within the Try part fails.  There is a lot more to it, but those are the basics.  In this case, because we know that there is one component that does not have ":" in its name, we know that this test will fail at least once, so to handle that specific situation, when that failure happens, it will then attempt to just get the regular component name, without trying to get just the left side of it.  Then it moves on to your iProperties line.

 

As for the Split method...  The (0) at the end is returning the first element within the resulting Array of String that the Split method creates.  An Array is zero based, which means its first element is Item(0), instead of Item(1), but we don't use the Item() call-out when working with Array's, just the (#) at the end of the variable, to specify which Index of element within we are working with.

 

Sub Main
	RUsure = MessageBox.Show("This will add the LFE No Prefix to the Part Numbers" _
	& vbLf & "" _ 
	& vbLf & "Are you sure you want to do it?", "ADD LFE NO PREFIX ",MessageBoxButtons.YesNo)

	If RUsure = vbNo Then Return

	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oLeafOccs As ComponentOccurrencesEnumerator = oAsmDef.Occurrences.AllLeafOccurrences
	'when you turn this on, you should also turn it back off when done
	ThisApplication.SilentOperation = True

	Dim sPrefix As String = InputBox ("LFE NO:","Set PN surfix", "######")
	Dim oOcc As ComponentOccurrence
	For Each oOcc In oLeafOccs
		Dim oName As String
		Try
			oName = oOcc.Name.Split(":")(0)
		Catch
			oName = oOcc.Name
		End Try
		
		iProperties.Value(oOcc.Name, "Project", "Part Number") = "LFE"&sPrefix & " - " & oName
	Next
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 10 of 10

aurel_e
Collaborator
Collaborator

Hi @WCrihfield 

 

it works on the other Pc as well.

Thank you for taking the time to explain it to me.

0 Likes