From cd4da51e6040614640a80333fabdc8d9cedbb451 Mon Sep 17 00:00:00 2001 From: edi271 Date: Wed, 26 Jan 2022 15:50:38 +0100 Subject: [PATCH] [TD] Insert new tool Area Annotation --- src/Mod/TechDraw/Gui/CommandExtensionPack.cpp | 168 ++++++++++++-- src/Mod/TechDraw/Gui/Resources/TechDraw.qrc | 1 + .../TechDraw_ExtensionAreaAnnotation.svg | 217 ++++++++++++++++++ src/Mod/TechDraw/Gui/Workbench.cpp | 4 + 4 files changed, 374 insertions(+), 16 deletions(-) create mode 100644 src/Mod/TechDraw/Gui/Resources/icons/TechDraw_ExtensionAreaAnnotation.svg diff --git a/src/Mod/TechDraw/Gui/CommandExtensionPack.cpp b/src/Mod/TechDraw/Gui/CommandExtensionPack.cpp index 429ed30ae9..18f75d3665 100644 --- a/src/Mod/TechDraw/Gui/CommandExtensionPack.cpp +++ b/src/Mod/TechDraw/Gui/CommandExtensionPack.cpp @@ -30,12 +30,10 @@ # include #endif //#ifndef _PreComp_ -#include - # include # include -#include -#include +# include +# include # include # include # include @@ -59,22 +57,17 @@ # include # include # include +# include +# include -#include - - -#include "DrawGuiUtil.h" -#include "MDIViewPage.h" -#include "ViewProviderPage.h" -#include "TaskLinkDim.h" +# include "ViewProviderBalloon.h" +# include "QGVPage.h" +# include "DrawGuiUtil.h" +# include "ViewProviderPage.h" +# include "TaskLinkDim.h" #include "TaskSelectLineAttributes.h" -///////////////////////////// -#include // needed -#include -///////////////////////////// - using namespace TechDrawGui; using namespace TechDraw; using namespace std; @@ -100,6 +93,7 @@ namespace TechDrawGui { std::vector& selection, TechDraw::DrawViewPart*& objFeat, std::string message); + std::string _createBalloon(Gui::Command* cmd, TechDraw::DrawViewPart* objFeat); //=========================================================================== // TechDraw_ExtensionHoleCircle @@ -1652,6 +1646,130 @@ bool CmdTechDrawExtendShortenLineGroup::isActive(void) return (havePage && haveView); } +//=========================================================================== +// TechDraw_ExtensionAreaAnnotation +//=========================================================================== + +DEF_STD_CMD_A(CmdTechDrawExtensionAreaAnnotation) + +CmdTechDrawExtensionAreaAnnotation::CmdTechDrawExtensionAreaAnnotation() + : Command("TechDraw_ExtensionAreaAnnotation") +{ + sAppModule = "TechDraw"; + sGroup = QT_TR_NOOP("TechDraw"); + sMenuText = QT_TR_NOOP("Calculate the area of selected faces"); + sToolTipText = QT_TR_NOOP("Select several faces\n\ + - click this tool"); + sWhatsThis = "TechDraw_ExtensionAreaAnnotation"; + sStatusTip = sToolTipText; + sPixmap = "TechDraw_ExtensionAreaAnnotation"; +} + +void CmdTechDrawExtensionAreaAnnotation::activated(int iMsg) +// calculate the area of selected faces, create output in a balloon +{ + Q_UNUSED(iMsg); + std::vector selection; + TechDraw::DrawViewPart* objFeat; + if (!_checkSel(this, selection, objFeat, "TechDraw calculate selected area")) + return; + double faceArea(0.0), totalArea(0.0), xCenter(0.0), yCenter(0.0); + int totalPoints(0); + const std::vector subNames = selection[0].getSubNames(); + if (!subNames.empty()) { + for (std::string name : subNames) { + int idx = TechDraw::DrawUtil::getIndexFromName(name); + std::vector faceEdges = objFeat->getFaceEdgesByIndex(idx); + // We filter arcs, circles etc. which are not allowed. + for (TechDraw::BaseGeomPtr geoPtr : faceEdges) + if (geoPtr->geomType != TechDraw::GENERIC) + throw Base::TypeError("CmdTechDrawAreaAnnotation - forbidden border element found\n"); + // We create a list of all points along the boundary of the face. + // The edges form a closed polygon, but their start- and endpoints may be interchanged. + std::vector facePoints; + TechDraw::GenericPtr firstEdge = + std::static_pointer_cast(faceEdges[0]); + facePoints.push_back(firstEdge->points.at(0)); + facePoints.push_back(firstEdge->points.at(1)); + for (long unsigned int n = 1; n < faceEdges.size() - 1; n++) + { + TechDraw::GenericPtr nextEdge = + std::static_pointer_cast(faceEdges[n]); + if ((nextEdge->points.at(0)-facePoints.back()).Length() < 0.01) + facePoints.push_back(nextEdge->points.at(1)); + else + facePoints.push_back(nextEdge->points.at(0)); + } + facePoints.push_back(facePoints.front()); + // We calculate the area, using triangles. Each having one point at (0/0). + faceArea = 0.0; + xCenter = xCenter + facePoints[0].x; + yCenter = yCenter + facePoints[0].y; + for (long unsigned int n = 0; n < facePoints.size() - 1; n++) + { + faceArea = faceArea + facePoints[n].x * facePoints[n+1].y - + facePoints[n].y * facePoints[n+1].x; + xCenter = xCenter + facePoints[n+1].x; + yCenter = yCenter + facePoints[n+1].y; + } + faceArea = abs(faceArea)/2.0; + totalArea = totalArea + faceArea; + totalPoints = totalPoints + facePoints.size(); + } + } + // if area calculation was successfull, wie start the command + Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Calculate Face Area")); + // at first we create the balloon + std::string balloonName = _createBalloon(this, objFeat); + TechDraw::DrawViewBalloon* balloon = nullptr; + balloon = dynamic_cast(this->getDocument()->getObject(balloonName.c_str())); + if (!balloon) + throw Base::TypeError("CmdTechDrawNewBalloon - balloon not found\n"); + // the balloon has been created successfull + // we calculate needed variables + double scale = objFeat->getScale(); + totalArea = totalArea*10*10; // Todo: get factor cm->mm if cm set + std::stringstream balloonText; + balloonText << " " << totalArea << " cm2 "; + xCenter = (xCenter/totalPoints)/scale; + yCenter = (yCenter/totalPoints)/scale; + // we set the attributes in the data tab's fields + balloon->SourceView.setValue(objFeat); + balloon->BubbleShape.setValue("Rectangle"); + balloon->EndType.setValue("None"); + balloon->KinkLength.setValue(0.0); + balloon->X.setValue(xCenter); + balloon->Y.setValue(-yCenter); + balloon->OriginX.setValue(xCenter); + balloon->OriginY.setValue(-yCenter); + balloon->ShapeScale.setValue(0.75); + balloon->ScaleType.setValue("Page"); + balloon->Text.setValue(balloonText.str()); + // we look for the ballons's view provider + TechDraw::DrawPage* page = objFeat->findParentPage(); + Gui::Document* guiDoc = Gui::Application::Instance->getDocument(page->getDocument()); + auto viewProvider = static_cast(guiDoc->getViewProvider(balloon)); + if (viewProvider) + { + // view provider successfull found, + // we set the attributes in the view tab's fields + viewProvider->Fontsize.setValue(2.0); + viewProvider->LineWidth.setValue(0.75); + viewProvider->LineVisible.setValue(false); + viewProvider->Color.setValue(App::Color(1.0f, 0.0f, 0.0f)); + } + Gui::Command::commitCommand(); + objFeat->touch(true); + Gui::Command::updateActive(); +} + +bool CmdTechDrawExtensionAreaAnnotation::isActive(void) +{ + bool havePage = DrawGuiUtil::needPage(this); + bool haveView = DrawGuiUtil::needView(this); + return (havePage && haveView); +} + //=========================================================================== // internal helper routines //=========================================================================== @@ -1663,6 +1781,23 @@ namespace TechDrawGui { return attributes; } + std::string _createBalloon(Gui::Command* cmd, TechDraw::DrawViewPart* objFeat) + // create a new balloon, return it's name as string + { + TechDraw::DrawPage* page = objFeat->findParentPage(); + page->balloonParent = objFeat; + Gui::Document* guiDoc = Gui::Application::Instance->getDocument(page->getDocument()); + ViewProviderPage* pageVP = dynamic_cast(guiDoc->getViewProvider(page)); + QGVPage* viewPage = pageVP->getGraphicsView(); + std::string featName = viewPage->getDrawPage()->getDocument()->getUniqueObjectName("Balloon"); + std::string pageName = viewPage->getDrawPage()->getNameInDocument(); + cmd->doCommand(cmd->Doc, "App.activeDocument().addObject('TechDraw::DrawViewBalloon','%s')", + featName.c_str()); + cmd->doCommand(cmd->Doc, "App.activeDocument().%s.addView(App.activeDocument().%s)", + pageName.c_str(), featName.c_str()); + return featName; + } + bool _checkSel(Gui::Command* cmd, std::vector& selection, TechDraw::DrawViewPart*& objFeat, @@ -1964,4 +2099,5 @@ void CreateTechDrawCommandsExtensions(void) rcCmdMgr.addCommand(new CmdTechDrawExtensionThreadBoltSide()); rcCmdMgr.addCommand(new CmdTechDrawExtensionThreadHoleBottom()); rcCmdMgr.addCommand(new CmdTechDrawExtensionThreadBoltBottom()); + rcCmdMgr.addCommand(new CmdTechDrawExtensionAreaAnnotation()); } diff --git a/src/Mod/TechDraw/Gui/Resources/TechDraw.qrc b/src/Mod/TechDraw/Gui/Resources/TechDraw.qrc index 58a0df1051..b4726f09cf 100644 --- a/src/Mod/TechDraw/Gui/Resources/TechDraw.qrc +++ b/src/Mod/TechDraw/Gui/Resources/TechDraw.qrc @@ -63,6 +63,7 @@ icons/TechDraw_ExtensionCreateLengthArc.svg icons/TechDraw_ExtensionIncreaseDecimal.svg icons/TechDraw_ExtensionDecreaseDecimal.svg + icons/TechDraw_ExtensionAreaAnnotation.svg icons/TechDraw_LengthDimension.svg icons/TechDraw_RadiusDimension.svg icons/TechDraw_Balloon.svg diff --git a/src/Mod/TechDraw/Gui/Resources/icons/TechDraw_ExtensionAreaAnnotation.svg b/src/Mod/TechDraw/Gui/Resources/icons/TechDraw_ExtensionAreaAnnotation.svg new file mode 100644 index 0000000000..f5332ae367 --- /dev/null +++ b/src/Mod/TechDraw/Gui/Resources/icons/TechDraw_ExtensionAreaAnnotation.svg @@ -0,0 +1,217 @@ + + + + + + + + + + image/svg+xml + + + + + + + + A 2 + + + + + + + + + + + + + diff --git a/src/Mod/TechDraw/Gui/Workbench.cpp b/src/Mod/TechDraw/Gui/Workbench.cpp index 438a0a4885..bc1ae11ff6 100644 --- a/src/Mod/TechDraw/Gui/Workbench.cpp +++ b/src/Mod/TechDraw/Gui/Workbench.cpp @@ -104,6 +104,8 @@ Gui::MenuItem* Workbench::setupMenuBar() const *toolattrib << "TechDraw_ExtensionCascadeHorizDimension"; *toolattrib << "TechDraw_ExtensionCascadeVertDimension"; *toolattrib << "TechDraw_ExtensionCascadeObliqueDimension"; + *toolattrib << "Separator"; + *toolattrib << "TechDraw_ExtensionAreaAnnotation"; // extension: centerlines and threading Gui::MenuItem* toolcenter = new Gui::MenuItem; @@ -275,6 +277,7 @@ Gui::ToolBarItem* Workbench::setupToolBars() const // *extattribs << "TechDraw_ExtensionCascadeHorizDimension"; // *extattribs << "TechDraw_ExtensionCascadeVertDimension"; // *extattribs << "TechDraw_ExtensionCascadeObliqueDimension"; + *extattribs << "TechDraw_ExtensionAreaAnnotation"; Gui::ToolBarItem *extcenter = new Gui::ToolBarItem(root); extcenter->setCommand("TechDraw Centerlines"); @@ -410,6 +413,7 @@ Gui::ToolBarItem* Workbench::setupCommandBars() const // *extattribs << "TechDraw_ExtensionCascadeHorizDimension"; // *extattribs << "TechDraw_ExtensionCascadeVertDimension"; // *extattribs << "TechDraw_ExtensionCascadeObliqueDimension"; + *extattribs << "TechDraw_ExtensionAreaAnnotation"; Gui::ToolBarItem *extcenter = new Gui::ToolBarItem(root); extcenter->setCommand("TechDraw Centerlines");