MultiExport-FreeCAD/Calepinage.FCMacro

189 lines
6.2 KiB
Plaintext

import importSVG
import FreeCAD
import FreeCADGui
import Draft
import os
class WindowDialog():
def __init__(self):
self.ui_file = os.path.join(FreeCAD.getUserMacroDir(True), 'Calepinage/Calepinage.ui')
self.form = FreeCADGui.PySideUic.loadUi(self.ui_file)
self._connect_widgets()
self.form.show()
def _connect_widgets(self):
self.form.pushButtonSVG.pressed.connect(lambda: self.export(False))
self.form.pushButtonCalepinage.pressed.connect(lambda: self.export(True))
# -------- MODIFICATION DE LA MACRO "exportSketchEnMasse-SVG" de Gauthier Brière -------- #
def _exportSketchBasic(self, sketchList):
outputRacine = App.activeDocument().getFileName().rpartition('.')[0]
for __O__ in sketchList:
__obj__ = []
__obj__.append(__O__)
print('Export SVG de : ' + __obj__[0].Label)
oldPlace = __obj__[0].Placement
__obj__[0].Placement=App.Placement(App.Vector(0,0,0), App.Rotation(App.Vector(1,0,0),0), App.Vector(0,0,0))
fichierSVG = u"" + outputRacine + '--' + __obj__[0].Label + '.svg'
importSVG.export(__obj__, fichierSVG)
__obj__[0].Placement = oldPlace
App.ActiveDocument.removeObject(__O__.Label)
App.activeDocument().recompute()
def _exportSketchCalepinage(self, sketchList, plateLength, plateWidth, spacing, laserSize):
outputRacine = App.activeDocument().getFileName().rpartition('.')[0]
copy_sketchList = sketchList[:]
__obj__ = []
currentX = 0.0
currentY = 0.0
isSVGFull = False
YBlockCounter = 0
SVGNameCounter = 1
while len(copy_sketchList) > 0:
maxY = 0.0
counter = 0
for __O__ in copy_sketchList:
boundingBox = __O__.Shape.BoundBox
if boundingBox.XLength + currentX > plateLength:
counter += 1
continue
if boundingBox.YLength + currentY > plateWidth:
YBlockCounter += 1
if YBlockCounter >= len(copy_sketchList):
isSVGFull = True
break
continue
__obj__.insert(0, __O__)
maxY = boundingBox.YLength if boundingBox.YLength > maxY else maxY
oldPlace = __obj__[0].Placement
__obj__[0].Placement=App.Placement(App.Vector(0,0,0), App.Rotation(App.Vector(1,0,0),0), App.Vector(0,0,0))
if currentX == 0.0:
__obj__[0].Placement.Base = FreeCAD.Vector(currentX, currentY, 0.0)
else:
__obj__[0].Placement.Base = FreeCAD.Vector(currentX + spacing, currentY, 0.0)
currentX += boundingBox.XLength
# __obj__[0].Placement = oldPlace
# App.ActiveDocument.removeObject(__O__.Label)
copy_sketchList.pop(counter)
counter += 1
App.activeDocument().recompute()
currentY += maxY + spacing
App.activeDocument().recompute()
if isSVGFull or len(copy_sketchList) == 0:
fichierSVG = u"" + outputRacine + '--CALEPINAGE-' + str(SVGNameCounter) + '.svg'
print('Export SVG de : --CALEPINAGE-' + str(SVGNameCounter) + '.svg')
importSVG.export(__obj__, fichierSVG)
for __i__ in __obj__:
App.ActiveDocument.removeObject(__i__.Label)
__obj__ = []
App.activeDocument().recompute()
currentX = 0.0
currentY = 0.0
isSVGFull = False
SVGNameCounter += 1
SVGBlockCounter = 0
'''def _exportSketchCalepinage(self, sketchList, plateLength, plateWidth, spacing, laserSize):
outputRacine = App.activeDocument().getFileName().rpartition('.')[0]
copy_sketchList = sketchList[:]
objects_to_export = []
currentX = 0.0
currentY = 0.0
isSVGFull = False
YBlockCounter = 0
SVGNameCounter = 1
while len(copy_sketchList) != 0:
maxY = 0.0
counter = 0
for obj in copy_sketchList:
boundingBox = obj.Shape.BoundBox
if boundingBox.XLength + currentX > plateLength:
counter += 1
continue
if boundingBox.YLength + currentY > plateWidth:
YBlockCounter += 1
if YBlockCounter >= len(copy_sketchList):
isSVGFull = True
break
continue
objects_to_export.insert(0, obj)
maxY = boundingBox.YLength if boundingBox.YLength > maxY else maxY
oldPlace = objects_to_export[0].Placement
objects_to_export[0].Placement = App.Placement(App.Vector(0, 0, 0), App.Rotation(App.Vector(1, 0, 0), 0), App.Vector(0, 0, 0))
if currentX == 0.0:
objects_to_export[0].Placement.Base = FreeCAD.Vector(currentX, currentY, 0.0)
else:
objects_to_export[0].Placement.Base = FreeCAD.Vector(currentX + spacing, currentY, 0.0)
currentX += boundingBox.XLength
counter_to_remove = copy_sketchList.index(obj)
copy_sketchList.pop(counter_to_remove)
counter += 1
App.activeDocument().recompute()
currentY += maxY + spacing
App.activeDocument().recompute()
if isSVGFull or len(copy_sketchList) == 0:
fichierSVG = outputRacine + '--CALEPINAGE-' + str(SVGNameCounter) + '.svg'
print('Export SVG de : --CALEPINAGE-' + str(SVGNameCounter) + '.svg')
importSVG.export(objects_to_export, fichierSVG)
for i in objects_to_export:
App.ActiveDocument.removeObject(i.Label)
App.activeDocument().recompute()
objects_to_export = []'''
# -------- END -------- #
# -------- MODIFICATION DE MA MACRO "QuickSVGExport" -------- #
def export(self, isCalepinage):
plateLength = self.form.doubleSpinBoxLength.value()
plateWidth = self.form.doubleSpinBoxWidth.value()
spacing = self.form.doubleSpinBoxSpacing.value()
laserSize = self.form.doubleSpinBoxLaserSize.value()
sketchList = []
for selectedObject in FreeCADGui.Selection.getSelectionEx():
try:
if hasattr(selectedObject.Object, 'Dir'):
sv0 = Draft.make_shape2dview(selectedObject.Object, FreeCAD.Vector(selectedObject.Object.Dir))
elif hasattr(selectedObject.Object, 'Objects'):
for child in selectedObject.Object.Objects:
if hasattr(child, 'Dir'):
sv0 = Draft.make_shape2dview(selectedObject.Object, FreeCAD.Vector(child.Dir))
break
print("FAILURE IF")
FreeCAD.ActiveDocument.recompute()
sk = Draft.make_sketch(sv0, autoconstraints=True)
sk.ViewObject.LineColor = (1.0, 0.0, 0.0)
FreeCAD.ActiveDocument.recompute()
sketchList.append(sk)
if hasattr(sv0, 'Name'):
App.ActiveDocument.removeObject(sv0.Name)
except:
print("FAILURE TRY")
if isCalepinage:
self._exportSketchCalepinage(sketchList, plateLength, plateWidth, spacing, laserSize)
else:
self._exportSketchBasic(sketchList)
# -------- END -------- #
if __name__ == '__main__':
d = WindowDialog()