[Solved]Datum Line with Python script ?
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
[Solved]Datum Line with Python script ?
Hello
I try to create a script to add a Datum Line at a FreeCAD doc.
Any help is welcomed.
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.
Re: Datum Line with Python script ?
Code: Select all
line = App.ActiveDocument.addObject("PartDesign::Line", "DatumLine")
Re: Datum Line with Python script ?
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.Chris_G wrote: ↑Sat Sep 17, 2022 12:20 pmCode: Select all
line = App.ActiveDocument.addObject("PartDesign::Line", "DatumLine")
Is there any way to make the D. Line pass from 2 points or I have to use the placement story ?
Thanks in advance
-
- Veteran
- Posts: 3179
- Joined: Thu Sep 24, 2020 10:31 pm
- Location: Hawaii
- Contact:
Re: Datum Line with Python script ?
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
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
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)
and translate from the origin to p1
and assign it to line
Let's check if we succeeded. First what is the direction of the line:
which indeed is the same as
Does the line pass through p1 and p2?
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.
If you make a Datum line with
Code: Select all
line = App.ActiveDocument.addObject("PartDesign::Line", "DatumLine")
Code: Select all
p1 = App.Vector(10,0,0)
p2 = App.Vector(0, 10, 0)
Code: Select all
rot = App.Rotation(App.Vector(0, 0, 1), p2 - p1)
Code: Select all
pl = App.Placement(p1, rot)
Code: Select all
line.Placement = pl
App.ActiveDocument.recompute()
Code: Select all
line.Shape.Curve.Direction
#Vector (-0.7071067811865476, 0.7071067811865476, 0.0)
Code: Select all
(p2-p1).normalize()
#Vector (-0.7071067811865475, 0.7071067811865475, 0.0)
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
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.
Re: Datum Line with Python script ?
@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.
PS. I will know what am I doing if I succeed. If not, I will have spend 1 month of my life for nothing.
-
- Veteran
- Posts: 3179
- Joined: Thu Sep 24, 2020 10:31 pm
- Location: Hawaii
- Contact:
Re: Datum Line with Python script ?
?? 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.
Re: Datum Line with Python script ?
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.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.
As it concerns me I am trying to use Analytic Geometry (and Linear Algebra and few drops of Vector Analysis) to simplify some tasks.