<commit_before># -*- coding: utf-8 -*-
"""
Written by <NAME>.
Email: danaukes<at>seas.harvard.edu.
Please see LICENSE.txt for full license.
"""
from popupcad.filetypes.constraints import ConstraintSystem
import popupcad
from popupcad.filetypes.popupcad_file import popupCADFile
import PySide.QtGui as qg
import PySide.QtCore as qc


class Sketch(popupCADFile):
    filetypes = {'sketch': 'Sketch File', 'dxf': 'DXF'}
    defaultfiletype = 'sketch'

    @classmethod
    def lastdir(cls):
        return popupcad.lastsketchdir

    @classmethod
    def setlastdir(cls, directory):
        popupcad.lastsketchdir = directory

    def __init__(self):
        super(Sketch, self).__init__()
        self.operationgeometry = []
        self.constraintsystem = ConstraintSystem()

    def copy(self, identical=True):
        new = type(self)()
        new.operationgeometry = [
            geom.copy(
                identical=True) for geom in self.operationgeometry if geom.isValid()]
        new.constraintsystem = self.constraintsystem.copy()
        if identical:
            new.id = self.id
        self.copy_file_params(new, identical)
        return new

    def upgrade(self, identical=True):
        new = type(self)()
        new.operationgeometry = [
            geom.upgrade(
                identical=True) for geom in self.operationgeometry if geom.isValid()]
        new.constraintsystem = self.constraintsystem.upgrade()
        if identical:
            new.id = self.id
        self.copy_file_params(new, identical)
        return new

    def addoperationgeometries(self, polygons):
        self.operationgeometry.extend(polygons)

    def cleargeometries(self):
        self.operationgeometry = []

    def edit(self, parent, design=None, **kwargs):
        from popupcad.guis.sketcher import Sketcher
        sketcher = Sketcher(
            parent,
            self,
            design,
            accept_method=self.edit_result,
            **kwargs)
        sketcher.show()
        sketcher.graphicsview.zoomToFit()

    def edit_result(self, sketch):
        self.operationgeometry = sketch.operationgeometry
        self.constraintsystem = sketch.constraintsystem

    def output_csg(self):
        import popupcad.geometry.customshapely
        shapelygeoms = []
        for item in self.operationgeometry:
            try:
                if not item.is_construction():
                    shapelyitem = item.outputshapely()
                    shapelygeoms.append(shapelyitem)
            except ValueError as ex:
                print(ex)
            except AttributeError as ex:
                shapelyitem = item.outputshapely()
                shapelygeoms.append(shapelyitem)
        shapelygeoms = popupcad.geometry.customshapely.unary_union_safe(
            shapelygeoms)
        shapelygeoms = popupcad.geometry.customshapely.multiinit(shapelygeoms)
        return shapelygeoms

    @classmethod
    def load_dxf(cls, filename, parent=None):
        import dxfgrabber
        import ezdxf
        import ezdxf.modern
        dxf = dxfgrabber.readfile(filename)
        dxf2 = ezdxf.readfile(filename)
        layer_names = dxf.layers.names()
        layer_names2 = [layer.dxf.name for layer in dxf2.layers]
        
        dialog = qg.QDialog()
        lw = qg.QListWidget()
        for item in layer_names2:
            lw.addItem(qg.QListWidgetItem(item))
        lw.setSelectionMode(lw.SelectionMode.ExtendedSelection)
        button_ok = qg.QPushButton('Ok')
        button_cancel = qg.QPushButton('Cancel')
        button_ok.clicked.connect(dialog.accept)
        button_cancel.clicked.connect(dialog.reject)

        layout = qg.QVBoxLayout()
        layout_buttons = qg.QHBoxLayout()
        layout_buttons.addWidget(button_ok)
        layout_buttons.addWidget(button_cancel)
        layout.addWidget(lw)
        layout.addLayout(layout_buttons)
        dialog.setLayout(layout)
        result = dialog.exec_()
        
        if result:
            selected_layers = [
                item.data(
                    qc.Qt.ItemDataRole.DisplayRole) for item in lw.selectedItems()]
            entities = dxf.entities
            entities2 = dxf2.entities
            generics = []
            for entity in entities2:
                if entity.dxf.layer in selected_layers:
