FreeCAD Logo FreeCAD 1.0
  • English Afrikaans Arabic Belarusian Catalan Czech German Greek Spanish Spanish Basque Finnish Filipino French Galician Croatian Hungarian Indonesian Italian Japanese Kabyle Korean Lithuanian Dutch Norwegian Bokmal Polish Portuguese Portuguese Romanian Russian Slovak Slovenian Serbian Swedish Turkish Ukrainian Valencian Vietnamese Chinese Chinese
  • Features
  • Download
  • Blog
  • Documentation
    Documentation index Getting started Users documentation The FreeCAD manual Workbenches documentation Python coding documentation C++ coding documentation Tutorials Frequently asked questions Privacy policy About FreeCAD
  • Contribute
    How to help Sponsor Report a bug Make a pull request Jobs and funding Contribution guidelines Developers handbook Translations
  • Community
    Code of conduct Forum The FPA GitHub GitLab Codeberg Mastodon Matrix IRC IRC via Webchat Gitter Discord Reddit Twitter Facebook LinkedIn Calendar
  • ♥ Donate

Donate

$
SEPA Information
Please set up your SEPA bank transfer to:
Beneficiary: The FreeCAD project association
IBAN: BE04 0019 2896 4531
BIC/SWIFT: GEBABEBBXXX
Bank agency: BNP Paribas Fortis
Address: Rue de la Station 64, 1360 Perwez, Belgium

While Stripe doesn't support monthly donations, you can still become a sponsor! Simply make a one-time donation equivalent to 12 months of support, and you'll gain access to the corresponding sponsoring tier. It's an easy and flexible way to contribute.

If you are not sure or not able to commit to a regular donation, but still want to help the project, you can do a one-time donation, of any amount.

Choose freely the amount you wish to donate one time only.

You can support FreeCAD by sponsoring it as an individual or organization through various platforms. Sponsorship provides a steady income for developers, allowing the FPA to plan ahead and enabling greater investment in FreeCAD. To encourage sponsorship, we offer different tiers, and unless you choose to remain anonymous, your name or company logo will be featured on our website accordingly.

from 1 USD / 1 EUR per month. You will not have your name displayed here, but you will have helped the project a lot anyway. Together, normal sponsors maintain the project on its feet as much as the bigger sponsors.

from 25 USD / 25 EUR per month. Your name or company name is displayed on this page.

from 100 USD / 100 EUR per month. Your name or company name is displayed on this page, with a link to your website, and a one-line description text.

from 200 USD / 200 EUR per month. Your name or company name and logo displayed on this page, with a link to your website and a custom description text. Companies that have helped FreeCAD early on also appear under Gold sponsors.

Instead of donating each month, you might find it more comfortable to make a one-time donation that, when divided by twelve, would give you right to enter a sponsoring tier. Don't hesitate to do so!

Choose freely the amount you wish to donate each month.

Please inform your forum name or twitter handle as a notein your transfer, or reach to us, so we can give you proper credits!

Generic macro icon. Create your personal icon with the same name of the macro Macro Draft Circle Tangent

Description
Makes tangents to Draft circles.

Macro version: 0.2
Last modified: 2022-11-27
FreeCAD version: -
Author: heda
Author
heda
Download
None
Links
Macros recipes
How to install macros
How to customize toolbars
Macro Version
0.2
Date last modified
2022-11-27
FreeCAD Version(s)
-
Default shortcut
None
See also
None

Description

Macro Draft Circle Tangent

Usage

Select and launch.

Install

Through the Addon manager.

Version

v0.2 2022-11-27 : Fixed draft point selection in Tree view.

v0.1 2022-11-21 : First release.

Code

Macro_Draft_Circle_Tangent.FCMacro

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# ***************************************************************************
# *   Copyright (c) 2022 heda <heda @ freecad forum>                        *
# *                                                                         *
# *   This file is part of the FreeCAD CAx development system.              *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************


__Name__ = 'Draft_Circle_Tangent'
__Comment__ = 'Makes tangents to draft circles.'
__Author__ = 'heda @ fc-forum'
__Version__ = '0.2'
__Date__ = '2022-11-27'
__License__ = 'LGPL-2.0-or-later'
__Web__ = ''
__Wiki__ = 'https://wiki.freecad.org/Macro_Draft_Circle_Tangent'
__Icon__ = ''
__Help__ = 'Select and launch.'
__Status__ = 'functional'
__Requires__ = 'tested on FreeCAD v0.20'
__Communication__ = ''
__Files__ = ''


__doc__ = """
makes tangents for draft circles and arcs
a) circle and vertex/point
b) two circles

select before launching macro

for circle and point the tangents are constructed,
as one would do with pen and paper
for circle and circle the angles are calculated,
one can also construct the tangents in a similar way to
circle/point way, i.e. intersections by additional construction circles

note: tesselation often makes the true tangent not look like a true tangent
this is to not overload rendering for larger projects
one can change the viewsetting deviation for the circle to minimum (0.01)
if one wants to better view the true tangent, if so, better turn that back
to default once viewed - it is easy to forget that this has been done,
and can in larger projects put fc to more or less a complete halt.
"""

