Skip to content

Instantly share code, notes, and snippets.

@Zagrebelin
Created May 6, 2016 11:16
Show Gist options
  • Save Zagrebelin/789cf6ad1146c192cfece122b312c579 to your computer and use it in GitHub Desktop.
Save Zagrebelin/789cf6ad1146c192cfece122b312c579 to your computer and use it in GitHub Desktop.
tflex cad loader
#encoding: utf-8
import os
import platform
from Microsoft.Win32 import Registry
import System
import clr
clr.AddReference('System.Windows.Forms')
import System.Windows.Forms as WinForms
class ApiLoader:
def __init__(self):
self._cadPath = self.get_path("T-FLEX CAD 3D 14", "Rus");
self._initialized = False
System.AppDomain.CurrentDomain.AssemblyResolve += self.assembly_resolve
def get_path(self, productName, language):
if not productName or not language:
return None
if platform.architecture()[0] == '64bit':
productName += 'x64'
reg_path = 'SOFTWARE\\TOP Systems\\%s\\%s' % (productName, language)
key = Registry.LocalMachine.OpenSubKey(reg_path)
if not key:
return
path = key.GetValue('ProductFolder', None)
if not path:
path = key.GetValue('SetupHelpPath', None)
key.Close()
return path
def assembly_resolve(self, sender, args):
if not self._cadPath:
return
try:
#name = args.Name
name = args
if ',' in name:
name = name.split(',')[0]
filename = os.path.join(self._cadPath, name)
if not os.path.exists(filename):
print('-')
return
os.chdir(self._cadPath)
return System.Reflection.Assembly.LoadFile(filename)
except Exception as ex:
msg = "Ошибка загрузки сборки %s:\n%s" %(name, ex.message)
WinForms.MessageBox.Show(msg, "Ошибка", WinForms.MessageBoxButtons.OK, WinForms.MessageBoxIcon.Error)
def InitializeTFlexCADAPI(self):
if self._initialized:
return True
if not self._cadPath:
raise Exception('T-FLEX CAD is not installed')
# Перед работой с API T-FLEX CAD его необходимо инициализировать
# В зависимости от параметров инициализации, будут или не будут
# доступны функции изменения документов и сохранение документов в файл.
# За это отвечает параметр setup.ReadOnly.
# Если setup.ReadOnly = false, то для работы программы требуется
# лицензия на сам T-FLEX CAD
setup = TFlex.ApplicationSessionSetup()
setup.ReadOnly = False
self._initialized = TFlex.Application.InitSession(setup)
return self._initialized
def Dispose(self):
if self._initialized:
TFlex.Application.ExitSession();
self._initialized = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment