Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Options

Trouble with simple if-else statment

greg_witkamp749greg_witkamp749 Member Posts: 5
edited December 2016 in FeatureScript
I'm just getting started with feature script, so please bear with me. Below is my entire script based off the first tutorial to make a slot. Instead of making a slot, it extrudes a part. What I want to be able to do is flip the side of the line/curve that the part is extruded to. I added a boolean annotation that I'm trying to use in an if statement that will assign the width of the part to either thickness 1 or 2 of the opThicken operation. When I add the if statment, I don't initially get any errors in the feature studio. As soon as I commit the if statment, it breaks the feature in my part studio. I have a feeling I have the if statement in the wrong place, or something dumb like that. Thanks ahead of time,

annotation { "Feature Type Name" : "Extruded Line" }
export const extrudedLine = defineFeature(function(context is Context, id is Id, definition is map)

    precondition
    {
        // Define the parameters of the feature type
        
        annotation { "Name" : "Part Path", "Filter" : EntityType.EDGE }
        definition.partPath is Query;
        
        annotation { "Name" : "Thickness" }
        isLength(definition.thickness, LENGTH_BOUNDS);
        
        annotation { "Name" : "Width" }
        isLength(definition.width, LENGTH_BOUNDS);
        
        annotation { "Name" : "Flip Direction" }
        definition.flipDir is boolean;
        
    }
    {
     var tk1 is number = 1;
     var tk2 is number = 1;
    var flipDir is boolean = definition.flipDir;      
   // THIS IF-ELSE STATEMENT WHAT IS CAUSING ME PROBLEMS 
    if (flipDir)
    {
        tk1 = definition.width;
    }
    else
    {
        tk2 = definition.width;
    }
   //       
        // Define the function's action
        opExtrude(context, id + "extrude1", {
                "entities" : definition.partPath,
                "direction" : evOwnerSketchPlane(context, {"entity" : definition.partPath}).normal,
                "endBound" : BoundingType.BLIND,
                "endDepth" : definition.thickness
        });
        opThicken(context, id + "thicken1", {
                "entities" : qCreatedBy(id + "extrude1", EntityType.BODY),
                "thickness1" : tk1 * inch,
                "thickness2" : tk2 * inch
        });
    });&nbsp;<br><div></div>

Comments

  • Options
    greg_witkamp749greg_witkamp749 Member Posts: 5
    I got it to work by adding ".value" after ".width"
    
    {
            tk1 = definition.width.value;
        <span>}
    I knew I was missing something dumb</span>
  • Options
    NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,391
    edited December 2016
    Hi Greg,

    Best practice is to not use tk1 * inch in the opThicken, just tk1 (where tk1 = definition.width). 

    definition.width is a ValueWithUnits - this lets the user enter any units in the feature dialog and Onshape will calculate the correct size. Internally all units are meters anyway. 

    Also change tk1 is number to just tk1 = 1 (shouldn't that be tk1 = 0?)
    Senior Director, Technical Services, EMEAI
  • Options
    greg_witkamp749greg_witkamp749 Member Posts: 5
    Thanks Neil,

    I made the changes you recommended. The next thing I'm trying to do is flip the direction of the opExtrude.

    I've played with this by putting a negative sign in front of it, but i haven't been able to figure how to go from a boolean parameter to a vector that will flip the direction. What would be the best way to do that?

    "direction" : evOwnerSketchPlane(context, {"entity" : definition.partPath}).normal,
  • Options
    NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,391
    edited December 2016
    I would probably create another variable like:

    var flip = direction.flip ? -1 : 1;

    then multiply your evOwnerSketchPlane.normal by that variable. 
    Senior Director, Technical Services, EMEAI
  • Options
    greg_witkamp749greg_witkamp749 Member Posts: 5
    I'm sure I need to learn some more about enums feature parameters to get this to work. I'm not sure where to look in the standard library, could you point me in the right direction?

  • Options
    NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,391
    edited December 2016
    sorry my typo - should be var flip = definition.flipDir ? -1 : 1;
    Senior Director, Technical Services, EMEAI
Sign In or Register to comment.