瀏覽代碼

- made FlatCAM so that whenever an associated file is double clicked, if there is an opened instance of FlatCAM, the file will be opened in the first instance without launching a new instance of FlatCAM. If FlatCAM is launched again it will spawn a new process (hopefully it will work when freezed).

Marius Stanciu 6 年之前
父節點
當前提交
bb9c35a527
共有 3 個文件被更改,包括 58 次插入8 次删除
  1. 2 2
      FlatCAM.py
  2. 52 6
      FlatCAMApp.py
  3. 4 0
      README.md

+ 2 - 2
FlatCAM.py

@@ -4,9 +4,10 @@ import os
 from PyQt5 import QtWidgets
 from PyQt5.QtCore import QSettings, Qt
 from FlatCAMApp import App
-from multiprocessing import freeze_support
 from flatcamGUI import VisPyPatches
 
+from multiprocessing import freeze_support
+
 if sys.platform == "win32":
     # cx_freeze 'module win32' workaround
     pass
@@ -58,5 +59,4 @@ if __name__ == '__main__':
         app.setAttribute(Qt.AA_EnableHighDpiScaling, False)
 
     fc = App()
-
     sys.exit(app.exec_())

+ 52 - 6
FlatCAMApp.py

@@ -29,6 +29,11 @@ import gc
 
 from xml.dom.minidom import parseString as parse_xml_string
 
+from multiprocessing.connection import Listener, Client
+from multiprocessing import Pool
+import socket
+from array import array
+
 # #######################################
 # #      Imports part of FlatCAM       ##
 # #######################################
@@ -51,7 +56,6 @@ from vispy.io import write_png
 
 from flatcamTools import *
 
-from multiprocessing import Pool
 import tclCommands
 
 import gettext
@@ -67,6 +71,40 @@ if '_' not in builtins.__dict__:
 # ########################################
 
 
+class ArgsThread(QtCore.QThread):
+    open_signal = pyqtSignal(list)
+
+    if sys.platform == 'win32':
+        address = (r'\\.\pipe\NPtest', 'AF_PIPE')
+    else:
+        address = ('/tmp/testipc', 'AF_UNIX')
+
+    def my_loop(self, address):
+        try:
+            listener = Listener(*address)
+            while True:
+                conn = listener.accept()
+                self.serve(conn)
+        except socket.error as e:
+            conn = Client(*address)
+            conn.send(sys.argv)
+            conn.send('close')
+            # close the current instance only if there are args
+            if len(sys.argv) > 1:
+                sys.exit()
+
+    def serve(self, conn):
+        while True:
+            msg = conn.recv()
+            if msg == 'close':
+                break
+            self.open_signal.emit(msg)
+        conn.close()
+
+    def run(self):
+        self.my_loop(self.address)
+
+
 class App(QtCore.QObject):
     """
     The main application class. The constructor starts the GUI.
@@ -200,6 +238,10 @@ class App(QtCore.QObject):
 
         self.main_thread = QtWidgets.QApplication.instance().thread()
 
+        self.new_launch = ArgsThread()
+        self.new_launch.start()
+        self.new_launch.open_signal[list].connect(self.on_startup_args)
+
         # #######################
         # # ## OS-specific ######
         # #######################
@@ -1747,7 +1789,7 @@ class App(QtCore.QObject):
             self.on_excellon_options_button)
 
         # when there are arguments at application startup this get launched
-        self.args_at_startup.connect(self.on_startup_args)
+        self.args_at_startup.connect(lambda: self.on_startup_args())
         self.log.debug("Finished connecting Signals.")
 
         # this is a flag to signal to other tools that the ui tooltab is locked and not accessible
@@ -2198,7 +2240,6 @@ class App(QtCore.QObject):
         if App.args:
             self.args_at_startup.emit()
 
-
     @staticmethod
     def copy_and_overwrite(from_path, to_path):
         """
@@ -2216,9 +2257,14 @@ class App(QtCore.QObject):
             from_new_path = os.path.dirname(os.path.realpath(__file__)) + '\\flatcamGUI\\VisPyData\\data'
             shutil.copytree(from_new_path, to_path)
 
-    def on_startup_args(self):
-        log.debug("Application was started with an argument. Processing ...")
-        for argument in App.args:
+    def on_startup_args(self, args=None):
+        if args:
+            args_to_process = args
+        else:
+            args_to_process = App.args
+            log.debug("Application was started with an argument. Processing ...")
+
+        for argument in args_to_process:
             if '.FlatPrj' in argument:
                 try:
                     project_name = str(argument)

+ 4 - 0
README.md

@@ -9,6 +9,10 @@ CAD program, and create G-Code for Isolation routing.
 
 =================================================
 
+27.08.2019
+
+- made FlatCAM so that whenever an associated file is double clicked, if there is an opened instance of FlatCAM, the file will be opened in the first instance without launching a new instance of FlatCAM. If FlatCAM is launched again it will spawn a new process (hopefully it will work when freezed).
+
 26.08.2019
 
 - added support for file associations with FlatCAM, for Windows