Beispiel #1
0
 def _setup(self, filename, multithread, newfile, visiable, subthread):
     # self.xlApp = win32com.client.Dispatch('Excel.Application') #使用这个,不能单独设置visiable
     self._myStream = None
     if multithread:
         self.multiThreadOperationInit()  # com组件多线程操作,在其子线程上也要运行该操作
     # elif (subthread and not multithread) or (not subthread):
     else:
         pythoncom.CoInitialize()  # COM组件单线程操作,注:这个可以不是主线程。
     if newfile:
         self.xlApp = win32com.client.DispatchEx("Excel.Application")  # 使用这个后,粘贴Excel会有提示,原因不明
     else:
         self.xlApp = win32com.client.dynamic.Dispatch('Excel.Application')  # 使用现有的Excel程序打开,如果没有才新建
     if multithread:
         self._myStream = pythoncom.CreateStreamOnHGlobal()
         pythoncom.CoMarshalInterface(self._myStream,
                                      pythoncom.IID_IDispatch,
                                      self.xlApp._oleobj_,
                                      pythoncom.MSHCTX_LOCAL,
                                      pythoncom.MSHLFLAGS_TABLESTRONG)
     elif not multithread and subthread:
         self._myStream = pythoncom.CoMarshalInterThreadInterfaceInStream(pythoncom.IID_IDispatch,
                                                                          self.xlApp._oleobj_)
     if filename:
         self.filename = filename
         self._xlBook = self.xlApp.Workbooks.Open(filename)
     else:
         self._xlBook = self.xlApp.Workbooks.Add()
         self.filename = ''
     self.xlApp.Visible = visiable
     self.xlApp.DisplayAlerts = visiable
    def __init__(self,excelApplication, appRunGet: str, appRunPost: str):
        try:
            dispXlApp = DispatchEnsurer.EnsureDispatch(excelApplication)
            logging.info("MarshalledCallbackInfo.__init__      : creating Marshalled interface pointer" )

            self.myStream = pythoncom.CreateStreamOnHGlobal()    
            pythoncom.CoMarshalInterface(self.myStream, 
                             pythoncom.IID_IDispatch, 
                             dispXlApp._oleobj_, 
                             pythoncom.MSHCTX_LOCAL, 
                             pythoncom.MSHLFLAGS_TABLESTRONG) 
            self.excelApplication = excelApplication
            self.appRunGet = appRunGet
            self.appRunPost = appRunPost
        except Exception as ex:
            logging.info("MarshalledCallbackInfo.__init__ error : " + 
                 LocalsEnhancedErrorMessager.Enhance(ex,str(locals())))
            raise 
Beispiel #3
0
    def check_password(self, path, name, password):
        pythoncom.CoInitialize()
        
        xl = win32com.client.DispatchEx('Excel.Application')
        xls_stream = pythoncom.CreateStreamOnHGlobal()   
         
        pythoncom.CoMarshalInterface(xls_stream, 
                                    pythoncom.IID_IDispatch, 
                                    xl._oleobj_, 
                                    pythoncom.MSHCTX_LOCAL, 
                                    pythoncom.MSHLFLAGS_TABLESTRONG) 

        xls_stream.Seek(0,0)
        
        myUnmarshaledInterface = pythoncom.CoUnmarshalInterface(xls_stream, pythoncom.IID_IDispatch)    
        xl = win32com.client.Dispatch(myUnmarshaledInterface)

        try:
            xl_wb = xl.Workbooks.Open('%s\\%s' %(path,name), False, True, None, password)
            if xl_wb.Name:
                xl_wb.Close(False)

                xls_stream.Seek(0,0)
                pythoncom.CoReleaseMarshalData(xls_stream)
                xls_stream = None
                pythoncom.CoUninitialize()

                return password
        except:
            xls_stream.Seek(0,0)
            pythoncom.CoReleaseMarshalData(xls_stream)
            xls_stream = None
            pythoncom.CoUninitialize()
            return None

        xls_stream.Seek(0,0)
        pythoncom.CoReleaseMarshalData(xls_stream)
        xls_stream = None
        pythoncom.CoUninitialize()
        return None
Beispiel #4
0
    def __init__(self, modelName, modelFileName, config):
        Plugins.Simulator.SimulatorBase.Model.__init__(self, modelName,
                                                       modelFileName,
                                                       'SimulationX', config)

        sim = None
        self._doc = None

        try:
            if not config['Plugins']['SimulationX'].has_key('version'):
                config['Plugins']['SimulationX']['version'] = 'Iti.Simx36'
                config.write()
            dispatch = config['Plugins']['SimulationX']['version']
            if dispatch == 'Iti.Simx36':
                ver = '3.6'
            elif dispatch == 'Iti.Simx37':
                ver = '3.7'
            else:
                ver = '3.5'
            # Make sure Modelica models can be simulated
            try:
                key = winreg.OpenKey(
                    winreg.HKEY_CURRENT_USER,
                    r'Software\ITI GmbH\SimulationX ' + ver + r'\Modelica', 0,
                    winreg.KEY_ALL_ACCESS)
            except WindowsError:
                key = winreg.CreateKeyEx(
                    winreg.HKEY_CURRENT_USER,
                    r'Software\ITI GmbH\SimulationX ' + ver + r'\Modelica', 0,
                    winreg.KEY_ALL_ACCESS)
            winreg.SetValueEx(key, 'AutoCreateSimModel', 0, winreg.REG_DWORD,
                              1)
            winreg.CloseKey(key)

            pythoncom.CoInitialize()

            # A dummy object to get result properties:
            self.integrationResults = SimulationXCsv.Results('')
            self.integrationSettings.resultFileExtension = 'csvx'

            self._availableIntegrationAlgorithms = [
                'BDF', 'MEBDF', 'CVODE', 'FixedStep'
            ]
            self._solverByName = dict([('BDF', 'MultiStepMethod2'),
                                       ('MEBDF', 'MEBDFDAE'),
                                       ('CVODE', 'CVODE'),
                                       ('FixedStep', 'FixStep')])

            self._IntegrationAlgorithmHasFixedStepSize = [
                False, False, False, True
            ]
            self._IntegrationAlgorithmCanProvideStepSizeResults = [
                True, True, False, False
            ]

            self.integrationSettings.algorithmName = self._availableIntegrationAlgorithms[
                0]
            self.simulationStopRequest = False

            # Open SimulationX
            sim = win32com.client.Dispatch(dispatch)

            # Show SimulationX window
            sim.Visible = True

            # Wait till SimulationX is initialized
            if sim.InitState == simUninitialized:
                while sim.InitState != simInitBase:
                    time.sleep(0.1)

            # SimulationX in non-interactive mode
            sim.Interactive = False

            # Load libraries
            if sim.InitState == simInitBase:
                sim.InitSimEnvironment()

            if len(modelFileName) == 1:
                strMsg = 'PySimulator: Load model'

                split = unicode.rsplit(modelFileName[0], '.', 1)
                if len(split) > 1:
                    suffix = split[1]
                else:
                    suffix = ''

                if suffix in ['ism', 'isx']:
                    # Try to load as file
                    try:
                        # Write tracing marker message to output window in SimulationX
                        sim.Trace(SimTraceMsgLocationFile, SimTraceMsgTypeInfo,
                                  strMsg, '')
                        self._doc = sim.Documents.Open(modelFileName[0])
                    except win32com.client.pywintypes.com_error:
                        self._doc = None
                        print 'SimulationX: COM Error.'

                elif suffix == 'mo':
                    # Try to load as library
                    try:
                        try:
                            sim.LoadLibrary(modelFileName[0])
                        except:
                            pass
                        # Write tracing marker message to output window in SimulationX
                        sim.Trace(SimTraceMsgLocationFile, SimTraceMsgTypeInfo,
                                  strMsg, '')
                        self._doc = sim.Documents.Open(modelName)
                    except win32com.client.pywintypes.com_error:
                        self._doc = None
                        print 'SimulationX: COM Error.'

                if suffix in modelExtension:
                    # Read complete tracing to string
                    strTracing = sim.GetTraceMessages(SimTraceMsgLocationFile)
                    # Find last occurrence of tracing marker message
                    pos = strTracing.rfind(strMsg)
                    if pos >= 0:
                        strTracing = strTracing[pos + len(strMsg):].strip()
                        if len(strTracing):
                            print strTracing
                            self._doc.Close(False)
                            self._doc = None

                if not type(self._doc) is types.NoneType:
                    self._marshalled_doc = pythoncom.CreateStreamOnHGlobal()
                    pythoncom.CoMarshalInterface(self._marshalled_doc,
                                                 pythoncom.IID_IDispatch,
                                                 self._doc._oleobj_,
                                                 pythoncom.MSHCTX_INPROC)
                    self._marshalled_doc.Seek(0, pythoncom.STREAM_SEEK_SET)
                else:
                    self._marshalled_doc = None
                    print 'SimulationX: Load error.'

        except:
            print 'SimulationX: Error.'

        finally:
            try:
                if not type(sim) is types.NoneType:
                    # SimulationX in interactive mode
                    sim.Interactive = True
            except:
                pass