1 # Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 ## \defgroup studyedit studyedit
23 # This module provides a new class \bStudyEditor to complement \bStudy
24 # and \bStudyBuilder classes.
28 This module provides a new class :class:`StudyEditor` to complement
29 :class:`Study` and :class:`StudyBuilder` classes.
35 from salome.kernel.logger import Logger
36 from salome.kernel import termcolor
37 logger = Logger("salome.kernel.studyedit", color = termcolor.PURPLE)
40 _DEFAULT_CONTAINER = "FactoryServer"
42 # The codec to use for strings that are displayed in Salome study tree is Latin-1
43 ENCODING_FOR_SALOME_STUDY = "iso-8859-1"
48 ## Return a \b StudyEditor instance to edit the study.
52 Return a :class:`StudyEditor` instance to edit the study.
56 _editor = StudyEditor()
59 ## This class provides utility methods to complement \b Study and
60 # \b StudyBuilder classes. Those methods may be moved in those classes
62 # The preferred way to get a StudyEditor object is through the method
63 # \b getStudyEditor which allows to reuse existing instances.
65 # \param study This instance attribute contains the underlying \b Study object.
66 # It can be used to access the study but the attribute itself should not
69 # \param builder This instance attribute contains the underlying \b StudyBuilder
70 # object. It can be used to edit the study but the attribute itself
71 # should not be modified.
75 This class provides utility methods to complement :class:`Study` and
76 :class:`StudyBuilder` classes. Those methods may be moved in those classes
78 The preferred way to get a StudyEditor object is through the method
79 :meth:`getStudyEditor` which allows to reuse existing instances.
83 This instance attribute contains the underlying :class:`Study` object.
84 It can be used to access the study but the attribute itself should not
87 .. attribute:: builder
89 This instance attribute contains the underlying :class:`StudyBuilder`
90 object. It can be used to edit the study but the attribute itself
91 should not be modified.
96 self.study = salome.myStudy
97 if self.study is None:
98 raise Exception("Can't create StudyEditor object: "
99 "Study doesn't exist")
100 self.builder = self.study.NewBuilder()
102 ## Find a component corresponding to the Salome module \b moduleName in
103 # the study. If none is found, create a new component and associate it
104 # with the corresponding engine (i.e. the engine named \b moduleName).
105 # Note that in Salome 5, the module name and engine name must be
106 # identical (every module must provide an engine with the same name).
107 # In Salome 6 it will be possible to define a different name for the
110 # \param moduleName (string) name of the module corresponding to the component
111 # (the module name is the string value in the
112 # attribute "AttributeComment" of the component)
114 # \param componentName (string) name of the new component if created.
115 # If \b None, use \b moduleName instead.
117 # \param icon (string) icon for the new component (attribute "AttributePixMap").
119 # \param containerName (string) name of the container in which the engine should be
122 # \return the SComponent found or created.
123 def findOrCreateComponent(self, moduleName, componentName = None,
124 icon = None, containerName = _DEFAULT_CONTAINER):
126 Find a component corresponding to the Salome module `moduleName` in
127 the study. If none is found, create a new component and associate it
128 with the corresponding engine (i.e. the engine named `moduleName`).
129 Note that in Salome 5, the module name and engine name must be
130 identical (every module must provide an engine with the same name).
131 In Salome 6 it will be possible to define a different name for the
134 :type moduleName: string
135 :param moduleName: name of the module corresponding to the component
136 (the module name is the string value in the
137 attribute "AttributeComment" of the component)
139 :type componentName: string
140 :param componentName: name of the new component if created. If
141 :const:`None`, use `moduleName` instead.
144 :param icon: icon for the new component (attribute "AttributePixMap").
146 :type containerName: string
147 :param containerName: name of the container in which the engine should be
150 :return: the SComponent found or created.
153 sComponent = self.study.FindComponent(moduleName)
154 if sComponent is None:
155 sComponent = self.builder.NewComponent(moduleName)
156 # Note that the NewComponent method set the "comment" attribute to the
157 # value of its argument (moduleName here)
158 if componentName is None:
159 componentName = moduleName
160 self.builder.SetName(sComponent, componentName)
162 # _MEM_ : This will be effective if and only if "moduleName"
163 # really corresponds to the module name (as specified in the
165 self.setIcon(sComponent, icon)
167 # This part will stay inactive until Salome 6. In Salome 6, the
168 # engine name will be stored separately from the module name.
169 # An internal convention (in this class) is to store the name of the
170 # associated engine in the parameter attribute of the scomponent (so that
171 # it could be retrieved in a future usage of this scomponent, for example,
172 # for the need of the function loadComponentEngine). The comment attribute
173 # SHOULD NOT be used for this purpose because it's used by the SALOME
174 # resources manager to identify the SALOME module and then localized
176 #attr = self.builder.FindOrCreateAttribute( sComponent, "AttributeParameter" )
177 #attr.SetString( "ENGINE_NAME", engineName )
179 engine = salome.lcc.FindOrLoadComponent(containerName, moduleName)
181 raise Exception("Cannot load engine %s in container %s. See "
182 "logs of container %s for more details." %
183 (moduleName, containerName, containerName))
184 self.builder.DefineComponentInstance(sComponent, engine)
188 ## Load the engine corresponding to \b sComponent in the container
189 # \b containerName, associate the engine with the component and load the
190 # CORBA objects of this component in the study.
191 def loadComponentEngine(self, sComponent,
192 containerName = _DEFAULT_CONTAINER):
194 Load the engine corresponding to `sComponent` in the container
195 `containerName`, associate the engine with the component and load the
196 CORBA objects of this component in the study.
198 # This part will stay inactive until Salome 6. In Salome 6, the
199 # engine name will be stored separately from the module name.
200 #attr = self.builder.FindOrCreateAttribute( sComponent, "AttributeParameter" )
201 #engineName = attr.GetString( "ENGINE_NAME" )
202 engine = salome.lcc.FindOrLoadComponent(containerName,
203 sComponent.GetComment())
205 raise Exception("Cannot load component %s in container %s. See "
206 "logs of container %s for more details." %
207 (sComponent.GetComment(), containerName,
209 self.builder.LoadWith(sComponent, engine)
211 ## Get the CORBA object associated with the SObject \b item, eventually by
212 # first loading it with the corresponding engine.
213 def getOrLoadObject(self, item):
215 Get the CORBA object associated with the SObject `item`, eventually by
216 first loading it with the corresponding engine.
218 object = item.GetObject()
219 if object is None: # the engine has not been loaded yet
220 sComponent = item.GetFatherComponent()
221 self.loadComponentEngine(sComponent)
222 object = item.GetObject()
225 ## Find an object under \b fatherItem in the study with the given
226 # attributes. Return the first one found if at least one exists,
227 # otherwise create a new one with the given attributes and return it.
229 # See \b setItem() for the description of the parameters.
230 def findOrCreateItem(self, fatherItem, name, fileType = None,
231 fileName = None, comment = None, icon = None,
232 IOR = None, typeId = None):
234 Find an object under `fatherItem` in the study with the given
235 attributes. Return the first one found if at least one exists,
236 otherwise create a new one with the given attributes and return it.
238 See :meth:`setItem` for the description of the parameters.
240 sObject = self.findItem(fatherItem, name, fileType, fileName, comment,
243 sObject = self.createItem(fatherItem, name, fileType, fileName,
244 comment, icon, IOR, typeId)
247 ## Find an item with given attributes under \b fatherItem in the study. If
248 # none is found, return \b None. If several items correspond to
249 # the parameters, only the first one is returned. The search is made
250 # only on given parameters (i.e. not \b None). To look explicitly
251 # for an empty attribute, use an empty string in the corresponding
254 # See \b setItem() for the description of the parameters.
255 def findItem(self, fatherItem, name = None, fileType = None,
256 fileName = None, comment = None, icon = None, IOR = None,
259 Find an item with given attributes under `fatherItem` in the study. If
260 none is found, return :const:`None`. If several items correspond to
261 the parameters, only the first one is returned. The search is made
262 only on given parameters (i.e. not :const:`None`). To look explicitly
263 for an empty attribute, use an empty string in the corresponding
266 See :meth:`setItem` for the description of the parameters.
269 childIterator = self.study.NewChildIterator(fatherItem)
270 while childIterator.More() and foundItem is None:
271 childItem = childIterator.Value()
273 (name is None or self.getName(childItem) == name) and \
274 (fileType is None or \
275 self.getFileType(childItem) == fileType) and \
276 (fileName is None or \
277 self.getFileName(childItem) == fileName) and \
278 (comment is None or self.getComment(childItem) == comment) and \
280 self.getIcon(childItem) == icon) and \
281 (IOR is None or childItem.GetIOR() == IOR) and \
283 self.getTypeId(childItem) == typeId):
284 foundItem = childItem
288 ## Create a new object named \b name under \b fatherItem in the study, with
289 # the given attributes. If an object named \b name already exists under
290 # the father object, the new object is created with a new name \b name_X
291 # where X is the first available index.
293 # param fatherItem (SObject) item under which the new item will be added.
294 # \return new SObject created in the study.
296 # See \b setItem() for the description of the other parameters.
297 def createItem(self, fatherItem, name, fileType = None, fileName = None,
298 comment = None, icon = None, IOR = None, typeId = None):
300 Create a new object named `name` under `fatherItem` in the study, with
301 the given attributes. If an object named `name` already exists under
302 the father object, the new object is created with a new name `name_X`
303 where X is the first available index.
305 :type fatherItem: SObject
306 :param fatherItem: item under which the new item will be added.
308 :return: new SObject created in the study
310 See :meth:`setItem` for the description of the other parameters.
312 aSObject = self.builder.NewObject(fatherItem)
314 aChildIterator = self.study.NewChildIterator(fatherItem)
318 anIdRE = re.compile("^" + aDelim + "[0-9]+")
319 aNameRE = re.compile("^" + name)
320 while aChildIterator.More():
321 aSObj = aChildIterator.Value()
322 aChildIterator.Next()
323 aName = aSObj.GetName()
324 if re.match(aNameRE,aName):
325 aTmp = aName[aLength:]
326 if re.match(anIdRE,aTmp):
328 anId = string.atol(aTmp[1:])
342 aName = aName + aDelim + str(aMaxId)
345 self.setItem(aSObject, aName, fileType, fileName, comment, icon,
350 ## Modify the attributes of an item in the study. Unspecified attributes
351 # (i.e. those set to \b None) are left unchanged.
353 # \param item (SObject) item to modify.
355 # \param name (string or unicode) item name (attribute \b AttributeName).
357 # \param fileType (string or unicode) item file type (attribute \b AttributeFileType).
359 # \param fileName (string or unicode) item file name (attribute \b AttributeExternalFileDef).
361 # \param comment (string or unicode) item comment (attribute \b AttributeComment). Note that
362 # this attribute will appear in the \b Value column in the object browser.
364 # \param icon (string or unicode) item icon name (attribute \b AttributePixMap).
366 # \param IOR (string) IOR of a CORBA object associated with the item
367 # (attribute \b AttributeIOR).
369 # \param typeId (integer) item type (attribute \b AttributeLocalID).
370 def setItem(self, item, name = None, fileType = None, fileName = None,
371 comment = None, icon = None, IOR = None, typeId = None):
373 Modify the attributes of an item in the study. Unspecified attributes
374 (i.e. those set to :const:`None`) are left unchanged.
377 :param item: item to modify.
379 :type name: string or unicode
380 :param name: item name (attribute 'AttributeName').
382 :type fileType: string or unicode
383 :param fileType: item file type (attribute 'AttributeFileType').
385 :type fileName: string or unicode
386 :param fileName: item file name (attribute
387 'AttributeExternalFileDef').
389 :type comment: string or unicode
390 :param comment: item comment (attribute 'AttributeComment'). Note that
391 this attribute will appear in the 'Value' column in
394 :type icon: string or unicode
395 :param icon: item icon name (attribute 'AttributePixMap').
398 :param IOR: IOR of a CORBA object associated with the item
399 (attribute 'AttributeIOR').
401 :type typeId: integer
402 :param typeId: item type (attribute 'AttributeLocalID').
404 logger.debug("setItem (ID=%s): name=%s, fileType=%s, fileName=%s, "
405 "comment=%s, icon=%s, IOR=%s" %
406 (item.GetID(), name, fileType, fileName, comment,
409 self.setName(item, name)
410 if fileType is not None:
411 self.setFileType(item, fileType)
412 if fileName is not None:
413 self.setFileName(item, fileName)
414 if comment is not None:
415 self.setComment(item, comment)
417 self.setIcon(item, icon)
419 self.builder.SetIOR(item, IOR)
420 if typeId is not None:
421 self.setTypeId(item, typeId)
423 ## Remove the given item from the study. Note that the items are never
424 # really deleted. They just don't appear in the study anymore.
426 # \param item (SObject) the item to be removed
428 # \param withChildren (boolean) if \b True, also remove the children of item
430 # \return \b True if the item was removed successfully, or
431 # \b False if an error happened.
432 def removeItem(self, item, withChildren = False ):
434 Remove the given item from the study. Note that the items are never
435 really deleted. They just don't appear in the study anymore.
438 :param item: the item to be removed
440 :type withChildren: boolean
441 :param withChildren: if :const:`True`, also remove the children of
444 :return: :const:`True` if the item was removed successfully, or
445 :const:`False` if an error happened.
450 self.builder.RemoveObjectWithChildren(item)
452 self.builder.RemoveObject(item)
458 ## Find an item tagged \b tag under \b fatherItem in the study tree or
459 # create it if there is none, then set its attributes.
461 # \param fatherItem (SObject) item under which the tagged item will be looked for
462 # and eventually created.
464 # \param tag integer) tag of the item to look for.
466 # \return the SObject at \b tag if found or created successfully, or
467 # \b None if an error happened.
469 # See \b setItem() for the description of the other parameters.
470 def setItemAtTag(self, fatherItem, tag, name = None, fileType = None,
471 fileName = None, comment = None, icon = None, IOR = None,
474 Find an item tagged `tag` under `fatherItem` in the study tree or
475 create it if there is none, then set its attributes.
477 :type fatherItem: SObject
478 :param fatherItem: item under which the tagged item will be looked for
479 and eventually created.
482 :param tag: tag of the item to look for.
484 :return: the SObject at `tag` if found or created successfully, or
485 :const:`None` if an error happened.
487 See :meth:`setItem` for the description of the other parameters.
489 found, sObj = fatherItem.FindSubObject(tag)
491 sObj = self.builder.NewObjectToTag(fatherItem, tag)
492 self.setItem(sObj, name, fileType, fileName, comment, icon,
496 ## Return the name of the object sObject
497 def getName(self, sObject):
498 val = sObject.GetName()
499 return unicode(val, ENCODING_FOR_SALOME_STUDY)
501 ## Set the name of the object sObject
502 def setName(self, sObject, name):
503 self.builder.SetName(sObject, name.encode(ENCODING_FOR_SALOME_STUDY))
505 ## Return the comment of the object sObject
506 def getComment(self, sObject):
507 val = sObject.GetComment()
508 return unicode(val, ENCODING_FOR_SALOME_STUDY)
510 ## Set the comment of the object sObject
511 def setComment(self, sObject, comment):
512 self.builder.SetComment(sObject, comment.encode(ENCODING_FOR_SALOME_STUDY))
514 ## Return the value of the attribute named \b attributeName on the object
515 # sObject, or \b default if the attribute doesn't exist.
516 def getAttributeValue(self, sObject, attributeName, default = None):
518 Return the value of the attribute named `attributeName` on the object
519 `sObject`, or `default` if the attribute doesn't exist.
522 found, attr = self.builder.FindAttribute(sObject, attributeName)
527 ## Set the value of the attribute named \b attributeName on the object
528 # sObject to the value \b attributeValue.
529 def setAttributeValue(self, sObject, attributeName, attributeValue):
531 Set the value of the attribute named `attributeName` on the object
532 `sObject` to the value `attributeValue`.
534 attr = self.builder.FindOrCreateAttribute(sObject, attributeName)
535 attr.SetValue(attributeValue)
537 ## Return the value of the attribute "AttributeLocalID" of the object
538 # sObject, or \b None if it is not set.
539 def getTypeId(self, sObject):
541 Return the value of the attribute "AttributeLocalID" of the object
542 `sObject`, or :const:`None` if it is not set.
544 return self.getAttributeValue(sObject, "AttributeLocalID")
546 ## Set the attribute "AttributeLocalID" of the object \b sObject to the
548 def setTypeId(self, sObject, value):
550 Set the attribute "AttributeLocalID" of the object `sObject` to the
553 self.setAttributeValue(sObject, "AttributeLocalID", value)
555 ## Return the value of the attribute "AttributeFileType" of the object
556 # sObject, or an empty string if it is not set.
557 def getFileType(self, sObject):
559 Return the value of the attribute "AttributeFileType" of the object
560 `sObject`, or an empty string if it is not set.
562 val = self.getAttributeValue(sObject, "AttributeFileType", "")
563 return unicode(val, ENCODING_FOR_SALOME_STUDY)
565 ## Set the attribute "AttributeFileType" of the object sObject to the
567 def setFileType(self, sObject, value):
569 Set the attribute "AttributeFileType" of the object `sObject` to the
572 self.setAttributeValue(sObject, "AttributeFileType",
573 value.encode(ENCODING_FOR_SALOME_STUDY))
575 ## Return the value of the attribute "AttributeExternalFileDef" of the
576 # object sObject, or an empty string if it is not set.
577 def getFileName(self, sObject):
579 Return the value of the attribute "AttributeExternalFileDef" of the
580 object `sObject`, or an empty string if it is not set.
582 val = self.getAttributeValue(sObject, "AttributeExternalFileDef", "")
583 return unicode(val, ENCODING_FOR_SALOME_STUDY)
585 ## Set the attribute "AttributeExternalFileDef" of the object sObject
586 # to the value value.
587 def setFileName(self, sObject, value):
589 Set the attribute "AttributeExternalFileDef" of the object `sObject`
590 to the value `value`.
592 self.setAttributeValue(sObject, "AttributeExternalFileDef",
593 value.encode(ENCODING_FOR_SALOME_STUDY))
595 ## Return the value of the attribute "AttributePixMap" of the object
596 # sObject, or an empty string if it is not set.
597 def getIcon(self, sObject):
599 Return the value of the attribute "AttributePixMap" of the object
600 `sObject`, or an empty string if it is not set.
603 found, attr = self.builder.FindAttribute(sObject, "AttributePixMap")
604 if found and attr.HasPixMap():
605 value = attr.GetPixMap()
606 return unicode(value, ENCODING_FOR_SALOME_STUDY)
608 ## Set the attribute "AttributePixMap" of the object sObject to the
610 def setIcon(self, sObject, value):
612 Set the attribute "AttributePixMap" of the object `sObject` to the
615 attr = self.builder.FindOrCreateAttribute(sObject, "AttributePixMap")
616 attr.SetPixMap(value.encode(ENCODING_FOR_SALOME_STUDY))