from math import sin, cos, pi, atan2, acos, asin

import Draft

doc = App.ActiveDocument
cgroup = doc.addObject('App::DocumentObjectGroup','Construction')
Vector, Placement = App.Vector, App.Placement
RotZero = Placement().Rotation

def get_curve(obj):
    return obj.Shape.Edge1.Curve

def point2vector(pt):
    return pt.toShape().Point

def get_intersection_points(obj1, obj2):
    doc.recompute()
    i1, i2 = get_curve(obj1).intersect(get_curve(obj2))
    return (point2vector(p) for p in (i1, i2))

def add_construction(obj):
    _ = cgroup.addObject(obj)

def mk_line(v1, v2, construction=True):
    line = Draft.make_line(v1, v2)
    if construction:
        line.ViewObject.DrawStyle = 'Dashdot'
        add_construction(line)
    return line

def mk_circle(centre, radius):
    circle = Draft.make_circle(radius, Placement(centre, RotZero), False)
    circle.ViewObject.DrawStyle = 'Dashed'
    add_construction(circle)
    return circle


## selection logic
SINGLE = False
msg = ('select exactly 2 items, 2 (draft) circles '
        'or 1 (draft) circle and one vertex or (draft) point')
selection = Gui.Selection.getSelection()
if len(selection) == 2:
    s1, s2 = selection
    r1, r2 = (hasattr(r, 'Radius') for r in selection)
    if r1 and r2:
        c1s, c2s = s1, s2
    elif not r1 and not r2:
        raise RunTimeWarning(msg)
    else:
        SINGLE = True
        circle = s1 if r1 else s2
        sx1, sx2 = Gui.Selection.getSelectionEx()
        sx = sx2 if r1 else sx1
        if sx.PickedPoints:
            point, = sx.PickedPoints
        else: # it is a draft point selected in tree view
            point = sx.Object.Shape.Point
    
else:
    raise RunTimeWarning(msg)

if SINGLE:

    o = circle.Placement.Base
    op = point - o

    ccircle = mk_circle(o + op/2, op.Length/2)

    v1, v2 = get_intersection_points(circle, ccircle)

    tangent1 = mk_line(point, v1, False)
    tangent2 = mk_line(point, v2, False)


else:

    # c1 is the larger one
    if c1s.Radius >= c2s.Radius:
        c1, c2 = c1s, c2s
    else:
        c1, c2 = c2s, c1s

    c1c, c2c = c1.Placement.Base, c2.Placement.Base

    centerline = mk_line(c1c, c2c)
    cc = c2c - c1c

    # construction circle outer tangent
    c3 = mk_circle(c1c, c1.Radius - c2.Radius)

    phi1 = atan2(cc.y, cc.x) + acos(c3.Radius / cc.Length)
    v1 = Vector(cos(phi1), sin(phi1), 0).normalize()
    line = mk_line(c1c, v1 * c1.Radius + c1c)
    ot1 = mk_line(c1c + v1 * c1.Radius, c2c + v1 * c2.Radius, False)

    phi2 = atan2(cc.y, cc.x) - acos(c3.Radius / cc.Length)
    v2 = Vector(cos(phi2), sin(phi2), 0).normalize()
    line2 = mk_line(c1c, v2 * c1.Radius + c1c)
    ot2 = mk_line(c1c + v2 * c1.Radius, c2c + v2 * c2.Radius, False)

    # construction circle inner tangent
    c4 = mk_circle(c2c, c1.Radius + c2.Radius)

    phi1 = atan2(cc.y, cc.x) + asin(c4.Radius / cc.Length) - pi/2
    v1 = Vector(cos(phi1), sin(phi1), 0).normalize()
    line = mk_line(c2c, -v1 * c2.Radius + c2c)
    ot1 = mk_line(c1c + v1 * c1.Radius, c2c - v1 * c2.Radius, False)

    phi2 = atan2(cc.y, cc.x) - asin(c4.Radius / cc.Length) + pi/2
    v2 = Vector(cos(phi2), sin(phi2), 0).normalize()
    line2 = mk_line(c2c, - v2 * c2.Radius + c2c)
    ot2 = mk_line(c1c + v2 * c1.Radius, c2c - v2 * c2.Radius, False)

cgroup.ViewObject.Visibility = False
doc.recompute()

This page is retrieved from https://wiki.freecad.org/Macro_Draft_Circle_Tangent

Get in touch!
Forum GitHub Mastodon Matrix IRC Gitter.im Discord Reddit Twitter Facebook LinkedIn

© The FreeCAD Team. Homepage image credits (top to bottom): ppemawm, r-frank, epileftric, regis, rider_mortagnais, bejant.

This project is supported by: , KiCad Services Corp. and other sponsors

GitHubImprove this page on GitHub