Subs and functions

Subs and functions

blandb
Mentor Mentor
1,221 Views
11 Replies
Message 1 of 12

Subs and functions

blandb
Mentor
Mentor

Hello all,

I am aware of what Subs and Functions are, but not sure how to make this work like I'm intending, and I may totally have all this backwards as well. What I am trying to do is... I have a series of nested select cases that are happening for instance: Choose a size, choose a thickness, then choose a finish. Then inside of this, I have set parameters to define what components part numbers I need from CC to add for this particular selection. Then just below that, I thought I would call a function that is essentially a repeat set of instructions for placing the defined part numbers from CC, patterning, etc.

 

Once done, I may need to do this again, but for a different size and for a different finish. So, instead of copying and pasting a lot of redundant code for each finish and size, I thought...hmm Just use a function. Have the select case define what part numbers to use, then that will call the function and pass the part numbers to the function and then based on that it would run the function and place the components with the part numbers I need from CC that we set in the select case. I just don't know how to make this happen. I was hoping to just define part numbers in the select case and then the code for each size/finish is the same, just changing what parts are being used. Below is an example:

 

Sub main
	Select Case Size
		Case "10"
			Select Case Thickness
				Case "1/8"
					Select Case Finish
						Case "Plain"
							Dim part_1 = "123"
							Dim part_2 = "456"
							Dim part_3 = "789"
							Call My_Function
						Case "Galvanized"
Dim part_1 = "111"
Dim part_2 = "222"
Dim part_3 = "333"
Call My_Function Case "Painted" End Select Case "1/4" End Select Case "20" Case "30" End Select End Sub Function My_Function Components.AddContentCenterPart("", "whatever CC info here", part_1) Components.AddContentCenterPart("", "whatever CC info here", part_2) Components.AddContentCenterPart("", "whatever CC info here", part_3) End Function 

 

Autodesk Certified Professional
0 Likes
Accepted solutions (3)
1,222 Views
11 Replies
Replies (11)
Message 2 of 12

Michael.Navara
Advisor
Advisor
Accepted solution

If you want to use part_1, part_2, etc in the function, you need to pass them as argument. Parameter type should be a string array which can contain as many part numbers as you want.

Also you can optimize your code with custom defined class. In my example I define class which contains variables for all filters you want (3 in this case) and string array for part numbers. Then you don't need your scary nested switch and you can replace them with single For Each loop and single If with three conditions. If the condition is met (It equals to three Select Case conditions)  you can pass part numbers as a single argument to the method MyFunction and do something useful with them.

If you want to add next size for example, only add new item to the partsCollection list at the begining of the Main method.

 

Sub Main
	Dim partsCollections = New List(Of PartsCollection)
	partsCollections.Add(New PartsCollection("10", "1/8", "Plain", {"123", "456", "789" }))
	partsCollections.Add(New PartsCollection("10", "1/8", "Galvanized", {"111", "222", "333" }))
	'...
	'Add next part collections as you want

	'Get parameters from anywhere
	Dim size = "10"
	Dim thickness = "1/8"
	Dim finish = "Plain"

	'Look for appropriate parts collection
	For Each partsCollection As PartsCollection In partsCollections
		If PartsCollection.Size = size And
			PartsCollection.Thickness = thickness And
			PartsCollection.Finish = finish Then

			'Do something useful with part numbers
			MyFunction(PartsCollection.PartNumbers)
		End If
	Next
End Sub

Private Sub MyFunction(partNumbers As String())
	For Each partNumber As String In partNumbers
		Logger.Debug(partNumber)
	Next
End Sub


Class PartsCollection
	Public Size As String
	Public Thickness As String
	Public Finish As String

	Public PartNumbers As String()

	Sub New(size As String, thickness As String, finish As String, partNumbers As String())
		Me.Size = size
		Me.Thickness = thickness
		Me.Finish = finish
		Me.PartNumbers = partNumbers
	End Sub
End Class

 

Message 3 of 12

blandb
Mentor
Mentor

Thanks for the reply. If I am following you, I fill out parts collection as below. This is just one size and the options. I now need to replicate this grouping 18 more times?


partsCollections.Add(New PartsCollection("80", "1", "Plain", {"xxx", "xxx", "xxx" }))
partsCollections.Add(New PartsCollection("80", "1", "Paint", {"xxx", "xxx", "xxx" }))
partsCollections.Add(New PartsCollection("80", "1", "SS", {"xxx", "xxx", "xxx" }))
partsCollections.Add(New PartsCollection("80", "2", "Plain", {"xxx", "xxx", "xxx" }))
partsCollections.Add(New PartsCollection("80", "2", "Paint", {"xxx", "xxx", "xxx" }))
partsCollections.Add(New PartsCollection("80", "2", "SS", {"xxx", "xxx", "xxx" }))
partsCollections.Add(New PartsCollection("80", "3", "Plain", {"xxx", "xxx", "xxx" }))
partsCollections.Add(New PartsCollection("80", "3", "Paint", {"xxx", "xxx", "xxx" }))
partsCollections.Add(New PartsCollection("80", "3", "SS", {"xxx", "xxx", "xxx" }))

 

