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 to Transform Sketch points/curves

bartyborrisbartyborris OS Professional Posts: 11 PRO
First, I am very new to FeatureScript, so please be patient.  I need to transform several points in a sketch and have not been able to find any examples or documentation (that I can understand enough to piece together into something useful... yet).  My thought was to simply use the 2D rotation matrix, something like what is shown below (which I know will not work!).  In general, if someone could point me to where I can find examples on how to do linear algebra like operations (e.g. create matrices and multiply them) and transforms on points/lines/curves within a sketch I would greatly appreciate it.  It appears there is lots of functionality (even SVD, which is great!), although I am struggling on how to utilize it. 

// create 2D rotation "matrix"
const rotMat = [ [cos(angle), -sin(angle)], [sin(angle), cos(angle)] ];

// create a 2D "vector"
var point1 = vector(0 , 1) * inch;
// or maybe:
var point1 = [0 * inch, 1 * inch];

// now rotate/transform the vector with the rotation matrix
point1 *= rotMat;



Comments

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 909
    You got pretty close actually -- here's a working version of your code:

            // create 2D rotation "matrix"
            var angle = 30 * degree;
            const rotMat = [[cos(angle), -sin(angle)], [sin(angle), cos(angle)]] as Matrix;
    
            // create a 2D "vector"
            var point1 = vector(0, 1) * inch;
    
            // now rotate/transform the vector with the rotation matrix
            point1 = rotMat * point1;
            
            println(point1);
    

    The first difference is that you have to use "as Matrix" to indicate that rotMat is a matrix and not just an array of arrays of numbers.  The second difference is that the matrix must be to the left of the vector when you multiply them (vectors are treated as column vectors).
    Ilya Baran \ Director of FeatureScript \ Onshape Inc
  • bartyborrisbartyborris OS Professional Posts: 11 PRO
    Thank you so much.  I was doing:

    const rotMat as Matrix = [ [cos(angle), -sin(angle)], [sin(angle), cos(angle)] ];


    which does not work.  I knew about the ordering for the matrix and vector, I was just being a little lazy after driving my self nuts trying to figure out what I was doing wrong :)


  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 427
    To clarify a bit, the mechanism making this work is the * operator, which is overloaded in the standard library for multiplying a Matrix and a Vector. When you give your variables the right type tags (in this case, via "as Matrix"), FeatureScript finds the correct overload and runs the right function, which performs matrix multiplication.

Sign In or Register to comment.