Script Draft line between sketch geometries

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
paiejean
Posts: 2
Joined: Fri May 26, 2023 6:58 pm

Script Draft line between sketch geometries

Post by paiejean »

I easily trace a line (or polyline) in the draft workshop, from a point in a sketch (i.e center of a circle) to a point in another sketch attached to different plane. The "snap center" option let me automaticaly reach the targeted center point.
2 sketch in diffrents planes with a wires link
2 sketch in diffrents planes with a wires link
PathBetweenCircle.jpg (24.6 KiB) Viewed 727 times
In Python console, that generate something like that :

>>> pl = FreeCAD.Placement()
>>> pl.Rotation.Q = (0.4376140853052063, 0.0011184694775569469, 0.04121467285014154, 0.8982171297137764)
>>> pl.Base = FreeCAD.Vector(-1000.0, 1000.0, 1000.0)
>>> points = [FreeCAD.Vector(-1000.0, 1000.0, 1000.0), FreeCAD.Vector(1000.0, -1000.0000000000002, -999.9999999999998)]
>>> line = Draft.makeWire(points, placement=pl, closed=False, face=True, support=None)

How could I script that kind of action? Especialy, how to obtain (or calculate) the numeric parameters of the pl.Rotation.Q operation?

Naively I try the following expressions
P1=(doc.findObjects(Label="Sketch1")[0]).Geometry[0].Center
P2=(doc.findObjects(Label="Sketch2")[0]).Geometry[0].Center
points=[P1, P2]
Draft.makeWire(points, closed=False, support=None)

The line is created but not in the right direction. It seems to stay in the plane of the inital sketch.

Then, the placement parameter seems required but I can't figure how to script it. I'm not so confident neither of the way I get the point's coordinates considering the resulting vectors have only 2 non zero coordinates (ex. print(P2) --> Vector (1000.0, 71000.0, 0.0)).

I already have a macro generating the multiple Sketch objects required for my project. I must then generate many Draft polylines linking them in such a way that the generated Wires could serve as path for "part sweep" operations.

I hope everything is clear enough.
smktec
Posts: 327
Joined: Thu Mar 05, 2020 5:37 pm

Re: Script Draft line between sketch geometries

Post by smktec »

your second sketch is on a different plane, the placement is given as it were on the xy plane with 90°rotation.
you need to find the centerpoint of the circle:

Code: Select all

C2=doc.findObjects(Label="Sketch2")[0] #get the object
E=C2.Shape.SubShapes[0] #get subshape (edge)
E.Curve.Center #get centerpoint
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Script Draft line between sketch geometries

Post by onekk »

Sketch have their orientation, but geometries are made in the sketch plane.

If you retrieve data from geometries they probaly are in XY coordinates so the Z=0 you see. You should tranform them in global coordinates, to make this you should see some things.

Usually as told above the Shape property should have the transformed coordinates, but it is hard to select the right thing as Shape has no knowledge of the underlying sketch.

Without a file it is difficult to tell.

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/
smktec
Posts: 327
Joined: Thu Mar 05, 2020 5:37 pm

Re: Script Draft line between sketch geometries

Post by smktec »

that is a good point that onnkk makes. My proposal can not be taken as a general solution. In particular if the object undergoes several transformations e.g. if it is part of a body which might have been transformed or if it is within one or several parts which could be subject to transformations either (translation and/or rotation). In this case the entire hirarchy has to be considered to find the global location.
paiejean
Posts: 2
Joined: Fri May 26, 2023 6:58 pm

Re: Script Draft line between sketch geometries

Post by paiejean »

Thank you all!

smktec, you have the right solution. :D Curve.Center is the 3D point I was looking for. Now I can do something like that

noWire=1
while noWire<=50:
sktNameSuffix=("000"+str(noWire))[-2:]
p1=(doc.findObjects(Label="startPoint_skt"+sktNameSuffix)[0]).Shape.SubShapes[0].Curve.Center
p2=(doc.findObjects(Label="middlePoint_skt"+sktNameSuffix)[0]).Shape.SubShapes[0].Curve.Center
p3=(doc.findObjects(Label="endPoint_skt"+sktNameSuffix)[0]).Shape.SubShapes[0].Curve.Center
Draft.makeWire([p1, p2, p3], closed=False, support=None)
noWire=noWire+1
smktec
Posts: 327
Joined: Thu Mar 05, 2020 5:37 pm

Re: Script Draft line between sketch geometries

Post by smktec »

I am glad that I could help you
Post Reply