[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

Re: Turning an object around one edge.

Post by manos »

onekk wrote: Thu Oct 06, 2022 5:12 am ....

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, ...

Kind Regards

Carlo D.
I did not expect that people would prefer to download a file with a sole prism instead of creating it with 2 click of mouse. Personally the time I need to download the file, to move it to the test directory and selecting it from the FC Gui is bigger than the time to create it with 2 clicks. I would prefer to create it than to download it since I repeat it is just a plain prism. But that's my wrong judgement.
not to complete your code to have s ready made method.

I do not expect any exception from people being so kind to help me. I expect 3 to 4 lines of code just as all helpers do usually . That's for I wrote the first few lines so helpers would not spend time to write those lines to create the prism and then select it etc.
Of course also it would be very useful to me just a description of the method e.g.: You first create a vector using the vertexes of the edge and then you add the distance of the origin from the edge etc, etc..

A third point is that I needed to emphasize that my question had not been answered so other people wanting to help me being encouraged to do that. Please consider that it is a crucial point for me after a 2 months effort.
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Turning an object around one edge.

Post by mario52 »

Hi
manos wrote: Thu Oct 06, 2022 7:17 am
I did not expect that people would prefer to download a file with a sole prism instead of creating it with 2 click of mouse. Personally the time I need to download the file, to move it to the test directory and selecting it from the FC Gui is bigger than the time to create it with 2 clicks. I would prefer to create it than to download it since I repeat it is just a plain prism. But that's my wrong judgement.
more easy for all to download one file
(instead create the file and search the exact object or situation ... many time)
thanks for the future

try:

Macro_D_Un_Jour_Rotation_object_on_direction (extract Macro_Rotate_To_Point Image)

Code: Select all

# -*- coding: utf-8 -*-
#################### Rotation object selected on direction ####################################################
##Macro_D_Un_Jour_Rotation_object_on_direction
##06/10/2022
##Mario52
##https://forum.freecadweb.org/viewtopic.php?p=631002#p631002
##Portion Rotate_To_Point
##

## Use:
## first  : select the object
## second : select the wire, edge, line

valeur = 10.0   # angle rotation value

FreeCAD.ActiveDocument.openTransaction("tyty ")    # memorise les actions (avec annuler restore)
#FreeCAD.ActiveDocument.commitTransaction()         # restore les actions  (avec annuler restore)
#FreeCAD.ActiveDocument.abortTransaction()          # abandonne les actions(avec annuler restore)

sel = Gui.Selection.getSelection()  # selection objet to rotate

## section selection wire, edge, line for give the direction 
try:
    selectedEdge = FreeCADGui.Selection.getSelectionEx()[1].SubObjects[0]         # select one element and axis second object (2 selections)
except Exception:
    try:
        selectedEdge = FreeCADGui.Selection.getSelectionEx()[0].SubObjects[1]     # select one element and axis same object   (2 selections)
    except Exception:
        try:
            selectedEdge = FreeCADGui.Selection.getSelectionEx()[0].SubObjects[0] # select one element and axis same object   (1 selection)
        except Exception:
            None
            print("oups")

try:
    direction = selectedEdge.Vertexes[1].Point.sub(selectedEdge.Vertexes[0].Point)# search the direction line or sub
except Exception:
    direction = App.Vector(0.0,0.0,0.0)

print(direction)

sel[0].Placement = App.Placement(App.Vector(0.0,0.0,0.0), App.Rotation(App.Vector(direction), valeur), App.Vector(selectedEdge.Vertexes[0].Point)).multiply(sel[0].Placement)

EDIT Paris 16h01 : adding "direction = App.Vector(0.0,0.0,0.0)"

mario
Last edited by mario52 on Thu Oct 06, 2022 2:03 pm, edited 1 time in total.
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

Re: Turning an object around one edge.

Post by manos »

mario52 wrote: Thu Oct 06, 2022 11:11 am
more easy for all to download one file
OK. I humbly agree.

For the rest of your e-mail I will try it at the afternoon.
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Turning an object around one edge.

Post by onekk »

manos wrote: Thu Oct 06, 2022 7:17 am ...
Thanks to mario52 for the answer,
mario52 wrote: Thu Oct 06, 2022 11:11 am ...

One minor problem:

Code: Select all

sel[0].Placement = App.Placement(App.Vector(0.0,0.0,0.0), App.Rotation(App.Vector(direction), valeur), App.Vector(selectedEdge.Vertexes[0].Point)).multiply(sel[0].Placement)
This seem to not work as:

Code: Select all

App.Vector(selectedEdge.Vertexes[0].Point)).multiply(sel[0].Placement)
gave an error when doing the multiply.

However I have put the formula with some correction and obtained this code, that seems to work.

Code: Select all

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

ROT0 = Rotation(0,0,0)
VEC0 = Vector(0,0,0)

DOC = FreeCAD.activeDocument

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

# Line around the object has to be rotates
line = Part.LineSegment(p1, p2)

# Line to show rotation
line2 = Part.LineSegment(p2, p3)

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

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

axis = p2.sub(p1)

angle = 60

wire_cp = Part.show(wire, "rot_wire")
wire_cp.Placement = Placement(VEC0, Rotation(axis, angle), p1)


wire_or = Part.show(wire.copy(), "or_wire")
Part.show(line_sh, "line")

Many thanks, I will note this code somewhere as it will be useful in future.

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

[SOLVED] Re: Turning an object around one edge.

Post by manos »

Thanks a lot @mario52 and @onekk for your extended answers.
Post Reply