major rework of Sketch creation, now properly handles Arcs in addition to Points on a Polygon

This commit is contained in:
Morgan 'ARR\!' Allen 2025-10-15 11:32:56 -07:00
parent 35363fac75
commit 6a639f2c7f

View file

@ -3,6 +3,7 @@ import os
import FreeCAD as App
from . import settings
import Part
import Sketcher
from kipy.board_types import Footprint3DModel, BoardPolygon, BoardSegment, PadStackShape
from kipy.geometry import PolygonWithHoles, PolyLine, PolyLineNode, Vector2
@ -39,6 +40,7 @@ class BoardSketchObject(BaseObject):
def setup_properties(self, feature):
super(BoardSketchObject, self).setup_properties(feature)
# TODO remove in favor of getting BoardOffset from parent Board object
feature.addProperty('App::PropertyPlacement', 'BoardOffset', 'KiConnect', 'Internal offset for zeroing out Footprint offset', hidden=True, read_only=True)
feature.addProperty('App::PropertyVectorList', 'Vectors', 'KiConnect', 'Board Outline as Vectors', hidden=True)
@ -47,41 +49,95 @@ class BoardSketchObject(BaseObject):
vectors = []
# board.get_shapes needs to be called to ensure polygons are actually up to date
# XXX get_polygon should be moved to the Sketches parent Board object
# then the board should be making the api refresh board call and process
# the polygon, instead of that functionality being in the API
board_polygon = self.get_api().get_polygon(self.polygon_id)
if not board_polygon:
raise BoardPolyNotFoundException('Board Polygon not found: ' + self.polygon_id)
for node in board_polygon.polygons[0].outline:
vectors.append(self.point_to_vector(node.point))
self.feature.Vectors = vectors
begin = None
arc_count = 0
start = None
# this probably needs a bit more control..
feature.Constraints = []
feature.Geometry = []
outline = board_polygon.polygons[0].outline
line = []
for vector in self.feature.Vectors:
if not start:
start = vector
begin = vector
for node in outline:
if node.has_arc:
arc_count = arc_count + 1
continue
if arc_count % 2 == 0:
continue
feature.addGeometry(Part.LineSegment(start, vector))
start = vector
arc = node.arc
feature.addGeometry(Part.LineSegment(start, begin))
arc_start = self.point_to_vector(arc.start)
arc_mid = self.point_to_vector(arc.mid)
arc_end = self.point_to_vector(arc.end)
if not start:
start = arc_start
# if there is already part of a LineSegment, connect it to the start of this Arc
if len(line) == 1:
idx = feature.addGeometry(Part.LineSegment(line[0], arc_start))
feature.addConstraint(Sketcher.Constraint("Coincident", idx - 1, 2, idx, 1))
if line[0].x == arc_start.x:
feature.addConstraint(Sketcher.Constraint("Vertical", idx))
if line[0].y == arc_start.y:
feature.addConstraint(Sketcher.Constraint("Horizontal", idx))
line = []
idx = feature.addGeometry(Part.Arc(arc_start, arc_mid, arc_end))
if idx > 1:
feature.addConstraint(Sketcher.Constraint("Coincident", idx - 1, 2, idx, 1))
line.append(arc_end)
elif node.has_point:
vector = self.point_to_vector(node.point)
if not start:
start = vector
line.append(vector)
else:
raise Exception('Hit unknown geometry type in board polygon')
if len(line) > 2: raise Exception('Too many points in line segment', line)
if len(line) == 2:
idx = feature.addGeometry(Part.LineSegment(line[0], line[1]))
feature.addConstraint(Sketcher.Constraint("Coincident", idx - 1, 2, idx, 1))
if line[0].x == line[1].x:
feature.addConstraint(Sketcher.Constraint("Vertical", idx))
if line[0].y == line[1].y:
feature.addConstraint(Sketcher.Constraint("Horizontal", idx))
line = [ line[1] ]
if len(line) == 1:
idx = feature.addGeometry(Part.LineSegment(start, line[0]))
feature.addConstraint(Sketcher.Constraint("Coincident", 0, 1, idx, 1))
feature.addConstraint(Sketcher.Constraint("Coincident", idx - 1, 2, idx, 2))
else:
FreeCAD.Console.PrintError("Bad line segment:", line)
feature.recompute()
class BoardSketchViewProvider(BaseViewProvider):
TYPE = 'KiConnect::BoardSketch'
#ICON = 'kicad/board.svg'
def makeBoardSketch(parent, kicad_board, polygon):
feature = App.ActiveDocument.addObject('Sketcher::SketchObjectPython', 'BoardSketch')
parent.addObject(feature)