solid7
Mechanical
- Jun 7, 2005
- 1,403
I've got a KW reaction that I really don't think is the best way to accomplish my task. I want to truncate a parameter, plus remove the units, so that I can convert it to a string, and build it into a part number. So, the reaction works... with limits. It is a bit clunky, and I have to believe that there is a better way.
What I want to do:
IF a number is less than 1 but greater than 0, drop the leading zero.
Always keep 3 place decimals.
Round to 3 places, and keep trailing zeroes, if they exist.
What I want to do:
IF a number is less than 1 but greater than 0, drop the leading zero.
Always keep 3 place decimals.
Round to 3 places, and keep trailing zeroes, if they exist.
Code:
Sub main(parameter)
Dim inPart as Part
Set inPart = GetPart(parameter)
Dim retWidth As Double
retWidth = inPart.Parameters.GetItem("Width").Value / 25.4
retWidth = Round(retWidth, 4)
Dim strWidth As String
If InStr(retWidth, ".") > 0 Then
If Len(Right(retWidth, Len(retWidth) - InStr(retWidth, "."))) > 1 Then
strWidth = Left(retWidth, InStr(retWidth, ".") + 1) + 0.1
Else
strWidth = (retWidth)
End If
Else
strWidth = retWidth
End If
If InStr(strWidth, ".") = 0 Then
strWidth = strWidth & ".0"
End If
inPart.Parameters.GetItem("WidthStr").Value = strWidth
End Sub
Function GetPart(inElement as Object) as Part
Dim holder As Object
Dim i as Integer
Set holder = inElement.Parent
i = 0
Do While i = 0
If TypeName(holder) = "Part" Then
Set GetPart = holder
Exit Function
ElseIf TypeName(holder) = "Application" Then
Set GetPart = Nothing
Exit Function
End If
Set holder = holder.Parent
Loop
End Function