1
/***************************************************************************
2
* Copyright (c) 2023 Wanderer Fan <wandererfan@gmail.com> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
# include <BRepAdaptor_Curve.hxx>
26
# include <BRep_Tool.hxx>
27
# include <Precision.hxx>
28
# include <ShapeExtend_Explorer.hxx>
29
# include <TopExp_Explorer.hxx>
31
# include <TopTools_HSequenceOfShape.hxx>
33
# include <QMessageBox>
35
#include <QTreeWidgetItem>
38
#include <App/Application.h>
39
#include <App/Document.h>
40
#include <App/DocumentObject.h>
43
#include <Base/UnitsApi.h>
44
#include <Gui/Application.h>
45
#include <Gui/BitmapFactory.h>
46
#include <Gui/Command.h>
47
#include <Gui/Document.h>
48
#include <Gui/Utilities.h>
49
#include <Gui/ViewProvider.h>
50
#include <Gui/WaitCursor.h>
52
#include "ui_DlgScale.h"
56
FC_LOG_LEVEL_INIT("Part",true,true)
58
using namespace PartGui;
60
DlgScale::DlgScale(QWidget* parent, Qt::WindowFlags fl)
61
: QDialog(parent, fl), ui(new Ui_DlgScale)
66
ui->dsbUniformScale->setDecimals(Base::UnitsApi::getDecimals());
67
ui->dsbXScale->setDecimals(Base::UnitsApi::getDecimals());
68
ui->dsbYScale->setDecimals(Base::UnitsApi::getDecimals());
69
ui->dsbZScale->setDecimals(Base::UnitsApi::getDecimals());
72
// this will mark as selected all the items in treeWidget that are selected in the document
73
Gui::ItemViewSelection sel(ui->treeWidget);
74
sel.applyFrom(Gui::Selection().getObjectsOfType(Part::Feature::getClassTypeId()));
75
sel.applyFrom(Gui::Selection().getObjectsOfType(App::Link::getClassTypeId()));
76
sel.applyFrom(Gui::Selection().getObjectsOfType(App::Part::getClassTypeId()));
79
void DlgScale::setupConnections()
81
connect(ui->rbUniform, &QRadioButton::toggled,
82
this, &DlgScale::onUniformScaleToggled);
85
void DlgScale::changeEvent(QEvent *e)
87
if (e->type() == QEvent::LanguageChange) {
88
ui->retranslateUi(this);
90
QDialog::changeEvent(e);
93
void DlgScale::onUniformScaleToggled(bool state)
95
// Base::Console().Message("DS::onUniformScaleToggled()\n");
97
// this is uniform scaling, so hide the non-uniform input fields
98
ui->dsbUniformScale->setEnabled(true);
99
ui->dsbXScale->setEnabled(false);
100
ui->dsbYScale->setEnabled(false);
101
ui->dsbZScale->setEnabled(false);
103
// this is non-uniform scaling, so hide the uniform input fields
104
ui->dsbUniformScale->setEnabled(false);
105
ui->dsbXScale->setEnabled(true);
106
ui->dsbYScale->setEnabled(true);
107
ui->dsbZScale->setEnabled(true);
111
//! find all the scalable objects in the active document and load them into the
113
void DlgScale::findShapes()
115
// Base::Console().Message("DS::findShapes()\n");
116
App::Document* activeDoc = App::GetApplication().getActiveDocument();
119
Gui::Document* activeGui = Gui::Application::Instance->getDocument(activeDoc);
120
m_document = activeDoc->getName();
121
m_label = activeDoc->Label.getValue();
123
std::vector<App::DocumentObject*> objs = activeDoc->getObjectsOfType<App::DocumentObject>();
125
for (auto obj : objs) {
126
Part::TopoShape topoShape = Part::Feature::getTopoShape(obj);
127
if (topoShape.isNull()) {
130
TopoDS_Shape shape = topoShape.getShape();
131
if (shape.IsNull()) continue;
132
if (canScale(shape)) {
133
QTreeWidgetItem* item = new QTreeWidgetItem(ui->treeWidget);
134
item->setText(0, QString::fromUtf8(obj->Label.getValue()));
135
item->setData(0, Qt::UserRole, QString::fromLatin1(obj->getNameInDocument()));
136
Gui::ViewProvider* vp = activeGui->getViewProvider(obj);
138
item->setIcon(0, vp->getIcon());
143
//! return true if shape can be scaled.
144
bool DlgScale::canScale(const TopoDS_Shape& shape) const
146
if (shape.IsNull()) {
149
// if the shape is a solid or a compound containing shapes, then we can scale it
150
TopAbs_ShapeEnum type = shape.ShapeType();
152
if (type == TopAbs_VERTEX) {
156
if (type == TopAbs_COMPOUND ||
157
type == TopAbs_COMPSOLID) {
159
xp.Init(shape, TopAbs_EDGE);
160
for ( ; xp.More() ; xp.Next()) {
161
// there is at least 1 edge inside the compound, so as long as it isn't null,
162
// we can scale this shape. We can stop looking as soon as we find a non-null
164
if (!xp.Current().IsNull()) {
165
// found a non-null edge
169
// did not find a non-null shape
172
// not a Vertex, Compound or CompSolid, must be one of Edge, Wire, Face, Shell or
173
// Solid, all of which we can scale.
180
void DlgScale::accept()
182
// Base::Console().Message("DS::accept()\n");
186
} catch (Base::AbortException&){
187
Base::Console().Message("DS::accept - apply failed!\n");
191
// create a FeatureScale for each scalable object
192
void DlgScale::apply()
194
// Base::Console().Message("DS::apply()\n");
197
QMessageBox::critical(this, windowTitle(),
198
tr("No scalable shapes selected"));
203
App::Document* activeDoc = App::GetApplication().getDocument(m_document.c_str());
205
QMessageBox::critical(this, windowTitle(),
206
tr("The document '%1' doesn't exist.").arg(QString::fromUtf8(m_label.c_str())));
209
activeDoc->openTransaction("Scale");
211
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
212
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/Part");
213
bool addBaseName = hGrp->GetBool("AddBaseObjectName", false);
215
std::vector<App::DocumentObject*> objects = this->getShapesToScale();
216
for (App::DocumentObject* sourceObj: objects) {
219
if (Part::Feature::getTopoShape(sourceObj).isNull()){
220
FC_ERR("Object " << sourceObj->getFullName()
221
<< " is not Part object (has no OCC shape). Can't scale it.");
226
name = sourceObj->getDocument()->getUniqueObjectName("Scale").c_str();
229
//QString baseName = QString::fromLatin1("Scale_%1").arg(sourceObjectName);
230
//label = QString::fromLatin1("%1_Scale").arg((*it)->text(0));
233
FCMD_OBJ_DOC_CMD(sourceObj,"addObject('Part::Scale','" << name << "')");
234
auto newObj = sourceObj->getDocument()->getObject(name.c_str());
236
this->writeParametersToFeature(*newObj, sourceObj);
238
Gui::Command::copyVisual(newObj, "ShapeAppearance", sourceObj);
239
Gui::Command::copyVisual(newObj, "LineColor", sourceObj);
240
Gui::Command::copyVisual(newObj, "PointColor", sourceObj);
242
FCMD_OBJ_HIDE(sourceObj);
245
activeDoc->commitTransaction();
246
Gui::Command::updateActive();
248
catch (Base::AbortException&){
251
catch (Base::Exception &err){
252
QMessageBox::critical(this,
254
tr("Creating Scale failed.\n%1")
255
.arg(QCoreApplication::translate("Exception", err.what())));
259
QMessageBox::critical(this, windowTitle(),
260
tr("Creating Scale failed.\n%1").arg(QString::fromUtf8("Unknown error")));
265
void DlgScale::reject()
270
//! retrieve the document objects associated with the selected items in the list
272
std::vector<App::DocumentObject*> DlgScale::getShapesToScale() const
274
// Base::Console().Message("DS::getShapesToScale()\n");
275
QList<QTreeWidgetItem *> items = ui->treeWidget->selectedItems();
276
App::Document* doc = App::GetApplication().getDocument(m_document.c_str());
278
throw Base::RuntimeError("Document lost");
280
std::vector<App::DocumentObject*> objects;
281
for (auto item : items) {
282
App::DocumentObject* obj = doc->getObject(item->data(0, Qt::UserRole).toString().toLatin1());
284
throw Base::RuntimeError("Object not found");
285
objects.push_back(obj);
290
//! return true if at least one item in the list widget corresponds to an
291
//! available document object in the document
292
bool DlgScale::validate()
294
QList<QTreeWidgetItem *> items = ui->treeWidget->selectedItems();
295
App::Document* doc = App::GetApplication().getDocument(m_document.c_str());
297
throw Base::RuntimeError("Document lost");
299
std::vector<App::DocumentObject*> objects;
300
for (auto item : items) {
301
App::DocumentObject* obj = doc->getObject(item->data(0, Qt::UserRole).toString().toLatin1());
303
throw Base::RuntimeError("Object not found");
304
objects.push_back(obj);
306
return !objects.empty();
309
//! update a FeatureScale with the parameters from the UI
310
void DlgScale::writeParametersToFeature(App::DocumentObject &feature, App::DocumentObject* base) const
312
// Base::Console().Message("DS::writeParametersToFeature()\n");
313
Gui::Command::doCommand(Gui::Command::Doc,"f = App.getDocument('%s').getObject('%s')", feature.getDocument()->getName(), feature.getNameInDocument());
319
Gui::Command::doCommand(Gui::Command::Doc,"f.Base = App.getDocument('%s').getObject('%s')", base->getDocument()->getName(), base->getNameInDocument());
321
Gui::Command::doCommand(Gui::Command::Doc,"f.Uniform = %s", ui->rbUniform->isChecked() ? "True" : "False");
322
Gui::Command::doCommand(Gui::Command::Doc,"f.UniformScale = %.7f", ui->dsbUniformScale->value());
323
Gui::Command::doCommand(Gui::Command::Doc,"f.XScale = %.7f", ui->dsbXScale->value());
324
Gui::Command::doCommand(Gui::Command::Doc,"f.YScale = %.7f", ui->dsbYScale->value());
325
Gui::Command::doCommand(Gui::Command::Doc,"f.ZScale = %.7f", ui->dsbZScale->value());
328
// ---------------------------------------
330
TaskScale::TaskScale()
332
widget = new DlgScale();
333
addTaskBox(Gui::BitmapFactory().pixmap("Part_Scale"), widget);
336
bool TaskScale::accept()
339
return (widget->result() == QDialog::Accepted);
342
bool TaskScale::reject()
348
void TaskScale::clicked(int id)
350
if (id == QDialogButtonBox::Apply) {
353
} catch (Base::AbortException&){
359
#include "moc_DlgScale.cpp"