Macro FlattenWire3Points

From FreeCAD Documentation
Revision as of 18:21, 26 February 2016 by Renatorivo (talk | contribs) (Created page with "Macro FlattenWire3Points")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

File:Text-x-python FlattenWire3Points

Description
This macro flattens draft wires that are not planar on a plane defined by 3 points

Author: Yorik
Author
Yorik
Download
None
Links
Macro Version
1.0
Date last modified
None
FreeCAD Version(s)
None
Default shortcut
None
See also
None

This macro flattens draft wires that are not planar on a plane defined by 3 points. To use this macro, select first 3 vertices from a single Draft Wire.

 import FreeCAD,FreeCADGui,Draft
 
 # check selection
 sel = FreeCADGui.Selection.getSelectionEx()
 ok = True
 if len(sel) != 1:
    FreeCAD.Console.PrintError("Please select 3 vertices from one Draft wire\n")
    ok = False
 sel = sel[0]
 if Draft.getType(sel.Object) not in ["Wire","BSpline"]:
    FreeCAD.Console.PrintError("Please select 3 vertices from one Draft wire\n")
    ok = False
 if len(sel.SubElementNames) != 3:
    FreeCAD.Console.PrintError("Please select 3 vertices from one Draft wire\n")
    ok = False
 for e in sel.SubElementNames:
    if not "Vertex" in e:
        FreeCAD.Console.PrintError("Please select 3 vertices from one Draft wire\n")
        ok = False
 
 if ok:
    # define a plane
    p1 = getattr(sel.Object.Shape,sel.SubElementNames[0]).Point
    p2 = getattr(sel.Object.Shape,sel.SubElementNames[1]).Point
    p3 = getattr(sel.Object.Shape,sel.SubElementNames[2]).Point
    p4 = p2.sub(p1).cross(p3.sub(p1))
    
    # project wire points
    points = []
    for p in sel.Object.Points:
        points.append(p.projectToPlane(p1,p4))
    sel.Object.Points = points
Other languages: