Ignore:
Timestamp:
2016-04-07T17:11:46+02:00 (8 years ago)
Author:
Adeel Asghar <adeel.asghar@…>
Branches:
Added-citation-metadata, maintenance/v1.14, maintenance/v1.15, maintenance/v1.16, maintenance/v1.17, maintenance/v1.18, maintenance/v1.19, maintenance/v1.20, maintenance/v1.21, maintenance/v1.22, maintenance/v1.23, master, omlib-staging
Children:
0b9b099
Parents:
8f6676e7
Message:

Check if interfaces are aligned or not.
Fixed fetch interface data.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • OMEdit/OMEditGUI/Editors/MetaModelEditor.cpp

    rf06d1664 rfcbacb9  
    145145
    146146/*!
     147 * \brief MetaModelEditor::getInterfacePoint
     148 * \param subModelName
     149 * \param interfaceName
     150 * \return
     151 */
     152QDomElement MetaModelEditor::getInterfacePoint(QString subModelName, QString interfaceName)
     153{
     154  QDomNodeList subModelList = mXmlDocument.elementsByTagName("SubModel");
     155  for (int i = 0 ; i < subModelList.size() ; i++) {
     156    QDomElement subModel = subModelList.at(i).toElement();
     157    if (subModel.attribute("Name").compare(subModelName) == 0) {
     158      QDomNodeList subModelChildren = subModel.childNodes();
     159      for (int j = 0 ; j < subModelChildren.size() ; j++) {
     160        QDomElement interfaceElement = subModelChildren.at(j).toElement();
     161        if (interfaceElement.tagName().compare("InterfacePoint") == 0 && interfaceElement.attribute("Name").compare(interfaceName) == 0) {
     162          return interfaceElement;
     163        }
     164      }
     165    }
     166  }
     167  return QDomElement();
     168}
     169
     170/*!
    147171 * \brief MetaModelEditor::getConnectionsElement
    148172 * Returns the Connections element tag.
     
    303327    connections.appendChild(connection);
    304328    setPlainText(mXmlDocument.toString());
     329    // check if interfaces are aligned
     330    if (interfacesAligned(pConnectionLineAnnotation->getStartComponentName(), pConnectionLineAnnotation->getEndComponentName())) {
     331      pConnectionLineAnnotation->setLineColor(QColor(Qt::black));
     332    } else {
     333      pConnectionLineAnnotation->setLineColor(QColor(Qt::red));
     334    }
    305335    return true;
    306336  }
     
    407437    QDomElement interfaceDataElement = interfaces.firstChildElement();
    408438    while (!interfaceDataElement.isNull()) {
    409       if (subModel.attribute("Name").compare(interfaceDataElement.attribute("model")) == 0
    410           && !existInterfaceData(subModel.attribute("Name"), interfaceDataElement.attribute("name"))) {
    411         QDomElement interfacePoint = mXmlDocument.createElement("InterfacePoint");
    412         interfacePoint.setAttribute("Name",interfaceDataElement.attribute("name"));
    413         interfacePoint.setAttribute("Position",interfaceDataElement.attribute("Position"));
    414         interfacePoint.setAttribute("Angle321",interfaceDataElement.attribute("Angle321"));
    415         subModel.appendChild(interfacePoint);
    416         setPlainText(mXmlDocument.toString());
    417 
    418         TLMInterfacePointInfo *pTLMInterfacePointInfo;
    419         pTLMInterfacePointInfo = new TLMInterfacePointInfo(subModel.attribute("Name"),"shaft3" , interfaceDataElement.attribute("name"));
    420         getModelWidget()->getDiagramGraphicsView()->getComponentObject(subModel.attribute("Name"))->addInterfacePoint(pTLMInterfacePointInfo);
     439      if (subModel.attribute("Name").compare(interfaceDataElement.attribute("model")) == 0) {
     440        QDomElement interfacePoint;
     441        if (existInterfaceData(subModel.attribute("Name"), interfaceDataElement)) {
     442          interfacePoint = getInterfacePoint(subModel.attribute("Name"), interfaceDataElement.attribute("Name"));
     443          interfacePoint.setAttribute("Name", interfaceDataElement.attribute("Name"));
     444          interfacePoint.setAttribute("Position", interfaceDataElement.attribute("Position"));
     445          interfacePoint.setAttribute("Angle321", interfaceDataElement.attribute("Angle321"));
     446          setPlainText(mXmlDocument.toString());
     447          // check if interface is aligned
     448          foreach (LineAnnotation* pConnectionLineAnnotation, mpModelWidget->getDiagramGraphicsView()->getConnectionsList()) {
     449            QString interfaceName = QString("%1.%2").arg(subModel.attribute("Name")).arg(interfaceDataElement.attribute("Name"));
     450            if (pConnectionLineAnnotation->getStartComponentName().compare(interfaceName) == 0) {
     451              alignInterfaces(pConnectionLineAnnotation->getStartComponentName(), pConnectionLineAnnotation->getEndComponentName(), false);
     452            }
     453            if (pConnectionLineAnnotation->getEndComponentName().compare(interfaceName) == 0) {
     454              alignInterfaces(pConnectionLineAnnotation->getStartComponentName(), pConnectionLineAnnotation->getEndComponentName(), false);
     455            }
     456          }
     457        } else {
     458          QDomElement interfacePoint = mXmlDocument.createElement("InterfacePoint");
     459          interfacePoint.setAttribute("Name", interfaceDataElement.attribute("Name"));
     460          interfacePoint.setAttribute("Position", interfaceDataElement.attribute("Position"));
     461          interfacePoint.setAttribute("Angle321", interfaceDataElement.attribute("Angle321"));
     462          subModel.appendChild(interfacePoint);
     463          setPlainText(mXmlDocument.toString());
     464          Component *pComponent = mpModelWidget->getDiagramGraphicsView()->getComponentObject(subModel.attribute("Name"));
     465          if (pComponent) {
     466            pComponent->insertInterfacePoint(interfaceDataElement.attribute("Name"));
     467          }
     468        }
    421469      }
    422470      interfaceDataElement = interfaceDataElement.nextSiblingElement();
     
    426474
    427475/*!
    428   Checks whether the interface already exists in MetaModel or not.
    429   \param interfaceName - the name for the interface to check.
    430   \return true on success.
    431   */
    432 bool MetaModelEditor::existInterfaceData(QString subModelName, QString interfaceName)
    433 {
    434   QDomNodeList subModelList = mXmlDocument.elementsByTagName("SubModel");
    435   for (int i = 0 ; i < subModelList.size() ; i++) {
    436     QDomElement subModel = subModelList.at(i).toElement();
    437     if (subModel.attribute("Name").compare(subModelName) == 0) {
    438       QDomNodeList subModelChildren = subModel.childNodes();
    439       for (int j = 0 ; j < subModelChildren.size() ; j++) {
    440         QDomElement interfaceElement = subModelChildren.at(j).toElement();
    441         if (interfaceElement.tagName().compare("InterfacePoint") == 0 && interfaceElement.attribute("Name").compare(interfaceName)== 0) {
    442            return true;
    443         }
    444       }
    445       break;
    446     }
    447   }
    448   return false;
     476 * \brief MetaModelEditor::interfacesAligned
     477 * Checkes whether specified TLM interfaces are aligned
     478 * \param interface1 First interface (submodel1.interface1)
     479 * \param interface2 Second interface (submodel2.interface2)
     480 * \return
     481 */
     482bool MetaModelEditor::interfacesAligned(QString interface1, QString interface2)
     483{
     484  //Extract rotation and position vectors to Qt matrices
     485  QGenericMatrix<3,1,double> CG_X1_PHI_CG;  //Rotation of X1 relative to CG expressed in CG
     486  QGenericMatrix<3,1,double> X1_C1_PHI_X1;  //Rotation of C1 relative to X1 expressed in X1
     487  QGenericMatrix<3,1,double> CG_X1_R_CG;      //Position of X1 relative to CG expressed in CG
     488  QGenericMatrix<3,1,double> X1_C1_R_X1;      //Position of C1 relative to X1 expressed in X1
     489  if(!getPositionAndRotationVectors(interface1,CG_X1_PHI_CG, X1_C1_PHI_X1,CG_X1_R_CG,X1_C1_R_X1)) return false;
     490
     491  QGenericMatrix<3,1,double> CG_X2_PHI_CG;  //Rotation of X2 relative to CG expressed in CG
     492  QGenericMatrix<3,1,double> X2_C2_PHI_X2;  //Rotation of C2 relative to X2 expressed in X2
     493  QGenericMatrix<3,1,double> CG_X2_R_CG;      //Position of X2 relative to CG expressed in CG
     494  QGenericMatrix<3,1,double> X2_C2_R_X2;      //Position of C2 relative to X2 expressed in X2
     495  if(!getPositionAndRotationVectors(interface2,CG_X2_PHI_CG, X2_C2_PHI_X2,CG_X2_R_CG,X2_C2_R_X2)) return false;
     496
     497  QGenericMatrix<3,1,double> CG_C1_R_CG, CG_C1_PHI_CG, CG_C2_R_CG, CG_C2_PHI_CG;
     498  QGenericMatrix<3,3,double> R_X1_C1, R_CG_X1, R_CG_C1, R_X2_C2, R_CG_X2, R_CG_C2;
     499
     500  //Compute rotation matrices for both interfaces relative to CG and make sure they are the same
     501  R_X1_C1 = getRotationMatrix(X1_C1_PHI_X1);    //Rotation matrix between X1 and C1
     502  R_CG_X1 = getRotationMatrix(CG_X1_PHI_CG);    //Rotation matrix between CG and X1
     503  R_CG_C1 = R_X1_C1*R_CG_X1;                       //Rotation matrix between CG and C1
     504  R_X2_C2 = getRotationMatrix(X2_C2_PHI_X2);    //Rotation matrix between X2 and C2
     505  R_CG_X2 = getRotationMatrix(CG_X2_PHI_CG);    //Rotation matrix between CG and X2
     506  R_CG_C2 = R_X2_C2*R_CG_X2;                       //Rotation matrix between CG and C2
     507
     508  bool success=true;
     509  for(int i=0; i<3; ++i) {
     510    for(int j=0; j<3; ++j) {
     511      if(!fuzzyCompare(R_CG_C1(i,j),R_CG_C2(i,j))) {
     512        success=false;
     513      }
     514    }
     515  }
     516
     517  //Compute positions for both interfaces relative to CG and make sure they are the same
     518  CG_C1_R_CG = CG_X1_R_CG + X1_C1_R_X1*R_CG_X1;   //Position of C1 relative to CG exressed in CG
     519  CG_C2_R_CG = CG_X2_R_CG + X2_C2_R_X2*R_CG_X2;   //Position of C2 relative to CG exressed in CG
     520
     521  for(int i=0; i<3; ++i) {
     522    if(!fuzzyCompare(CG_C1_R_CG(0,i),CG_C2_R_CG(0,i))) {
     523      success=false;
     524    }
     525  }
     526
     527  return success;
    449528}
    450529
     
    500579
    501580/*!
     581 * \brief MetaModelEditor::existInterfaceData
     582 * Checks whether the interface already exists in MetaModel or not.
     583 * \param subModelName
     584 * \param interfaceElement
     585 * \return
     586 */
     587bool MetaModelEditor::existInterfaceData(QString subModelName, QDomElement interfaceDataElement)
     588{
     589  QDomNodeList subModelList = mXmlDocument.elementsByTagName("SubModel");
     590  for (int i = 0 ; i < subModelList.size() ; i++) {
     591    QDomElement subModel = subModelList.at(i).toElement();
     592    if (subModel.attribute("Name").compare(subModelName) == 0) {
     593      QDomNodeList subModelChildren = subModel.childNodes();
     594      for (int j = 0 ; j < subModelChildren.size() ; j++) {
     595        QDomElement interfaceElement = subModelChildren.at(j).toElement();
     596        if (interfaceElement.tagName().compare("InterfacePoint") == 0 &&
     597            interfaceElement.attribute("Name").compare(interfaceDataElement.attribute("Name")) == 0 &&
     598            interfaceElement.attribute("Position").compare(interfaceDataElement.attribute("Position")) == 0 &&
     599            interfaceElement.attribute("Angle321").compare(interfaceDataElement.attribute("Angle321")) == 0) {
     600          return true;
     601        }
     602      }
     603      break;
     604    }
     605  }
     606  return false;
     607}
     608
     609/*!
    502610 * \brief MetaModelEditor::getRotationMatrix
    503611 * Computes the corresponding rotation matrix for specified rotation vector
     
    630738 * \param fromSubModel Full name of first interfae (X1.C1)
    631739 * \param toSubModel Full name of second interface (X2.C2)
    632  */
    633 void MetaModelEditor::alignInterfaces(QString fromInterface, QString toInterface)
    634 {
    635 
     740 * \param showError
     741 */
     742void MetaModelEditor::alignInterfaces(QString fromInterface, QString toInterface, bool showError)
     743{
    636744  //Extract rotation and position vectors to Qt matrices
    637745  QGenericMatrix<3,1,double> CG_X1_PHI_CG;  //Rotation of X1 relative to CG expressed in CG
     
    673781  setPlainText(mXmlDocument.toString());
    674782
     783  // get the relevant connection
     784  LineAnnotation* pFoundConnectionLineAnnotation = 0;
     785  foreach (LineAnnotation* pConnectionLineAnnotation, mpModelWidget->getDiagramGraphicsView()->getConnectionsList()) {
     786    if (pConnectionLineAnnotation->getStartComponentName().compare(fromInterface) == 0 &&
     787        pConnectionLineAnnotation->getEndComponentName().compare(toInterface) == 0) {
     788      pFoundConnectionLineAnnotation = pConnectionLineAnnotation;
     789      break;
     790    }
     791  }
    675792  //Give error message if alignment failed
    676   if(!interfacesAligned(fromInterface, toInterface))
    677   {
    678     mpMainWindow->getMessagesWidget()->addGUIMessage(MessageItem(MessageItem::MetaModel, "",false,0,0,0,0,"Alignment operation failed.",Helper::scriptingKind,Helper::errorLevel));
    679   }
    680 }
    681 
     793  if (!interfacesAligned(fromInterface, toInterface)) {
     794    if (showError) {
     795      mpMainWindow->getMessagesWidget()->addGUIMessage(MessageItem(MessageItem::MetaModel, "", false, 0, 0, 0, 0,
     796                                                                   "Alignment operation failed.", Helper::scriptingKind,Helper::errorLevel));
     797    }
     798    if (pFoundConnectionLineAnnotation) {
     799      pFoundConnectionLineAnnotation->setLineColor(QColor(Qt::red));
     800    }
     801  } else {
     802    if (pFoundConnectionLineAnnotation) {
     803      pFoundConnectionLineAnnotation->setLineColor(QColor(Qt::black));
     804    }
     805  }
     806}
    682807
    683808/*!
     
    693818  return (qAbs(p1 - p2) <= 0.00001 * qMin(qAbs(p1), qAbs(p2)));
    694819}
    695 
    696 
    697 /*!
    698  * \brief MetaModelEditor::interfacesAligned
    699  * Checkes whether specified TLM interfaces are aligned
    700  * \param interface1 First interface (submodel1.interface1)
    701  * \param interface2 Second interface (submodel2.interface2)
    702  * \return
    703  */
    704 bool MetaModelEditor::interfacesAligned(QString interface1, QString interface2)
    705 {
    706   //Extract rotation and position vectors to Qt matrices
    707   QGenericMatrix<3,1,double> CG_X1_PHI_CG;  //Rotation of X1 relative to CG expressed in CG
    708   QGenericMatrix<3,1,double> X1_C1_PHI_X1;  //Rotation of C1 relative to X1 expressed in X1
    709   QGenericMatrix<3,1,double> CG_X1_R_CG;      //Position of X1 relative to CG expressed in CG
    710   QGenericMatrix<3,1,double> X1_C1_R_X1;      //Position of C1 relative to X1 expressed in X1
    711   if(!getPositionAndRotationVectors(interface1,CG_X1_PHI_CG, X1_C1_PHI_X1,CG_X1_R_CG,X1_C1_R_X1)) return false;
    712 
    713   QGenericMatrix<3,1,double> CG_X2_PHI_CG;  //Rotation of X2 relative to CG expressed in CG
    714   QGenericMatrix<3,1,double> X2_C2_PHI_X2;  //Rotation of C2 relative to X2 expressed in X2
    715   QGenericMatrix<3,1,double> CG_X2_R_CG;      //Position of X2 relative to CG expressed in CG
    716   QGenericMatrix<3,1,double> X2_C2_R_X2;      //Position of C2 relative to X2 expressed in X2
    717   if(!getPositionAndRotationVectors(interface2,CG_X2_PHI_CG, X2_C2_PHI_X2,CG_X2_R_CG,X2_C2_R_X2)) return false;
    718 
    719   QGenericMatrix<3,1,double> CG_C1_R_CG, CG_C1_PHI_CG, CG_C2_R_CG, CG_C2_PHI_CG;
    720   QGenericMatrix<3,3,double> R_X1_C1, R_CG_X1, R_CG_C1, R_X2_C2, R_CG_X2, R_CG_C2;
    721 
    722   //Compute rotation matrices for both interfaces relative to CG and make sure they are the same
    723   R_X1_C1 = getRotationMatrix(X1_C1_PHI_X1);    //Rotation matrix between X1 and C1
    724   R_CG_X1 = getRotationMatrix(CG_X1_PHI_CG);    //Rotation matrix between CG and X1
    725   R_CG_C1 = R_X1_C1*R_CG_X1;                       //Rotation matrix between CG and C1
    726   R_X2_C2 = getRotationMatrix(X2_C2_PHI_X2);    //Rotation matrix between X2 and C2
    727   R_CG_X2 = getRotationMatrix(CG_X2_PHI_CG);    //Rotation matrix between CG and X2
    728   R_CG_C2 = R_X2_C2*R_CG_X2;                       //Rotation matrix between CG and C2
    729 
    730   bool success=true;
    731   for(int i=0; i<3; ++i) {
    732     for(int j=0; j<3; ++j) {
    733       if(!fuzzyCompare(R_CG_C1(i,j),R_CG_C2(i,j))) {
    734         success=false;
    735       }
    736     }
    737   }
    738 
    739   //Compute positions for both interfaces relative to CG and make sure they are the same
    740   CG_C1_R_CG = CG_X1_R_CG + X1_C1_R_X1*R_CG_X1;   //Position of C1 relative to CG exressed in CG
    741   CG_C2_R_CG = CG_X2_R_CG + X2_C2_R_X2*R_CG_X2;   //Position of C2 relative to CG exressed in CG
    742 
    743   for(int i=0; i<3; ++i) {
    744     if(!fuzzyCompare(CG_C1_R_CG(0,i),CG_C2_R_CG(0,i))) {
    745       success=false;
    746     }
    747   }
    748 
    749   return success;
    750 }
    751 
    752820
    753821/*!
Note: See TracChangeset for help on using the changeset viewer.