96 lines
3.5 KiB
Python
96 lines
3.5 KiB
Python
import os
|
|
import FreeCADGui as Gui
|
|
import FreeCAD as App
|
|
import Materials
|
|
import Part
|
|
|
|
from kipy import KiCad
|
|
from kipy.board_types import Footprint3DModel, BoardPolygon, BoardSegment, PadStackShape
|
|
from kipy.util.board_layer import BoardLayer
|
|
|
|
from . import settings
|
|
from .bases import BaseObject, BaseViewProvider
|
|
|
|
gold_mat_uuid = '85257e2c-be3f-40a1-b03f-0bd4ba58ca08'
|
|
materials_manager = Materials.MaterialManager()
|
|
gold = materials_manager.Materials[gold_mat_uuid]
|
|
|
|
class CopperObject(BaseObject):
|
|
TYPE = 'KiConnect::Copper'
|
|
|
|
class CopperViewProvider(BaseViewProvider):
|
|
ICON = 'kicad/show_all_copper_layers.svg'
|
|
TYPE = 'KiConnect::Copper'
|
|
|
|
class Copper():
|
|
def __init__(self, kicad_board, kiconn_board):
|
|
self.nets = {}
|
|
|
|
self.kicad_board = kicad_board
|
|
self.kiconn_board = kiconn_board
|
|
|
|
feature = App.ActiveDocument.addObject('App::DocumentObjectGroupPython', 'Copper')
|
|
|
|
CopperObject(self, feature)
|
|
CopperViewProvider(self, feature.ViewObject)
|
|
|
|
self.feature = feature
|
|
|
|
self.create_net_sketches()
|
|
self.draw_nets()
|
|
|
|
feature.recompute()
|
|
|
|
def create_net_sketches(self):
|
|
for net in self.kicad_board.get_nets():
|
|
# this needs to be handled better, if there is an empty net, it will result
|
|
# in an empty sketch and an error
|
|
if net.name == '': continue
|
|
|
|
face = App.ActiveDocument.addObject('Part::Face', net.name)
|
|
face.ShapeMaterial = gold
|
|
'''
|
|
if self.feature.HideUnconnected and net.name.startswith('unconnected_'):
|
|
face.Hidden = True
|
|
'''
|
|
|
|
sketch = App.ActiveDocument.addObject('Sketcher::SketchObject')
|
|
sketch.Placement.Base.z = settings.BOARD_THICKNESS + 0.01
|
|
sketch.Visibility = False
|
|
|
|
face.Sources = sketch
|
|
|
|
self.nets[net.name] = face
|
|
self.feature.addObject(face)
|
|
|
|
def draw_nets(self):
|
|
# setup or clear net arrays/geometry
|
|
for net_name in self.nets:
|
|
self.nets[net_name].Sources[0].Geometry = []
|
|
|
|
for pad in self.kicad_board.get_pads():
|
|
if BoardLayer.BL_F_Cu not in pad.padstack.layers:
|
|
continue
|
|
|
|
f_cu = pad.padstack.copper_layer(BoardLayer.BL_F_Cu)
|
|
|
|
center = (App.Vector(pad.position.x, -pad.position.y) / 1000000.0) - self.kiconn_board.offset
|
|
|
|
sketch = self.nets[pad.net.name].Sources[0]
|
|
|
|
if f_cu.shape in [ PadStackShape.PSS_ROUNDRECT, PadStackShape.PSS_RECTANGLE ]:
|
|
size = App.Vector(f_cu.size.x, f_cu.size.y) / 1000000.0 / 2
|
|
|
|
sketch.addGeometry([
|
|
Part.LineSegment(App.Vector(center.x + size.x, center.y + size.y), App.Vector(center.x + size.x, center.y - size.y)),
|
|
Part.LineSegment(App.Vector(center.x + size.x, center.y - size.y), App.Vector(center.x - size.x, center.y - size.y)),
|
|
Part.LineSegment(App.Vector(center.x - size.x, center.y - size.y), App.Vector(center.x - size.x, center.y + size.y)),
|
|
Part.LineSegment(App.Vector(center.x - size.x, center.y + size.y), App.Vector(center.x + size.x, center.y + size.y)),
|
|
])
|
|
elif f_cu.shape == PadStackShape.PSS_CIRCLE:
|
|
drill = pad.padstack.drill
|
|
|
|
if drill.diameter.x == drill.diameter.y:
|
|
rad = drill.diameter.x / 1000000.0 / 2
|
|
sketch.addGeometry(Part.Circle(App.Vector(center.x, center.y), App.Vector(0, 0, 1), rad))
|
|
|