Files
create/src/Mod/TechDraw/Gui/CommandCreateDims.cpp
2016-07-11 15:32:16 +02:00

918 lines
37 KiB
C++

/***************************************************************************
* Copyright (c) 2014 Luke Parry <l.parry@warwick.ac.uk> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QMessageBox>
# include <iostream>
# include <string>
# include <sstream>
# include <cstdlib>
# include <exception>
# include <boost/regex.hpp>
#endif //#ifndef _PreComp_
# include <App/DocumentObject.h>
# include <Gui/Action.h>
# include <Gui/Application.h>
# include <Gui/BitmapFactory.h>
# include <Gui/Command.h>
# include <Gui/Control.h>
# include <Gui/Document.h>
# include <Gui/Selection.h>
# include <Gui/MainWindow.h>
# include <Gui/FileDialog.h>
# include <Gui/ViewProvider.h>
# include <Mod/Part/App/PartFeature.h>
# include <Mod/TechDraw/App/DrawViewPart.h>
# include <Mod/TechDraw/App/DrawProjGroupItem.h>
# include <Mod/TechDraw/App/DrawProjGroup.h>
# include <Mod/TechDraw/App/DrawViewDimension.h>
# include <Mod/TechDraw/App/DrawPage.h>
# include <Mod/TechDraw/App/DrawUtil.h>
# include "MDIViewPage.h"
# include "ViewProviderPage.h"
using namespace TechDrawGui;
using namespace std;
//internal functions
bool _checkSelection(Gui::Command* cmd);
int _isValidSingleEdge(Gui::Command* cmd, bool trueDim=true);
bool _isValidVertexes(Gui::Command* cmd);
int _isValidEdgeToEdge(Gui::Command* cmd, bool trueDim=true);
bool _isTrueAllowed(TechDraw::DrawViewPart* objFeat, const std::vector<std::string> &SubNames);
enum EdgeType{
isInvalid,
isHorizontal,
isVertical,
isDiagonal,
isCircle,
isCurve,
isAngle
};
//===========================================================================
// TechDraw_NewDimension
//===========================================================================
DEF_STD_CMD_A(CmdTechDrawNewDimension);
CmdTechDrawNewDimension::CmdTechDrawNewDimension()
: Command("TechDraw_NewDimension")
{
sAppModule = "TechDraw";
sGroup = QT_TR_NOOP("TechDraw");
sMenuText = QT_TR_NOOP("Insert a dimension into the drawing");
sToolTipText = QT_TR_NOOP("Insert a new dimension");
sWhatsThis = "TechDraw_NewDimension";
sStatusTip = sToolTipText;
sPixmap = "Dimension";
}
void CmdTechDrawNewDimension::activated(int iMsg)
{
bool result = _checkSelection(this);
if (!result)
return;
std::vector<Gui::SelectionObject> selection = getSelection().getSelectionEx();
TechDraw::DrawViewPart * objFeat = dynamic_cast<TechDraw::DrawViewPart *>(selection[0].getObject());
const std::vector<std::string> &SubNames = selection[0].getSubNames();
TechDraw::DrawViewDimension *dim = 0;
std::string FeatName = getUniqueObjectName("Dimension");
std::string dimType;
bool centerLine = false;
std::vector<App::DocumentObject *> objs;
std::vector<std::string> subs;
//All Dimensions start as Projected
//bool trueDimAllowed = _isTrueAllowed(objFeat,SubNames);
//int edgeType = _isValidSingleEdge(this,trueDimAllowed);
int edgeType = _isValidSingleEdge(this,false);
if (edgeType) {
if (edgeType < isCircle) {
dimType = "Distance";
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
} else if (edgeType == isCircle) {
dimType = "Radius";
centerLine = true;
} else {
dimType = "Radius";
}
} else if (_isValidVertexes(this)) {
dimType = "Distance";
objs.push_back(objFeat);
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
subs.push_back(SubNames[1]);
} else if (_isValidEdgeToEdge(this)) {
int edgeCase = _isValidEdgeToEdge(this);
objs.push_back(objFeat);
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
subs.push_back(SubNames[1]);
switch (edgeCase) {
//TODO: This didn't have the breaks in it before 17 May, but didn't
// seem to crash either, so should check whether execution can even
// get here -Ian-
case isHorizontal:
dimType = "DistanceX";
break;
case isVertical:
dimType = "DistanceY";
break;
case isDiagonal:
dimType = "Distance";
break;
case isAngle:
dimType = "Angle";
default:
break;
}
} else {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect Selection"),
QObject::tr("Can't make a Dimension from this selection"));
return;
}
openCommand("Create Dimension");
doCommand(Doc,"App.activeDocument().addObject('TechDraw::DrawViewDimension','%s')",FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.Type = '%s'",FeatName.c_str()
,dimType.c_str());
if (centerLine) {
doCommand(Doc,"App.activeDocument().%s.CentreLines = True", FeatName.c_str());
} else {
doCommand(Doc,"App.activeDocument().%s.CentreLines = False", FeatName.c_str());
}
std::string contentStr;
if (dimType == "Angle") {
contentStr = "%value%\x00b0";
} else if (dimType == "Radius") {
contentStr = "r%value%";
}
doCommand(Doc,"App.activeDocument().%s.FormatSpec = '%s'",FeatName.c_str()
,contentStr.c_str());
dim = dynamic_cast<TechDraw::DrawViewDimension *>(getDocument()->getObject(FeatName.c_str()));
dim->References.setValues(objs, subs);
doCommand(Doc,"App.activeDocument().%s.ProjectionType = 'Projected'",FeatName.c_str());
std::vector<App::DocumentObject*> pages = getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId());
TechDraw::DrawPage *page = dynamic_cast<TechDraw::DrawPage *>(pages.front());
std::string PageName = page->getNameInDocument();
doCommand(Doc,"App.activeDocument().%s.addView(App.activeDocument().%s)",PageName.c_str(),FeatName.c_str());
commitCommand();
//Horrible hack to force Tree update
double x = objFeat->X.getValue();
objFeat->X.setValue(x);
}
bool CmdTechDrawNewDimension::isActive(void)
{
// TODO: Also ensure that there's a part selected?
return hasActiveDocument();
}
//===========================================================================
// TechDraw_NewRadiusDimension
//===========================================================================
DEF_STD_CMD_A(CmdTechDrawNewRadiusDimension);
CmdTechDrawNewRadiusDimension::CmdTechDrawNewRadiusDimension()
: Command("TechDraw_NewRadiusDimension")
{
sAppModule = "TechDraw";
sGroup = QT_TR_NOOP("TechDraw");
sMenuText = QT_TR_NOOP("Insert a new radius dimension into the drawing");
sToolTipText = QT_TR_NOOP("Insert a new radius dimension feature for the selected view");
sWhatsThis = "TechDraw_NewRadiusDimension";
sStatusTip = sToolTipText;
sPixmap = "Dimension_Radius";
}
void CmdTechDrawNewRadiusDimension::activated(int iMsg)
{
bool result = _checkSelection(this);
if (!result)
return;
std::vector<Gui::SelectionObject> selection = getSelection().getSelectionEx();
TechDraw::DrawViewPart * objFeat = dynamic_cast<TechDraw::DrawViewPart *>(selection[0].getObject());
const std::vector<std::string> &SubNames = selection[0].getSubNames();
TechDraw::DrawViewDimension *dim = 0;
std::string FeatName = getUniqueObjectName("Dimension");
bool centerLine = false;
std::vector<App::DocumentObject *> objs;
std::vector<std::string> subs;
//All Dimensions start as Projected
//bool trueDimAllowed = _isTrueAllowed(objFeat,SubNames);
//int edgeType = _isValidSingleEdge(this,trueDimAllowed);
int edgeType = _isValidSingleEdge(this,false); if (edgeType == isCircle) {
centerLine = true;
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
} else {
std::stringstream edgeMsg;
edgeMsg << "Can't make a radius Dimension from this selection (edge type: " << edgeType << ")";
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect Selection"),
QObject::tr(edgeMsg.str().c_str()));
return;
}
openCommand("Create Dimension");
doCommand(Doc,"App.activeDocument().addObject('TechDraw::DrawViewDimension','%s')",FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.Type = '%s'",FeatName.c_str()
,"Radius");
if (centerLine) {
doCommand(Doc,"App.activeDocument().%s.CentreLines = True", FeatName.c_str());
} else {
doCommand(Doc,"App.activeDocument().%s.CentreLines = False", FeatName.c_str());
}
doCommand(Doc, "App.activeDocument().%s.FormatSpec = 'r%%value%%'", FeatName.c_str());
dim = dynamic_cast<TechDraw::DrawViewDimension *>(getDocument()->getObject(FeatName.c_str()));
dim->References.setValues(objs, subs);
doCommand(Doc,"App.activeDocument().%s.ProjectionType = 'Projected'",FeatName.c_str());
std::vector<App::DocumentObject*> pages = getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId());
TechDraw::DrawPage *page = dynamic_cast<TechDraw::DrawPage *>(pages.front());
std::string PageName = page->getNameInDocument();
doCommand(Doc,"App.activeDocument().%s.addView(App.activeDocument().%s)",PageName.c_str(),FeatName.c_str());
commitCommand();
//Horrible hack to force Tree update
double x = objFeat->X.getValue();
objFeat->X.setValue(x);
}
bool CmdTechDrawNewRadiusDimension::isActive(void)
{
// TODO: Also ensure that there's a part selected?
return hasActiveDocument();
}
//===========================================================================
// TechDraw_NewDiameterDimension
//===========================================================================
DEF_STD_CMD_A(CmdTechDrawNewDiameterDimension);
CmdTechDrawNewDiameterDimension::CmdTechDrawNewDiameterDimension()
: Command("TechDraw_NewDiameterDimension")
{
sAppModule = "TechDraw";
sGroup = QT_TR_NOOP("TechDraw");
sMenuText = QT_TR_NOOP("Insert a new diameter dimension into the drawing");
sToolTipText = QT_TR_NOOP("Insert a new diameter dimension feature for the selected view");
sWhatsThis = "TechDraw_NewDiameterDimension";
sStatusTip = sToolTipText;
sPixmap = "Dimension_Diameter";
}
void CmdTechDrawNewDiameterDimension::activated(int iMsg)
{
bool result = _checkSelection(this);
if (!result)
return;
std::vector<Gui::SelectionObject> selection = getSelection().getSelectionEx();
TechDraw::DrawViewPart * objFeat = dynamic_cast<TechDraw::DrawViewPart *>(selection[0].getObject());
const std::vector<std::string> &SubNames = selection[0].getSubNames();
TechDraw::DrawViewDimension *dim = 0;
std::string FeatName = getUniqueObjectName("Dimension");
bool centerLine = false;
std::vector<App::DocumentObject *> objs;
std::vector<std::string> subs;
//All Dimensions start as Projected
//bool trueDimAllowed = _isTrueAllowed(objFeat,SubNames);
//int edgeType = _isValidSingleEdge(this,trueDimAllowed);
int edgeType = _isValidSingleEdge(this,false);
if (edgeType == isCircle) {
centerLine = true;
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
} else {
std::stringstream edgeMsg;
edgeMsg << "Can't make a diameter Dimension from this selection (edge type: " << edgeType << ")";
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect Selection"),
QObject::tr(edgeMsg.str().c_str()));
return;
}
openCommand("Create Dimension");
doCommand(Doc,"App.activeDocument().addObject('TechDraw::DrawViewDimension','%s')",FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.Type = '%s'",FeatName.c_str()
,"Diameter");
if (centerLine) {
doCommand(Doc,"App.activeDocument().%s.CentreLines = True", FeatName.c_str());
} else {
doCommand(Doc,"App.activeDocument().%s.CentreLines = False", FeatName.c_str());
}
doCommand(Doc, "App.activeDocument().%s.FormatSpec = '\u00d8%%value%%'", FeatName.c_str()); // \u00d8 is Capital O with stroke
dim = dynamic_cast<TechDraw::DrawViewDimension *>(getDocument()->getObject(FeatName.c_str()));
dim->References.setValues(objs, subs);
doCommand(Doc,"App.activeDocument().%s.ProjectionType = 'Projected'",FeatName.c_str());
std::vector<App::DocumentObject*> pages = getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId());
TechDraw::DrawPage *page = dynamic_cast<TechDraw::DrawPage *>(pages.front());
std::string PageName = page->getNameInDocument();
doCommand(Doc,"App.activeDocument().%s.addView(App.activeDocument().%s)",PageName.c_str(),FeatName.c_str());
commitCommand();
//Horrible hack to force Tree update
double x = objFeat->X.getValue();
objFeat->X.setValue(x);
}
bool CmdTechDrawNewDiameterDimension::isActive(void)
{
// TODO: Also ensure that there's a part selected?
return hasActiveDocument();
}
//===========================================================================
// TechDraw_NewLengthDimension
//===========================================================================
DEF_STD_CMD_A(CmdTechDrawNewLengthDimension);
CmdTechDrawNewLengthDimension::CmdTechDrawNewLengthDimension()
: Command("TechDraw_NewLengthDimension")
{
sAppModule = "TechDraw";
sGroup = QT_TR_NOOP("TechDraw");
sMenuText = QT_TR_NOOP("Insert a new length dimension into the drawing");
sToolTipText = QT_TR_NOOP("Insert a new length dimension");
sWhatsThis = "TechDraw_NewLengthDimension";
sStatusTip = sToolTipText;
sPixmap = "Dimension_Length";
}
void CmdTechDrawNewLengthDimension::activated(int iMsg)
{
bool result = _checkSelection(this);
if (!result)
return;
std::vector<Gui::SelectionObject> selection = getSelection().getSelectionEx();
TechDraw::DrawViewPart * objFeat = dynamic_cast<TechDraw::DrawViewPart *>(selection[0].getObject());
const std::vector<std::string> &SubNames = selection[0].getSubNames();
TechDraw::DrawViewDimension *dim = 0;
std::string FeatName = getUniqueObjectName("Dimension");
std::string dimType;
std::vector<App::DocumentObject *> objs;
std::vector<std::string> subs;
//All Dimensions start as Projected
//bool trueDimAllowed = _isTrueAllowed(objFeat,SubNames);
//int edgeType = _isValidSingleEdge(this,trueDimAllowed);
int edgeType = _isValidSingleEdge(this,false);
if ((edgeType == isHorizontal) ||
(edgeType == isVertical) ||
(edgeType == isDiagonal)) {
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
} else if (_isValidVertexes(this)) {
objs.push_back(objFeat);
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
subs.push_back(SubNames[1]);
} else if ((_isValidEdgeToEdge(this) == isHorizontal) ||
(_isValidEdgeToEdge(this) == isVertical) ||
(_isValidEdgeToEdge(this) == isVertical)) {
objs.push_back(objFeat);
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
subs.push_back(SubNames[1]);
} else {
std::stringstream edgeMsg;
edgeMsg << "Can't make a length Dimension from this selection (edge type: " << edgeType << ")";
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect Selection"),
QObject::tr(edgeMsg.str().c_str()));
return;
}
openCommand("Create Dimension");
doCommand(Doc,"App.activeDocument().addObject('TechDraw::DrawViewDimension','%s')", FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.Type = '%s'", FeatName.c_str()
, "Distance");
dim = dynamic_cast<TechDraw::DrawViewDimension *>(getDocument()->getObject(FeatName.c_str()));
dim->References.setValues(objs, subs);
doCommand(Doc, "App.activeDocument().%s.FormatSpec = '%%value%%'", FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.ProjectionType = 'Projected'",FeatName.c_str());
std::vector<App::DocumentObject*> pages = getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId());
TechDraw::DrawPage *page = dynamic_cast<TechDraw::DrawPage *>(pages.front());
std::string PageName = page->getNameInDocument();
doCommand(Doc,"App.activeDocument().%s.addView(App.activeDocument().%s)",PageName.c_str(),FeatName.c_str());
commitCommand();
//Horrible hack to force Tree update
double x = objFeat->X.getValue();
objFeat->X.setValue(x);
}
bool CmdTechDrawNewLengthDimension::isActive(void)
{
// TODO: Also ensure that there's a part selected?
return hasActiveDocument();
}
//===========================================================================
// TechDraw_NewDistanceXDimension
//===========================================================================
DEF_STD_CMD_A(CmdTechDrawNewDistanceXDimension);
CmdTechDrawNewDistanceXDimension::CmdTechDrawNewDistanceXDimension()
: Command("TechDraw_NewDistanceXDimension")
{
sAppModule = "TechDraw";
sGroup = QT_TR_NOOP("TechDraw");
sMenuText = QT_TR_NOOP("Insert a new horizontal dimension into the drawing");
sToolTipText = QT_TR_NOOP("Insert a new horizontal-distance dimension");
sWhatsThis = "TechDraw_NewDistanceXDimension";
sStatusTip = sToolTipText;
sPixmap = "Dimension_Horizontal";
}
void CmdTechDrawNewDistanceXDimension::activated(int iMsg)
{
bool result = _checkSelection(this);
if (!result)
return;
std::vector<Gui::SelectionObject> selection = getSelection().getSelectionEx();
TechDraw::DrawViewPart * objFeat = dynamic_cast<TechDraw::DrawViewPart *>(selection[0].getObject());
const std::vector<std::string> &SubNames = selection[0].getSubNames();
TechDraw::DrawViewDimension *dim = 0;
std::string FeatName = getUniqueObjectName("Dimension");
std::string dimType;
std::vector<App::DocumentObject *> objs;
std::vector<std::string> subs;
//All Dimensions start as Projected
//bool trueDimAllowed = _isTrueAllowed(objFeat,SubNames);
//int edgeType = _isValidSingleEdge(this,trueDimAllowed);
int edgeType = _isValidSingleEdge(this,false);
if ((edgeType == isHorizontal) ||
(edgeType == isDiagonal)) {
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
} else if (_isValidVertexes(this)) {
objs.push_back(objFeat);
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
subs.push_back(SubNames[1]);
} else if (_isValidEdgeToEdge(this) == isHorizontal) {
objs.push_back(objFeat);
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
subs.push_back(SubNames[1]);
} else {
std::stringstream edgeMsg;
edgeMsg << "Can't make a horizontal Dimension from this selection (edge type: " << edgeType << ")";
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect Selection"),
QObject::tr(edgeMsg.str().c_str()));
return;
}
openCommand("Create Dimension");
doCommand(Doc,"App.activeDocument().addObject('TechDraw::DrawViewDimension','%s')",FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.Type = '%s'",FeatName.c_str()
,"DistanceX");
dim = dynamic_cast<TechDraw::DrawViewDimension *>(getDocument()->getObject(FeatName.c_str()));
dim->References.setValues(objs, subs);
doCommand(Doc, "App.activeDocument().%s.FormatSpec = '%%value%%'", FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.ProjectionType = 'Projected'",FeatName.c_str());
std::vector<App::DocumentObject*> pages = getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId());
TechDraw::DrawPage *page = dynamic_cast<TechDraw::DrawPage *>(pages.front());
std::string PageName = page->getNameInDocument();
doCommand(Doc,"App.activeDocument().%s.addView(App.activeDocument().%s)",PageName.c_str(),FeatName.c_str());
commitCommand();
//Horrible hack to force Tree update
double x = objFeat->X.getValue();
objFeat->X.setValue(x);
}
bool CmdTechDrawNewDistanceXDimension::isActive(void)
{
// TODO: Also ensure that there's a part selected?
return hasActiveDocument();
}
//===========================================================================
// TechDraw_NewDistanceYDimension
//===========================================================================
DEF_STD_CMD_A(CmdTechDrawNewDistanceYDimension);
CmdTechDrawNewDistanceYDimension::CmdTechDrawNewDistanceYDimension()
: Command("TechDraw_NewDistanceYDimension")
{
sAppModule = "TechDraw";
sGroup = QT_TR_NOOP("TechDraw");
sMenuText = QT_TR_NOOP("Insert a new vertical dimension into the drawing");
sToolTipText = QT_TR_NOOP("Insert a new vertical distance dimension");
sWhatsThis = "TechDraw_NewDistanceYDimension";
sStatusTip = sToolTipText;
sPixmap = "Dimension_Vertical";
}
void CmdTechDrawNewDistanceYDimension::activated(int iMsg)
{
bool result = _checkSelection(this);
if (!result)
return;
std::vector<Gui::SelectionObject> selection = getSelection().getSelectionEx();
TechDraw::DrawViewPart * objFeat = dynamic_cast<TechDraw::DrawViewPart *>(selection[0].getObject());
const std::vector<std::string> &SubNames = selection[0].getSubNames();
TechDraw::DrawViewDimension *dim = 0;
std::string FeatName = getUniqueObjectName("Dimension");
std::string dimType;
std::vector<App::DocumentObject *> objs;
std::vector<std::string> subs;
//All Dimensions start as Projected
//bool trueDimAllowed = _isTrueAllowed(objFeat,SubNames);
//int edgeType = _isValidSingleEdge(this,trueDimAllowed);
int edgeType = _isValidSingleEdge(this,false);
if ((edgeType == isVertical) ||
(edgeType == isDiagonal)) {
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
} else if (_isValidVertexes(this)) {
objs.push_back(objFeat);
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
subs.push_back(SubNames[1]);
} else if (_isValidEdgeToEdge(this) == isVertical) {
objs.push_back(objFeat);
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
subs.push_back(SubNames[1]);
} else {
std::stringstream edgeMsg;
edgeMsg << "Can't make a vertical Dimension from this selection (edge type: " << edgeType << ")";
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect Selection"),
QObject::tr(edgeMsg.str().c_str()));
return;
}
openCommand("Create Dimension");
doCommand(Doc,"App.activeDocument().addObject('TechDraw::DrawViewDimension','%s')",FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.Type = '%s'",FeatName.c_str()
,"DistanceY");
dim = dynamic_cast<TechDraw::DrawViewDimension *>(getDocument()->getObject(FeatName.c_str()));
dim->References.setValues(objs, subs);
doCommand(Doc, "App.activeDocument().%s.FormatSpec = '%%value%%'", FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.ProjectionType = 'Projected'",FeatName.c_str());
std::vector<App::DocumentObject*> pages = getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId());
TechDraw::DrawPage *page = dynamic_cast<TechDraw::DrawPage *>(pages.front());
std::string PageName = page->getNameInDocument();
doCommand(Doc,"App.activeDocument().%s.addView(App.activeDocument().%s)",PageName.c_str(),FeatName.c_str());
commitCommand();
//Horrible hack to force Tree update
double x = objFeat->X.getValue();
objFeat->X.setValue(x);
}
bool CmdTechDrawNewDistanceYDimension::isActive(void)
{
// TODO: Also ensure that there's a part selected?
return hasActiveDocument();
}
//===========================================================================
// TechDraw_NewAngleDimension
//===========================================================================
DEF_STD_CMD_A(CmdTechDrawNewAngleDimension);
CmdTechDrawNewAngleDimension::CmdTechDrawNewAngleDimension()
: Command("TechDraw_NewAngleDimension")
{
sAppModule = "TechDraw";
sGroup = QT_TR_NOOP("TechDraw");
sMenuText = QT_TR_NOOP("Insert a new angle dimension into the drawing");
sToolTipText = QT_TR_NOOP("Insert a new angle dimension");
sWhatsThis = "TechDraw_NewAngleDimension";
sStatusTip = sToolTipText;
sPixmap = "Dimension_Angle";
}
void CmdTechDrawNewAngleDimension::activated(int iMsg)
{
bool result = _checkSelection(this);
if (!result)
return;
std::vector<Gui::SelectionObject> selection = getSelection().getSelectionEx();
TechDraw::DrawViewPart * objFeat = dynamic_cast<TechDraw::DrawViewPart *>(selection[0].getObject());
const std::vector<std::string> &SubNames = selection[0].getSubNames();
TechDraw::DrawViewDimension *dim = 0;
std::string FeatName = getUniqueObjectName("Dimension");
std::vector<App::DocumentObject *> objs;
std::vector<std::string> subs;
//All Dimensions start as Projected
//bool trueDimAllowed = _isTrueAllowed(objFeat,SubNames);
//int edgeType = _isValidSingleEdge(this,trueDimAllowed);
int edgeType = _isValidSingleEdge(this,false);
if (edgeType == isAngle) {
objs.push_back(objFeat);
objs.push_back(objFeat);
subs.push_back(SubNames[0]);
subs.push_back(SubNames[1]);
} else {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect Selection"),
QObject::tr("Can't make an angle Dimension from this selection"));
return;
}
openCommand("Create Dimension");
doCommand(Doc,"App.activeDocument().addObject('TechDraw::DrawViewDimension','%s')",FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.Type = '%s'",FeatName.c_str()
,"Angle");
doCommand(Doc,"App.activeDocument().%s.FormatSpec = '%s'",FeatName.c_str()
,"%value%\u00b0"); // \u00b0 is degree sign
dim = dynamic_cast<TechDraw::DrawViewDimension *>(getDocument()->getObject(FeatName.c_str()));
dim->References.setValues(objs, subs);
doCommand(Doc,"App.activeDocument().%s.ProjectionType = 'Projected'",FeatName.c_str());
std::vector<App::DocumentObject*> pages = getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId());
TechDraw::DrawPage *page = dynamic_cast<TechDraw::DrawPage *>(pages.front());
std::string PageName = page->getNameInDocument();
doCommand(Doc,"App.activeDocument().%s.addView(App.activeDocument().%s)",PageName.c_str(),FeatName.c_str());
commitCommand();
//Horrible hack to force Tree update
double x = objFeat->X.getValue();
objFeat->X.setValue(x);
}
bool CmdTechDrawNewAngleDimension::isActive(void)
{
// TODO: Also ensure that there's a part selected?
return hasActiveDocument();
}
void CreateTechDrawCommandsDims(void)
{
Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
rcCmdMgr.addCommand(new CmdTechDrawNewDimension());
rcCmdMgr.addCommand(new CmdTechDrawNewRadiusDimension());
rcCmdMgr.addCommand(new CmdTechDrawNewDiameterDimension());
rcCmdMgr.addCommand(new CmdTechDrawNewLengthDimension());
rcCmdMgr.addCommand(new CmdTechDrawNewDistanceXDimension());
rcCmdMgr.addCommand(new CmdTechDrawNewDistanceYDimension());
rcCmdMgr.addCommand(new CmdTechDrawNewAngleDimension());
}
//===========================================================================
// Selection Validation Helpers
//===========================================================================
//! common checks of Selection for Dimension commands
bool _checkSelection(Gui::Command* cmd) {
std::vector<Gui::SelectionObject> selection = cmd->getSelection().getSelectionEx();
if (selection.size() == 0) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect selection"),
QObject::tr("Select an object first"));
return false;
}
TechDraw::DrawViewPart * objFeat = dynamic_cast<TechDraw::DrawViewPart *>(selection[0].getObject());
if(!objFeat) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect selection"),
QObject::tr("No Feature in selection"));
return false;
}
const std::vector<std::string> &SubNames = selection[0].getSubNames();
if (SubNames.size() != 1 && SubNames.size() != 2){
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect selection"),
QObject::tr("Wrong number of objects selected"));
return false;
}
std::vector<App::DocumentObject*> pages = cmd->getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId());
if (pages.empty()){
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect selection"),
QObject::tr("Create a page to insert."));
return false;
}
return true;
}
//! verify that Selection contains a valid Geometry for a single Edge Dimension (True or Projected)
int _isValidSingleEdge(Gui::Command* cmd, bool trueDim) {
int edgeType = isInvalid;
std::vector<Gui::SelectionObject> selection = cmd->getSelection().getSelectionEx();
TechDraw::DrawViewPart * objFeat = dynamic_cast<TechDraw::DrawViewPart *>(selection[0].getObject());
const std::vector<std::string> &SubNames = selection[0].getSubNames();
if (SubNames.size() == 1) { //only 1 subshape selected
if (DrawUtil::getGeomTypeFromName(SubNames[0]) == "Edge") { //the Name starts with "Edge"
int GeoId = DrawUtil::getIndexFromName(SubNames[0]);
TechDrawGeometry::BaseGeom* geom = NULL;
if (trueDim) {
int ref = objFeat->getEdgeRefByIndex(GeoId);
geom = objFeat->getCompleteEdge(ref); //project edge onto its shape to get 2D geom
} else {
geom = objFeat->getProjEdgeByIndex(GeoId);
}
if (!geom) {
Base::Console().Error("Logic Error: no geometry for GeoId: %d\n",GeoId);
return isInvalid;
}
if(geom->geomType == TechDrawGeometry::GENERIC) {
TechDrawGeometry::Generic* gen1 = static_cast<TechDrawGeometry::Generic *>(geom);
if(gen1->points.size() > 2) { //the edge is a polyline
return isInvalid;
}
Base::Vector2D line = gen1->points.at(1) - gen1->points.at(0);
if(fabs(line.fY) < FLT_EPSILON ) {
edgeType = isHorizontal;
} else if(fabs(line.fX) < FLT_EPSILON) {
edgeType = isVertical;
} else {
edgeType = isDiagonal;
}
} else if (geom->geomType == TechDrawGeometry::CIRCLE ||
geom->geomType == TechDrawGeometry::ELLIPSE ||
geom->geomType == TechDrawGeometry::ARCOFCIRCLE ||
geom->geomType == TechDrawGeometry::ARCOFELLIPSE ) {
edgeType = isCircle;
} else if (geom->geomType == TechDrawGeometry::BSPLINE) {
edgeType = isCurve;
} else {
edgeType = isInvalid;
}
}
}
return edgeType;
}
//! verify that Selection contains valid geometries for a Vertex to Vertex Dimension
bool _isValidVertexes(Gui::Command* cmd) {
std::vector<Gui::SelectionObject> selection = cmd->getSelection().getSelectionEx();
const std::vector<std::string> &SubNames = selection[0].getSubNames();
if(SubNames.size() == 2) { //there are 2
if (DrawUtil::getGeomTypeFromName(SubNames[0]) == "Vertex" && //they both start with "Vertex"
DrawUtil::getGeomTypeFromName(SubNames[1]) == "Vertex") {
return true;
}
}
return false;
}
//! verify that the Selection contains valid geometries for an Edge to Edge Dimension
int _isValidEdgeToEdge(Gui::Command* cmd, bool trueDim) {
//TODO: can the edges be in 2 different features??
int edgeType = isInvalid;
std::vector<Gui::SelectionObject> selection = cmd->getSelection().getSelectionEx();
TechDraw::DrawViewPart* objFeat0 = dynamic_cast<TechDraw::DrawViewPart *>(selection[0].getObject());
//TechDraw::DrawViewPart* objFeat1 = dynamic_cast<TechDraw::DrawViewPart *>(selection[1].getObject());
const std::vector<std::string> &SubNames = selection[0].getSubNames();
if(SubNames.size() == 2) { //there are 2
if (DrawUtil::getGeomTypeFromName(SubNames[0]) == "Edge" && //they both start with "Edge"
DrawUtil::getGeomTypeFromName(SubNames[1]) == "Edge") {
int GeoId0 = DrawUtil::getIndexFromName(SubNames[0]);
int GeoId1 = DrawUtil::getIndexFromName(SubNames[1]);
TechDrawGeometry::BaseGeom* geom0 = NULL;
TechDrawGeometry::BaseGeom* geom1 = NULL;
if (trueDim) {
int ref0 = objFeat0->getEdgeRefByIndex(GeoId0);
int ref1 = objFeat0->getEdgeRefByIndex(GeoId1);
geom0 = objFeat0->getCompleteEdge(ref0);
geom1 = objFeat0->getCompleteEdge(ref1);
} else {
geom0 = objFeat0->getProjEdgeByIndex(GeoId0);
geom1 = objFeat0->getProjEdgeByIndex(GeoId1);
}
if ((!geom0) || (!geom1)) {
Base::Console().Error("Logic Error: no geometry for GeoId: %d or GeoId: %d\n",GeoId0,GeoId1);
return isInvalid;
}
if(geom0->geomType == TechDrawGeometry::GENERIC &&
geom1->geomType == TechDrawGeometry::GENERIC) {
TechDrawGeometry::Generic *gen0 = static_cast<TechDrawGeometry::Generic *>(geom0);
TechDrawGeometry::Generic *gen1 = static_cast<TechDrawGeometry::Generic *>(geom1);
if(gen0->points.size() > 2 ||
gen1->points.size() > 2) { //the edge is a polyline
return isInvalid;
}
Base::Vector2D line0 = gen0->points.at(1) - gen0->points.at(0);
Base::Vector2D line1 = gen1->points.at(1) - gen1->points.at(0);
double xprod = fabs(line0.fX * line1.fY - line0.fY * line1.fX);
if(xprod > FLT_EPSILON) { //edges are not parallel
return isAngle;
}
if(fabs(line0.fX) < FLT_EPSILON && fabs(line1.fX) < FLT_EPSILON) {
edgeType = isHorizontal;
} else if(fabs(line0.fY) < FLT_EPSILON && fabs(line1.fY) < FLT_EPSILON) {
edgeType = isVertical;
} else {
edgeType = isDiagonal;
}
} else {
return isInvalid;
}
}
}
return edgeType;
}
//! verify that each SubName has a corresponding Edge geometry in objFeat->Source
bool _isTrueAllowed(TechDraw::DrawViewPart* objFeat, const std::vector<std::string> &SubNames)
{
std::vector<std::string>::const_iterator it = SubNames.begin();
bool trueDimAllowed = true;
for (; it != SubNames.end(); it++) {
int idx = DrawUtil::getIndexFromName((*it));
int ref = objFeat->getEdgeRefByIndex(idx);
if (ref < 0) {
trueDimAllowed = false;
}
}
return trueDimAllowed;
}