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.

Get a Vector from an Edge

Alex_PittAlex_Pitt Member Posts: 65 PRO
Is there a way to convert an edge (query) to a vector? 

I've asked Chat GPT but it just seems to make stuff up...

"Hi, Chat GPT here,
In Onshape FeatureScript, you can convert an edge to a vector using the opEdgeToVector function. This function takes an edge as input and returns a vector representing the direction of the edge.

Here's a simple example demonstrating how to use opEdgeToVector:

javascriptCopy codeimport(path : "onshape/std/geometry.fs", version : "252.0");

// Define a simple edge
const edge = edge(context, {
    start: vector(0, 0, 0),
    end: vector(1, 1, 1)
});

// Convert the edge to a vector
const edgeVector = opEdgeToVector(edge);

// Print the result
println("Edge vector: ", edgeVector);

This script creates a simple edge from the point (0, 0, 0) to the point (1, 1, 1), then converts this edge to a vector using opEdgeToVector, and finally prints the resulting vector..."

Tagged:

Best Answer

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,688
    edited April 9 Answer ✓
    You got it.
    Senior Director, Technical Services, EMEAI

Answers

  • GregBrownGregBrown Member, Onshape Employees, csevp Posts: 197
    You’ll need to look at the help/tutorials a bit to get the fundamentals. ChatGPT clearly does not understand the difference between queries and evaluation, amongst other hallucinations it’s having. 

    Evaluate is what you need to do to a query that refers to an entity like an edge. 

    Check out evEdgeTangentLine in the docs….
  • Alex_PittAlex_Pitt Member Posts: 65 PRO
    @GregBrown thanks. Haha yes! ChatGTP can be very "creative"!

    I'm not up to speed enough for the help to mean much... evEdgeTangentLine(context, map) returns line.  If this will somehow return a vector from an edge query I will investigate further.  

    Re tutorials, I did the Fundamentals course a few years ago which is very good but I'll need to revisit. I'll keep plugging away. I'm sure it will twig in time.
  • Alex_PittAlex_Pitt Member Posts: 65 PRO
    This could be helpful...

    var myLine is Line = evEdgeTangentLine(context, {
        "edge" : definition.edge1,
        "parameter" : 0.5
    });
    var myPoint is Vector = myLine.origin + myLine.direction * 2.5 * inch;
    
    debug(context, myLine);
    debug(context, myPoint);
  • Alex_PittAlex_Pitt Member Posts: 65 PRO
    When I tweak the above to this...
       
    var myLine is Line = evEdgeTangentLine(context, {
        "edge" : edge1,
        "parameter" : 0
        });
         var myPoint is Vector = myLine.origin + myLine.direction *0*mm;
        debug(context, myLine);
        debug(context, myPoint);

    The debug looks like this.

    debug: Line direction (-0, -0, -1)
    origin (0.1 meter, 0meter, 0.3 meter) //that's the top point of my selected edge
    debug: Vector (0.1meter, 0 meter, 0.3 meter) //same point as above
    debug: Vector (0.1 meter, 0 meter, 0.3 meter)
      Result: Regeneration complete

    I can now get the length of my edge. (0.3m) by multiplying the respective components of the Line direction unit vector (-0, -0, -1) and the myLine vector (0.1m,0m,0.3m).

    Seems a very convoluted way to do it though. 
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,688
    Is it the length you are looking for? evLength?
    Senior Director, Technical Services, EMEAI
  • Alex_PittAlex_Pitt Member Posts: 65 PRO
    @NeilCooke Thanks, evLength is great as I do need the length. I also need the unit vector (line direction) of my edge. I plan to multiply the two, to get a vector to feed into opTransform. (All part of my cunning plan to move a part along an edge).

    I'm unsure how to get the unit vector so I'll have a look through the help. Maybe one of the evaluate functions...
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,688
    evEdgeTangentLine returns a Line object where the “direction” is a unit vector. 
    Senior Director, Technical Services, EMEAI
  • Alex_PittAlex_Pitt Member Posts: 65 PRO
    @NeilCooke thanks. Yes I saw that.  

    debug: Line direction (-0, -0, -1)
    origin (0.1 meter, 0meter, 0.3 meter)

    A "Line" is a special kind of "map" is that right? Direction + Point. Infinite length. 

    Unsure how to isolate the "direction" component of that "Line".

    Looking to the help...

    "...Map elements are accessed or changed using the map key. If the map key is a string that is a valid identifier, dot syntax may be used.

    var val = map2.a;       // v is 1
    val = map2["a"];        // v is 1
    
    map2.a = 0;             // map2 is now { "a" : 0 }
    map2.b = 1;             // map2 is now { "a" : 0, "b" : 1 }
    So maybe..

    myLine.direction 
    ?
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,688
    edited April 9 Answer ✓
    You got it.
    Senior Director, Technical Services, EMEAI
  • Alex_PittAlex_Pitt Member Posts: 65 PRO
    edited April 9
    It works! :smile:
    Thanks Neil & Greg.

    ...
          var myEdgeLength = evLength(context, { "entities" : myEdge });
           
           var myLine is Line = evEdgeTangentLine(context, {"edge" : myEdge, "parameter" : 0  });

           var myVector is Vector = myLine.direction*myEdgeLength;
           
           opTransform(context, id + 0 + "transform1", {"bodies" : myBody, "transform" : transform(myVector)  });  
       
    });
Sign In or Register to comment.