- 积分
- 1905
- 明经币
- 个
- 注册时间
- 2022-4-4
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
- # -*- coding: utf-8 -*
- import io
- import os
- import clr
- import sys
- import traceback
- import json
- import typing
- import codecs
- from collections import defaultdict
- from datetime import datetime
- from pycad.system import *
- from pycad.system.decorators import prinf
- sys.path.append(r"G:\MyStudio\Pycad\support")
- sys.path.append(r"G:\MyStudio\Pycad")
- clr.AddReference("System.Windows.Forms")
- clr.AddReference("IronPython")
- clr.AddReference("IronPython.Modules")
- from System.Windows.Forms import OpenFileDialog, DialogResult
- import System
- from System.Text import Encoding
- from System.IO import MemoryStream
- from IronPython.Hosting import Python
- dllpath = System.Reflection.Assembly.GetExecutingAssembly().Location
- dlldir = os.path.dirname(dllpath)
- libdir = os.path.join(dlldir, "lib")
- supportdir = os.path.join(dlldir, "support")
- extensionsdir = os.path.join(dlldir, "extensions")
- searchpaths = [dlldir, libdir, supportdir, extensionsdir]
- class JsonOperate:
- def __init__(self, path, filename):
- self.path = os.path.normpath(path)
- self.filename = os.path.normpath(os.path.join(self.path, filename))
- if not os.path.exists(self.path):
- os.makedirs(self.extensions_path)
- if not os.path.exists(self.filename):
- with open(self.filename, "w") as f:
- json.dump({}, f)
- def readjson(self):
- with open(self.filename, "r", encoding="utf-8") as f:
- return json.load(f)
- def writejson(self, data):
- with open(self.filename, "w", encoding="utf-8") as f:
- json.dump(data, f, indent=4)
- # --------------------Select PythonScrips And Get Filepaths ---------------------------#
- class LoadPythonScript:
- config_path = os.path.join(extensionsdir, 'loadconfig.json')
- json_operator = JsonOperate(extensionsdir, 'loadconfig.json')
- def __init__(self):
- self.paths = []
- self.load_paths()
- def add_paths(self):
- dlg = OpenFileDialog()
- dlg.Title = "Select PyScript Files"
- dlg.Multiselect = True
- dlg.Filter = "PyScript (*.py)|*.py"
- res = dlg.ShowDialog()
- if res == DialogResult.OK:
- for path in dlg.FileNames:
- if path not in self.paths: # 如果路径不存在,则添加
- self.paths.append(path)
- self.save_paths()
- def remove_paths(self, paths):
- for path in paths:
- if path in self.paths:
- self.paths.remove(path)
- self.save_paths()
- def load_paths(self):
- config = self.json_operator.readjson()
- self.paths = config.get('scripts', [])
- def save_paths(self):
- config = {
- "scripts": self.paths
- }
- self.json_operator.writejson(config)
- # --------------------Create Logservice -------------------------------#
- class LoggingUtils:
- _logPath = os.path.join(dlldir, "pycad.log")
- @staticmethod
- def LogError(message):
- now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
- logMessage = f"[{now}] {message}\n"
- with open(LoggingUtils._logPath, "a") as file:
- file.write(logMessage)
- prinf(logMessage)
- # --------------------Create PycadEngine -------------------------------#
- class PycadEngine:
- def __init__(self):
- self.engine = Python.CreateEngine()
- self.set_searchpaths(searchpaths)
-
- def set_searchpaths(self, paths: list):
- engine_searchpaths = self.engine.GetSearchPaths()
- for path in paths:
- engine_searchpaths.Add(path)
- self.engine.SetSearchPaths(engine_searchpaths)
- def execute_script(self, file_path):
- self.scope = self.engine.CreateScope()
- try:
- self.engine.ExecuteFile(file_path, self.scope)
- except System.Exception as ex:
- error = traceback.format_exc()
- message = f"Error script: {ex},error:{error}"
- LoggingUtils.LogError( message)
- def execute_code(self, code):
- self.scope = self.engine.CreateScope()
- try:
- self.engine.Execute(code, self.scope)
- except System.Exception as ex:
- message = f"Error code: {ex}"
- LoggingUtils.LogError( message)
-
- def set_scope_variable(self, var_name, var_value):
- self.scope.SetVariable(var_name, var_value)
-
- def get_scope_variable(self, var_name):
- return self.scope.GetVariable(var_name)
- # --------------------Initial PycadSystem ---------------------------#
- class PycadInitial:
- def __init__(self, scripts: list):
- self.PycadEngine = PycadEngine()
- self.scripts = scripts
-
- def pyrun(self):
- for script in self.scripts:
- self.PycadEngine.execute_script(script)
复制代码
|
评分
-
查看全部评分
|