#                    if isinstance(entity, dxfgrabber.entities.Line):
                    if isinstance(entity, ezdxf.modern.graphics.Line):
                        from popupcad.filetypes.genericshapes import GenericLine
                        import numpy
                        points = numpy.array(
                            [entity.dxf.start[:2], entity.dxf.end[:2]])
                        points *= popupcad.internal_argument_scaling
                        generics.append(
                            GenericLine.gen_from_point_lists(
                                points.tolist(),
                                []))
#                    elif isinstance(entity, dxfgrabber.entities.Arc):
#                        pass
#                        from popupcad.filetypes.genericshapes import GenericCircle
#                        generics.append(GenericCircle.gen_from_point_lists([entity.center[:2],(entity.radius,0)],[]))
#                    elif isinstance(entity, dxfgrabber.entities.LWPolyline):
                    elif isinstance(entity, ezdxf.modern.graphics.LWPolyline):
                        from popupcad.filetypes.genericshapes import GenericPolyline
                        from popupcad.filetypes.genericshapes import GenericPoly
                        import numpy
                        points = numpy.array(entity.dxf.points)
                        points *= popupcad.internal_argument_scaling
                        if entity.is_closed:
                            generics.append(
                                GenericPoly.gen_from_point_lists(
                                    points.tolist(),
                                    []))
                        else:
                            generics.append(
                                GenericPolyline.gen_from_point_lists(
                                    points.tolist(),
                                    []))
#                    elif isinstance(entity, dxfgrabber.entities.Insert):
#                        pass
#                    elif isinstance(entity, dxfgrabber.entities.Circle):
#                        pass
##                        from popupcad.filetypes.genericshapes import GenericCircle
##                        generics.append(GenericCircle.gen_from_point_lists([entity.center[:2],(entity.radius,0)],[]))
#                    elif isinstance(entity, dxfgrabber.entities.Spline):
#                        pass
                    else:
                        print(entity)
            new = cls()
            new.addoperationgeometries(generics)
            return filename, new
        else:
            return None, None

    @classmethod
    def open_filename(cls, parent=None):
        filterstring, selectedfilter = cls.buildfilters()
        filename, selectedfilter = qg.QFileDialog.getOpenFileName(
            parent, 'Open', cls.lastdir(), filter=filterstring, selectedFilter=selectedfilter)
        if filename:
            if 'sketch' in selectedfilter:
                object1 = cls.load_yaml(filename)
                return filename, object1
            elif 'dxf' in selectedfilter:
                return cls.load_dxf(filename, parent)
        return None, None

    def saveAs(self, parent=None):<commit_msg>full switch to ezdxf<commit_after># -*- coding: utf-8 -*-
"""
Written by <NAME>.
Email: danaukes<at>seas.harvard.edu.
Please see LICENSE.txt for full license.
"""
from popupcad.filetypes.constraints import ConstraintSystem
import popupcad
from popupcad.filetypes.popupcad_file import popupCADFile
import PySide.QtGui as qg
import PySide.QtCore as qc


