[SOLVED] Turning an object around one edge.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

[SOLVED] Turning an object around one edge.

Post by manos »

I have created an object e.g. a PRISM. One of its edges ends at Vertex1 @ Vertex2. The Global coordinates of those two vertexes are known. x1,y1,z1 and x2,y2,z2 . Now on I want to turn the object around this edge using a script like the following. I tried many ways but Rotation still remains to me unpredictable.
Any help please ?

Code: Select all

sel=FreeCADGui.Selection.getSelectionEx()[0]
obj=sel.Object
rot=FreeCAD.Rotation(FreeCAD.Vector(A,B,C), 10)
#centre = FreeCAD.Vector(5,5,0)
pos=obj.Placement.Base   
newplace=FreeCAD.Placement(pos,rot)
obj.Placement=newplace
 
Any idea about the needed values of A,B,C ? Maybe using the centre ? The obj.Placement.Base = Vector (15.0, 25.0, 10.0)
Last edited by manos on Fri Oct 07, 2022 4:52 am, edited 3 times in total.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Turning an object around one edge.

Post by onekk »

manos wrote: Wed Oct 05, 2022 10:43 am ...

Code: Select all

import FreeCAD
from FreeCAD import Placement, Rotation, Vector
import Part


DOC = FreeCAD.activeDocument

p1 = Vector(0, 10, 0)
p2 = Vector(1, 5, 5)
p3 = Vector(5, 5, 5)

line = Part.LineSegment(p1, p2)
line2 = Part.LineSegment(p2, p3)

line_sh = line.toShape()
line2_sh = line2.toShape()

wire = Part.Wire([line_sh, line2_sh])

wire_or = Part.show(wire, "wire")
wire_or = Part.show(wire.copy(), "wire_or")

Part.show(line_sh, "line")


I have done a MWE for you, you whant to rotate wire around the axis visualized by line

copy this code into an empty py document loaded into FC and see if I've guessed right.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

Re: Turning an object around one edge.

Post by manos »

onekk wrote: Wed Oct 05, 2022 11:58 am
Carlo D.
Carlo thanks. I will check it in the afternoon.

PS. What is a MWE ?
User avatar
wandererfan
Veteran
Posts: 6268
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Turning an object around one edge.

Post by wandererfan »

manos wrote: Wed Oct 05, 2022 12:27 pm PS. What is a MWE ?
Minimal Working Example. Something that shows the problem with all the non-problematic bits removed.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Turning an object around one edge.

Post by onekk »

manos wrote: Wed Oct 05, 2022 12:27 pm
onekk wrote: Wed Oct 05, 2022 11:58 am
Carlo D.
Carlo thanks. I will check it in the afternoon.

PS. What is a MWE ?
As said by @wandererfan the file I have posted is a MWE in that sense, so it could be used at test file as you or an helper could simply add lines that make the correct rotation, without relying on other files.

Take it as an example file, in your case you have to load only this file add code and launch it to see results without to have a second file with a line to select and then run the code.

There is no solution in the file, but as example @edwilliams16 would step in :D and put some lines that do the math magick to use the line as a rotation axis and rotate the whole wire around that line, it will be more easy as he has to add only the "relevant lines" and not write down an entire "test program".

Hope it helps.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

Re: Turning an object around one edge.

Post by manos »

onekk wrote: Wed Oct 05, 2022 3:29 pm
Regards

Carlo D.
Excuse me 1000 times @onekk . I wrote : "I have created an object e.g. an edge" instead of "I have created an object e.g. a PRISM" . The edge I mentioned belongs to that prism. etc, etc
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Turning an object around one edge.

Post by onekk »

manos wrote: Wed Oct 05, 2022 5:01 pm Excuse me 1000 times @onekk . I wrote : "I have created an object e.g. an edge" instead of "I have created an object e.g. a PRISM" . The edge I mentioned belongs to that prism. etc, etc
Not a problem once you have managed to calculate correctly an arbitrary axis of rotation you could use it as you want.

The example is to define a line with two arbitrary point and rotate around this line as it an axis.

It is enough abstract to be very useful. The two point could be an arbitrary edge of a poligon or a solid.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

Re: Turning an object around one edge.

Post by manos »

onekk wrote: Wed Oct 05, 2022 6:57 pm
Carlo D.
Dear Carlo.
As you can easily seen in my snippet, I do not have any doubt about the selection or creation of an object. The code you sent me concerns all those. I need the continuation of my code. In other words the lines "next", not the lines "before".

Thanks anyway.
Last edited by manos on Wed Oct 05, 2022 9:23 pm, edited 1 time in total.
manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

Re: Turning an object around one edge.

Post by manos »

wandererfan wrote: Wed Oct 05, 2022 2:26 pm
manos wrote: Wed Oct 05, 2022 12:27 pm PS. What is a MWE ?
Minimal Working Example. Something that shows the problem with all the non-problematic bits removed.
Thank you wanderefan.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Turning an object around one edge.

Post by onekk »

manos wrote: Wed Oct 05, 2022 7:31 pm
Dear Carlo.
As you can easily seen in my snippet, I do not have any doubt about the selection or creation of an object. The code you sent me concerns all those. I need the continuation of my code. In other words the lines "next", not the lines "before".

Thanks anyway.
Yes but putting a similar code you force helpers to spent more time to create other file or to download other files if you are kind enough to attach one.

You probably are misunderstanding the whole concept of MWE.

Main scope is to ease the work of the person that is so kind to help you, not to complete your code to have s ready made method.

Kind Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply