Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX

Status
Not open for further replies.

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.

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

 
Replies continue below

Recommended for you



try to use the VBA function format

Code:
inPart.Parameters.GetItem("WidthStr").Value = format(strWidth,".000")

Eric N.
indocti discant et ament meminisse periti
 
As opposed to, or in addition to, what, exactly?

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 Function

 
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:
inPart.Parameters.GetItem("ThicknessStr").Value = format(strThickness,".000")

by

Code:
inPart.Parameters.GetItem("ThicknessStr").Value = format(retThickness ,".000")

and remove those lines

Code:
retThickness = Round(retThickness, 2)

Dim strThickness As String



Eric N.
indocti discant et ament meminisse periti
 
Sorry, I pasted wrong code previously. I had already swapped strThickness for retThickness

However, your code is erroring out on this:

Code:
inPart.Parameters.GetItem("ThicknessStr").Value = format(retThickness ,".000")

VB_Err_s5mjce.jpg
 
ok my mistake this is VBA code.

Do you NEED CATScript or can you run VBScript?

Eric N.
indocti discant et ament meminisse periti
 
I will check tomorrow if EKL does have a format function... I do not have KD3 that license at home. I ll post something tomorrow.

Eric N.
indocti discant et ament meminisse periti
 
This is a reaction that uses embedded code. There must be no external links to any other files.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor