signal emit when closing a sketch
Forum rules
and Helpful information
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!
Also, be nice to others! Read the FreeCAD code of conduct!
- ebrahim raeyat
- Posts: 621
- Joined: Sun Sep 09, 2018 7:00 pm
- Location: Iran
- Contact:
signal emit when closing a sketch
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:
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.
I have an axis that their lines is very close to each other, like below:
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.
Re: signal emit when closing a sketch
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)?
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)?
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Re: signal emit when closing a sketch
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.
- 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 32 times
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
- ebrahim raeyat
- Posts: 621
- Joined: Sun Sep 09, 2018 7:00 pm
- Location: Iran
- Contact:
Re: signal emit when closing a sketch
I use sketcher, here is my scripts: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)?
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))
- ebrahim raeyat
- Posts: 621
- Joined: Sun Sep 09, 2018 7:00 pm
- Location: Iran
- Contact:
Re: signal emit when closing a sketch
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 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.
Re: signal emit when closing a sketch
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".
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Re: signal emit when closing a sketch
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 15 times
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
- ebrahim raeyat
- Posts: 621
- Joined: Sun Sep 09, 2018 7:00 pm
- Location: Iran
- Contact:
Re: signal emit when closing a sketch
Thanks. I apply expression, but it did not work.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".
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.
Code: Select all
t.setExpression('.Placement.Base', f'{sketch.Name}.Geometry[{i}].Location')
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))
- ebrahim raeyat
- Posts: 621
- Joined: Sun Sep 09, 2018 7:00 pm
- Location: Iran
- Contact:
Re: signal emit when closing a sketch
very thanks. it is exactly what i want. Thanks.
Re: signal emit when closing a sketch
Glad to help. As you can see, I gave names to the dimensions, which made it easy to reference them.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.