]> SALOME platform Git repositories - modules/yacs.git/blobdiff - src/hmi/commandsProc.cxx
Salome HOME
Copyright update 2022
[modules/yacs.git] / src / hmi / commandsProc.cxx
index 68dfa00b67afee0692012d45f299ff5bfe6ff22b..063473ec7ef4500e19251c02140f3756c3b1fb58 100644 (file)
@@ -1,9 +1,9 @@
-// Copyright (C) 2006-2012  CEA/DEN, EDF R&D
+// Copyright (C) 2006-2022  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -49,6 +49,7 @@
 #include "PresetPorts.hxx"
 #include "ComponentDefinition.hxx"
 #include "SalomeContainer.hxx"
+#include "SalomeHPContainer.hxx"
 #include "SalomeComponent.hxx"
 #include "TypeCode.hxx"
 #include "RuntimeSALOME.hxx"
@@ -147,7 +148,7 @@ TypeOfElem ProcInvoc::getTypeOfNode(YACS::ENGINE::Node* node)
   else if (dynamic_cast<YACS::ENGINE::ForLoop*>(node))          nodeType = FORLOOP;
   else if (dynamic_cast<YACS::ENGINE::WhileLoop*>(node))        nodeType = WHILELOOP;
   else if (dynamic_cast<YACS::ENGINE::Switch*>(node))           nodeType = SWITCH;
-  else if (dynamic_cast<YACS::ENGINE::ForEachLoop*>(node))      nodeType = FOREACHLOOP;
+  else if (dynamic_cast<YACS::ENGINE::ForEachLoopGen*>(node))      nodeType = FOREACHLOOP;
   else if (dynamic_cast<YACS::ENGINE::OptimizerLoop*>(node))    nodeType = OPTIMIZERLOOP;
   else if (dynamic_cast<YACS::ENGINE::PresetNode*>(node))       nodeType = PRESETNODE;
   else if (dynamic_cast<YACS::ENGINE::OutNode*>(node))          nodeType = OUTNODE;
@@ -291,7 +292,7 @@ bool CommandAddNodeFromCatalog::localExecute()
               (dynamic_cast<YACS::ENGINE::Switch*>(father))->edSetNode(_swCase,son);
               break;
             case FOREACHLOOP:
-              (dynamic_cast<YACS::ENGINE::ForEachLoop*>(father))->edSetNode(son);
+              (dynamic_cast<YACS::ENGINE::ForEachLoopGen*>(father))->edSetNode(son);
               break;
             case OPTIMIZERLOOP:
               (dynamic_cast<YACS::ENGINE::OptimizerLoop*>(father))->edSetNode(son);
