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.

How do I find the distance between two planes?

Kira_LeeKira_Lee Member, Developers Posts: 17 PRO
edited October 2021 in FeatureScript
The user chooses a plane to draw on (it will be parallel to one of the three given planes - Front, Top or Right). From this, I want to be able to change the y- coordinate in a sketch based off of this length value. It isn't going through my if statements and it's just using "X" that I set above it. I have tried using boxDis in my if statement instead because I would think Vec is redundant, but this didn't work either. I am new to Feature Script, so any help would be greatly appreciated.
var realPlane = evPlane(context, {
"face" : definition.myPlane});
//should return a 3D vector where 2 values are 0var Vec = planeToWorld(realPlane, vector(0, 0) * inch);//Predefines the variable Xvar X is number = 0;if (Vec[0] != 0){var X is number = 0;}if (Vec[1] != 0){var X is number = 1;}else{var X is number = 2;}//sets the distance between our plane and the origin as a vector (we take the z coordinate)var boxDis = project(realPlane, vector(0, 0, 0) * inch);
//Create sketchvar champSketch = newSketch(context, id + "champSketch", {"sketchPlane" : qCreatedBy(makeId("Front"), EntityType.FACE)});skLineSegment(champSketch, "ChampLine1", {"start" : vector(definition.OuterDiameter / 2, boxDis[X]),"end" : vector(definition.OuterDiameter / 2 - (0.117 * inch), boxDis[X])});


Comments

  • Kira_LeeKira_Lee Member, Developers Posts: 17 PRO
    This is different than normal coding, the whole code has to be put into the "if/else" statement. This triples the entire size of my code, but it works - variables cannot be defined in the "if" statement and then be used outside of it. If there is an easier/cleaner way to do this, I'd love to know. Thank you in advance for the help. This is the snippet of that code fixed:

            //make our face as a plane
            var realPlane = evPlane(context, {
                    "face" : definition.myPlane
                });
            
            //should return a 3D vector where 2 values are 0
            var Vec = planeToWorld(realPlane, vector(0, 0) * inch);
           
            //sets the distance between our plane and the origin as a vector (we take the z coordinate)
            var boxDis = project(realPlane, vector(0, 0, 0) * inch);
           
            if (Vec[0] != 0)
            {
                var X is number = 0;
            //Create sketch
            var champSketch = newSketch(context, id + "champSketch", {
                    "sketchPlane" : qCreatedBy(makeId("Front"), EntityType.FACE)
                });
            skLineSegment(champSketch, "ChampLine1", {
                        "start" : vector(definition.OuterDiameter / 2, boxDis[X]),
                        "end" : vector(definition.OuterDiameter / 2 - (0.117 * inch), boxDis[X])
                    });
            }
            if (Vec[1] != 0)
            {
                var X is number = 1;
            //Create sketch
            var champSketch = newSketch(context, id + "champSketch", {
                    "sketchPlane" : qCreatedBy(makeId("Front"), EntityType.FACE)
                });
            skLineSegment(champSketch, "ChampLine1", {
                        "start" : vector(definition.OuterDiameter / 2, boxDis[X]),
                        "end" : vector(definition.OuterDiameter / 2 - (0.117 * inch), boxDis[X])
                    });
            }
            else
            {
                var X is number = 2;
            //Create sketch
            var champSketch = newSketch(context, id + "champSketch", {
                    "sketchPlane" : qCreatedBy(makeId("Front"), EntityType.FACE)
                });
            skLineSegment(champSketch, "ChampLine1", {
                        "start" : vector(definition.OuterDiameter / 2, boxDis[X]),
                        "end" : vector(definition.OuterDiameter / 2 - (0.117 * inch), boxDis[X])
                    });
            }
            

            
  • MichaelPascoeMichaelPascoe Member Posts: 875 PRO
    edited October 2021
    Here is a measure feature I wrote a while back. I use it for measuring multiple distances in one feature. It will return the parallel distance. Perhaps you can use it as an example: Measure

    For efficiency, learning how to use java script functions will help in cases similar to this.

    One way to simplify your code would be to create your variables outside the if statements, then, define the variables inside the if statements. This will let you create the sketch once while letting the variable change depending on the statement.

    Example:

            var startDef;
            var endDef;
            var X;
    
            if (Vec[0] != 0)
            {
                X = 0;
                startDef = vector(definition.OuterDiameter / 2, boxDis[X]);
                endDef = vector(definition.OuterDiameter / 2 - (0.117 * inch), boxDis[X]);
            }
            else if (Vec[1] != 0)
            {
                X = 1;
                startDef = vector(definition.OuterDiameter / 2, boxDis[X]);
                endDef = vector(definition.OuterDiameter / 2 - (0.117 * inch), boxDis[X]);
            }
            else
            {
                X = 2;
                startDef = vector(definition.OuterDiameter / 2, boxDis[X]);
                endDef = vector(definition.OuterDiameter / 2 - (0.117 * inch), boxDis[X]);
            }
    
            //Create sketch
            var champSketch = newSketch(context, id + "champSketch", {
                    "sketchPlane" : qCreatedBy(makeId("Front"), EntityType.FACE)
                });
    
            skLineSegment(champSketch, "ChampLine1", {
                        "start" : startDef,
                        "end" : endDef
                    });

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,120
            var X;
    
            if (abs(Vec[0]) > TOLERANCE.zeroLength)
                X = 0;
            else if (abs(Vec[1]) > TOLERANCE.zeroLength)
                X = 1;
            else
                X = 2;
    
            var startDef = vector(definition.OuterDiameter / 2, boxDis[X]);
            var endDef = vector(definition.OuterDiameter / 2 - (0.117 * inch), boxDis[X]);
    
            //Create sketch
            var champSketch = newSketch(context, id + "champSketch", {
                    "sketchPlane" : qCreatedBy(makeId("Front"), EntityType.FACE)
                });
    
            skLineSegment(champSketch, "ChampLine1", {
                        "start" : startDef,
                        "end" : endDef
                    });
    Two suggestions to the above: remove the repeated code from the if statements, and use tolerant comparisons.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • MichaelPascoeMichaelPascoe Member Posts: 875 PRO
    edited October 2021
    The master has spoken. 

    oops, didn't see that those were the same.
Sign In or Register to comment.