Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX
Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX
(OP)
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





RE: Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX
try to use the VBA function format
CODE --> vba
inPart.Parameters.GetItem("WidthStr").Value = format(strWidth,".000")indocti discant et ament meminisse periti
RE: Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX
I have already used the property, minus the formatting. However, I have also tried to conditionally format, prior to that.
I tested that as a standalone, and it doesn't work.
CODE
Dim inPart as Part Set inPart = GetPart(parameter) Dim retThickness As Double retThickness = inPart.Parameters.GetItem("Thickness").Value / 25.4 retThickness = Round(retThickness, 2) Dim strThickness As String inPart.Parameters.GetItem("ThicknessStr").Value = format(strThickness,".000") 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 FunctionRE: Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX
CODE -->
Dim strThickness As String inPart.Parameters.GetItem("ThicknessStr").Value = format(strThickness,".000")wont do much if you do not assign strThickness
now plz replace
CODE --> replace
inPart.Parameters.GetItem("ThicknessStr").Value = format(strThickness,".000")by
CODE --> with
inPart.Parameters.GetItem("ThicknessStr").Value = format(retThickness ,".000")and remove those lines
CODE --> remove
indocti discant et ament meminisse periti
RE: Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX
However, your code is erroring out on this:
CODE
inPart.Parameters.GetItem("ThicknessStr").Value = format(retThickness ,".000")RE: Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX
Do you NEED CATScript or can you run VBScript?
indocti discant et ament meminisse periti
RE: Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX
indocti discant et ament meminisse periti
RE: Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX