Module: RRT_RUBY::RRTGeneric

Defined in:
lib/rrt_ruby/rrt_generic.rb

Overview

We do not attempt to duplicate the interface, just make the most usual tasks easier.

Defined Under Namespace

Classes: Element, ElementException, Finder, FinderException, ModelException

Constant Summary collapse

VERSION_MAJOR =
1
VERSION_MINOR =
0
OLEAPP_NAME =
'RoseRT.Application'

Class Method Summary collapse

Class Method Details

.current_modelObject

Returns the pathname for the current model if an OLE server is present, nil otherwise



202
203
204
205
206
207
208
209
# File 'lib/rrt_ruby/rrt_generic.rb', line 202

def RRTGeneric.current_model
	begin
		rt = WIN32OLE.connect(RRTGeneric::OLEAPP_NAME)
		return rt.CurrentModel.GetFileName
	rescue
		return nil
	end
end

.find_element(model, name, type) ⇒ Object

Given a valid model (OLE object) it will return the first OLE object matching name and type.

This wraps the FindModelElements method of the RRTEI API.

Raises:



190
191
192
193
194
195
196
197
198
199
# File 'lib/rrt_ruby/rrt_generic.rb', line 190

def RRTGeneric.find_element model,name,type
	raise FinderException.new(""),"no model specified" unless model
	col=model.FindModelElements(name)
	count=col.Count
	1.upto(count){|i|
		it=col.GetAt(i)
		return it if it && it.Name==name && it.IsClass(type)
	}
	return nil
end

.open_model(pathname, logger = nil) ⇒ Object

This is a convenience method that makes some assumptions about the use of the win32ole interface.

It is here to be used with scripts that don’t need the full Finder functionality or want to work directly with the RRTEI interface.

It only takes a block to make sure we clean up the application object afterwards. This cleanup has a sideeffect: open RRT instances will be closed after the block finishes.

RRT only seems to create one OLE application server (instance started with this session do not register as OLE servers).



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rrt_ruby/rrt_generic.rb', line 151

def RRTGeneric.open_model pathname,logger=nil
	#opens the model with the given pathname and returns a reference to the Win32OLE model object.
	#first check the pathname
	if File.exists?(pathname) then 
		#check if there is an open RRT instance with this model
		begin 
			rt = WIN32OLE.connect(RRTGeneric::OLEAPP_NAME)
		rescue
			#no OLE Server present, create a new instance
			logger.info("No OLE Server found. Creating new instance") if logger
			rt=WIN32OLE.new(RRTGeneric::OLEAPP_NAME)
		end
		begin
			# We are in Windows so filenames are case insensitive
			if rt.CurrentModel.GetFileName.upcase==pathname.upcase
				logger.debug("Using existing model") if logger
				model = rt.CurrentModel  
				#it was open before, so don't close the app at the end
				exit_app=false
			else
				logger.info("Opening model") if logger
				model=rt.openmodel(pathname)
				exit_app=true
			end
			yield model
		ensure 
			if rt && exit_app 
				rt.exit 
				rt.ole_free()
			end
		end
		return model
	else
		logger.fatal("File not found #{pathname}") if logger
	end if block_given?
end