class Sketch(popupCADFile):
    filetypes = {'sketch': 'Sketch File', 'dxf': 'DXF'}
    defaultfiletype = 'sketch'

    @classmethod
    def lastdir(cls):
        return popupcad.lastsketchdir

    @classmethod
    def setlastdir(cls, directory):
        popupcad.lastsketchdir = directory

    def __init__(self):
        super(Sketch, self).__init__()
        self.operationgeometry = []
        self.constraintsystem = ConstraintSystem()

    def copy(self, identical=True):
        new = type(self)()
        new.operationgeometry = [
            geom.copy(
                identical=True) for geom in self.operationgeometry if geom.isValid()]
        new.constraintsystem = self.constraintsystem.copy()
        if identical:
            new.id = self.id
        self.copy_file_params(new, identical)
        return new

    def upgrade(self, identical=True):
        new = type(self)()
        new.operationgeometry = [
            geom.upgrade(
                identical=True) for geom in self.operationgeometry if geom.isValid()]
        new.constraintsystem = self.constraintsystem.upgrade()
        if identical:
            new.id = self.id
        self.copy_file_params(new, identical)
        return new

    def addoperationgeometries(self, polygons):
        self.operationgeometry.extend(polygons)

    def cleargeometries(self):
        self.operationgeometry = []

    def edit(self, parent, design=None, **kwargs):
        from popupcad.guis.sketcher import Sketcher
        sketcher = Sketcher(
            parent,
            self,
            design,
            accept_method=self.edit_result,
            **kwargs)
        sketcher.show()
        sketcher.graphicsview.zoomToFit()

    def edit_result(self, sketch):
        self.operationgeometry = sketch.operationgeometry
        self.constraintsystem = sketch.constraintsystem

    def output_csg(self):
        import popupcad.geometry.customshapely
        shapelygeoms = []
        for item in self.operationgeometry:
            try:
                if not item.is_construction():
                    shapelyitem = item.outputshapely()
                    shapelygeoms.append(shapelyitem)
            except ValueError as ex:
                print(ex)
            except AttributeError as ex:
                shapelyitem = item.outputshapely()
                shapelygeoms.append(shapelyitem)
        shapelygeoms = popupcad.geometry.customshapely.unary_union_safe(
            shapelygeoms)
        shapelygeoms = popupcad.geometry.customshapely.multiinit(shapelygeoms)
        return shapelygeoms

    @classmethod
    def load_dxf(cls, filename, parent=None):
        import ezdxf
        import ezdxf.modern
        dxf = ezdxf.readfile(filename)
        layer_names = [layer.dxf.name for layer in dxf.layers]
        
        dialog = qg.QDialog()
        lw = qg.QListWidget()
        for item in layer_names:
            lw.addItem(qg.QListWidgetItem(item))
        lw.setSelectionMode(lw.SelectionMode.ExtendedSelection)
        button_ok = qg.QPushButton('Ok')
        button_cancel = qg.QPushButton('Cancel')
        button_ok.clicked.connect(dialog.accept)
        button_cancel.clicked.connect(dialog.reject)

        layout = qg.QVBoxLayout()
        layout_buttons = qg.QHBoxLayout()
        layout_buttons.addWidget(button_ok)
        layout_buttons.addWidget(button_cancel)
        layout.addWidget(lw)
        layout.addLayout(layout_buttons)
        dialog.setLayout(layout)
        result = dialog.exec_()
        
        if result:
            selected_layers = [
                item.data(
                    qc.Qt.ItemDataRole.DisplayRole) for item in lw.selectedItems()]
            entities = dxf.entities
            generics = []
            for entity in entities:
                if entity.dxf.layer in selected_layers:
                    if isinstance(entity, ezdxf.modern.graphics.Line):
                        from popupcad.filetypes.genericshapes import GenericLine
                        import numpy
                        points = numpy.array(
                            [entity.dxf.start[:2], entity.dxf.end[:2]])
                        points *= popupcad.internal_argument_scaling
                        generics.append(
                            GenericLine.gen_from_point_lists(
                                points.tolist(),
                                []))
                    elif isinstance(entity, ezdxf.modern.graphics.LWPolyline):
                        from popupcad.filetypes.genericshapes import GenericPolyline
                        from popupcad.filetypes.genericshapes import GenericPoly
                        import numpy
                        points = numpy.array(entity.dxf.points)
                        points *= popupcad.internal_argument_scaling
                        if entity.is_closed:
                            generics.append(
                                GenericPoly.gen_from_point_lists(
                                    points.tolist(),
                                    []))
                        else:
                            generics.append(
                                GenericPolyline.gen_from_point_lists(
                                    points.tolist(),
                                    []))
                    else:
                        print(entity)
            new = cls()
            new.addoperationgeometries(generics)
            return filename, new
        else:
            return None, None

    @classmethod
    def open_filename(cls, parent=None):
        filterstring, selectedfilter = cls.buildfilters()
        filename, selectedfilter = qg.QFileDialog.getOpenFileName(
            parent, 'Open', cls.lastdir(), filter=filterstring, selectedFilter=selectedfilter)
        if filename:
            if 'sketch' in selectedfilter:
                object1 = cls.load_yaml(filename)
                return filename, object1
            elif 'dxf' in selectedfilter:
                return cls.load_dxf(filename, parent)
        return None, None

    def saveAs(self, parent=None):
{'fraction_non_alphanumeric': 0.08089807555238775, 'fraction_numerical': 0.0014967925873129008, 'mean_word_length': 1.6956772334293948, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7530224', 'n_tokens_mistral': 3824, 'n_tokens_neox': 3646, 'n_words': 710}