Text after string

Text after string

madstrolle
Enthusiast Enthusiast
743 Views
11 Replies
Message 1 of 12

Text after string

madstrolle
Enthusiast
Enthusiast

Hey out there.

 

The picture underneath shows an earliere problem i've posted. It was solved with the code i have posted under the picture (thanks to @JelteDeJong )

 

Here comes my issue:

I have triggered the code to run after save. But unfortunately this means that all the files that i save will get the "_XX" string in the end. 

When I save an assembly it puts "_" after all files. 

Is it possible to change the code, so if there's no "_" after the file, it should be left as it is?

 

E.g

39836_C17 -> code should run.

39836         -> code should NOT run.

 

I have tried to figure it out myself, without any luck ☹️

 

madstrolle_1-1694168439847.png

 

Code:

Dim partnumber As String = iProperties.Value("Project", "Part Number")
Dim parts = partnumber.Split("_")
Dim lastPart = parts(parts.Count() -1)

Dim oldDescription As String = iProperties.Value("Project", "Description")
Dim descriptionParts = oldDescription.Split("_")
Dim descriptionFirstPart = descriptionParts(0)

Dim newDescription = String.Format("{0}_{1}",descriptionFirstPart,lastPart) 
iProperties.Value("Project", "Description") = newDescription

 

0 Likes
Accepted solutions (2)
744 Views
11 Replies
Replies (11)
Message 2 of 12

Michael.Navara
Advisor
Advisor

Edit the last but one line to 

Dim newDescription = If (String.IsNullOrEmpty(lastPart), descriptionFirstPart, String.Join("_", descriptionFirstPart, lastPart))

 

Message 3 of 12

madstrolle
Enthusiast
Enthusiast

Hi @Michael.Navara ,

Thank you very much for your help.

Unfortunately, it does'nt seem to work.

Have I done it correct?

 

Dim partnumber As String = iProperties.Value("Project", "Part Number")
Dim parts = partnumber.Split("_")
Dim lastPart = parts(parts.Count() -1)

Dim oldDescription As String = iProperties.Value("Project", "Description")
Dim descriptionParts = oldDescription.Split("_")
Dim descriptionFirstPart = descriptionParts(0)

Dim newDescription = If (String.IsNullOrEmpty(lastPart), descriptionFirstPart, String.Join("_", descriptionFirstPart, lastPart))
iProperties.Value("Project", "Description") = newDescription

 

 

0 Likes
Message 4 of 12

WCrihfield
Mentor
Mentor

Just another variation of that type of code you could try out.

Dim sPN As String = iProperties.Value("Project", "Part Number")
If sPN.Contains("_") Then
	Dim sLastPart As String = sPN.Split("_").Last
	If sLastPart = "" Then Exit Sub 'or Return, nothing to copy
	Dim sDesc As String = iProperties.Value("Project", "Description")
	If sDesc.EndsWith("_" & sLastPart) Then Exit Sub 'or Return, do not need to change it
	If sDesc.Contains("_") Then
		sDesc = sDesc.Split("_").First
		iProperties.Value("Project", "Description") = sDesc & "_" & sLastPart
	End If
End If

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 12

madstrolle
Enthusiast
Enthusiast

Hi @WCrihfield and @Michael.Navara ,

 

First of all, thank you for your time 😊

Unfortunately it's still not working.

I have tried to combine and/or merge the codes, but it's just not working.

 

Just to show you:

 

If there is "_10" after the text string it works. (picture 1)

If there is nothing after the text string, it copy/paste the whole "Part Number" (Picture 2)

 

madstrolle_1-1695104651095.png

 

madstrolle_0-1695104022639.png

 

 

0 Likes
Message 6 of 12

WCrihfield
Mentor
Mentor
Accepted solution

Hi @madstrolle.  The last code I posted would not do anything if there was not a "_" character present in the value of the Part Number.  Then, if there was an "_" present in the value of the Part Number, but there was nothing after that character, it would not go any further, because it checks to see if anything was retrieved from after the "_" character in the next line, and if it was an empty String, it exits the rule (or Sub routine).  Then, if it was not an empty String, it proceeds to get the current value of the Description iProperty.  Then it check if that already 'EndsWith' an "_" character, followed by the value that was after that in the Part Number...and if it does, it exits the rule, instead of adding that stuff to the end the Description again.  Then it checks if the Description contains the "_" character, and only if it does, it continues to get the portion of the Description that is before that character.  However, my code does not do anything if the Description did not already have that character, which was a design flaw in my code example.  That 'If' statement needed to include handling the 'Else' condition for when that was not the case, so it would go ahead and add that "_" character and following text from the Part Number to the end of the Description, like the following.

Else
	iProperties.Value("Project", "Description") = sDesc & "_" & sLastPart