Also, In your example, I don't see where what you have done is populating anything that was filled out with the add content center items as I had shown? 

Autodesk Certified Professional
0 Likes
Message 4 of 12

Frederick_Law
Mentor
Mentor

From your code, you don't know the different between sub and function.

You declared a function and use it like a sub.

 

sub run the code and done.

https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures...

 

function run the code and return something. 

https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures...

 

 

0 Likes
Message 5 of 12

Frederick_Law
Mentor
Mentor
Accepted solution

My interpretation of you code:

 

Sub main
	Select Case Size
		Case "10"
			Select Case Thickness
				Case "1/8"
					Select Case Finish
						Case "Plain"
							Dim part_1 = "123"
							Dim part_2 = "456"
							Dim part_3 = "789"
						Case "Galvanized"
                            Dim part_1 = "111"
                            Dim part_2 = "222"
                            Dim part_3 = "333"
						Case "Painted"
					End Select
				Case "1/4"
			End Select
		Case "20"
		Case "30"
	End Select
Call My_Sub (Size, Thickness, Finish, part_1)
Call My_Sub (Size, Thickness, Finish, part_2)
Call My_Sub (Size, Thickness, Finish, part_3)
End Sub


sub My_Sub (Size, Thickness, Finish, partnumber as String)
	Components.AddContentCenterPart("", "whatever CC info here", partnumber)
End sub

 

 

Updated.

0 Likes
Message 6 of 12

blandb
Mentor
Mentor

As mentioned earlier, I could be way off base lol. As I was under the interpretation that a sub was just the main focus of the code, and a function was some other separate routine that was called when needed.

 

Also, after re-reading this, I realized that I didn't really specify what the "123", "456" represent, but that would be the part number I am specifying. So there for the Dim part_1 = "123" would just be me entering what the PN should be. Then somehow that number gets trickled down to where the content center utilizes that number instead of the temp placeholder I have in there called partnumber in the components.add line.

Autodesk Certified Professional
0 Likes
Message 7 of 12

Frederick_Law
Mentor
Mentor

Instead of Dim part_1, you can do:

Call My_Sub (Size, Thickness, Finish, "123")
Call My_Sub (Size, Thickness, Finish, "456")
Call My_Sub (Size, Thickness, Finish, "789")

 

If you look at CC, part number could be a combine of Size, Thickness, Finish:

(Size & "-" & Thickness & "-" & Finish)

"10-1/8-Plain"

This way you can skip all those Case statements.

0 Likes
Message 8 of 12

Michael.Navara
Advisor
Advisor

Yes, you need to add one line for each case and specify which part numbers are utilized in this case.

Sorry I don't mention how to use part number for CC. I only log the input part number to the iLogic log console (line 27). You can implement whatever you want to do in this place. 

 

Also you can pass whole PartsCollection as an argument and you can use it's properties (Size, Thickness, Finish and PartNumbers) for your process of inserting CC parts.

 

In attachment is complete sample how to insert sccrew from CC using my code sample

0 Likes
Message 9 of 12

Curtis_Waguespack
Consultant
Consultant
Accepted solution

@blandb 

 

see tips 4 and 5 for examples of how we pass info from subs and functions:

https://static.au-uw2-prd.autodesk.com/Class_Presentation_MFG501293_ClassPresentation-MFG501293-Wagu...

 

Recall that subs and functions are pretty much the same, but functions just pass information back up to the line from which the function was called. Subs don't pass info back.

 

I don't quite follow what your data looks like, but it seems like it would be something like the table below. So I'm not sure the nested select case statements offer much value.

 

If the data does look something like this table, then I think @Michael.Navara approach makes sense.

Here is another example using that general approach:

https://forums.autodesk.com/t5/inventor-forum/ilogic-code-help-with-lookup-tables/td-p/2891822

 

Or you could use something like the example at this link, if it makes more sense to you:

https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-look-up-table-without-excel/td-p/1...

 

 

Curtis_Waguespack_0-1699468449007.png

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 



EESignature

Message 10 of 12

blandb
Mentor
Mentor

Thanks all for your time and efforts. I was able to take some items from each of you, and clarifications to get me moving forward. thanks.

Autodesk Certified Professional
0 Likes
Message 11 of 12

Frederick_Law
Mentor
Mentor

"7 blind person and an elephant"  😎 

0 Likes
Message 12 of 12

Frederick_Law
Mentor
Mentor

sub can sent and get Arguments using byval and byref.

This way sub can get more then one Argument.

 

https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures...

0 Likes