Macro Perpendicular To Wire

From FreeCAD Documentation
Revision as of 21:43, 28 January 2017 by Mario52 (talk | contribs) (Created page with "Macro Perpendicular To Wire")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

File:Text-x-python Macro Perpendicular To Wire

Description
This macro positioned your object perpendicularly to wire selected.

Macro version: 0.0 (22/01/2017)
Author: Mario52
Author
Mario52
Download
None
Links
Macro Version
0.0 (22/01/2017)
Date last modified
None
FreeCAD Version(s)
None
Default shortcut
None
See also
None

Description

This macro positioned your object perpendicularly to wire selected.

Use

  1. : select the path
  2. : select the object to align
  3. : run the macro

Script

Macro Perpendicular To Wire.FCMacro

__title__   = "Macro Perpendicular To Wire"
__author__  = "Mario52"
__url__     = "https://www.freecadweb.org/wiki/index.php?title=Macro_Perpendicular_To_Wire"
__version__ = "00.00"
__date__ = "22/01/2017"

import Draft, Part

try:
    sel = FreeCADGui.Selection.getSelection()                               # Select an object
    
    lineSelected = sel[0]                                                   # first object the Path
    myCircle     = sel[1]                                                   # second object
    
    pointsDirection  = []
    pointsDirection = lineSelected.Shape.discretize(Number=500)             # discretize the path line first selection
    
    v=pointsDirection[0].sub(pointsDirection[1])                            # avec vecteurs 1 et 2 (direction debut ligne)
    r=App.Rotation(App.Vector(0,0,1),v)
    
    pl=FreeCAD.Placement()                                                  # placement object
    pl.Rotation.Q = r.Q
    pl.Base = pointsDirection[0]
    myCircle.Placement = pl
    
    del pointsDirection[:]
    FreeCAD.ActiveDocument.recompute()
except Exception:
    print "Select twoo objects 1:The path 2:The objet to align, Not subObject"

Options

Principle :

the line is cut in x points with "discretize()" (here Number=500 cuts begin 0 to 499 you can modify)

pointsDirection = lineSelected.Shape.discretize(Number=500)             # discretize the path line first selection

the perpendicularity is calculate between 2 points modify :

1:

v=pointsDirection[0].sub(pointsDirection[1])          # perpendicular of first > second point

2:

v=pointsDirection[-1].sub(pointsDirection[-2])       # perpendicular of last > before last point
pl.Base = pointsDirection[-1]                        # position base last point

3:

v=pointsDirection[100].sub(pointsDirection[101])   # perpendicular of point 100 > point 101
pl.Base = pointsDirection[100]                     # position base point 100

4:

v=pointsDirection[0].sub(pointsDirection[-1])         # perpendicular of first point > last point
pl.Base = pointsDirection[0]                          # position base first point

for discretize other parameters

#http://forum.freecadweb.org/viewtopic.php?f=12&t=16336#p129468
#Discretizes the edge and returns a list of points.
#The function accepts keywords as argument:
#discretize(Number=n) => gives a list of 'n' equidistant points
#discretize(QuasiNumber=n) => gives a list of 'n' quasi equidistant points (is faster than the method above)
#discretize(Distance=d) => gives a list of equidistant points with distance 'd'
#discretize(Deflection=d) => gives a list of points with a maximum deflection 'd' to the edge
#discretize(QuasiDeflection=d) => gives a list of points with a maximum deflection 'd' to the edge (faster)
#discretize(Angular=a,Curvature=c,[Minimum=m]) => gives a list of points with an angular deflection of 'a'
#and a curvature deflection of 'c'. Optionally a minimum number of points
#can be set which by default is set to 2.

The discussion on the forum [Spiralbohrer]

Other languages: