signal emit when closing a sketch

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
User avatar
ebrahim raeyat
Posts: 181
Joined: Sun Sep 09, 2018 7:00 pm
Location: Iran
Contact:

Re: signal emit when closing a sketch

Postby ebrahim raeyat » Sat Sep 28, 2019 9:56 am

Thanks @chrisb. this is my complete script:

Code: Select all

import Sketcher
import FreeCAD
import FreeCADGui as Gui
import Part
import Draft

font_size = 1500
XMIN = -1000
YMIN = -2000
XMAX = 10000
YMAX = 20000
o = 500
l = 1000
r = 1500
y1 = YMAX + o
y2 = y1 + 2 * l
y3 = y2 + l
y4 = y3 + l
yc = y4 + r
yt = font_size / 3
no_of_edge_in_on_axis = 4

# x_grid_group.addObject(x_sketch)


def create_x_grid(sketch, xcoord, text, i):
    g = Part.makeLine(FreeCAD.Vector(xcoord, YMIN, 0), FreeCAD.Vector(xcoord, YMAX, 0))
    Part.show(g)
    # x_grid_group.addObject(g0)
    g1 = sketch.addGeometry(Part.LineSegment(FreeCAD.Vector(xcoord, y1, 0), FreeCAD.Vector(xcoord, y2, 0)))
    g2 = sketch.addGeometry(Part.LineSegment(FreeCAD.Vector(xcoord, y2, 0), FreeCAD.Vector(xcoord, y3, 0)))
    g3 = sketch.addGeometry(Part.LineSegment(FreeCAD.Vector(xcoord, y3, 0), FreeCAD.Vector(xcoord, y4, 0)))
    g4 = sketch.addGeometry(Part.Circle(App.Vector(xcoord, yc, 0), App.Vector(0, 0, 1), r), False)

    t = Draft.makeText(text, App.Vector(xcoord, yc - font_size / 3, 0))
    t.ViewObject.FontSize = font_size
    t.ViewObject.Justification = 'Center'

    # x_grid_group.addObject(t)
    sketch.addConstraint(Sketcher.Constraint('DistanceX', g1, 1, xcoord))
    sketch.addConstraint(Sketcher.Constraint('DistanceY', g1, 1, y1))
    sketch.addConstraint(Sketcher.Constraint('Coincident', g1, 2, g2, 1))
    sketch.addConstraint(Sketcher.Constraint('Coincident', g2, 2, g3, 1))
    sketch.addConstraint(Sketcher.Constraint('Vertical', g1))
    sketch.addConstraint(Sketcher.Constraint('Vertical', g3))
    sketch.addConstraint(Sketcher.Constraint('Radius', g4, r))
    sketch.addConstraint(Sketcher.Constraint('PointOnObject', g3, 2, g4))
    sketch.addConstraint(Sketcher.Constraint('Perpendicular', g4, g3))
    const_num = sketch.addConstraint(Sketcher.Constraint('DistanceX', -1, 1, g4, 3, xcoord))
    doc.recompute()
    sketch.renameConstraint(const_num, f'{text}_x')
    sketch.toggleDriving(const_num)
    doc.recompute()
    const_num = sketch.addConstraint(Sketcher.Constraint('DistanceY', -1, 1, g4, 3, yc))
    sketch.renameConstraint(const_num, f'{text}_y')
    sketch.toggleDriving(const_num)
    t.setExpression('.Placement.Base.x', f'{sketch.Name}.Constraints.{text}_x')
    t.setExpression('.Placement.Base.y', f'{sketch.Name}.Constraints.{text}_y - {yt} mm')

    if not i == no_of_edge_in_on_axis - 1:
        sketch.addConstraint(Sketcher.Constraint('DistanceY', g4, 3, g4 - no_of_edge_in_on_axis, 3, 0))


def create_grids(coords, texts, sketch, direction='x'):
    if direction == 'x':
        # x_circles_vertex_numbers = []
        i = no_of_edge_in_on_axis - 1
        for coord, text in zip(coords, texts):
            # x_circles_vertex_numbers.append(i * 7)
            create_x_grid(sketch, coord, text, i)
            i += no_of_edge_in_on_axis
    elif direction == 'y':
        # y_circles_vertex_numbers = []
        i = no_of_edge_in_on_axis - 1
        for coord, text in zip(coords, texts):
            # y_circles_vertex_numbers.append(i * 7)
            create_x_grid(sketch, coord, text, i)
            i += no_of_edge_in_on_axis

    doc.recompute()


