Code: Select all
OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.2
Locale: English/United States (en_US)
Installed mods:
* Help 1.0.3
https://forum.freecadweb.org/viewtopic. ... 27#p563627
I worked it out, in his example the values are hard coded, so I tweaked it to:
Code: Select all
from math import sqrt
# -*- coding: utf-8 -*-
import FreeCAD, FreeCADGui
import Draft
import math
# from math import cos, sin, radians
from PySide import QtGui
convert = 25.4
Placement = App.Placement
doc = App.ActiveDocument
#===============================================================================
vec = App.Vector
def tangent2C(p1, p2, rad1, rad2):
"""calculate four tangent point to join two circles
Parameters:
p1 Vector Center of circle1
p2 Vector Center of circle2
rad1 float Radius of circle1
rad2 float Radius of circle2
Return:
[tg1, tg2, tg3, tg4] tangent points.
If one circle strictly encloses the other or are they are concentric, return []
"""
l = (p1 - p2).Length
if abs(rad1 - rad2) > l or l == 0:
return []
else:
c = (rad1 - rad2) / l
s = sqrt(1 - c * c)
n = (p2 - p1) / l # unit vector along line of centers
m = vec(0, 0, 1).cross(n) # orthogonal vector
vp = c * n + s * m # unit vectors from center to tangent points
vm = c * n - s * m
return [c1 + rad1 * vp, c1 + rad1 * vm, c2 + rad2 * vp, c2 + rad2 * vm]
#============================================================================
r1 = Gui.Selection.getSelection()[0].Radius * .039370078
r2 = Gui.Selection.getSelection()[1].Radius * .039370078
r1 = float(r1)
r2 = float(r2)
selX1 = FreeCADGui.Selection.getSelectionEx()[2]
selX2 = FreeCADGui.Selection.getSelectionEx()[3]
v1 = selX1.Object.Shape
X = v1.Point.x * .039370078
Y = v1.Point.y * .039370078
Z = v1.Point.z * .039370078
multlist = [[X, Y, Z]]
v2 = selX2.Object.Shape
X = v2.Point.x * .039370078
Y = v2.Point.y * .039370078
Z = v2.Point.z * .039370078
multlist.append([X, Y, Z])
CLx = multlist[0][0]
CLy = multlist[0][1]
CSx = multlist[1][0]
CSy = multlist[1][1]
print('csx', CSx)
print('csy', CSy)
#============================================================================
c2 = vec(CSx, CSy, 0)
c1 = vec(CLx, CLy, 0)
#c1 = vec(-35.8, 9.07, 0)
#c2 = vec(-28.3, 16.4, 0)
#r1 = 3
#r2 = 5
pto = tangent2C(c1, c2, r1, r2)
hline1 = Draft.make_line(vec(pto[1][0] * convert , pto[1][1] * convert, 0), vec(pto[3][0] * convert , pto[3][1] * convert, 0))
hline2 = Draft.make_line(vec(pto[0][0] * convert , pto[0][1] * convert, 0), vec(pto[2][0] * convert , pto[2][1] * convert, 0))
print(pto[1][0])
print(pto[1][1])
for pt in pto:
# print(pt[0][0])
# print(pt[0][1])
print(pt)
Thank you @edwilliams16 for all your help. And of course tweak macro as needed. And if only one tangent is needed just delete the unwanted one.
I will do another post with tangents from an external point to a circle.
Edit:
This is in draft mode.