[Solved]Datum Line with Python script ?

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

[Solved]Datum Line with Python script ?

Post by manos »

Hello
I try to create a script to add a Datum Line at a FreeCAD doc.
Any help is welcomed.
Last edited by manos on Sat Sep 17, 2022 8:43 pm, edited 1 time in total.
User avatar
Chris_G
Veteran
Posts: 2598
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Datum Line with Python script ?

Post by Chris_G »

Code: Select all

line = App.ActiveDocument.addObject("PartDesign::Line", "DatumLine")
manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

Re: Datum Line with Python script ?

Post by manos »

Chris_G wrote: Sat Sep 17, 2022 12:20 pm

Code: Select all

line = App.ActiveDocument.addObject("PartDesign::Line", "DatumLine")
Gris_G Thanks for your prompt response. Unfortunately the only thing I managed to do is a Datum line passing from (0,0,0) point.
Is there any way to make the D. Line pass from 2 points or I have to use the placement story ?

Thanks in advance
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Datum Line with Python script ?

Post by edwilliams16 »

These object constructors all create their results in some default position. You move them where you want via their Placement property.
If you make a Datum line with

Code: Select all

line = App.ActiveDocument.addObject("PartDesign::Line", "DatumLine")
you find it goes through the origin along the z-axis. Suppose you would like it to go through point p1 in the direction p2 -p1 with, say

Code: Select all

p1 = App.Vector(10,0,0)
p2 = App.Vector(0, 10, 0)
We create the required Placement. We rotate the z-axis into the direction p2 - p1 (technically, to represent a direction this should be normalized to unit length - but FreeCAD does this for us)

Code: Select all

rot = App.Rotation(App.Vector(0, 0, 1), p2 - p1)
and translate from the origin to p1

Code: Select all

pl = App.Placement(p1, rot)
and assign it to line

Code: Select all

line.Placement = pl
App.ActiveDocument.recompute()
Let's check if we succeeded. First what is the direction of the line:

Code: Select all

 line.Shape.Curve.Direction
#Vector (-0.7071067811865476, 0.7071067811865476, 0.0)
which indeed is the same as

Code: Select all

(p2-p1).normalize()
#Vector (-0.7071067811865475, 0.7071067811865475, 0.0)
Does the line pass through p1 and p2?

Code: Select all

for i, p in enumerate([p1, p2]):
    dist, vects, info = line.Shape.distToShape(Part.Shape(Part.Vertex(p)))
    print(f'Distance from p{i + 1} to Datum line {dist}')
    #Distance from p1  to Datum line 0.0
    #Distance from p2 to Datum line 2.5121479338940403e-15
Yes.
I assume you know what you are doing and really want a Part Design Line, not just a Part Line. The former are used for construction purposes in the Part Design workbench.
manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

Re: Datum Line with Python script ?

Post by manos »

@edwilliams16 thank you so much for your extended answer. Fortunately datum lines can be handled with placement which is much more flexible than attachment.
PS. I will know what am I doing if I succeed. If not, I will have spend 1 month of my life for nothing.
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Datum Line with Python script ?

Post by edwilliams16 »

manos wrote: Sat Sep 17, 2022 8:42 pm Fortunately datum lines can be handled with placement which is much more flexible than attachment.
?? Both can place objects in any location and orientation.

Using Placement places the object in the coordinate system of its container (e.g. global if at the root level of the document, or in the body's coordinate system if in a body). Using an attachment with its attachment offset places the object relative to the attach point. The latter can be easier to make parametric, but you need to be careful of TNP.
manos
Posts: 432
Joined: Thu Nov 12, 2020 10:48 am
Location: Greece

Re: Datum Line with Python script ?

Post by manos »

edwilliams16 wrote: Sat Sep 17, 2022 5:37 pm
Yes.
I assume you know what you are doing and really want a Part Design Line, not just a Part Line. The former are used for construction purposes in the Part Design workbench.
I am sorry I didn't answer earlier. Your answer is more than complete. It seems your are experienced on translation and rotation. I wish some day you complete Wiki:Placement and Wiki:Attachment pages.
As it concerns me I am trying to use Analytic Geometry (and Linear Algebra and few drops of Vector Analysis) to simplify some tasks.
Post Reply