How to get a vector normal to a Sketch?

Need help, or want to share a macro? Post here!
Post Reply
freedman
Veteran
Posts: 3090
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

How to get a vector normal to a Sketch?

Post by freedman »

The subject says it all, I have a sketch and need a vector normal-to. I searched and didn't get much.
Thanks
wmayer
Founder
Posts: 19944
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: How to get a vector normal to a Sketch?

Post by wmayer »

Code: Select all

sketch = App.ActiveDocument.Sketch
plm = sketch.Placement
vec = App.Vector(0,0,1)
plm.Rotation.multVec(vec)
heda
Veteran
Posts: 1266
Joined: Sat Dec 12, 2015 5:49 pm

Re: How to get a vector normal to a Sketch?

Post by heda »

don't really know, but could be as simple as

Code: Select all

sketch.Placement.Matrix.col(2)
at least that works for the one case I tested with just a sketch.
if you have nested placements, attachments - suppose that has to be un-nested as well before one arrives at the "global" normal

theoretically, if your matrix is

Code: Select all

[[r1],
 [r2],
 [r3},
 [r4]]
the normal should be

Code: Select all

[[r2 x r3],
 [r3 x r1],
 [r1 x r2]]
no harm in reducing the 4x4 to a 3x3 first...

btw, believe the matrix here is a fc-implemented one, so for convenience it really ought to have a .normal() method imho.
freedman
Veteran
Posts: 3090
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: How to get a vector normal to a Sketch?

Post by freedman »

Thanks wmayer, I always forget that.

I wanted it in Global coords. Changed this:
plm = sketch.Placement
to
plm = sketch.getGlobalPlacement()
btw, believe the matrix here is a fc-implemented one, so for convenience it really ought to have a .normal() method imho.
That would be good. I will verify "sketch.Placement.Matrix.col(2)" to the multVec approach. Thank you
paullee
Veteran
Posts: 4855
Joined: Wed May 04, 2016 3:58 pm

Re: How to get a vector normal to a Sketch?

Post by paullee »

I learned from someone else
- normal = Sketch.getGlobalPlacement().Rotation.multVec(FreeCAD.Vector(0,0,1))

Wondering what is different if it is
- normal = Sketch.Placement().Rotation.multVec(FreeCAD.Vector(0,0,1))

?

Thanks
freedman
Veteran
Posts: 3090
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: How to get a vector normal to a Sketch?

Post by freedman »

normal = Sketch.Placement.Rotation.multVec(FreeCAD.Vector(0,0,1))
This is just the local (0,0,0,1)

Below I did a test; #1) I took a sketch and created an offset (normal) version, then took the difference between the two as a vector. #2) used the standard multVec. I suppose the difference could be a problem if I was plotting a path to Mars :) . The sketch is off axis in the global. @wmayer

Code: Select all

sketchgplbase = sketch.getGlobalPlacement().Base 
sketch2 = sketch
sketch2.AttachmentOffset.Base.z += 5
sketch2gplbase = sketch2.getGlobalPlacement().Base 
direction = sketch2gplbase - sketchgplbase
direction.normalize()
print("1",direction)
print("2",sketch.getGlobalPlacement().Rotation.multVec(FreeCAD.Vector(0,0,1)))

1 Vector (0.02497325279932969, -0.007005166641426478, 0.9996635755517687)
2 Vector (0.024973252799321794, -0.007005166641426546, 0.9996635755517689)
Post Reply