@@ -504,8 +505,10 @@ bool CommandReparentNode::localReverse()
 
 // ----------------------------------------------------------------------------
 CommandPutInComposedNode::CommandPutInComposedNode(std::string position,
-                                                   std::string newParent,std::string type)
-  : Command(), _position(position), _newParent(newParent), _type(type)
+                                                   std::string newParent,
+                                                  std::string type,
+                                                  bool toSaveRestoreLinks)
+  : Command(), _position(position), _newParent(newParent), _type(type), _toSaveRestoreLinks(toSaveRestoreLinks)
 {
   DEBTRACE("CommandPutInComposedNode::CommandPutInComposedNode " << _position << " " << _newParent);
   _newpos ="";
@@ -513,7 +516,8 @@ CommandPutInComposedNode::CommandPutInComposedNode(std::string position,
 
 std::string CommandPutInComposedNode::dump()
 {
-  string ret ="CommandPutInComposedNode " + _position + " " + _newParent + " " + _type;
+  string save = _toSaveRestoreLinks ? "true" : "false";
+  string ret ="CommandPutInComposedNode " + _position + " " + _newParent + " " + _type + " " + save;
   return ret;
 }
 
@@ -532,8 +536,8 @@ bool CommandPutInComposedNode::localExecute()
       SubjectNode * snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
       Subject *subo = GuiContext::getCurrent()->_mapOfSubjectNode[oldFather];
       SubjectComposedNode* sop = dynamic_cast<SubjectComposedNode*>(subo);
-      //save existing links
-      snode->saveLinks();
+      if (_toSaveRestoreLinks)
+       snode->saveLinks(); //save existing links
       //remove external links
       snode->removeExternalLinks();
       snode->removeExternalControlLinks();
@@ -544,15 +548,39 @@ bool CommandPutInComposedNode::localExecute()
       //refresh node views
       sop->update(CUT, ProcInvoc::getTypeOfNode(node), snode);
 
-      //create a ComposedNode (type _type) with name _newParent
-      YACS::ENGINE::Catalog *catalog = YACS::ENGINE::getSALOMERuntime()->getBuiltinCatalog();
-      Node* nodeToClone = catalog->_composednodeMap[_type];
-      Node* composednode = nodeToClone->clone(0);
-      composednode->setName(_newParent);
-      //add the new composednode as child of oldfather
-      oldFather->edAddChild(composednode);
-      //create the subject composednode
-      SubjectNode *scomposednode = sop->addSubjectNode(composednode,"",catalog,"",_type);
+      // try to find a node with the given name:
+      //   success: use it as target composed node
+      //   fail:    create such a node and use it
+      std::list<Node*> children = proc->getChildren();
+      Node* composednode = 0;
+      SubjectNode *scomposednode = 0;
+      for (list<Node*>::iterator it = children.begin(); it != children.end(); ++it)
+       {
+         if ( _newParent == (*it)->getName() )
+           {
+             //get an existing ComposedNode with name _newParent
+             composednode = (*it);
+             break;
+           }
+       }
+      // target node was found
+      if ( composednode )
+       {
+         scomposednode = GuiContext::getCurrent()->_mapOfSubjectNode[composednode];
+       }
+      // creation of target node
+      else 
+       {
+         //create a ComposedNode (type _type) with name _newParent
+         YACS::ENGINE::Catalog *catalog = YACS::ENGINE::getSALOMERuntime()->getBuiltinCatalog();
+         Node* nodeToClone = catalog->_composednodeMap[_type];
+         composednode = nodeToClone->cloneWithoutCompAndContDeepCpy(0);
+         composednode->setName(_newParent);
+         //add the new composednode as child of oldfather
+         oldFather->edAddChild(composednode);
+         //create the subject composednode
+         scomposednode = sop->addSubjectNode(composednode,"",catalog,"",_type);
+       }
 
       //add the old node as child of new composednode
       (dynamic_cast<YACS::ENGINE::ComposedNode*>(composednode))->edAddChild(node);
@@ -560,8 +588,8 @@ bool CommandPutInComposedNode::localExecute()
       //add the subject node to subject composednode
       (dynamic_cast<SubjectComposedNode*>(scomposednode))->houseKeepingAfterCutPaste(false, snode);
       snode->setParent(scomposednode);
-      //restore links
-      snode->restoreLinks();
+      if (_toSaveRestoreLinks)
+       snode->restoreLinks(); //restore links
       //refresh all views
       scomposednode->update(PASTE, ProcInvoc::getTypeOfNode(node), snode);
       snode->recursiveUpdate(RENAME, 0, snode);
@@ -680,8 +708,8 @@ bool CommandCopyNode::localExecute()
       if (Loop *loop = dynamic_cast<Loop*>(newFather))
         if (!loop->edGetDirectDescendants().empty())
           throw YACS::Exception("Already a node in a new parent of Loop type");
-      //_clone = node->clone(newFather);
-      _clone = node->clone(0);
+      //_clone = node->cloneWithoutCompAndContDeepCpy(newFather);
+      _clone = node->cloneWithoutCompAndContDeepCpy(0);
       if (!_clone)
         throw YACS::Exception("Node cannot be cloned");
       int nodeSuffix = -1;
@@ -872,7 +900,7 @@ bool CommandRenameContainer::localExecute()
       container->setName(_newName);
       proc->containerMap[_newName] = container;
       YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(container));
-      SubjectContainer *scont = GuiContext::getCurrent()->_mapOfSubjectContainer[container]; 
+      SubjectContainerBase *scont(GuiContext::getCurrent()->_mapOfSubjectContainer[container]);
       scont-> update(RENAME, 0, scont);
       scont->notifyComponentsChange(ASSOCIATE, CONTAINER, scont);
     }
@@ -898,7 +926,7 @@ bool CommandRenameContainer::localReverse()
       container->setName(_oldName);
       proc->containerMap[_oldName] = container;
       YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(container));
