Changeset bf804e32 in OpenModelica


Ignore:
Timestamp:
2023-03-30T11:40:26+02:00 (13 months ago)
Author:
Adeel Asghar <adeel.asghar@…>
Parents:
23c6c6f6
git-author:
anotheruserofgithub <96748782+anotheruserofgithub@…> (03/30/23 11:27:48)
git-committer:
Adeel Asghar <adeel.asghar@…> (03/30/23 11:40:26)
Message:

[OMEdit] Support OBJ & 3DS files as CAD shapes (#10377)

  • Support OBJ & 3DS files as CAD shapes
  • Clean up functions for CAD file checks
  • Add helper functions for CAD type checks
Location:
OMEdit/OMEditLIB/Animation
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • OMEdit/OMEditLIB/Animation/AnimationUtil.h

    r6814223c rbf804e32  
    3636#define ANIMATIONUTIL_H
    3737
     38#include <QFileInfo>
    3839#include <QString>
    39 #include <QRegExp>
    40 #include <QFileInfo>
    41 
    42 #include <QOpenGLContext> // must be included before OSG headers
    43 
    44 #include <sys/stat.h>
     40
    4541#include <string>
    46 #include <osg/Vec3>
    4742
    4843#include "MainWindow.h"
     
    126121}
    127122
    128 /*! \brief Checks if the type is a cad file
    129  */
    130 inline bool isCADType(const std::string& typeName)
    131 {
    132   return ((typeName.size() >= 12 && std::string(typeName.begin(), typeName.begin() + 11) == "modelica://")
    133           || (typeName.size() >= 8 && std::string(typeName.begin(), typeName.begin() + 7) == "file://"));
    134 }
    135 
    136 
    137 inline bool dxfFileType(const std::string& typeName)
    138 {
    139   return typeName.substr(typeName.size()-3) == std::string("dxf");
    140 }
    141 
    142 
    143 inline bool stlFileType(const std::string& typeName)
    144 {
    145   return typeName.substr(typeName.size()-3) == std::string("stl");
    146 }
    147 
    148 
    149 /*! \brief Get file name of the cad file
    150  */
    151 inline std::string extractCADFilename(const std::string& s)
    152 {
    153   QString str(s.c_str());
     123/*!
     124 * \brief Gets the filename of the CAD file
     125 */
     126inline std::string extractCADFilename(const std::string& typeName)
     127{
     128  QString str(typeName.c_str());
    154129  if (str.startsWith("modelica://")) {
    155130    const QString absoluteFileName = MainWindow::instance()->getOMCProxy()->uriToFilename(str);
    156131    return absoluteFileName.toStdString();
    157132  } else {
    158     std::string fileKey = "file://";
    159     std::string s2 = s.substr(fileKey.length(), s.length());
    160     return s2;
     133    const std::string fileKey = "file://";
     134    return typeName.substr(fileKey.length(), typeName.length());
    161135  }
    162136}
    163137
     138/*!
     139 * \brief Checks if the type is a CAD file
     140 */
     141inline bool isCADFile(const std::string& typeName)
     142{
     143  return ((typeName.size() >= 12 && std::string(typeName.begin(), typeName.begin() + 11) == "modelica://") ||
     144          (typeName.size() >=  8 && std::string(typeName.begin(), typeName.begin() +  7) == "file://"));
     145}
     146
     147inline bool isDXFFile(const std::string& fileName)
     148{
     149  return fileName.substr(fileName.size() - 3) == "dxf";
     150}
     151
     152inline bool isSTLFile(const std::string& fileName)
     153{
     154  return fileName.substr(fileName.size() - 3) == "stl";
     155}
     156
     157inline bool isOBJFile(const std::string& fileName)
     158{
     159  return fileName.substr(fileName.size() - 3) == "obj";
     160}
     161
     162inline bool is3DSFile(const std::string& fileName)
     163{
     164  return fileName.substr(fileName.size() - 3) == "3ds";
     165}
     166
     167inline bool isDXFType(const std::string& type)
     168{
     169  return type == "DXF";
     170}
     171
     172inline bool isSTLType(const std::string& type)
     173{
     174  return type == "STL";
     175}
     176
     177inline bool isOBJType(const std::string& type)
     178{
     179  return type == "OBJ";
     180}
     181
     182inline bool is3DSType(const std::string& type)
     183{
     184  return type == "3DS";
     185}
     186
     187inline bool isCADType(const std::string& type)
     188{
     189  return isDXFType(type) || isSTLType(type) || isOBJType(type) || is3DSType(type);
     190}
     191
     192inline bool isSimpleCADType(const std::string& type)
     193{
     194  return isDXFType(type) || isSTLType(type);
     195}
     196
     197inline bool isAdvancedCADType(const std::string& type)
     198{
     199  return isOBJType(type) || is3DSType(type);
     200}
     201
    164202inline const char* boolToString(bool b)
    165203{
     
    167205}
    168206
    169 
    170207#endif //ANIMATIONUTIL_H
  • OMEdit/OMEditLIB/Animation/ViewerWidget.cpp

    r740a8c7 rbf804e32  
    404404    if (mpSelectedVisualizer->isShape()) {
    405405      ShapeObject* shape = mpSelectedVisualizer->asShape();
    406       if (shape->_type.compare("dxf") == 0 or shape->_type.compare("stl") == 0) {
    407         QString msg = tr("Texture feature is not applicable for %1 files.").arg(shape->_type.compare("dxf") == 0 ? "DXF" : "STL");
     406      if (isSimpleCADType(shape->_type)) {
     407        QString msg = tr("Texture feature is not applicable for %1 files.").arg(shape->_type.c_str());
    408408        MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica, msg,
    409409                                                              Helper::scriptingKind, Helper::notificationLevel));
     
    427427    if (mpSelectedVisualizer->isShape()) {
    428428      ShapeObject* shape = mpSelectedVisualizer->asShape();
    429       if (shape->_type.compare("dxf") == 0 or shape->_type.compare("stl") == 0) {
    430         QString msg = tr("Texture feature is not applicable for %1 files.").arg(shape->_type.compare("dxf") == 0 ? "DXF" : "STL");
     429      if (isSimpleCADType(shape->_type)) {
     430        QString msg = tr("Texture feature is not applicable for %1 files.").arg(shape->_type.c_str());
    431431        MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica, msg,
    432432                                                              Helper::scriptingKind, Helper::notificationLevel));
     
    455455    if (mpSelectedVisualizer->isShape()) {
    456456      ShapeObject* shape = mpSelectedVisualizer->asShape();
    457       if (shape->_type.compare("dxf") == 0 or shape->_type.compare("stl") == 0) {
    458         QString msg = tr("Texture feature is not applicable for %1 files.").arg(shape->_type.compare("dxf") == 0 ? "DXF" : "STL");
     457      if (isSimpleCADType(shape->_type)) {
     458        QString msg = tr("Texture feature is not applicable for %1 files.").arg(shape->_type.c_str());
    459459        MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica, msg,
    460460                                                              Helper::scriptingKind, Helper::notificationLevel));
  • OMEdit/OMEditLIB/Animation/ViewerWidget.h

    r740a8c7 rbf804e32  
    4848
    4949#include "AbstractAnimationWindow.h"
     50#include "AnimationUtil.h"
    5051#include "Util/Helper.h"
    5152
  • OMEdit/OMEditLIB/Animation/Visualization.cpp

    r740a8c7 rbf804e32  
    4242#include <osg/GL> // for having direct access to glClear()
    4343
     44#include <osg/Array>
    4445#include <osg/Drawable>
    4546#include <osg/Shape>
     
    317318    shape._type = std::string(expNode->value());
    318319
    319     if (isCADType(shape._type))
     320    if (isCADFile(shape._type))
    320321    {
    321322      shape._fileName = extractCADFilename(shape._type);
     
    328329      }
    329330
    330       if (dxfFileType(shape._fileName)) {
    331         shape._type = "dxf";
    332       } else if (stlFileType(shape._fileName)) {
    333         shape._type = "stl";
     331      if (isDXFFile(shape._fileName)) {
     332        shape._type = "DXF";
     333      } else if (isSTLFile(shape._fileName)) {
     334        shape._type = "STL";
     335      } else if (isOBJFile(shape._fileName)) {
     336        shape._type = "OBJ";
     337      } else if (is3DSFile(shape._fileName)) {
     338        shape._type = "3DS";
    334339      }
    335340    }
     
    791796        // Store the radius of relevant shapes
    792797        for (ShapeObject& shape : relevantShapes) {
    793           // Consider OpenSceneGraph shape drawables only
    794           if (shape._type.compare("dxf") == 0 || shape._type.compare("stl") == 0) {
     798          // Consider OSG shape drawables only
     799          if (isCADType(shape._type)) {
    795800            continue;
    796801          }
     
    12821287    transf->setName(shape._id);
    12831288
    1284     if (shape._type.compare("stl") == 0)
    1285     { //cad node
     1289    if (isAdvancedCADType(shape._type))
     1290    { //advanced cad node
     1291      //std::cout<<"It's an advanced cad and the filename is "<<shape._fileName<<std::endl;
     1292      osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(shape._fileName);
     1293      if (node.valid())
     1294      {
     1295        osg::ref_ptr<CADFile> cad = new CADFile(node.get());
     1296
     1297        transf->addChild(cad.get());
     1298      }
     1299    }
     1300    else if (isSTLType(shape._type))
     1301    { //stl node
    12861302      //std::cout<<"It's a stl and the filename is "<<shape._fileName<<std::endl;
    12871303      // Disable mesh optimization because it is too expensive (see OSG commit a082b57)
     
    12951311      }
    12961312    }
    1297     else if (shape._type.compare("dxf") == 0)
     1313    else if (isDXFType(shape._type))
    12981314    { //geode with dxf drawable
    12991315      //std::cout<<"It's a dxf and the filename is "<<shape._fileName<<std::endl;
     
    14131429     {
    14141430      ShapeObject* shape = _visualizer->asShape();
    1415       if (shape->_type.compare("dxf") == 0 or shape->_type.compare("stl") == 0)
     1431      if (isCADType(shape->_type))
    14161432      {
    14171433        //it's a cad file so we have to rescale the underlying geometry vertices
     
    15461562    osg::ref_ptr<osg::StateSet> stateSet = nullptr;
    15471563    bool geometryColors = false;
     1564    bool is3DSShape = false;
    15481565
    15491566    if (_visualizer->isShape()) {
    15501567      ShapeObject* shape = _visualizer->asShape();
    1551       if (shape->_type.compare("dxf") == 0 or shape->_type.compare("stl") == 0) {
     1568      if (isCADType(shape->_type)) {
    15521569        osg::ref_ptr<osg::Transform> transformNode = shape->getTransformNode();
    15531570        if (transformNode.valid() && transformNode->getNumChildren() > 0) {
     
    15561573            stateSet = cad->getOrCreateStateSet();
    15571574            geometryColors = !shape->getVisualProperties()->getColor().custom();
     1575            is3DSShape = is3DSType(shape->_type);
    15581576          }
    15591577        }
     
    15771595      changeTransparencyOfMaterial(ss, transparency);
    15781596      if (geometryColors) {
    1579         changeTransparencyOfGeometry(node, transparency);
    1580       }
    1581     }
     1597        if (is3DSShape) {
     1598          changeTransparencyOfGeometry<osg::Vec4ubArray, 255>(node, transparency);
     1599        } else {
     1600          changeTransparencyOfGeometry<osg::Vec4Array, 1>    (node, transparency);
     1601        }
     1602      }
     1603    }
     1604
    15821605    if (changeTexture) {
    15831606      //set texture
     
    17001723 * changes transparency of a geode's geometry
    17011724 */
     1725template<typename Vec4Array, unsigned int scale>
    17021726void UpdateVisitor::changeTransparencyOfGeometry(osg::Geode& geode, const float transparency)
    17031727{
    1704   osg::Vec4::value_type opacity = 1.0 - transparency;
     1728  using Vec4 = typename Vec4Array::ElementDataType;
     1729  using type = typename Vec4::value_type;
     1730  type opacity = (1.0 - transparency) * scale;
    17051731  unsigned int num = geode.getNumDrawables();
    17061732  for (unsigned int i = 0; i < num; i++) {
     
    17091735      osg::Geometry* geometry = drawable->asGeometry();
    17101736      if (geometry) {
    1711         osg::Vec4Array* colors = dynamic_cast<osg::Vec4Array*>(geometry->getColorArray());
     1737        Vec4Array* colors = dynamic_cast<Vec4Array*>(geometry->getColorArray());
    17121738        if (colors) {
    1713           for (osg::Vec4& color : colors->asVector()) {
     1739          for (Vec4& color : colors->asVector()) {
    17141740            color.a() = opacity;
    17151741          }
     
    18621888
    18631889  osg::Matrix3 T0;
    1864   if (type == "stl" || type == "dxf")
     1890  if (isCADType(type))
    18651891  {
    18661892    T0 = osg::Matrix3(dirs._lDir[0], dirs._lDir[1], dirs._lDir[2],
  • OMEdit/OMEditLIB/Animation/Visualization.h

    r740a8c7 rbf804e32  
    100100  void changeColorOfMaterial(osg::StateSet* ss, const osg::Material::ColorMode mode, const QColor color, const float specular);
    101101  void changeTransparencyOfMaterial(osg::StateSet* ss, const float transparency);
     102  template<typename Vec4Array, unsigned int scale>
    102103  void changeTransparencyOfGeometry(osg::Geode& geode, const float transparency);
    103104public:
Note: See TracChangeset for help on using the changeset viewer.