663 lines
29 KiB
C++
663 lines
29 KiB
C++
/***************************************************************************
|
|
* Copyright (c) 2013 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 <sstream>
|
|
# include <cstring>
|
|
# include <cstdlib>
|
|
# include <exception>
|
|
# include <QString>
|
|
# include <QStringList>
|
|
# include <QRegExp>
|
|
#include <BRepBuilderAPI_MakeVertex.hxx>
|
|
#include <BRepBuilderAPI_MakeEdge.hxx>
|
|
#include <BRepExtrema_DistShapeShape.hxx>
|
|
#endif
|
|
|
|
#include <QLocale>
|
|
|
|
#include <App/Application.h>
|
|
#include <Base/Console.h>
|
|
#include <Base/Exception.h>
|
|
#include <Base/Parameter.h>
|
|
#include <Base/Quantity.h>
|
|
#include <Base/UnitsApi.h>
|
|
|
|
#include <Mod/Measure/App/Measurement.h>
|
|
|
|
#include "Geometry.h"
|
|
#include "DrawViewPart.h"
|
|
#include "DrawViewDimension.h"
|
|
#include "DrawUtil.h"
|
|
|
|
|
|
#include <Mod/TechDraw/App/DrawViewDimensionPy.h> // generated from DrawViewDimensionPy.xml
|
|
|
|
using namespace TechDraw;
|
|
|
|
//===========================================================================
|
|
// DrawViewDimension
|
|
//===========================================================================
|
|
|
|
PROPERTY_SOURCE(TechDraw::DrawViewDimension, TechDraw::DrawView)
|
|
|
|
const char* DrawViewDimension::TypeEnums[]= {"Distance",
|
|
"DistanceX",
|
|
"DistanceY",
|
|
"DistanceZ",
|
|
"Radius",
|
|
"Diameter",
|
|
"Angle",
|
|
NULL};
|
|
|
|
const char* DrawViewDimension::MeasureTypeEnums[]= {"True",
|
|
"Projected",
|
|
NULL};
|
|
|
|
enum RefType{
|
|
invalidRef,
|
|
oneEdge,
|
|
twoEdge,
|
|
twoVertex,
|
|
vertexEdge
|
|
};
|
|
|
|
DrawViewDimension::DrawViewDimension(void)
|
|
{
|
|
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
|
|
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Labels");
|
|
std::string fontName = hGrp->GetASCII("LabelFont", "Sans");
|
|
hGrp = App::GetApplication().GetUserParameter()
|
|
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Dimensions");
|
|
double fontSize = hGrp->GetFloat("FontSize", 4.0);
|
|
|
|
ADD_PROPERTY_TYPE(References2D,(0,0),"",(App::PropertyType)(App::Prop_None),"Projected Geometry References");
|
|
ADD_PROPERTY_TYPE(References3D,(0,0),"",(App::PropertyType)(App::Prop_None),"3D Geometry References");
|
|
ADD_PROPERTY_TYPE(Font ,(fontName.c_str()),"Format",App::Prop_None, "The name of the font to use");
|
|
ADD_PROPERTY_TYPE(Fontsize,(fontSize) ,"Format",(App::PropertyType)(App::Prop_None),"Dimension text size in mm");
|
|
ADD_PROPERTY_TYPE(FormatSpec,("%value%") ,"Format",(App::PropertyType)(App::Prop_None),"Dimension Format");
|
|
ADD_PROPERTY_TYPE(LineWidth,(0.5) ,"Format",(App::PropertyType)(App::Prop_None),"Dimension line weight");
|
|
//ADD_PROPERTY_TYPE(CentreLines,(0) ,"Format",(App::PropertyType)(App::Prop_None),"Arc Dimension Center Mark");
|
|
|
|
Type.setEnums(TypeEnums); //dimension type: length, radius etc
|
|
ADD_PROPERTY(Type,((long)0));
|
|
MeasureType.setEnums(MeasureTypeEnums);
|
|
ADD_PROPERTY(MeasureType, ((long)0)); //True or Projected measurement
|
|
|
|
|
|
//hide the properties the user can't edit in the property editor
|
|
References2D.setStatus(App::Property::Hidden,true);
|
|
References3D.setStatus(App::Property::Hidden,true);
|
|
|
|
//hide the DrawView properties that don't apply to Dimensions
|
|
ScaleType.setStatus(App::Property::ReadOnly,true);
|
|
ScaleType.setStatus(App::Property::Hidden,true);
|
|
Scale.setStatus(App::Property::ReadOnly,true);
|
|
Scale.setStatus(App::Property::Hidden,true);
|
|
Rotation.setStatus(App::Property::ReadOnly,true);
|
|
Rotation.setStatus(App::Property::Hidden,true);
|
|
|
|
measurement = new Measure::Measurement();
|
|
}
|
|
|
|
DrawViewDimension::~DrawViewDimension()
|
|
{
|
|
delete measurement;
|
|
measurement = 0;
|
|
}
|
|
|
|
void DrawViewDimension::onChanged(const App::Property* prop)
|
|
{
|
|
if (!isRestoring()) {
|
|
if (prop == &MeasureType) {
|
|
// Base::Console().Message("TRACE -DVD::onChanged(MeasureType) - MeasureType: %d Measurehas3D: %d thisHas3D: %d\n",
|
|
// MeasureType.getValue(),measurement->has3DReferences(),has3DReferences());
|
|
if (MeasureType.isValue("True") && !measurement->has3DReferences()) {
|
|
Base::Console().Warning("Dimension %s missing Reference to 3D model. Must be Projected.\n", getNameInDocument());
|
|
MeasureType.setValue("Projected");
|
|
}
|
|
}
|
|
if (prop == &References3D) { //have to rebuild the Measurement object
|
|
// Base::Console().Message("TRACE -DVD::onChanged(References3D) - MeasureType: %d has3D: %d thisHas3D: %d\n",
|
|
// MeasureType.getValue(),measurement->has3DReferences(),has3DReferences());
|
|
clear3DMeasurements(); //Measurement object
|
|
if (!(References3D.getValues()).empty()) {
|
|
setAll3DMeasurement();
|
|
} else {
|
|
if (MeasureType.isValue("True")) { //empty 3dRefs, but True
|
|
MeasureType.touch(); //run MeasureType logic for this case
|
|
}
|
|
}
|
|
}
|
|
|
|
DrawView::onChanged(prop);
|
|
}
|
|
|
|
}
|
|
|
|
void DrawViewDimension::onDocumentRestored()
|
|
{
|
|
if (has3DReferences()) {
|
|
setAll3DMeasurement();
|
|
}
|
|
}
|
|
|
|
|
|
short DrawViewDimension::mustExecute() const
|
|
{
|
|
bool result = 0;
|
|
if (!isRestoring()) {
|
|
result = (References2D.isTouched() ||
|
|
Type.isTouched() ||
|
|
MeasureType.isTouched());
|
|
}
|
|
if (result) {
|
|
return result;
|
|
}
|
|
return DrawView::mustExecute();
|
|
}
|
|
|
|
App::DocumentObjectExecReturn *DrawViewDimension::execute(void)
|
|
{
|
|
if (!has2DReferences()) { //too soon
|
|
return App::DocumentObject::StdReturn;
|
|
}
|
|
|
|
//TODO: if MeasureType = Projected and the Projected shape changes, the Dimension may become invalid (see tilted Cube example)
|
|
|
|
return App::DocumentObject::execute();;
|
|
}
|
|
|
|
std::string DrawViewDimension::getFormatedValue() const
|
|
{
|
|
QString str = QString::fromUtf8(FormatSpec.getStrValue().data(),FormatSpec.getStrValue().size());
|
|
double val = std::abs(getDimValue());
|
|
|
|
Base::Quantity qVal;
|
|
qVal.setValue(val);
|
|
if (Type.isValue("Angle")) {
|
|
qVal.setUnit(Base::Unit::Angle);
|
|
} else {
|
|
qVal.setUnit(Base::Unit::Length);
|
|
}
|
|
QString userStr = qVal.getUserString(); //this handles mm to inch/km/parsec etc and decimal positions
|
|
QRegExp rx2(QString::fromUtf8("\\D*$"));
|
|
QString userVal = userStr;
|
|
userVal.remove(rx2);
|
|
|
|
QRegExp rx(QString::fromUtf8("%(\\w+)%")); //any word bracketed by %
|
|
QStringList list;
|
|
int pos = 0;
|
|
|
|
while ((pos = rx.indexIn(str, pos)) != -1) {
|
|
list << rx.cap(0);
|
|
pos += rx.matchedLength();
|
|
}
|
|
|
|
QString repl = userVal;
|
|
if (showUnits()) {
|
|
repl = userStr;
|
|
}
|
|
|
|
for(QStringList::const_iterator it = list.begin(); it != list.end(); ++it) {
|
|
if(*it == QString::fromUtf8("%value%")){
|
|
str.replace(*it,repl);
|
|
// } else { //insert additional placeholder replacement logic here
|
|
}
|
|
}
|
|
return str.toUtf8().constData();
|
|
}
|
|
|
|
|
|
double DrawViewDimension::getDimValue() const
|
|
{
|
|
double result = 0.0;
|
|
if (!has2DReferences()) { //happens during Dimension creation
|
|
Base::Console().Message("INFO - DVD::getDimValue - Dimension has no References\n");
|
|
return result;
|
|
}
|
|
|
|
if (!getViewPart()->hasGeometry()) { //happens when loading saved document
|
|
Base::Console().Message("INFO - DVD::getDimValue ViewPart has no Geometry yet\n");
|
|
return result;
|
|
}
|
|
|
|
if (MeasureType.isValue("True")) {
|
|
// True Values
|
|
if (!measurement->has3DReferences()) {
|
|
return result;
|
|
}
|
|
|
|
if(Type.isValue("Distance")) {
|
|
result = measurement->delta().Length();
|
|
} else if(Type.isValue("DistanceX")){
|
|
Base::Vector3d delta = measurement->delta();
|
|
result = delta.x;
|
|
} else if(Type.isValue("DistanceY")){
|
|
Base::Vector3d delta = measurement->delta();
|
|
result = delta.y;
|
|
} else if(Type.isValue("DistanceZ")){
|
|
Base::Vector3d delta = measurement->delta();
|
|
result = delta.z;
|
|
} else if(Type.isValue("Radius")){
|
|
result = measurement->radius();
|
|
} else if(Type.isValue("Diameter")){
|
|
result = measurement->radius() * 2.0;
|
|
} else if(Type.isValue("Angle")){
|
|
result = measurement->angle();
|
|
} else {
|
|
throw Base::Exception("getDimValue() - Unknown Dimension Type (1)");
|
|
}
|
|
} else {
|
|
// Projected Values
|
|
const std::vector<App::DocumentObject*> &objects = References2D.getValues();
|
|
const std::vector<std::string> &subElements = References2D.getSubValues();
|
|
|
|
if (!checkReferences2D()) {
|
|
Base::Console().Error("Error: DVD - %s - 2D references are corrupt\n",getNameInDocument());
|
|
return result;
|
|
}
|
|
if ( Type.isValue("Distance") ||
|
|
Type.isValue("DistanceX") ||
|
|
Type.isValue("DistanceY") ) {
|
|
if (getRefType() == oneEdge) {
|
|
//TODO: Check for straight line Edge?
|
|
int idx = DrawUtil::getIndexFromName(subElements[0]);
|
|
TechDrawGeometry::BaseGeom* geom = getViewPart()->getProjEdgeByIndex(idx);
|
|
TechDrawGeometry::Generic* gen;
|
|
if (geom && geom->geomType == TechDrawGeometry::GeomType::GENERIC) {
|
|
gen = static_cast<TechDrawGeometry::Generic*>(geom);
|
|
} else {
|
|
Base::Console().Error("Error: DVD - %s - 2D references are corrupt\n",getNameInDocument());
|
|
return result;
|
|
}
|
|
Base::Vector2d start = gen->points[0];
|
|
Base::Vector2d end = gen->points[1];
|
|
Base::Vector2d line = end - start;
|
|
if (Type.isValue("Distance")) {
|
|
result = line.Length() / getViewPart()->Scale.getValue();
|
|
} else if (Type.isValue("DistanceX")) {
|
|
return fabs(line.x) / getViewPart()->Scale.getValue();
|
|
} else {
|
|
result = fabs(line.y) / getViewPart()->Scale.getValue();
|
|
}
|
|
}else if (getRefType() == twoEdge) {
|
|
//only works for straight line edges
|
|
int idx0 = DrawUtil::getIndexFromName(subElements[0]);
|
|
int idx1 = DrawUtil::getIndexFromName(subElements[1]);
|
|
TechDrawGeometry::BaseGeom* geom0 = getViewPart()->getProjEdgeByIndex(idx0);
|
|
TechDrawGeometry::Generic* gen0;
|
|
if (geom0 && geom0->geomType == TechDrawGeometry::GeomType::GENERIC) {
|
|
gen0 = static_cast<TechDrawGeometry::Generic*>(geom0);
|
|
} else {
|
|
Base::Console().Error("Error: DVD - %s - 2D references are corrupt\n",getNameInDocument());
|
|
return result;
|
|
}
|
|
TechDrawGeometry::BaseGeom* geom1 = getViewPart()->getProjEdgeByIndex(idx1);
|
|
TechDrawGeometry::Generic* gen1;
|
|
if (geom1 && geom1->geomType == TechDrawGeometry::GeomType::GENERIC) {
|
|
gen1 = static_cast<TechDrawGeometry::Generic*>(geom1);
|
|
} else {
|
|
Base::Console().Error("Error: DVD - %s - 2D references are corrupt\n",getNameInDocument());
|
|
return result;
|
|
}
|
|
Base::Vector2d s0 = gen0->points[0];
|
|
Base::Vector2d e0 = gen0->points[1];
|
|
Base::Vector2d s1 = gen1->points[0];
|
|
Base::Vector2d e1 = gen1->points[1];
|
|
if (Type.isValue("Distance")) {
|
|
result = dist2Segs(s0,e0,s1,e1) / getViewPart()->Scale.getValue();
|
|
} else if (Type.isValue("DistanceX")) {
|
|
Base::Vector2d p1 = geom0->nearPoint(geom1);
|
|
Base::Vector2d p2 = geom1->nearPoint(geom0);
|
|
result = fabs(p1.x - p2.x) / getViewPart()->Scale.getValue();
|
|
} else if (Type.isValue("DistanceY")) {
|
|
Base::Vector2d p1 = geom0->nearPoint(geom1);
|
|
Base::Vector2d p2 = geom1->nearPoint(geom0);
|
|
result = fabs(p1.y - p2.y) / getViewPart()->Scale.getValue();
|
|
}
|
|
} else if (getRefType() == twoVertex) {
|
|
int idx0 = DrawUtil::getIndexFromName(subElements[0]);
|
|
int idx1 = DrawUtil::getIndexFromName(subElements[1]);
|
|
TechDrawGeometry::Vertex* v0 = getViewPart()->getProjVertexByIndex(idx0);
|
|
TechDrawGeometry::Vertex* v1 = getViewPart()->getProjVertexByIndex(idx1);
|
|
if ((v0 == nullptr) ||
|
|
(v1 == nullptr) ) {
|
|
Base::Console().Error("Error: DVD - %s - 2D references are corrupt\n",getNameInDocument());
|
|
return result;
|
|
}
|
|
Base::Vector2d start = v0->pnt;
|
|
Base::Vector2d end = v1->pnt;
|
|
Base::Vector2d line = end - start;
|
|
if (Type.isValue("Distance")) {
|
|
result = line.Length() / getViewPart()->Scale.getValue();
|
|
} else if (Type.isValue("DistanceX")) {
|
|
result = fabs(line.x) / getViewPart()->Scale.getValue();
|
|
} else {
|
|
result = fabs(line.y) / getViewPart()->Scale.getValue();
|
|
}
|
|
} else if (getRefType() == vertexEdge) {
|
|
int idx0 = DrawUtil::getIndexFromName(subElements[0]);
|
|
int idx1 = DrawUtil::getIndexFromName(subElements[1]);
|
|
TechDrawGeometry::BaseGeom* e;
|
|
TechDrawGeometry::Vertex* v;
|
|
if (DrawUtil::getGeomTypeFromName(subElements[0]) == "Edge") {
|
|
e = getViewPart()->getProjEdgeByIndex(idx0);
|
|
v = getViewPart()->getProjVertexByIndex(idx1);
|
|
} else {
|
|
e = getViewPart()->getProjEdgeByIndex(idx1);
|
|
v = getViewPart()->getProjVertexByIndex(idx0);
|
|
}
|
|
if ((v == nullptr) ||
|
|
(e == nullptr) ) {
|
|
Base::Console().Error("Error: DVD - %s - 2D references are corrupt\n",getNameInDocument());
|
|
return result;
|
|
}
|
|
Base::Vector2d nearPoint = e->nearPoint(v->pnt);
|
|
Base::Vector2d line = nearPoint - v->pnt;
|
|
if (Type.isValue("Distance")) {
|
|
result = e->minDist(v->pnt) / getViewPart()->Scale.getValue();
|
|
} else if (Type.isValue("DistanceX")) {
|
|
result = fabs(line.x) / getViewPart()->Scale.getValue();
|
|
} else {
|
|
result = fabs(line.y) / getViewPart()->Scale.getValue();
|
|
}
|
|
} //else tarfu
|
|
} else if(Type.isValue("Radius")){
|
|
//only 1 reference for a Radius
|
|
int idx = DrawUtil::getIndexFromName(subElements[0]);
|
|
TechDrawGeometry::BaseGeom* base = getViewPart()->getProjEdgeByIndex(idx);
|
|
TechDrawGeometry::Circle* circle;
|
|
if( (base && base->geomType == TechDrawGeometry::GeomType::CIRCLE) ||
|
|
(base && base->geomType == TechDrawGeometry::GeomType::ARCOFCIRCLE)) {
|
|
circle = static_cast<TechDrawGeometry::Circle*> (base);
|
|
} else {
|
|
Base::Console().Error("Error: DVD - %s - 2D references are corrupt\n",getNameInDocument());
|
|
return result;
|
|
}
|
|
result = circle->radius / getViewPart()->Scale.getValue(); //Projected BaseGeom is scaled for drawing
|
|
|
|
} else if(Type.isValue("Diameter")){
|
|
//only 1 reference for a Diameter
|
|
int idx = DrawUtil::getIndexFromName(subElements[0]);
|
|
TechDrawGeometry::BaseGeom* base = getViewPart()->getProjEdgeByIndex(idx);
|
|
TechDrawGeometry::Circle* circle;
|
|
if ((base && base->geomType == TechDrawGeometry::GeomType::CIRCLE) ||
|
|
(base && base->geomType == TechDrawGeometry::GeomType::ARCOFCIRCLE)) {
|
|
circle = static_cast<TechDrawGeometry::Circle*> (base);
|
|
} else {
|
|
return result;
|
|
}
|
|
result = (circle->radius * 2.0) / getViewPart()->Scale.getValue(); //Projected BaseGeom is scaled for drawing
|
|
} else if(Type.isValue("Angle")){
|
|
// Must project lines to 2D so cannot use measurement framework this time
|
|
//Relcalculate the measurement based on references stored.
|
|
//WF: why not use projected geom in GeomObject and Vector2d.GetAngle? intersection pt & direction issues?
|
|
//TODO: do we need to distinguish inner vs outer angle? -wf
|
|
// if(subElements.size() != 2) {
|
|
// throw Base::Exception("FVD - Two references required for angle measurement");
|
|
// }
|
|
if (getRefType() != twoEdge) {
|
|
// throw Base::Exception("DVD - Two edge references required for angle measurement");
|
|
Base::Console().Error("Error: DVD - %s - 2D references are corrupt\n",getNameInDocument());
|
|
return result;
|
|
}
|
|
int idx0 = DrawUtil::getIndexFromName(subElements[0]);
|
|
int idx1 = DrawUtil::getIndexFromName(subElements[1]);
|
|
auto viewPart( dynamic_cast<TechDraw::DrawViewPart *>(objects[0]) );
|
|
if( viewPart == nullptr ) {
|
|
Base::Console().Message("INFO - DVD::getDimValue - References2D not DrawViewPart\n");
|
|
return 0.0;
|
|
}
|
|
TechDrawGeometry::BaseGeom* edge0 = viewPart->getProjEdgeByIndex(idx0);
|
|
TechDrawGeometry::BaseGeom* edge1 = viewPart->getProjEdgeByIndex(idx1);
|
|
TechDrawGeometry::Generic *gen1;
|
|
TechDrawGeometry::Generic *gen2;
|
|
if (edge0 && edge0->geomType == TechDrawGeometry::GeomType::GENERIC) {
|
|
gen1 = static_cast<TechDrawGeometry::Generic*>(edge0);
|
|
} else {
|
|
Base::Console().Error("Error: DVD - %s - 2D references are corrupt\n",getNameInDocument());
|
|
return result;
|
|
}
|
|
if (edge1 && edge1->geomType == TechDrawGeometry::GeomType::GENERIC) {
|
|
gen2 = static_cast<TechDrawGeometry::Generic*>(edge1);
|
|
} else {
|
|
Base::Console().Error("Error: DVD - %s - 2D references are corrupt\n",getNameInDocument());
|
|
return result;
|
|
}
|
|
|
|
Base::Vector3d p1S(gen1->points.at(0).x, gen1->points.at(0).y, 0.);
|
|
Base::Vector3d p1E(gen1->points.at(1).x, gen1->points.at(1).y, 0.);
|
|
|
|
Base::Vector3d p2S(gen2->points.at(0).x, gen2->points.at(0).y, 0.);
|
|
Base::Vector3d p2E(gen2->points.at(1).x, gen2->points.at(1).y, 0.);
|
|
|
|
Base::Vector3d dir1 = p1E - p1S;
|
|
Base::Vector3d dir2 = p2E - p2S;
|
|
|
|
// Line Intersetion (taken from ViewProviderSketch.cpp)
|
|
double det = dir1.x*dir2.y - dir1.y*dir2.x;
|
|
if ((det > 0 ? det : -det) < 1e-10)
|
|
throw Base::Exception("Invalid selection - Det = 0");
|
|
|
|
double c1 = dir1.y*gen1->points.at(0).x - dir1.x*gen1->points.at(0).y;
|
|
double c2 = dir2.y*gen2->points.at(1).x - dir2.x*gen2->points.at(1).y;
|
|
double x = (dir1.x*c2 - dir2.x*c1)/det;
|
|
double y = (dir1.y*c2 - dir2.y*c1)/det;
|
|
|
|
// Intersection point
|
|
Base::Vector3d p0 = Base::Vector3d(x,y,0);
|
|
|
|
Base::Vector3d lPos((double) X.getValue(), (double) Y.getValue(), 0.);
|
|
//Base::Vector3d delta = lPos - p0;
|
|
|
|
// Create vectors point towards intersection always
|
|
Base::Vector3d a = -p0, b = -p0;
|
|
a += ((p1S - p0).Length() < FLT_EPSILON) ? p1E : p1S;
|
|
b += ((p2S - p0).Length() < FLT_EPSILON) ? p2E : p2S;
|
|
|
|
double angle2 = atan2( a.x*b.y - a.y*b.x, a.x*b.x + a.y*b.y );
|
|
result = angle2 * 180. / M_PI;
|
|
} else {
|
|
throw Base::Exception("getDimValue() - Unknown Dimension Type (2)");
|
|
} //endif Angle
|
|
} //endif Projected
|
|
return result;
|
|
}
|
|
|
|
DrawViewPart* DrawViewDimension::getViewPart() const
|
|
{
|
|
if (References2D.getValues().empty()) {
|
|
return nullptr;
|
|
}
|
|
return dynamic_cast<TechDraw::DrawViewPart * >(References2D.getValues().at(0));
|
|
}
|
|
|
|
int DrawViewDimension::getRefType() const
|
|
{
|
|
int refType = invalidRef;
|
|
const std::vector<std::string> &subElements = References2D.getSubValues();
|
|
if (subElements.size() == 1) {
|
|
refType = getRefType1(subElements[0]);
|
|
} else if (subElements.size() == 2) {
|
|
refType = getRefType2(subElements[0],subElements[1]);
|
|
}
|
|
return refType;
|
|
}
|
|
|
|
//static
|
|
int DrawViewDimension::getRefType1(const std::string g1)
|
|
{
|
|
int refType = invalidRef;
|
|
if (DrawUtil::getGeomTypeFromName(g1) == "Edge") {
|
|
refType = oneEdge;
|
|
}
|
|
return refType;
|
|
}
|
|
|
|
//static
|
|
int DrawViewDimension::getRefType2(const std::string g1, const std::string g2)
|
|
{
|
|
int refType = invalidRef;
|
|
if ((DrawUtil::getGeomTypeFromName(g1) == "Edge") &&
|
|
(DrawUtil::getGeomTypeFromName(g2) == "Edge")) {
|
|
refType = twoEdge;
|
|
} else if ((DrawUtil::getGeomTypeFromName(g1) == "Vertex") &&
|
|
(DrawUtil::getGeomTypeFromName(g2) == "Vertex")) {
|
|
refType = twoVertex;
|
|
} else if (((DrawUtil::getGeomTypeFromName(g1) == "Vertex") &&
|
|
(DrawUtil::getGeomTypeFromName(g2) == "Edge")) ||
|
|
((DrawUtil::getGeomTypeFromName(g1) == "Edge") &&
|
|
(DrawUtil::getGeomTypeFromName(g2) == "Vertex")) ) {
|
|
refType = vertexEdge;
|
|
}
|
|
//} else add different types here - Vertex-Face, ...
|
|
|
|
return refType;
|
|
}
|
|
|
|
//! validate 2D references - only checks if they exist, not if they are the right type
|
|
bool DrawViewDimension::checkReferences2D() const
|
|
{
|
|
Base::Console().Message("TRACE - DVD::checkReferences2D() - %s\n",getNameInDocument());
|
|
bool result = true;
|
|
//const std::vector<App::DocumentObject*> &objects = References2D.getValues();
|
|
const std::vector<std::string> &subElements = References2D.getSubValues();
|
|
|
|
for (auto& s: subElements) {
|
|
int idx = DrawUtil::getIndexFromName(s);
|
|
if (DrawUtil::getGeomTypeFromName(s) == "Edge") {
|
|
TechDrawGeometry::BaseGeom* geom = getViewPart()->getProjEdgeByIndex(idx);
|
|
if (geom == nullptr) {
|
|
Base::Console().Message("TRACE - DVD::checkRef2D - %s is invalid\n",s.c_str());
|
|
result = false;
|
|
break;
|
|
}
|
|
} else if (DrawUtil::getGeomTypeFromName(s) == "Vertex") {
|
|
TechDrawGeometry::Vertex* v = getViewPart()->getProjVertexByIndex(idx);
|
|
if (v == nullptr) {
|
|
Base::Console().Message("TRACE - DVD::checkRef2D - %s is invalid\n",s.c_str());
|
|
result = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
Base::Console().Message("TRACE - DVD::checkReferences2D returns %d\n",result);
|
|
return result;
|
|
}
|
|
|
|
|
|
//!add Dimension 3D references to measurement
|
|
void DrawViewDimension::setAll3DMeasurement()
|
|
{
|
|
measurement->clear();
|
|
const std::vector<App::DocumentObject*> &Objs = References3D.getValues();
|
|
const std::vector<std::string> &Subs = References3D.getSubValues();
|
|
int end = Objs.size();
|
|
int i = 0;
|
|
for ( ; i < end; i++) {
|
|
static_cast<void> (measurement->addReference3D(Objs.at(i), Subs.at(i)));
|
|
}
|
|
}
|
|
|
|
//delete all previous measurements
|
|
void DrawViewDimension::clear3DMeasurements()
|
|
{
|
|
//set sublinklist to empty?
|
|
measurement->clear();
|
|
}
|
|
|
|
void DrawViewDimension::dumpRefs2D(char* text) const
|
|
{
|
|
Base::Console().Message("DUMP - %s\n",text);
|
|
const std::vector<App::DocumentObject*> &objects = References2D.getValues();
|
|
const std::vector<std::string> &subElements = References2D.getSubValues();
|
|
std::vector<App::DocumentObject*>::const_iterator objIt = objects.begin();
|
|
std::vector<std::string>::const_iterator subIt = subElements.begin();
|
|
int i = 0;
|
|
for( ;objIt != objects.end();objIt++,subIt++,i++) {
|
|
Base::Console().Message("DUMP - ref: %d object: %s subElement: %s\n",i,(*objIt)->getNameInDocument(),(*subIt).c_str());
|
|
}
|
|
}
|
|
|
|
double DrawViewDimension::dist2Segs(Base::Vector2d s1,
|
|
Base::Vector2d e1,
|
|
Base::Vector2d s2,
|
|
Base::Vector2d e2) const
|
|
{
|
|
gp_Pnt start(s1.x,s1.y,0.0);
|
|
gp_Pnt end(e1.x,e1.y,0.0);
|
|
TopoDS_Vertex v1 = BRepBuilderAPI_MakeVertex(start);
|
|
TopoDS_Vertex v2 = BRepBuilderAPI_MakeVertex(end);
|
|
BRepBuilderAPI_MakeEdge makeEdge1(v1,v2);
|
|
TopoDS_Edge edge1 = makeEdge1.Edge();
|
|
|
|
start = gp_Pnt(s2.x,s2.y,0.0);
|
|
end = gp_Pnt(e2.x,e2.y,0.0);
|
|
v1 = BRepBuilderAPI_MakeVertex(start);
|
|
v2 = BRepBuilderAPI_MakeVertex(end);
|
|
BRepBuilderAPI_MakeEdge makeEdge2(v1,v2);
|
|
TopoDS_Edge edge2 = makeEdge2.Edge();
|
|
|
|
BRepExtrema_DistShapeShape extss(edge1, edge2);
|
|
if (!extss.IsDone()) {
|
|
throw Base::Exception("FVD - BRepExtrema_DistShapeShape failed");
|
|
}
|
|
int count = extss.NbSolution();
|
|
double minDist = 0.0;
|
|
if (count != 0) {
|
|
minDist = extss.Value();
|
|
} //TODO: else { explode }
|
|
|
|
return minDist;
|
|
}
|
|
|
|
bool DrawViewDimension::has2DReferences(void) const
|
|
{
|
|
return (References2D.getSize() > 0);
|
|
}
|
|
|
|
bool DrawViewDimension::has3DReferences(void) const
|
|
{
|
|
return (References3D.getSize() > 0);
|
|
}
|
|
|
|
bool DrawViewDimension::showUnits() const
|
|
{
|
|
bool result = false;
|
|
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
|
|
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Dimensions");
|
|
result = hGrp->GetBool("ShowUnits", true);
|
|
return result;
|
|
}
|
|
|
|
PyObject *DrawViewDimension::getPyObject(void)
|
|
{
|
|
if (PythonObject.is(Py::_None())) {
|
|
// ref counter is set to 1
|
|
PythonObject = Py::Object(new DrawViewDimensionPy(this),true);
|
|
}
|
|
return Py::new_reference_to(PythonObject);
|
|
}
|