File size: 11,738 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | // SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
* *
* Copyright (c) 2023 Ondsel <development@ondsel.com> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD 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, or (at your option) any later version. *
* *
* FreeCAD is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
***************************************************************************/
#include <cmath>
#include <vector>
#include <App/Application.h>
#include <App/Document.h>
#include <App/DocumentObjectGroup.h>
#include <App/FeaturePythonPyImp.h>
#include <App/Link.h>
#include <App/PropertyPythonObject.h>
#include <App/Range.h>
#include <Base/Console.h>
#include <Base/Placement.h>
#include <Base/Rotation.h>
#include <Base/Tools.h>
#include <Base/Interpreter.h>
#include <QObject>
#include <Mod/Part/App/PartFeature.h>
#include <Mod/PartDesign/App/Body.h>
#include <Mod/Spreadsheet/App/Cell.h>
#include "AssemblyObject.h"
#include "AssemblyLink.h"
#include "BomObject.h"
#include "BomObjectPy.h"
using namespace Assembly;
// ================================ Assembly Object ============================
PROPERTY_SOURCE(Assembly::BomObject, Spreadsheet::Sheet)
BomObject::BomObject()
: Spreadsheet::Sheet()
{
ADD_PROPERTY_TYPE(
columnsNames,
("Index"),
"Bom",
(App::PropertyType)(App::Prop_None),
"List of the columns of the Bill of Materials."
);
ADD_PROPERTY_TYPE(
detailSubAssemblies,
(true),
"Bom",
(App::PropertyType)(App::Prop_None),
"Detail sub-assemblies components."
);
ADD_PROPERTY_TYPE(
detailParts,
(true),
"Bom",
(App::PropertyType)(App::Prop_None),
"Detail Parts sub-components."
);
ADD_PROPERTY_TYPE(
onlyParts,
(false),
"Bom",
(App::PropertyType)(App::Prop_None),
"Only Part containers will be added. Solids like PartDesign Bodies will be ignored."
);
}
BomObject::~BomObject() = default;
PyObject* BomObject::getPyObject()
{
if (PythonObject.is(Py::_None())) {
// ref counter is set to 1
PythonObject = Py::Object(new BomObjectPy(this), true);
}
return Py::new_reference_to(PythonObject);
}
App::DocumentObjectExecReturn* BomObject::execute()
{
generateBOM();
return Spreadsheet::Sheet::execute();
}
void BomObject::saveCustomColumnData()
{
// This function saves data that is not automatically generated.
dataElements.clear();
std::tuple<App::CellAddress, App::CellAddress> res = getUsedRange();
int maxRow = std::get<1>(res).row();
int nameColIndex = getColumnIndex("Name");
for (int row = 1; row <= maxRow; ++row) {
size_t col = 0;
// Here we do not iterate columnName : columnsNames.getValues() because they may have
// changed
for (size_t i = 0; i < columnsNames.getValues().size(); ++i) {
std::string columnName = getText(0, i);
if (columnName != "Index" && columnName != "Name" && columnName != "Quantity"
&& columnName != "File Name") {
// Base::Console().warning("row col %d %d\n", row, col);
// save custom data if any.
std::string text = getText(row, col);
if (text != "") {
std::string objName = getText(row, nameColIndex);
BomDataElement el(objName, columnName, text);
dataElements.push_back(el);
}
}
++col;
}
}
}
void BomObject::generateBOM()
{
saveCustomColumnData();
clearAll();
obj_list.clear();
size_t row = 0;
size_t col = 0;
// Populate headers
for (auto& columnName : columnsNames.getValues()) {
setCell(App::CellAddress(row, col), columnName.c_str());
++col;
}
++row;
auto* assembly = getAssembly();
if (assembly) {
addObjectChildrenToBom(assembly->getOutList(), row, "");
}
else {
addObjectChildrenToBom(getDocument()->getRootObjectsIgnoreLinks(), row, "");
}
}
void BomObject::addObjectChildrenToBom(
std::vector<App::DocumentObject*> objs,
size_t& row,
std::string index
)
{
int quantityColIndex = getColumnIndex("Quantity");
bool hasQuantityCol = hasQuantityColumn();
size_t siblingsInitialRow = row;
if (index != "") {
index = index + ".";
}
size_t sub_i = 1;
for (auto* child : objs) {
if (!child) {
continue;
}
if (auto* asmLink = freecad_cast<AssemblyLink*>(child)) {
child = asmLink->getLinkedAssembly();
if (!child) {
continue;
}
}
else if (child->isDerivedFrom<App::Link>()) {
child = static_cast<App::Link*>(child)->getLinkedObject();
if (!child) {
continue;
}
}
if (!child->isDerivedFrom<AssemblyObject>() && !child->isDerivedFrom<App::Part>()
&& !(child->isDerivedFrom<Part::Feature>() && !onlyParts.getValue())) {
continue;
}
if (hasQuantityCol && row != siblingsInitialRow) {
// Check if the object is not already in (case of links). And if so just increment.
// Note: an object can be used in several parts. In which case we do no want to blindly
// increment.
bool found = false;
for (size_t i = siblingsInitialRow; i <= row; ++i) {
size_t idInList = i - 1; // -1 for the header
if (idInList < obj_list.size() && child == obj_list[idInList]) {
int qty = std::stoi(getText(i, quantityColIndex)) + 1;
setCell(App::CellAddress(i, quantityColIndex), std::to_string(qty).c_str());
found = true;
break;
}
}
if (found) {
continue;
}
}
std::string sub_index = index + std::to_string(sub_i);
++sub_i;
addObjectToBom(child, row, sub_index);
++row;
if ((child->isDerivedFrom<AssemblyObject>() && detailSubAssemblies.getValue())
|| (!child->isDerivedFrom<AssemblyObject>() && child->isDerivedFrom<App::Part>()
&& detailParts.getValue())) {
addObjectChildrenToBom(child->getOutList(), row, sub_index);
}
}
}
void BomObject::addObjectToBom(App::DocumentObject* obj, size_t row, std::string index)
{
obj_list.push_back(obj);
size_t col = 0;
for (auto& columnName : columnsNames.getValues()) {
if (columnName == "Index") {
setCell(App::CellAddress(row, col), (std::string("'") + index).c_str());
}
else if (columnName == "Name") {
setCell(App::CellAddress(row, col), obj->Label.getValue());
}
else if (columnName == "File Name") {
setCell(App::CellAddress(row, col), obj->getDocument()->getFileName());
}
else if (columnName == "Quantity") {
setCell(App::CellAddress(row, col), std::to_string(1).c_str());
}
else if (columnName.starts_with(".")) {
// Column names that start with a dot are considered property names
// Extract the property name
std::string baseName = columnName.substr(1);
auto propertyValue = getBomPropertyValue(obj, baseName);
if (!propertyValue.empty()) {
setCell(App::CellAddress(row, col), propertyValue.c_str());
}
}
else {
// load custom data if any.
for (auto& el : dataElements) {
if (el.objName == obj->Label.getValue() && el.columnName == columnName) {
setCell(App::CellAddress(row, col), el.value.c_str());
break;
}
}
}
++col;
}
}
std::string BomObject::getBomPropertyValue(App::DocumentObject* obj, const std::string& baseName)
{
App::Property* prop = obj->getPropertyByName(baseName.c_str());
if (!prop) {
Base::Console().warning("Property not found: %s\n", baseName.c_str());
return QObject::tr("N/A").toStdString();
}
// Only support a subset of property types for BOM
if (auto propStr = freecad_cast<App::PropertyString*>(prop)) {
return propStr->getValue();
}
if (auto propQuantity = freecad_cast<App::PropertyQuantity*>(prop)) {
return propQuantity->getQuantityValue().getUserString();
}
if (auto propEnum = freecad_cast<App::PropertyEnumeration*>(prop)) {
return propEnum->getValueAsString();
}
if (auto propFloat = freecad_cast<App::PropertyFloat*>(prop)) {
return std::to_string(propFloat->getValue());
}
if (auto propInt = freecad_cast<App::PropertyInteger*>(prop)) {
return std::to_string(propInt->getValue());
}
if (auto propBool = freecad_cast<App::PropertyBool*>(prop)) {
return propBool->getValue() ? "True" : "False";
}
Base::Console().warning("Property type not supported for: %s\n", prop->getName());
return QObject::tr("Not supported").toStdString();
}
AssemblyObject* BomObject::getAssembly() const
{
for (auto& obj : getInList()) {
if (obj->isDerivedFrom<AssemblyObject>()) {
return static_cast<AssemblyObject*>(obj);
}
}
return nullptr;
}
bool BomObject::hasQuantityColumn() const
{
for (auto& columnName : columnsNames.getValues()) {
if (columnName == "Quantity") {
return true;
}
}
return false;
}
std::string Assembly::BomObject::getText(size_t row, size_t col) const
{
const Spreadsheet::Cell* cell = getCell(App::CellAddress(row, col));
std::string cellName;
if (cell) {
cell->getStringContent(cellName);
// getStringContent is adding a ' before the string for whatever reason.
if (!cellName.empty() && cellName.front() == '\'') {
cellName.erase(0, 1); // Remove the first character if it's a '
}
}
return cellName;
}
int BomObject::getColumnIndex(std::string name) const
{
int col = 0;
for (auto& columnName : columnsNames.getValues()) {
if (columnName == name) {
return col;
}
++col;
}
return -1;
}
|