Sketcher: show bspline control point using python

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
onekk
Veteran
Posts: 6145
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Sketcher: show bspline control point using python

Post by onekk »

from: https://forum.freecadweb.org/viewtopic. ... 55#p197155

I have thought that it will work, so I've put together this code to show the control points:

Code: Select all

import FreeCAD
import FreeCADGui
import Sketcher
import Part

from FreeCAD import Vector, Rotation


DOC_NAME = "test_sketch"
DOC = FreeCAD.activeDocument()

# CMPN_NAME = "Components"
# DOC.addObject("App::DocumentObjectGroup",CMPN_NAME)

def clear_doc():
    """
    Clear the active document deleting all the objects
    """
    for obj in DOC.Objects:
       DOC.removeObject(obj.Name)

if DOC is None:
    FreeCAD.newDocument(DOC_NAME)
    FreeCAD.setActiveDocument(DOC_NAME)
    DOC = FreeCAD.activeDocument()
    VIEW = FreeCAD.Gui.ActiveDocument.ActiveView
else:
    clear_doc()
    VIEW = FreeCAD.Gui.ActiveDocument.ActiveView

def setview():
    """Rearrange View"""
    DOC.recompute()
    VIEW.viewAxometric()
    VIEW.setAxisCross(True)
    VIEW.fitAll()


# BSPLINE

p1 = Vector(-371.377228,250.588409,0)
p2 = Vector(-381.755615,175.192322,0)
p3 = Vector(-346.957428,157.182724,0)
p4 = Vector(-320.400909,212.432480,0)
p5 = Vector(-330.474060,269.513733,0)

spline = Part.BSplineCurve([p1, p2, p3, p4, p5],None,None,False,3,None,False)

sk = DOC.addObject('Sketcher::SketchObject','Sketch')
sk.Placement = App.Placement(App.Vector(0.000000, 0.000000, 0.000000), App.Rotation(0.000000, 0.000000, 0.000000, 1.000000))

FreeCADGui.ActiveDocument.setEdit(sk.Name)

sk.addGeometry(spline, False)
# Sketcher magic function :
sk.exposeInternalGeometry(0)

DOC.recompute()

#print(dir(sk))


#FreeCADGui.Selection.addSelection(DOC.Name,sk.Name)
VIEW.viewTop()
VIEW.setAxisCross(True)
#VIEW.fitSelection()
but with Appimage of FreeCAD 0.20, Libs: 0.20R25288

the result is:
sketcher.png
sketcher.png (69.09 KiB) Viewed 1424 times
Now from the code above, i have tried to select the shape and then show (fit selection), but I haven't found a way to visualize the curve with the control points in "edit mode" enlarged to fit the screen:

sadly a:

Code: Select all

print(dir(FreeCADGui.ActiveDocument.ActiveView)
will show only a fitAll

So I have some questions about sketcher:

1) how to select the curve and fit it to the screen
2) eventually toggle the grid off?


TIA and Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply