使用Ironpython 3.0 创建一个自己的PYCAD环境
# -*- 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 =
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)
社会我枫哥,人狠话不多 闻人南131 发表于 2023-7-20 14:47
社会我枫哥,人狠话不多
多谢兄弟们鼓励,不然我坚持不下去,现在总归用上了自己的pycad 社会我枫哥,人狠话不多+1 上次根据你的帖子我搞定了犀牛里面ghpython调用Cpython库的问题。不过这次这个我看不太懂了,能否多说几句,讲讲具体实现了什么效果? 陨落 发表于 2023-7-21 13:11
上次根据你的帖子我搞定了犀牛里面ghpython调用Cpython库的问题。不过这次这个我看不太懂了,能否多说几句 ...
这个是核心代码,用c# 创建一个执行Ironpython脚本dll,后面在Ironpython脚本中进行加载,运行插件
社会我枫哥,人狠话不多+1 看不懂不会用 终于ironpy3了 Make PyCAD great again.
页:
[1]
2