1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_WidgetSketchCreator.cpp
4 // Created: 08 June 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "PartSet_WidgetSketchCreator.h"
8 #include "PartSet_Module.h"
10 #include <XGUI_ModuleConnector.h>
11 #include <XGUI_Workshop.h>
12 #include <XGUI_Displayer.h>
13 #include <XGUI_SelectionMgr.h>
14 #include <XGUI_OperationMgr.h>
16 #include <GeomAPI_Face.h>
18 #include <ModelAPI_Session.h>
19 #include <ModelAPI_ResultBody.h>
20 #include <ModelAPI_AttributeSelection.h>
21 #include <ModelAPI_AttributeSelectionList.h>
23 #include <SketchPlugin_SketchEntity.h>
24 #include <FeaturesPlugin_CompositeBoolean.h>
26 #include <ModuleBase_Tools.h>
27 #include <ModuleBase_Operation.h>
28 #include <ModuleBase_IPropertyPanel.h>
29 #include <Config_WidgetAPI.h>
33 #include <QFormLayout>
34 #include <QMessageBox>
36 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
37 PartSet_Module* theModule,
38 const Config_WidgetAPI* theData,
39 const std::string& theParentId)
40 : ModuleBase_ModelWidget(theParent, theData, theParentId), myModule(theModule)
42 QFormLayout* aLayout = new QFormLayout(this);
43 ModuleBase_Tools::adjustMargins(aLayout);
45 QString aLabelText = QString::fromStdString(theData->widgetLabel());
46 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
47 myLabel = new QLabel(aLabelText, this);
48 if (!aLabelIcon.isEmpty())
49 myLabel->setPixmap(QPixmap(aLabelIcon));
52 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
53 myTextLine = new QLineEdit(this);
54 myTextLine->setReadOnly(true);
55 myTextLine->setToolTip(aToolTip);
56 myTextLine->installEventFilter(this);
58 aLayout->addRow(myLabel, myTextLine);
61 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
65 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
67 QList<QWidget*> aControls;
68 aControls.append(myTextLine);
72 bool PartSet_WidgetSketchCreator::restoreValue()
74 CompositeFeaturePtr aCompFeature =
75 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
76 if (aCompFeature->numberOfSubs() > 0) {
77 FeaturePtr aSubFeature = aCompFeature->subFeature(0);
78 myTextLine->setText(QString::fromStdString(aSubFeature->data()->name()));
83 bool PartSet_WidgetSketchCreator::storeValueCustom() const
88 void PartSet_WidgetSketchCreator::activateCustom()
90 CompositeFeaturePtr aCompFeature =
91 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
92 if (aCompFeature->numberOfSubs() == 0)
93 connect(myModule, SIGNAL(operationLaunched()), SLOT(onStarted()));
96 void PartSet_WidgetSketchCreator::onStarted()
98 disconnect(myModule, SIGNAL(operationLaunched()), this, SLOT(onStarted()));
100 // Check that model already has bodies
101 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
102 XGUI_Workshop* aWorkshop = aConnector->workshop();
103 XGUI_Displayer* aDisp = aWorkshop->displayer();
104 QObjectPtrList aObjList = aDisp->displayedObjects();
105 bool aHasBody = false;
107 foreach(ObjectPtr aObj, aObjList) {
108 aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
109 if (aBody.get() != NULL) {
116 // Launch Sketch operation
117 CompositeFeaturePtr aCompFeature =
118 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
119 FeaturePtr aSketch = aCompFeature->addFeature("Sketch");
121 ModuleBase_Operation* anOperation = myModule->createOperation("Sketch");
122 anOperation->setFeature(aSketch);
123 myModule->sendOperation(anOperation);
124 //connect(anOperation, SIGNAL(aborted()), aWorkshop->operationMgr(), SLOT(abortAllOperations()));
126 // Break current operation
127 QMessageBox::warning(this, tr("Extrusion Cut"),
128 tr("There are no bodies found. Operation aborted."), QMessageBox::Ok);
129 ModuleBase_Operation* aOp = myModule->workshop()->currentOperation();
134 bool PartSet_WidgetSketchCreator::focusTo()
136 CompositeFeaturePtr aCompFeature =
137 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
138 if (aCompFeature->numberOfSubs() == 0)
139 return ModuleBase_ModelWidget::focusTo();
141 connect(myModule, SIGNAL(operationResumed(ModuleBase_Operation*)), SLOT(onResumed(ModuleBase_Operation*)));
142 SessionPtr aMgr = ModelAPI_Session::get();
143 bool aIsOp = aMgr->isOperation();
144 // Open transaction if it was closed before
146 aMgr->startOperation();
152 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
154 CompositeFeaturePtr aCompFeature =
155 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
156 CompositeFeaturePtr aSketchFeature =
157 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
158 if (aSketchFeature->numberOfSubs() == 0) {
160 SessionPtr aMgr = ModelAPI_Session::get();
161 bool aIsOp = aMgr->isOperation();
164 aMgr->abortOperation();
167 DataPtr aData = aSketchFeature->data();
168 AttributeSelectionPtr aSelAttr =
169 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
170 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
171 if (aSelAttr.get()) {
172 ResultPtr aRes = aSelAttr->context();
173 GeomShapePtr aShape = aSelAttr->value();
175 AttributeSelectionListPtr aSelList =
176 aCompFeature->data()->selectionList(FeaturesPlugin_CompositeBoolean::BOOLEAN_OBJECTS_ID());
177 aSelList->append(aRes, GeomShapePtr());
178 updateObject(aCompFeature);