Here is an updated version of my previous code, which includes that extra 'Else' condition.

Dim sPN As String = iProperties.Value("Project", "Part Number")
If sPN.Contains("_") Then
	Dim sLastPart As String = sPN.Split("_").Last
	If sLastPart <> "" Then
		Dim sDesc As String = iProperties.Value("Project", "Description")
		If sDesc.EndsWith("_" & sLastPart) = False Then
			If sDesc.Contains("_") Then
				sDesc = sDesc.Split("_").First
				iProperties.Value("Project", "Description") = sDesc & "_" & sLastPart
			Else
				iProperties.Value("Project", "Description") = sDesc & "_" & sLastPart
			End If
		End If
	End If
End If

I hope this correction will fix the problems you were having.  By the way, I also restructured the code a bit to not use the 'Exit Sub' phrase, just in case this block of code is being used within a larger iLogic rule, or something like that.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 12

madstrolle
Enthusiast
Enthusiast

Hi @WCrihfield ,

 It works, thanks😊

 

And thank you for the explanation👍

 

 

0 Likes
Message 8 of 12

madstrolle
Enthusiast
Enthusiast

Dear Mr. @WCrihfield ,

 

I have one extra small thing🙃 

It just came around that we are using these partnumbers also eg. 41787_W02_01.

And every time we use these partnumbers, it should also not do anything.

As you can see in the picture it puts the 01 after "Plate". 

Is there a way to add some code, that tells it to do nothing when the phrase _W02_01 is at the end?

Partnumber is always XXXXX_WXX_XX, so it could also be like this eg. 42155_W24_05

I have tried different things, but i'm not a programmer, so i'm just trying stuff😊

And again thanks a lot.

 

madstrolle_1-1695624935247.png

 

 

0 Likes
Message 9 of 12

WCrihfield
Mentor
Mentor

Hi @madstrolle.  Could we simply test if the Part Number's value contains two of the "_" characters, and if so, do nothing?  That would be easy if we split this line:

Dim sLastPart As String = sPN.Split("_").Last

...up into multiple parts.  For instance, we could could set sPN.Split("_") as the value of another variable (like oPNParts).  That variable would then hold a String Array [String()].  Then we can check how many entries it has using oPNParts.Length.  If the Array's length is greater than 2, then we know that the Part Number had at least 2 of those characters in it.  You can either check if its length is greater than 2 (> 2) then if it is, use Exit Sub or Return to exit the rule ; Or you can choose to only move forward if the array length = 2, without immediately exiting the rule.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 10 of 12

madstrolle
Enthusiast
Enthusiast

Hi @WCrihfield ,

 

I have tried to change the code in relation to your explanation.

Unfortunately i'm not getting anywhere. It's not working.

So before I pull all my hair out 😄 and it's not to much trouble, i would like to ask for a lifeline 😊

Thanks.

0 Likes
Message 11 of 12

WCrihfield
Mentor
Mentor
Accepted solution

Sure, no problem.  I left the one line in there, but left it commented out, just so you can see the alternate route I mentioned.  I use that tactic a lot to avoid starting another 'If statement' that I will have to close-out later with another 'End If', when I can so easily avoid it.  It is efficient, but not ideal for every situation.  But I left the new If statement in there, and uncommented for you also.

 

Dim sPN As String = iProperties.Value("Project", "Part Number")
If sPN.Contains("_") Then
	Dim oPNParts() As String = sPN.Split("_")
	'If oPNParts.Length > 2 Then Exit Sub 'avoids extra level of If statements
	If oPNParts.Length = 2 Then
		Dim sLastPart As String = oPNParts.Last
		If sLastPart <> "" Then
			Dim sDesc As String = iProperties.Value("Project", "Description")
			If sDesc.EndsWith("_" & sLastPart) = False Then
				If sDesc.Contains("_") Then
					sDesc = sDesc.Split("_").First
					iProperties.Value("Project", "Description") = sDesc & "_" & sLastPart
				Else
					iProperties.Value("Project", "Description") = sDesc & "_" & sLastPart
				End If
			End If
		End If
	End If
End If

 

When the Part Number's value String contains the "_" character, and then it is 'split' using the "_" character, you get an array of Strings that were before and/or after those characters, but that character will not be present in any of those sub Strings.  So, if there was just one "_" present, and it had text before and after it, the array would have 2 strings in it.  If the part number had 2 "_" characters in it, and there was text before and after every one of them, then the array would have 3 strings in it.  Does that make sense?  There are other ways to check how many "_" characters are present in the part number, but I just stuck with the Split method for now, since we were already using it here.  I hope this works OK for you. 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 12 of 12

madstrolle
Enthusiast
Enthusiast

Hi @WCrihfield ,

Thank you very much!

It's working perfect😊👍

0 Likes