Can I open a DXF file in the viewer from the code, select an entity and pass the selected entity data? #878
-
Hello everyone, In the past I have used complicated mathematics to select the required entity for my functions. Unfortunately, this does not always work the way I would like it to work. I recently discovered the qtviewer addon and am trying to see if it is possible to open the dxf file in the viewer from my .py, select an entity, pass the selected entity's information back to my .py, close the viewer and continue with my code. This would be a game changer for me. The selection of entities in the viewer itself works very nice, so why not make use of that. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
have a look at Here is another example of what I think you are asking for (which only works if the above PR is merged, or you can switch to that branch to try it out): import signal
from typing import Optional
import ezdxf
from ezdxf.addons.drawing.qtviewer import CADGraphicsViewWithOverlay, CADWidget, CADViewer
from ezdxf.addons.xqt import QtCore as qc, QtGui as qg, QtWidgets as qw
from ezdxf.audit import Auditor
from ezdxf.document import Drawing
from ezdxf.entities import DXFGraphic
class ElementSelectorView(CADGraphicsViewWithOverlay):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.selected = None
def keyPressEvent(self, event: qg.QKeyEvent) -> None:
if event.key() == qc.Qt.Key_Return:
element = self.current_hovered_element
if element is not None:
self.selected = element
self.close()
def select_element(doc: Drawing, layout: str = 'Model', show_controls: bool = False) -> Optional[DXFGraphic]:
signal.signal(signal.SIGINT, signal.SIG_DFL) # handle Ctrl+C properly
app = qw.QApplication.instance()
if app is None:
app = qw.QApplication([])
view = ElementSelectorView()
cad = CADWidget(view)
if show_controls:
viewer = CADViewer(cad=cad)
view.closing.connect(viewer.close)
viewer.set_document(doc, Auditor(doc), layout=layout)
viewer.show()
else:
cad.set_document(doc, layout=layout)
cad.show()
app.exec()
return view.selected
def main():
doc = ezdxf.new()
msp = doc.modelspace()
doc.layers.add("MyLayer")
doc.layers.add("OtherLayer")
msp.add_line((0, 0), (1, 0), dxfattribs={"layer": "MyLayer"})
msp.add_line((1, 0), (1, 1), dxfattribs={"layer": "OtherLayer"})
selected_element = select_element(doc)
print(f'element selected: {selected_element}')
selected_element = select_element(doc, show_controls=True)
print(f'element selected: {selected_element}')
if __name__ == '__main__':
main() |
Beta Was this translation helpful? Give feedback.
have a look at
element_picker.py
in this PR #880Here is another example of what I think you are asking for (which only works if the above PR is merged, or you can switch to that branch to try it out):
move the mouse to select elements. Left click to cycle through candidate elements under the cursor. Press enter to select the entity and close the window.
I wasn't sure whether you wanted the full controls such as the layers list or if you just wanted the CAD widget so the example below has both