File size: 10,344 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | /***************************************************************************
* Copyright (c) 2022 WandererFan <wandererfan@gmail.com> *
* *
* 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 <TopoDS_Shape.hxx>
#include <BRep_Tool.hxx>
#include <BRepAdaptor_Curve.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <TopExp.hxx>
#include <App/GeoFeature.h>
#include <App/DocumentObject.h>
#include <App/Document.h>
#include <App/Link.h>
#include <Base/Console.h>
#include <Mod/Measure/App/ShapeFinder.h>
#include <Mod/Part/App/TopoShape.h>
#include <Mod/PartDesign/App/Body.h>
#include <Mod/PartDesign/App/Feature.h>
#include "DimensionReferences.h"
#include "DrawUtil.h"
#include "DrawViewPart.h"
#include "ShapeUtils.h"
#include "CosmeticVertex.h"
using namespace TechDraw;
using namespace Measure;
using DU = DrawUtil;
using SU = ShapeUtils;
ReferenceEntry::ReferenceEntry( App::DocumentObject* docObject, const std::string& subName, App::Document* document)
{
setObject(docObject);
setSubName(subName);
setDocument(document);
if (docObject) {
setObjectName(docObject->getNameInDocument());
if (document == nullptr) {
setDocument(docObject->getDocument());
}
}
}
ReferenceEntry::ReferenceEntry(const ReferenceEntry& other)
{
setObject(other.getObject());
setSubName(other.getSubName(true));
setObjectName(other.getObjectName());
setDocument(other.getDocument());
}
ReferenceEntry& ReferenceEntry::operator=(const ReferenceEntry& otherRef)
{
if (this == &otherRef) {
return *this;
}
setObject(otherRef.getObject());
setSubName(otherRef.getSubName(true));
setObjectName(otherRef.getObjectName());
setDocument(otherRef.getDocument());
return *this;
}
bool ReferenceEntry::operator==(const ReferenceEntry& otherRef) const
{
return getObjectName() == otherRef.getObjectName() && getSubName() == otherRef.getSubName();
}
TopoDS_Shape ReferenceEntry::getGeometry() const
{
// first, make sure the object has not been deleted!
App::DocumentObject* obj = getDocument()->getObject(getObjectName().c_str());
if (!obj) {
return {};
}
if (getSubName().empty()) {
return {};
}
if ( getObject()->isDerivedFrom<TechDraw::DrawViewPart>() ) {
// 2d geometry from DrawViewPart will be rotated and scaled
return getGeometry2d();
}
// 3d geometry
return ShapeFinder::getLocatedShape(*getObject(), getSubName(true));
}
//! get a shape for this 2d reference
TopoDS_Shape ReferenceEntry::getGeometry2d() const
{
std::string gType;
try {
auto dvp = getObject<TechDraw::DrawViewPart>(); //NOLINT cppcoreguidelines-pro-type-static-cast-downcast
gType = geomType();
if (gType == "Vertex") {
// getVertex throws on not found, but we want to return null
// shape
auto vgeom = dvp->getVertex(getSubName());
if (!vgeom) {
return {};
}
return vgeom->getOCCVertex();
}
if (gType == "Edge") {
auto egeom = dvp->getEdge(getSubName());
if (!egeom) {
return {};
}
return egeom->getOCCEdge();
}
if (gType == "Face") {
auto fgeom = dvp->getFace(getSubName());
if (!fgeom) {
return {};
}
return fgeom->toOccFace();
}
}
catch (...) {
Base::Console().message("RE::getGeometry2d - no shape for dimension 2d reference - gType: **%s**\n", gType.c_str());
}
return {};
}
std::string ReferenceEntry::getSubName(bool longForm) const
{
if (longForm) {
return m_subName;
}
return ShapeFinder::getLastTerm(m_subName);
}
App::DocumentObject* ReferenceEntry::getObject() const
{
if (!getDocument()) {
return nullptr;
}
App::DocumentObject* obj = getDocument()->getObject(getObjectName().c_str());
if (!obj) {
return nullptr;
}
return obj;
}
//! return the reference geometry as a Part::TopoShape.
Part::TopoShape ReferenceEntry::asTopoShape() const
{
TopoDS_Shape geom = getGeometry();
if (geom.IsNull()) {
return {};
}
if (geom.ShapeType() == TopAbs_VERTEX) {
TopoDS_Vertex vert = TopoDS::Vertex(geom);
return asTopoShapeVertex(vert);
}
if (geom.ShapeType() == TopAbs_EDGE) {
TopoDS_Edge edge = TopoDS::Edge(geom);
return asTopoShapeEdge(edge);
}
if (geom.ShapeType() == TopAbs_FACE) {
TopoDS_Face face = TopoDS::Face(geom);
return asTopoShapeFace(face);
}
throw Base::RuntimeError("Dimension Reference has unsupported geometry");
}
//! returns unscaled, unrotated version of inShape. inShape is assumed to be a 2d shape, but this is not enforced.
Part::TopoShape ReferenceEntry::asCanonicalTopoShape() const
{
if (is3d()) {
return asTopoShape();
}
// this is a 2d reference
auto dvp = getObject<DrawViewPart>(); //NOLINT cppcoreguidelines-pro-type-static-cast-downcast
auto rawTopoShape = asTopoShape();
return ReferenceEntry::asCanonicalTopoShape(rawTopoShape, *dvp);
}
//! static public method returns unscaled, unrotated version of inShape. inShape is assumed to be a 2d shape,
//! but this is not enforced. 3d shapes should not be made canonical.
//! 2d shapes are inverted in Y direction and need to be inverted before and after rotation
//! operations.
Part::TopoShape ReferenceEntry::asCanonicalTopoShape(const Part::TopoShape& inShape, const DrawViewPart& dvp)
{
gp_Ax2 OXYZ;
auto unscaledShape = SU::scaleShape(inShape.getShape(), 1.0 / dvp.getScale());
if (dvp.Rotation.getValue() != 0.0) {
auto rotationDeg = dvp.Rotation.getValue();
unscaledShape = SU::invertGeometry(unscaledShape);
unscaledShape = SU::rotateShape(unscaledShape, OXYZ, -rotationDeg);
unscaledShape = SU::invertGeometry(unscaledShape);
}
return {unscaledShape};
}
Part::TopoShape ReferenceEntry::asTopoShapeVertex(const TopoDS_Vertex& vert)
{
return { vert };
}
Part::TopoShape ReferenceEntry::asTopoShapeEdge(const TopoDS_Edge &edge)
{
return { edge };
}
Part::TopoShape ReferenceEntry::asTopoShapeFace(const TopoDS_Face &face)
{
return { face };
}
std::string ReferenceEntry::geomType() const
{
return DrawUtil::getGeomTypeFromName(getSubName());
}
GeomType ReferenceEntry::geomEdgeType() const
{
int geoId = TechDraw::DrawUtil::getIndexFromName(getSubName());
auto dvp = getObject<TechDraw::DrawViewPart>();
BaseGeomPtr geom = dvp->getGeomByIndex(geoId);
if (geomType() == "Edge" && geom) {
return geom->getGeomType();
}
return GeomType::NOTDEF;
}
bool ReferenceEntry::isWholeObject() const
{
return getSubName().empty();
}
//! true if this reference point to 3d model geometry
bool ReferenceEntry::is3d() const
{
if (getObject() &&
getObject()->isDerivedFrom<TechDraw::DrawViewPart>() &&
!getSubName().empty()) {
// this is a well formed 2d reference
return false;
}
if (getObject() &&
getObject()->isDerivedFrom<TechDraw::DrawViewPart>() &&
getSubName().empty()) {
// this is a broken 3d reference, so it should be treated as 3d
return true;
}
// either we have no object or we have an object and it is a 3d object
return true;
}
//! true if the target of this reference has a shape
bool ReferenceEntry::hasGeometry() const
{
if (!getObject()) {
return false;
}
if ( getObject()->isDerivedFrom<TechDraw::DrawViewPart>() ) {
// 2d reference
return hasGeometry2d();
}
// 3d reference
// TODO: shouldn't this be ShapeFinder.getLocatedShape?
auto shape = Part::Feature::getTopoShape(getObject(), Part::ShapeOption::ResolveLink | Part::ShapeOption::Transform);
auto subShape = shape.getSubShape(getSubName().c_str());
return !subShape.IsNull();
}
//! check if this 2d reference has valid geometry in the model
bool ReferenceEntry::hasGeometry2d() const
{
auto dvp = getObject<TechDraw::DrawViewPart>(); //NOLINT cppcoreguidelines-pro-type-static-cast-downcast
if (getSubName().empty()) {
return false;
}
int geomNumber = DU::getIndexFromName(getSubName());
std::string gType = geomType();
if (gType == "Vertex") {
auto vert = dvp->getProjVertexByIndex(geomNumber);
if (vert) {
return true;
}
}
else if (gType == "Edge") {
auto edge = dvp->getGeomByIndex(geomNumber);
if (edge) {
return true;
}
}
else if (gType == "Face") {
auto face = dvp->getFace(getSubName());
if (face) {
return true;
}
}
return false;
}
|