Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

converting text string into a number

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
Anonymous
8792 Views, 8 Replies

converting text string into a number

Morning All,

I have a custom iProperty with a text value of "Plate, Steel 140 mm". Is it possible to strip out the 140 (text) from the string and convert it to a number?

 

Thanks,

 

Mike Stalter

8 REPLIES 8
Message 2 of 9
Anonymous
in reply to: Anonymous

Hi Mike,

 

If you know where the numbers in the string will be you can use the Mid(yourstring,14,3) function to get the characters then use CDbl(yourstringhere) function to convert it into a Double (numeric value).....(take a look in iLogic under the 'Strings' section, theres some useful snippets in there)

 

Example :

 

test = Mid(iProperties.Value("Custom", "CUSTOMPROP"),14,3)
your_numerical_value = CDbl(test)

That will extract the 14th, 15th and 16th character in from the left (this is defined by the "14,3" in the mid function) and convert it to a Double.

 

 

If you can't guarantee the position of the numbers in the string you can use some code like this :

 

Dim test As String = iProperties.Value("Custom", "CUSTOMPROP")
Dim test2 As String = "" 'create an empty string to pass the digits into

Dim CharArray() As Char = test.ToCharArray() 'split the string into characters and pass into an array
For Each chara As Char In CharArray
     If Char.IsDigit(chara) Then
	 	test2 = test2 & chara 'look at each character in the array, if it is a digit then concatenate it into the test2 string
     End If
Next

yourparam = CDbl(test2)

 

This chunk of code will take your string, break it into characters and pass each character into an array, check each character in the array to see if it is a digit and if it is concatenate it in a new string.

 

Because we know the new string only contains digits, we can use the CDbl(yourstringhere) function to convert it into a Double.

 

Maybe the second option is a bit of overkill but it may be of use in the future!

 

Tom

 

 

 

 

 

Message 3 of 9
augusto.goncalves
in reply to: Anonymous

Hi,

 

Have you tried Document.UnitsOfMeasue.GetValueFromExpression method? It is safer as consider the unit in the case and return you in Inventor internal unit.

 

Regards,

Augusto Goncalves

Autodesk Developer Network

Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
Message 4 of 9
Anonymous
in reply to: augusto.goncalves

Gustavo,

 This is a custom iProperties text string containing the 140 mm. I'm not sure of the code you're refering to or if it would extract out the 140 from it. Can you show me an example?

 

Thanks,

Mike Stalter

Message 5 of 9
augusto.goncalves
in reply to: Anonymous

Try this:

 

>>>

        Dim doc As Inventor.Document = m_inventorApplication.ActiveDocument

        param.Value = doc.UnitsOfMeasure.GetValueFromExpression("140 mm", UnitsTypeEnum.kDefaultDisplayLengthUnits)

<<<

Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
Message 6 of 9
Anonymous
in reply to: Anonymous

Tom,

 I appreciate the reply. Since I can't be sure of the character positions in the string, your second method would work better. It's possible the string could have spaces in different locations too. Maybe I'm going about this the wrong way. Rather than try to extract the number out of the string, I could prompt with an input box for the thickness of the material and then use it to create the custom iProperty. I'm not sure if an input box returns a numeric value if you type in a number. If so, I'd need to convert the thickness to a string first and then add other text to that string to create the "Plate, Steel 140 mm" text value to plug into the custom iProperty. Is there an iLogic ability to convert a number to a text string and then add text to that string? I might be able to declare the input from the input box as a string even tho I'm typing in a number.

 

Mike

Message 7 of 9
MjDeck
in reply to: Anonymous

Mike,

 It's easier to build a string around a number than it is to extract a number out of a string.  See the attachment for an iLogic rule that will prompt you for input and add that input into another string.


Mike Deck
Software Developer
Autodesk, Inc.

Message 8 of 9
rhasell
in reply to: MjDeck

Hi

(I had to edit the post, the screen dumps went missing)

Is the property a user defined Parameter? eg "Plate, Steel <LENGTH>"

If so then do as follows:

Right click on the variable, select "Custom Property format"

Clear the unit string box.

Note the preview.

The check box of "apply to existing" will pass the changes through to all exported parameters.

Reg.

Reg
2024.2
Please Accept as a solution / Kudos
Message 9 of 9
maxim.teleguz
in reply to: Anonymous

Actually it is much easier, use the trim method. 

Dim myparam As String
myparam = iProperties.Value("Custom", "WHERE-USED")
myparam = (Mid(myparam, 10, 9)).Trim

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report