|
| 1 | +/*This file is part of the FEBio Studio source code and is licensed under the MIT license |
| 2 | +listed below. |
| 3 | +
|
| 4 | +See Copyright-FEBio-Studio.txt for details. |
| 5 | +
|
| 6 | +Copyright (c) 2021 University of Utah, The Trustees of Columbia University in |
| 7 | +the City of New York, and others. |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in all |
| 17 | +copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +SOFTWARE.*/ |
| 26 | +#include "stdafx.h" |
| 27 | +#include "ExtrudeMapTool.h" |
| 28 | +#include <MeshTools/FEExtrudeFaces.h> |
| 29 | +#include "ModelDocument.h" |
| 30 | +#include "MainWindow.h" |
| 31 | +#include <GeomLib/GMeshObject.h> |
| 32 | +#include <QFile> |
| 33 | +#include <MeshLib/FENodeData.h> |
| 34 | +#include "DlgImportData.h" |
| 35 | + |
| 36 | +CExtrudeMapTool::CExtrudeMapTool(CMainWindow* wnd) : CBasicTool(wnd, "Extrude map", HAS_APPLY_BUTTON) |
| 37 | +{ |
| 38 | + m_D = 1; |
| 39 | + m_nsegs = 1; |
| 40 | + m_useLocalNormal = true; |
| 41 | + m_meshBias = 1; |
| 42 | + m_symmetricBias = false; |
| 43 | + addResourceProperty(&m_filename, "map file:"); |
| 44 | + addDoubleProperty(&m_D, "distance"); |
| 45 | + addIntProperty(&m_nsegs, "segments"); |
| 46 | + addBoolProperty(&m_useLocalNormal, "use local normal"); |
| 47 | + addDoubleProperty(&m_meshBias, "mesh bias"); |
| 48 | + addBoolProperty(&m_symmetricBias, "symmetric mesh bias"); |
| 49 | +} |
| 50 | + |
| 51 | +bool CExtrudeMapTool::OnApply() |
| 52 | +{ |
| 53 | + GMeshObject* po = dynamic_cast<GMeshObject*>(GetActiveObject()); |
| 54 | + if (po == nullptr) |
| 55 | + { |
| 56 | + SetErrorString("You need to select an Editable Mesh object."); |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + FSMesh* pm = po->GetFEMesh(); |
| 61 | + if (pm == nullptr) |
| 62 | + { |
| 63 | + SetErrorString("The selected object has no mesh."); |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + int n = pm->CountSelectedFaces(); |
| 68 | + if (n == 0) |
| 69 | + { |
| 70 | + SetErrorString("No faces are selected. Cannot apply tool."); |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + vector<int> node; |
| 75 | + vector<double> data[3]; |
| 76 | + |
| 77 | + QFile mapFile(m_filename); |
| 78 | + if (!mapFile.open(QIODeviceBase::Text | QIODeviceBase::ReadOnly)) |
| 79 | + { |
| 80 | + SetErrorString("mapFile.errorString()"); |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + QTextStream in(&mapFile); |
| 85 | + QString txt = in.readAll(); |
| 86 | + mapFile.close(); |
| 87 | + |
| 88 | + CDlgImportData dlg(txt, DataType::DOUBLE); |
| 89 | + if (!dlg.exec()) return true; |
| 90 | + |
| 91 | + QList<QStringList> values = dlg.GetValues(); |
| 92 | + for (QStringList& fields : values) |
| 93 | + { |
| 94 | + int n = fields.count(); |
| 95 | + if ((n != 2) && (n != 4)) |
| 96 | + { |
| 97 | + SetErrorString("Invalid data in map file."); |
| 98 | + return false; |
| 99 | + } |
| 100 | + node.push_back(fields[0].toInt()); |
| 101 | + for (int i = 0; i < n - 1; ++i) |
| 102 | + { |
| 103 | + double di = fields.at(i + 1).toDouble(); |
| 104 | + data[i].push_back(di); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + int fields = 1; |
| 109 | + if ((data[0].size() == data[1].size()) && |
| 110 | + (data[0].size() == data[2].size())) fields = 3; |
| 111 | + |
| 112 | + // create a nodeset |
| 113 | + FSNodeSet nodeSet(po); |
| 114 | + for (int i = 0; i < node.size(); ++i) |
| 115 | + { |
| 116 | + int nid = node[i]; |
| 117 | + int m = pm->NodeIndexFromID(nid); |
| 118 | + nodeSet.add(m); |
| 119 | + } |
| 120 | + |
| 121 | + // create a data map |
| 122 | + FENodeData map(po); |
| 123 | + if (fields == 1) |
| 124 | + { |
| 125 | + map.Create(&nodeSet, 0, DATA_SCALAR); |
| 126 | + for (int i = 0; i < node.size(); ++i) |
| 127 | + { |
| 128 | + map.set(i, data[0][i]); |
| 129 | + } |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + map.Create(&nodeSet, 0, DATA_VEC3); |
| 134 | + for (int i = 0; i < node.size(); ++i) |
| 135 | + { |
| 136 | + map.set(i, vec3d(data[0][i], data[1][i], data[2][i])); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + FEExtrudeFaces mod; |
| 141 | + mod.SetExtrusionDistance(m_D); |
| 142 | + mod.SetSegments(m_nsegs); |
| 143 | + mod.SetUseNormalLocal(m_useLocalNormal); |
| 144 | + mod.SetMeshBiasFactor(m_meshBias); |
| 145 | + mod.SetSymmetricBias(m_symmetricBias); |
| 146 | + mod.SetNodalMap(&map); |
| 147 | + |
| 148 | + CModelDocument* doc = dynamic_cast<CModelDocument*>(GetDocument()); |
| 149 | + bool b = doc->ApplyFEModifier(mod, GetActiveObject()); |
| 150 | + if (!b) SetErrorString(QString::fromStdString(mod.GetErrorString())); |
| 151 | + updateUi(); |
| 152 | + |
| 153 | + return b; |
| 154 | +} |
0 commit comments