import importSVG import FreeCAD import FreeCADGui import Draft import os import Mesh class WindowDialog(): def __init__(self): self.outputRacine = self._createFolder() self.ui_file = os.path.join(FreeCAD.getUserMacroDir(True), 'MultiExport/MultiExport.ui') self.form = FreeCADGui.PySideUic.loadUi(self.ui_file) self._connect_widgets() self._populate_selector_list() self.form.show() def _connect_widgets(self): self.form.pushButtonSVG.pressed.connect(lambda: self.exportSVG(False)) self.form.pushButtonCalepinage.pressed.connect(lambda: self.exportSVG(True)) self.form.pushButtonSTLSingle.pressed.connect(lambda: self.exportSTL(True)) self.form.pushButtonSTLMulti.pressed.connect(lambda: self.exportSTL(False)) self.form.listWidgetSelector.itemSelectionChanged.connect(self._on_selector_item_changed) def _populate_selector_list(self): self.form.listWidgetSelector.clear() # Clear the list first for obj in FreeCAD.ActiveDocument.Objects: if obj.Label.startswith("EXP-"): self.form.listWidgetSelector.addItem(obj.Label) def _on_selector_item_changed(self): selected_items = self.form.listWidgetSelector.selectedItems() selected_labels = [item.text() for item in selected_items] FreeCADGui.Selection.clearSelection() # Clear previous selection # Select objects in FreeCAD based on labels selected in the list for obj in FreeCAD.ActiveDocument.Objects: if obj.Label in selected_labels: FreeCADGui.Selection.addSelection(obj) def _createFolder(self): folderPath = f"{FreeCAD.activeDocument().getFileName().rpartition('.')[0]}-Exports/" if not os.path.exists(folderPath): os.makedirs(folderPath) return folderPath # -------- MODIFICATION DE LA MACRO "exportSketchEnMasse-SVG" de Gauthier Brière -------- # def _exportSketchBasic(self, sketchList): for __O__ in sketchList: __obj__ = [] __obj__.append(__O__) print('Export SVG de : ' + __obj__[0].Label) oldPlace = __obj__[0].Placement __obj__[0].Placement=FreeCAD.Placement(FreeCAD.Vector(0,0,0), FreeCAD.Rotation(FreeCAD.Vector(1,0,0),0), FreeCAD.Vector(0,0,0)) fichierSVG = u"" + self.outputRacine + '--' + __obj__[0].Label + '.svg' importSVG.export(__obj__, fichierSVG) __obj__[0].Placement = oldPlace FreeCAD.ActiveDocument.removeObject(__O__.Label) FreeCAD.activeDocument().recompute() def _exportSketchCalepinage(self, sketchList, plateLength, plateWidth, spacing, laserSize): copy_sketchList = sketchList[:] __obj__ = [] currentX = 0.0 currentY = 0.0 isSVGFull = False YBlockCounter = 0 SVGNameCounter = 1 maxY = 0.0 index = 0 listSize = len(copy_sketchList) errorCount = 0 while listSize > 0: while index < listSize: __O__ = copy_sketchList[index] boundingBox = __O__.Shape.BoundBox if (boundingBox.XLength > plateLength) or (boundingBox.YLength > plateWidth): copy_sketchList.pop(index) listSize = len(copy_sketchList) errorCount += 1 FreeCAD.ActiveDocument.removeObject(__O__.Label) FreeCAD.activeDocument().recompute() continue if boundingBox.XLength + currentX > plateLength: if boundingBox.YLength + currentY > plateWidth: YBlockCounter += 1 if YBlockCounter >= listSize: isSVGFull = True break index += 1 continue if boundingBox.YLength + currentY > plateWidth: YBlockCounter += 1 if YBlockCounter >= listSize: isSVGFull = True break index += 1 continue __obj__.insert(0, __O__) maxY = boundingBox.YLength if boundingBox.YLength > maxY else maxY __obj__[0].Placement=FreeCAD.Placement(FreeCAD.Vector(0,0,0), FreeCAD.Rotation(FreeCAD.Vector(1,0,0),0), FreeCAD.Vector(0,0,0)) if currentX == 0.0: __obj__[0].Placement.Base = FreeCAD.Vector(-boundingBox.XMin + currentX, -boundingBox.YMin + currentY, 0.0) currentX += boundingBox.XLength else: __obj__[0].Placement.Base = FreeCAD.Vector(-boundingBox.XMin + currentX + spacing, -boundingBox.YMin + currentY, 0.0) currentX += boundingBox.XLength + spacing copy_sketchList.pop(index) listSize = len(copy_sketchList) FreeCAD.activeDocument().recompute() index = 0 YBlockCounter = 0 currentX = 0.0 currentY += maxY + spacing maxY = 0.0 index = 0 listSize = len(copy_sketchList) FreeCAD.activeDocument().recompute() if listSize <= 0 or isSVGFull: fichierSVG = u"" + self.outputRacine + '--CALEPINAGE-' + str(SVGNameCounter) + '.svg' print('Export SVG de : ' + u"" + str(os.path.basename(self.outputRacine)) + '--CALEPINAGE-' + str(SVGNameCounter) + '.svg') importSVG.export(__obj__, fichierSVG) for __i__ in __obj__: FreeCAD.ActiveDocument.removeObject(__i__.Label) FreeCAD.activeDocument().recompute() __obj__.clear() maxY = 0.0 index = 0 currentX = 0.0 currentY = 0.0 isSVGFull = False SVGNameCounter += 1 YBlockCounter = 0 listSize = len(copy_sketchList) if errorCount == 1: print(f"ERREUR : {errorCount} sketch ne rentre pas sur le plateau") elif errorCount > 1: print(f"ERREUR : {errorCount} sketchs ne rentrent pas sur le plateau") # -------- END -------- # def exportSVG(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(): 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 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'): FreeCAD.ActiveDocument.removeObject(sv0.Name) if isCalepinage: self._exportSketchCalepinage(sketchList, plateLength, plateWidth, spacing, laserSize) else: self._exportSketchBasic(sketchList) def exportSTL(self, singleMode): __obj__ = [] for selectedObject in FreeCADGui.Selection.getSelectionEx(): __obj__.append(selectedObject.Object) if not singleMode: filename = u"" + self.outputRacine + '--' + __obj__[0].Label + '.stl' if hasattr(Mesh, "exportOptions"): options = Mesh.exportOptions(self.outputRacine) Mesh.export(__obj__, filename, options) else: Mesh.export(__obj__, filename) print(f'Export STL de : {__obj__[0].Label}.stl') __obj__.clear() if singleMode: filename = u"" + self.outputRacine + '--' + __obj__[0].Label + '.stl' if len(__obj__) == 1 else u"" + self.outputRacine + '.stl' if hasattr(Mesh, "exportOptions"): options = Mesh.exportOptions(self.outputRacine) Mesh.export(__obj__, filename, options) else: Mesh.export(__obj__, filename) if len(__obj__) == 1: print(f'Export STL de : {__obj__[0].Label}.stl') else: print(f'Export STL de {os.path.basename(self.outputRacine)}.stl') del __obj__ if __name__ == '__main__': try: d = WindowDialog() except Exception as e: print(e)