-      SubjectContainer *scont = GuiContext::getCurrent()->_mapOfSubjectContainer[container]; 
+      SubjectContainerBase *scont(GuiContext::getCurrent()->_mapOfSubjectContainer[container]);
       scont-> update(RENAME, 0, scont);
       scont->notifyComponentsChange(ASSOCIATE, CONTAINER, scont);
     }
@@ -2442,6 +2470,7 @@ bool CommandSetSwitchSelect::localExecute()
       setErrorMsg(ex);
       return false;
     }
+  return true;
 }
 
 bool CommandSetSwitchSelect::localReverse()
@@ -2464,6 +2493,7 @@ bool CommandSetSwitchSelect::localReverse()
       setErrorMsg(ex);
       return false;
     }
+  return true;
 }
 
 // ----------------------------------------------------------------------------
@@ -3081,22 +3111,19 @@ bool CommandAddControlLink::localReverse()
 
 // ----------------------------------------------------------------------------
 
-CommandAddContainer::CommandAddContainer(std::string name,
-                                         std::string refContainer)
+CommandAddContainerBase::CommandAddContainerBase(std::string name, std::string refContainer)
   : Command(), _name(name), _containerToClone(refContainer), _subcont(0)
 {
-  DEBTRACE("CommandAddContainer::CommandAddContainer " << name << " " << refContainer);
+  DEBTRACE("CommandAddContainerBase::CommandAddContainerBase " << name << " " << refContainer);
 }
 
-std::string CommandAddContainer::dump()
+CommandAddContainerBase::~CommandAddContainerBase()
 {
-  string ret ="CommandAddContainer " + _name + " " + _containerToClone;
-  return ret;
 }
 
-bool CommandAddContainer::localExecute()
+bool CommandAddContainerBase::localExecute()
 {
-  DEBTRACE("CommandAddContainer::localExecute");
+  DEBTRACE("CommandAddContainerBase::localExecute");
   try
     {
       Proc* proc = GuiContext::getCurrent()->getProc();
@@ -3105,7 +3132,7 @@ bool CommandAddContainer::localExecute()
           GuiContext::getCurrent()->_lastErrorMessage = "There is already a container with that name";
           return false;
         }
-      Container *container = new SalomeContainer();
+      Container *container(createNewInstance());
       if (! _containerToClone.empty())
         {
           if (proc->containerMap.count(_containerToClone))
@@ -3130,15 +3157,15 @@ bool CommandAddContainer::localExecute()
     }
   catch (Exception& ex)
     {
-      DEBTRACE("CommandAddContainer::localExecute() : " << ex.what());
+      DEBTRACE("CommandAddContainerBase::localExecute() : " << ex.what());
       setErrorMsg(ex);
       return false;
     }
 }
 
