86 lines
2.7 KiB
Python
86 lines
2.7 KiB
Python
import os
|
|
|
|
import FreeCAD as App
|
|
from . import settings
|
|
import Part
|
|
|
|
from kipy.board_types import Footprint3DModel, BoardPolygon, BoardSegment, PadStackShape
|
|
from kipy.geometry import PolygonWithHoles, PolyLine, PolyLineNode, Vector2
|
|
from kipy.proto.common.types import KiCadObjectType
|
|
from kipy.util.board_layer import BoardLayer
|
|
|
|
from .bases import BaseObject, BaseViewProvider
|
|
|
|
class BoardSketchObject(BaseObject):
|
|
TYPE = 'KiConnect::BoardSketch'
|
|
|
|
save_keys = [ 'polygon_id' ]
|
|
|
|
def __init__(self, feature, kicad_board, board_polygon):
|
|
self.board_polygon = board_polygon
|
|
self.polygon_id = board_polygon.id.value
|
|
|
|
super(BoardSketchObject, self).__init__(feature)
|
|
|
|
#feature.Visibility = False
|
|
|
|
def execute(self, feature):
|
|
feature.recompute()
|
|
|
|
def point_to_vector(self, point, offset=True):
|
|
return (
|
|
App.Vector(point.x,
|
|
-point.y
|
|
)) / 1000000.0 - (self.feature.BoardOffset.Base if offset else 0)
|
|
|
|
def setup_properties(self, feature):
|
|
super(BoardSketchObject, self).setup_properties(feature)
|
|
|
|
feature.addProperty('App::PropertyPlacement', 'BoardOffset', 'KiConnect', 'Internal offset for zeroing out Footprint offset', hidden=True, read_only=True)
|
|
feature.addProperty('App::PropertyVectorList', 'Vectors', 'KiConnect', 'Internal offset for zeroing out Footprint offset', hidden=True)
|
|
|
|
def sync_from(self):
|
|
feature = self.feature
|
|
vectors = []
|
|
|
|
# board.get_shapes needs to be called to ensure polygons are actually up to date
|
|
board_polygon = self.get_api().get_polygon(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
|
|
start = None
|
|
|
|
# this probably needs a bit more control..
|
|
feature.Constraints = []
|
|
feature.Geometry = []
|
|
|
|
for vector in self.feature.Vectors:
|
|
if not start:
|
|
start = vector
|
|
begin = vector
|
|
|
|
continue
|
|
|
|
feature.addGeometry(Part.LineSegment(start, vector))
|
|
start = vector
|
|
|
|
feature.addGeometry(Part.LineSegment(start, begin))
|
|
|
|
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)
|
|
|
|
BoardSketchObject(feature, kicad_board, polygon)
|
|
BoardSketchViewProvider(feature.ViewObject)
|
|
|
|
return feature
|