×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX

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.

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") 

Eric N.
indocti discant et ament meminisse periti

RE: Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX

(OP)
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 

RE: 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

retThickness = Round(retThickness, 2)

Dim strThickness As String 

Eric N.
indocti discant et ament meminisse periti

RE: Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX

(OP)
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") 

RE: Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX

ok my mistake this is VBA code.

Do you NEED CATScript or can you run VBScript?

Eric N.
indocti discant et ament meminisse periti

RE: Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX

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

RE: Knowledgeware Reaction - Drop Leading Zeroes, Round to .XX

(OP)
This is a reaction that uses embedded code. There must be no external links to any other files.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources