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