doc = App.newDocument("grid")
# x_grid_group = doc.addObject("App::DocumentObjectGroup", "x_grid")
x_sketch = doc.addObject('Sketcher::SketchObject', 'x_sketch')
create_grids([0, 500, 1000, 1500], ['A', 'B', 'C', 'D'], x_sketch)
# sketch.addConstraint(Sketcher.Constraint('Coincident',i+3,2,i+4,1))
User avatar
ebrahim raeyat
Posts: 181
Joined: Sun Sep 09, 2018 7:00 pm
Location: Iran
Contact:

Re: signal emit when closing a sketch

Postby ebrahim raeyat » Sun Sep 29, 2019 7:32 am

This is my complete code for x and y axis:

Code: Select all

import Sketcher
import FreeCAD
import FreeCADGui as Gui
import Part
import Draft

font_size = 500
o = 500
l = 500
r = 500
yt = font_size / 3
no_of_edge_in_on_axis = 4

# x_grid_group.addObject(x_sketch)


def create_x_grid(sketch, xcoord, text, i, YMIN, YMAX, y1, y2, y3, y4, yc):
    g = Part.makeLine(FreeCAD.Vector(xcoord, YMIN, 0), FreeCAD.Vector(xcoord, YMAX, 0))
    # g.DrawStyle = "Dashdot"
    Part.show(g)
    # x_grid_group.addObject(g0)
    g1 = sketch.addGeometry(Part.LineSegment(FreeCAD.Vector(xcoord, y1, 0), FreeCAD.Vector(xcoord, y2, 0)))
    g2 = sketch.addGeometry(Part.LineSegment(FreeCAD.Vector(xcoord, y2, 0), FreeCAD.Vector(xcoord, y3, 0)))
    g3 = sketch.addGeometry(Part.LineSegment(FreeCAD.Vector(xcoord, y3, 0), FreeCAD.Vector(xcoord, y4, 0)))
    g4 = sketch.addGeometry(Part.Circle(FreeCAD.Vector(xcoord, yc, 0), FreeCAD.Vector(0, 0, 1), r), False)

    t = Draft.makeText(text, FreeCAD.Vector(xcoord, yc, 0))
    t.ViewObject.FontSize = font_size
    t.ViewObject.Justification = 'Center'

    # x_grid_group.addObject(t)
    sketch.addConstraint(Sketcher.Constraint('DistanceX', g1, 1, xcoord))
    sketch.addConstraint(Sketcher.Constraint('DistanceY', g1, 1, y1))
    sketch.addConstraint(Sketcher.Constraint('Coincident', g1, 2, g2, 1))
    sketch.addConstraint(Sketcher.Constraint('Coincident', g2, 2, g3, 1))
    sketch.addConstraint(Sketcher.Constraint('Vertical', g1))
    sketch.addConstraint(Sketcher.Constraint('Vertical', g3))
    sketch.addConstraint(Sketcher.Constraint('Radius', g4, r))
    sketch.addConstraint(Sketcher.Constraint('PointOnObject', g3, 2, g4))
    sketch.addConstraint(Sketcher.Constraint('Perpendicular', g4, g3))
    const_num = sketch.addConstraint(Sketcher.Constraint('DistanceX', -1, 1, g4, 3, xcoord))
    sketch.renameConstraint(const_num, f'{text}_x')
    sketch.toggleDriving(const_num)
    const_num = sketch.addConstraint(Sketcher.Constraint('DistanceY', -1, 1, g4, 3, yc))
    sketch.renameConstraint(const_num, f'{text}_y')
    sketch.toggleDriving(const_num)
    t.setExpression('.Placement.Base.x', f'{sketch.Name}.Constraints.{text}_x')
    t.setExpression('.Placement.Base.y', f'{sketch.Name}.Constraints.{text}_y - {yt} mm')

    if not i == no_of_edge_in_on_axis - 1:
        sketch.addConstraint(Sketcher.Constraint('DistanceY', g4, 3, g4 - no_of_edge_in_on_axis, 3, 0))
        sketch.addConstraint(Sketcher.Constraint('DistanceY', g1, 2, g1 - no_of_edge_in_on_axis, 2, 0))


