Alternative way to replace if then statement

Alternative way to replace if then statement

JohnBoySarcia
Advocate Advocate
623 Views
8 Replies
Message 1 of 9

Alternative way to replace if then statement

JohnBoySarcia
Advocate
Advocate

Hi, Good day.

 

May I ask if there's a better way rather than using If then else if statement on this example. Although those statement works fine but we believe it's not the right way to do it since it makes processing slower

Example

I have current parameters row1A to row10A which has precomputed values (all numerical unit)

I have a selection dropdown list for Pattern (W, X, Y, Z) in text unit

Pattern should be treated as either Z or not Z, since W, X, Y are act as the same

row1A to row10A if less than or equal to 0 should be resulted to 0

Conditions on current If, elseif, then example

This is the current code using If, then statement

If row2A=row1A and pattern <> Z Then

row2A=row1A-1

Elseif row1A <>row2A And row1>row2 And pattern = Z Then

row2A=row1-2

Elseif row1A <>row2A And row1<row2 And pattern = Z Then

row2A=row1A

Elseif row2A <=0 Then

row2A = 0

Else

End if

 

Same goes on other rows. Looking if possible to use looping command. Thanks by showing the actual code if provided as I'm don't have programming knowledge, only as inventor user. 

0 Likes
Accepted solutions (1)
624 Views
8 Replies
Replies (8)
Message 2 of 9

Andrii_Humeniuk
Advisor
Advisor

Hi @JohnBoySarcia . In the case when you need to perform several checks in one condition ("If row2A = row1A And pattern <> Z Then"), the if-then-else construction is the most optimal. I modified your code a bit, it seems to be more logical.

If row2A = row1A And pattern <> Z Then
	row2A = row1A - 1
Else If row1A <> row2A And pattern = Z Then
	If row1 > row2 Then
		row2A = row1 - 2
	Else If row1 <= row2 Then
		row2A = row1A
	End If
Else If row2A < 0 Then
	row2A = 0
End If

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 3 of 9

JohnBoySarcia
Advocate
Advocate

Yes, that's only an example and already works fine on my side. On my example it's only ten rows but it can be twenty-five rows or even more. Using that code would need to repeat on each row

0 Likes
Message 4 of 9

Frederick_Law
Mentor
Mentor
0 Likes
Message 5 of 9

JohnBoySarcia
Advocate
Advocate

Thanks for the reply. I have used this case on ranges, If I'm dealing only on row one with several condition, I apply this. My problem is how it was looped to do row2 to row10 and give the corresponding result per row. Would you mind giving a sample using parameters I've mentioned above. Thanks

0 Likes
Message 6 of 9

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @JohnBoySarcia 

 

I think you could do something like this.

 

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

 

For i = 1 To 9

	row_1 = Parameter("row" & i & "A")
	row_2 = Parameter("row" & i + 1 & "A")

	If Pattern = "Z" Then
		If row_1 > row_2 Then
			row_2 = row_1 - 2
		ElseIf row_1 <= row_2 Then
			row_2 = row_1
		End If
	Else ' Pattern <> Z 
		If row_2 = row_1 Then row_2 = row_1 - 1
	End If

	If row_2 < 0 Then row_2 = 0

	Parameter("row" & i & "A") = row_1
	Parameter("row" & i + 1 & "A") = row_2

Next

 

EESignature

0 Likes
Message 7 of 9

JohnBoySarcia
Advocate
Advocate

Thank you very much for this sample. It works in ten rows excellently. However, I've encountered a problem when populating it up to 25 rows as when I run for i = 1 to 13 it gets an error on line row14A = Parameter("row"&i+13&"A") Making it read as row27A. Also, the program doesn't stop when let say at row9 the value is zero then the next program will stop

0 Likes
Message 8 of 9

Curtis_Waguespack
Consultant
Consultant

Hi @JohnBoySarcia 

 

@JohnBoySarcia wrote:

 when populating it up to 25 rows as when I run for i = 1 to 13 it gets an error on line row14A = Parameter("row"&i+13&"A") Making it read as row27A.

 

If the number of rows is 25, the only adjustment to the example would be the first line:

For i = 1 To 24

 

The for loop is incrementing the variable called "i" by one with each loop.

 

So for the first loop i= 1, so line 3 and 4 would return the following:

 row_1 = Parameter("row" & i & "A") , would return in Parameter("row1A")

 row_2 = Parameter("row" & i+1 & "A") , would return in Parameter("row2A")

 

For the 2nd loop i = 2, so line 3 and 4 would return the following:

 row_1 = Parameter("row" & i & "A") , would return in Parameter("row2A")

 row_2 = Parameter("row" & i+1 & "A") , would return in Parameter("row3A")

 

For the 13th loop i = 13, so line 3 and 4 would return the following:

 row_1 = Parameter("row" & i & "A") , would return in Parameter("row13A")

 row_2 = Parameter("row" & i+1 & "A") , would return in Parameter("row14A")

 

 

@JohnBoySarcia wrote:

Also, the program doesn't stop when let say at row9 the value is zero then the next program will stop


I'm not sure I understand this well enough to incorporate that into the logic, but you can use an Exit For to exit out of the For loop.

 

oMax = 15
For i = 1 To 10	
	x = i * 3	
	If x > oMax Then 
		MsgBox("x exceeded max of " & oMax,, "i is " & i)
		Exit For
	End If
Next

 

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

EESignature

0 Likes
Message 9 of 9

JohnBoySarcia
Advocate
Advocate

Although the previously requested already accept the solution and works good on my side. I have a problem with this program that will overwrite a specific row into a new value. This is the program

Dim requiredHoles As Integer = 322
Dim rows() As Integer = {32, 32, 30, 30, 30, 28, 28, 26, 26, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0, 0, 0}
Dim totalHoles As Integer = 0
Dim i As Integer = 0

While totalHoles < requiredHoles AndAlso i < rows.Length
	totalHoles += rows(i)
	i += 1
End While

If totalHoles > requiredHoles Then
	rows(i - 1) -= (totalHoles - requiredHoles)
End If

For j As Integer = i To rows.Length - 1
	rows(j) = 0
Next

For j As Integer = 0 To rows.Length - 1
	rows(j) = Rows(j)
	Next

.The result of this one is 32, 32, 30,30,30,28,28,26,26,26,24,10,0,0,0,0,0,..... which is correct but it doesn't replace the current values on rows parameters. Any lacking code on the program? Thanks

0 Likes