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: 250
Joined: Sun Sep 09, 2018 7:00 pm
Location: Iran
Contact:

signal emit when closing a sketch

Postby ebrahim raeyat » Fri Sep 27, 2019 3:30 am

First excuse me for posting sketch related subject in this forum, because i didn't find related subject for SKETCH.

I have an axis that their lines is very close to each other, like below:
axis.png
axis.png (12.67 KiB) Viewed 241 times

i decide to draw them with sketch to allowing the user to move them in a desired place. then i want to know the signal that emit when a sketch is closed to move text label in appropriate place in circles.
axis1.png
axis1.png (5.65 KiB) Viewed 241 times
axis2.png
axis2.png (4.47 KiB) Viewed 241 times
chrisb
Posts: 22526
Joined: Tue Mar 17, 2015 9:14 am

Re: signal emit when closing a sketch

Postby chrisb » Fri Sep 27, 2019 4:46 am

Moved to Help forum. That's where Sketcher issues are discussed.

If I understand you right the issue is to keep the text centered inside of the circles right? Are your Sketches created from PartDesign workbench (i.e. inside of a PartDesign body) or from Sketcher (outside of PartDesign body)?
chrisb
Posts: 22526
Joined: Tue Mar 17, 2015 9:14 am

Re: signal emit when closing a sketch

Postby chrisb » Fri Sep 27, 2019 5:33 am

Here is my PartDesign proposal:
- Create a sketch with only the degrees of freedom left, the user is allowed to move
- Create a ShapeString
- Move the ShapeString into the body
- Create a DatumPoint in the middle of the circle
- Attach theShapeString to the DatumPoint
- Change the AttachmentOffset of the ShapeString so that it is centered in the circle.

Now the ShapeString follows changes made in Sketcher. If AutoUpdate is unchecked, the Update will happen as soon as Sketcher is closed.
Attachments
movingShapeString.FCStd
(7.56 KiB) Downloaded 1 time
User avatar
ebrahim raeyat
Posts: 250
Joined: Sun Sep 09, 2018 7:00 pm
Location: Iran
Contact:

Re: signal emit when closing a sketch

Postby ebrahim raeyat » Fri Sep 27, 2019 1:11 pm

chrisb wrote:
Fri Sep 27, 2019 4:46 am
Moved to Help forum. That's where Sketcher issues are discussed.

If I understand you right the issue is to keep the text centered inside of the circles right? Are your Sketches created from PartDesign workbench (i.e. inside of a PartDesign body) or from Sketcher (outside of PartDesign body)?
I use sketcher, here is my scripts:

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

# x_grid_group.addObject(x_sketch)


def create_x_grid(sketch, xcoord, text):
    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))


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

    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, 5000], ['A', 'B', 'C'], x_sketch)
# sketch.addConstraint(Sketcher.Constraint('Coincident',i+3,2,i+4,1))
User avatar
ebrahim raeyat
Posts: 250
Joined: Sun Sep 09, 2018 7:00 pm
Location: Iran
Contact:

Re: signal emit when closing a sketch

Postby ebrahim raeyat » Fri Sep 27, 2019 1:19 pm

chrisb wrote:
Fri Sep 27, 2019 5:33 am
Here is my PartDesign proposal:
- Create a sketch with only the degrees of freedom left, the user is allowed to move
- Create a ShapeString
- Move the ShapeString into the body
- Create a DatumPoint in the middle of the circle
- Attach theShapeString to the DatumPoint
- Change the AttachmentOffset of the ShapeString so that it is centered in the circle.

Now the ShapeString follows changes made in Sketcher. If AutoUpdate is unchecked, the Update will happen as soon as Sketcher is closed.
yes, it works and thanks a lot, i examined that, but i think when i want to export that to pdf or dxf it is time consuming for exporting a letter shape, isn't it? or this is the best solution? thanks.
chrisb
Posts: 22526
Joined: Tue Mar 17, 2015 9:14 am

Re: signal emit when closing a sketch

Postby chrisb » Fri Sep 27, 2019 4:24 pm

If you don't use PartDesign, you can use Expressions for the Placement of the Letters. Besides that, just try if it is too time consuming, or as we say in FreeCAD: "It's done when it's done".
chrisb
Posts: 22526
Joined: Tue Mar 17, 2015 9:14 am

Re: signal emit when closing a sketch

Postby chrisb » Fri Sep 27, 2019 5:03 pm

For my taste there are too many degrees of Freedom. Here is how I would do it outside of PartDesign (Letter C only):
Attachments
movingText.FCStd
(7.11 KiB) Downloaded 2 times
User avatar
ebrahim raeyat
Posts: 250
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 7:15 am

chrisb wrote:
Fri Sep 27, 2019 4:24 pm
If you don't use PartDesign, you can use Expressions for the Placement of the Letters. Besides that, just try if it is too time consuming, or as we say in FreeCAD: "It's done when it's done".
Thanks. I apply expression, but it did not work.
expersion.png
expersion.png (9.22 KiB) Viewed 149 times
when i apply it in model, it did not give me an error but when i move circle, it gives me this error:

Code: Select all

>>> t.setExpression('.Placement.Base', 'x_sketch.Geometry[3].Location')
>>> doc.recompute()
1

Code: Select all

<Exception> Property '' not found
in property binding 'Placement'
Recompute failed! Please check report view.
this is how i use it in script:

Code: Select all

t.setExpression('.Placement.Base', f'{sketch.Name}.Geometry[{i}].Location')
complete file:

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
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'
    t.setExpression('.Placement.Base', f'{sketch.Name}.Geometry[{i}].Location')

    # 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))


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, 5000], ['A', 'B', 'C'], x_sketch)
# sketch.addConstraint(Sketcher.Constraint('Coincident',i+3,2,i+4,1))
User avatar
ebrahim raeyat
Posts: 250
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 7:48 am

chrisb wrote:
Fri Sep 27, 2019 5:03 pm
For my taste there are too many degrees of Freedom. Here is how I would do it outside of PartDesign (Letter C only):
very thanks. it is exactly what i want. Thanks.
chrisb
Posts: 22526
Joined: Tue Mar 17, 2015 9:14 am

Re: signal emit when closing a sketch

Postby chrisb » Sat Sep 28, 2019 8:50 am

Glad to help. As you can see, I gave names to the dimensions, which made it easy to reference them.