-bool CommandAddContainer::localReverse()
+bool CommandAddContainerBase::localReverse()
 {
-  DEBTRACE("CommandAddContainer::localReverse");
+  DEBTRACE("CommandAddContainerBase::localReverse");
   try
     {
       Proc* proc = GuiContext::getCurrent()->getProc();
@@ -3161,6 +3188,40 @@ bool CommandAddContainer::localReverse()
 
 // ----------------------------------------------------------------------------
 
+CommandAddContainer::CommandAddContainer(std::string name, std::string refContainer):CommandAddContainerBase(name,refContainer)
+{
+}
+
+std::string CommandAddContainer::dump()
+{
+  string ret ="CommandAddContainer " + _name + " " + _containerToClone;
+  return ret;
+}
+
+Container *CommandAddContainer::createNewInstance() const
+{
+  return new SalomeContainer;
+}
+
+// ----------------------------------------------------------------------------
+
+CommandAddHPContainer::CommandAddHPContainer(std::string name, std::string refContainer):CommandAddContainerBase(name,refContainer)
+{
+}
+
+std::string CommandAddHPContainer::dump()
+{
+  string ret ="CommandAddHPContainer " + _name + " " + _containerToClone;
+  return ret;
+}
+
+Container *CommandAddHPContainer::createNewInstance() const
+{
+  return new SalomeHPContainer;
+}
+
+// ----------------------------------------------------------------------------
+
 CommandSetNodeProperties::CommandSetNodeProperties(std::string position, std::map<std::string,std::string> properties)
   : Command(), _position(position), _properties(properties)
 {
@@ -3327,10 +3388,13 @@ bool CommandSetContainerProperties::localExecute()
       Proc* proc = GuiContext::getCurrent()->getProc();
       if (proc->containerMap.count(_container))
         {
-          Container *ref = proc->containerMap[_container];
+          Container *ref(proc->containerMap[_container]);
           YASSERT(ref);
           _oldProp = ref->getProperties();
           ref->setProperties(_properties);
+          SubjectContainerBase *scont(GuiContext::getCurrent()->_mapOfSubjectContainer[ref]);
+          scont->update(UPDATE, 0, scont);
+          scont->notifyComponentsChange(ASSOCIATE, CONTAINER, scont);
           return true;
         }
       GuiContext::getCurrent()->_lastErrorMessage = "container not found: " + _container;
@@ -3722,7 +3786,7 @@ bool CommandAddComponentInstance::localReverse()
       YASSERT(!_subcompo->hasServices());
       Container *cont = compo->getContainer();
       YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(cont));
-      SubjectContainer *subcont = GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
+      SubjectContainerBase *subcont = GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
       subcont->detachComponent(_subcompo);
       GuiContext::getCurrent()->_mapOfSubjectComponent.erase(compo);
       proc->removeComponentInstance(compo);
@@ -3842,7 +3906,7 @@ bool CommandSetContainer::localExecute()
                 _oldcont = pyNode->getContainer()->getName();
               pyNode->setContainer(cont);
               SubjectNode* snode = GuiContext::getCurrent()->_mapOfSubjectNode[pyNode];
-              SubjectContainer *subcont = GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
+              SubjectContainerBase *subcont = GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
               snode->update(ASSOCIATE, 0, subcont);
               return true;
             }
@@ -3879,7 +3943,7 @@ bool CommandSetContainer::localReverse()
             {
               pyNode->setContainer(cont);
               SubjectNode* snode = GuiContext::getCurrent()->_mapOfSubjectNode[pyNode];
-              SubjectContainer *subcont = GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
+              SubjectContainerBase *subcont = GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
               snode->update(ASSOCIATE, 0, subcont);
               return true;
             }
@@ -3940,7 +4004,7 @@ bool CommandAssociateComponentToContainer::localExecute()
               YASSERT(GuiContext::getCurrent()->_mapOfSubjectComponent.count(compo));
               SubjectComponent *scomp =  GuiContext::getCurrent()->_mapOfSubjectComponent[compo];
               YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(cont));
-              SubjectContainer *subcont =  GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
+              SubjectContainerBase *subcont =  GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
               scomp->addSubjectReference(subcont);
               if (scomp->_subRefContainer)
                 subcont->moveComponent(scomp->_subRefContainer);
@@ -3983,7 +4047,7 @@ bool CommandAssociateComponentToContainer::localReverse()
               YASSERT(GuiContext::getCurrent()->_mapOfSubjectComponent.count(compo));
               SubjectComponent *scomp =  GuiContext::getCurrent()->_mapOfSubjectComponent[compo];
               YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(cont));
-              SubjectContainer *subcont =  GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
+              SubjectContainerBase *subcont =  GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
               scomp->addSubjectReference(subcont);
               if (scomp->_subRefContainer)
                 subcont->moveComponent(scomp->_subRefContainer);
@@ -4090,7 +4154,7 @@ bool CommandAssociateServiceToComponent::localReverse()
             {
               //component instance does not exist anymore recreate it
               ComponentInstance *oldcompo = service->getComponent();
-              compo = oldcompo->clone();
+              compo = oldcompo->cloneAlways();
               compo->setName(_oldInstance);
               proc->addComponentInstance(compo, _oldInstance);
               Container *cont = proc->containerMap[_oldcont];
@@ -4226,7 +4290,7 @@ bool CommandAddComponentFromCatalog::localReverse()
         throw YACS::Exception("Component instance with services attached, not removed");
       Container *cont = compo->getContainer();
       YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(cont));
-      SubjectContainer *subcont = GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
+      SubjectContainerBase *subcont = GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
       subcont->detachComponent(subCompo);
       //remove componentInstance from proc, from context
       if (_createdInstance)