They are not macro, but more some help for beginner to familiarize with code and maybe some inspiration for other to share some code.
Code: Select all
import FreeCAD
import Part
from FreeCAD import Vector
import DraftVecUtils
def calc_perp(line, p_len, p_pos=0.5, p_dir="l"):
"""Draw perpendicular line.
calculate perpendicular line to the given line.
Parameters:
name type description
line obj Part.LineSegment obj
p_len float Length of the perpendicular
p_pos float (0.0, 1.0) position of the perpendicular
0.0 = starting point 1.0 = ending point
if not specified middle point 0.5 is assumed
p_dir string (l, r) l = left, r = right default 'left'
"""
v1 = line.StartPoint
v2 = line.EndPoint
chord = v2.sub(v1)
v_dir = -1 if p_dir == "l" else 1
perp = chord.cross(Vector(0, 0, v_dir))
startp = v1.add(chord.multiply(p_pos))
if not DraftVecUtils.isNull(perp):
perp.normalize()
plen = perp.multiply(p_len)
# print(plen)
endp = startp.add(plen)
line = Part.LineSegment(startp, endp)
return line
EDIT2: 30/11/2021 added a missing import statement
EDIT3: 01/12/2021 integrated a 'ternary if statement' with many thanks to @heda and @sliptonic
Hoping the code is clear, main code is scavenged from /Draft/ImportDXF.py where a bulge is caclulated, I've modified the code to obtain a perpendicular line to the given Part.LineSegment, see Docstring for other infos.
Process a list of "rotation" and "position" commands
Code: Select all
def pro_rot(obj, cmds):
"""Process a series of rotation commands.
Helper for math impaired people.
pass in cmds a tuple with a tuple element to obtain a rotation and
a translation using:
("X, Y or Z", number in degree and sign), example ("X", 90), ("Z", -90)
("M", Vector(x, y, z)), example ("M", Vector(10, 0, 10))
positions and placement are done "in order" of appearance, remeber to
put a colon if the tuple is a singleton, example of cmd:
r_pos = (("X", 90), )
pro_rot(obj, r_pos)
"""
m = obj.Placement.Matrix
for cmd_pos in cmds:
if cmd_pos[0] == "X":
m.rotateX(math.radians(cmd_pos[1]))
elif cmd_pos[0] == "Y":
m.rotateY(math.radians(cmd_pos[1]))
elif cmd_pos[0] == "Z":
m.rotateZ(math.radians(cmd_pos[1]))
elif cmd_pos[0] == "M":
m.move(cmd_pos[1])
obj.Placement.Matrix = m
This way it will be more easy and immediate to make complex rotation and moves (at least for "vectors math" impaired people like me).
If there is any error, feel free to signal them here.
Feel free to comment and maybe improve the code.
Regards
Carlo D.