def create_y_grid(sketch, ycoord, text, i, XMIN, XMAX, x1, x2, x3, x4, xc):
    text = str(text)
    g = Part.makeLine(FreeCAD.Vector(XMIN, ycoord, 0), FreeCAD.Vector(XMAX, ycoord, 0))
    # g.DrawStyle = "Dashdot"
    Part.show(g)
    # x_grid_group.addObject(g0)
    g1 = sketch.addGeometry(Part.LineSegment(FreeCAD.Vector(x1, ycoord, 0), FreeCAD.Vector(x2, ycoord, 0)))
    g2 = sketch.addGeometry(Part.LineSegment(FreeCAD.Vector(x2, ycoord, 0), FreeCAD.Vector(x3, ycoord, 0)))
    g3 = sketch.addGeometry(Part.LineSegment(FreeCAD.Vector(x3, ycoord, 0), FreeCAD.Vector(x4, ycoord, 0)))
    g4 = sketch.addGeometry(Part.Circle(FreeCAD.Vector(xc, ycoord, 0), FreeCAD.Vector(0, 0, 1), r), False)

    t = Draft.makeText(text, FreeCAD.Vector(xc, ycoord, 0))
    t.ViewObject.FontSize = font_size
    t.ViewObject.Justification = 'Center'

    # x_grid_group.addObject(t)
    sketch.addConstraint(Sketcher.Constraint('DistanceY', g1, 1, ycoord))
    sketch.addConstraint(Sketcher.Constraint('DistanceX', g1, 1, x1))
    sketch.addConstraint(Sketcher.Constraint('Coincident', g1, 2, g2, 1))
    sketch.addConstraint(Sketcher.Constraint('Coincident', g2, 2, g3, 1))
    sketch.addConstraint(Sketcher.Constraint('Horizontal', g1))
    sketch.addConstraint(Sketcher.Constraint('Horizontal', g3))
    sketch.addConstraint(Sketcher.Constraint('Radius', g4, r))
    sketch.addConstraint(Sketcher.Constraint('PointOnObject', g3, 2, g4))
    sketch.addConstraint(Sketcher.Constraint('Perpendicular', g4, g3))
    const_num = sketch.addConstraint(Sketcher.Constraint('DistanceY', -1, 1, g4, 3, ycoord))
    sketch.renameConstraint(const_num, f'y_{text}')
    sketch.toggleDriving(const_num)
    const_num = sketch.addConstraint(Sketcher.Constraint('DistanceX', -1, 1, g4, 3, xc))
    sketch.renameConstraint(const_num, f'x_{text}')
    sketch.toggleDriving(const_num)
    t.setExpression('.Placement.Base.x', f'{sketch.Name}.Constraints.x_{text}')
    t.setExpression('.Placement.Base.y', f'{sketch.Name}.Constraints.y_{text}')

    if not i == no_of_edge_in_on_axis - 1:
        sketch.addConstraint(Sketcher.Constraint('DistanceX', g4, 3, g4 - no_of_edge_in_on_axis, 3, 0))
        sketch.addConstraint(Sketcher.Constraint('DistanceX', g1, 2, g1 - no_of_edge_in_on_axis, 2, 0))


def create_grids(grids, boundBox, direction='x'):
    XMIN = boundBox.XMin
    XMAX = boundBox.XMax
    YMIN = boundBox.YMin
    YMAX = boundBox.YMax

    coords = list(grids.values())
    texts = list(grids.keys())
    doc = FreeCAD.ActiveDocument
    if direction == 'x':
        y1 = YMAX + o
        y2 = y1 + l
        y3 = y2 + l
        y4 = y3 + l
        yc = y4 + r
        sketch = doc.addObject('Sketcher::SketchObject', 'x_sketch')
        # x_circles_vertex_numbers = []
        i = no_of_edge_in_on_axis - 1
        for coord, text in zip(coords, texts):
            # x_circles_vertex_numbers.append(i * 7)
            create_x_grid(sketch, coord, text, i, YMIN, YMAX, y1, y2, y3, y4, yc)
            i += no_of_edge_in_on_axis
    elif direction == 'y':
        x1 = XMIN - o
        x2 = x1 - l
        x3 = x2 - l
        x4 = x3 - l
        xc = x4 - r
        sketch = doc.addObject('Sketcher::SketchObject', 'y_sketch')
        i = no_of_edge_in_on_axis - 1
        for coord, text in zip(coords, texts):
            print(text, type(text))
            # x_circles_vertex_numbers.append(i * 7)
            create_y_grid(sketch, coord, text, i, XMIN, XMAX, x1, x2, x3, x4, xc)
            i += no_of_edge_in_on_axis

    doc.recompute()


if __name__ == '__main__':
    doc = FreeCAD.newDocument("grid")
    # x_grid_group = doc.addObject("App::DocumentObjectGroup", "x_grid")

    create_grids([0, 500, 1000, 1500], ['A', 'B', 'C', 'D'], x_sketch)
# sketch.addConstraint(Sketcher.Constraint('Coincident',i+3,2,i+4,1))
before correction:
axis1.png
axis1.png (201.33 KiB) Viewed 34 times
after correction:
axis2.png
axis2.png (219.02 KiB) Viewed 34 times