[TD]code clean ups

- QtConcurrent include and CMake for Win

- include clean up dvp, dvs, dvd

- variable names and nested if clean up

- remove debug code
This commit is contained in:
Wanderer Fan
2022-07-29 14:17:24 -04:00
committed by WandererFan
parent 459dde957c
commit e97b95341a
36 changed files with 772 additions and 840 deletions

View File

@@ -24,35 +24,21 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <sstream>
#include <Bnd_Box.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <BRepAlgoAPI_Common.hxx>
#include <BRepAlgoAPI_Cut.hxx>
#include <BRepBndLib.hxx>
#include <BRepBuilderAPI_Copy.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <BRepBuilderAPI_MakeSolid.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <BRep_Builder.hxx>
#include <BRepPrimAPI_MakeCylinder.hxx>
#include <BRepPrimAPI_MakePrism.hxx>
#include <BRepPrim_Cylinder.hxx>
#include <BRepProj_Projection.hxx>
#include <BRepTools.hxx>
#include <Geom_Plane.hxx>
#include <gp_Ax1.hxx>
#include <gp_Ax2.hxx>
#include <gp_Ax3.hxx>
#include <gp_Dir.hxx>
#include <gp_Pln.hxx>
#include <gp_Pnt.hxx>
#include <HLRAlgo_Projector.hxx>
#include <HLRBRep_Algo.hxx>
#include <HLRBRep_HLRToShape.hxx>
#include <TopExp_Explorer.hxx>
#include <TopExp.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
@@ -61,42 +47,38 @@
#include <TopoDS_Shell.hxx>
#include <TopoDS_Solid.hxx>
#include <TopoDS_Vertex.hxx>
#endif
#include <chrono>
#include <sstream>
#include <QFile>
#include <QFileInfo>
#include "QtConcurrent/qtconcurrentrun.h"
#include <QtConcurrentRun>
#include <App/Application.h>
#include <App/Document.h>
#include <App/Material.h>
#include <Base/BoundBox.h>
#include <Base/Exception.h>
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Parameter.h>
#include <Mod/Part/App/PartFeature.h>
#include <Mod/Part/App/TopoShape.h>
#include "Preferences.h"
#include "Geometry.h"
#include "GeometryObject.h"
#include "Cosmetic.h"
#include "EdgeWalker.h"
#include "DrawProjectSplit.h"
#include "DrawProjGroupItem.h"
#include "DrawPage.h"
#include "DrawUtil.h"
#include "DrawViewDetail.h"
#include "DrawViewSection.h"
#include "Geometry.h"
#include "GeometryObject.h"
#include "Preferences.h"
#include "DrawViewDetail.h"
using namespace TechDraw;
using namespace std;
//===========================================================================
// DrawViewDetail
//===========================================================================
@@ -104,7 +86,9 @@ using namespace std;
PROPERTY_SOURCE(TechDraw::DrawViewDetail, TechDraw::DrawViewPart)
DrawViewDetail::DrawViewDetail() :
m_waitingForDetail(false)
m_waitingForDetail(false),
m_saveDvp(nullptr),
m_saveDvs(nullptr)
{
static const char *dgroup = "Detail";
@@ -129,66 +113,71 @@ DrawViewDetail::~DrawViewDetail()
short DrawViewDetail::mustExecute() const
{
if (!isRestoring()) {
if (
AnchorPoint.isTouched() ||
Radius.isTouched() ||
BaseView.isTouched() ||
Reference.isTouched()
) {
return true;
}
if (isRestoring()) {
TechDraw::DrawView::mustExecute();
}
if (AnchorPoint.isTouched() ||
Radius.isTouched() ||
BaseView.isTouched() ||
Reference.isTouched()) {
return 1;
}
return TechDraw::DrawView::mustExecute();
}
void DrawViewDetail::onChanged(const App::Property* prop)
{
if (!isRestoring()) {
if (prop == &Reference) {
std::string lblText = "Detail " +
std::string(Reference.getValue());
Label.setValue(lblText);
}
if ((prop == &Reference) ||
(prop == &Radius) ||
(prop == &BaseView)) {
requestPaint();
}
if (prop == &AnchorPoint) {
// to see AnchorPoint changes repainting is not enough, we must recompute
recomputeFeature(true);
}
if (prop == &ScaleType) {
auto page = findParentPage();
// if ScaleType is "Page", the user cannot change it
if (ScaleType.isValue("Page")) {
Scale.setStatus(App::Property::ReadOnly, true);
// apply the page-wide Scale
if (page) {
if (std::abs(page->Scale.getValue() - getScale()) > FLT_EPSILON) {
Scale.setValue(page->Scale.getValue());
Scale.purgeTouched();
}
if (isRestoring()) {
DrawView::onChanged(prop);
return;
}
if (prop == &Reference) {
std::string lblText = "Detail " +
std::string(Reference.getValue());
Label.setValue(lblText);
}
if ((prop == &Reference) ||
(prop == &Radius) ||
(prop == &BaseView)) {
requestPaint();
}
if (prop == &AnchorPoint) {
// to see AnchorPoint changes repainting is not enough, we must recompute
recomputeFeature(true);
}
if (prop == &ScaleType) {
auto page = findParentPage();
// if ScaleType is "Page", the user cannot change it
if (ScaleType.isValue("Page")) {
Scale.setStatus(App::Property::ReadOnly, true);
// apply the page-wide Scale
if (page) {
if (std::abs(page->Scale.getValue() - getScale()) > FLT_EPSILON) {
Scale.setValue(page->Scale.getValue());
Scale.purgeTouched();
}
}
else if (ScaleType.isValue("Custom")) {
// allow the change Scale
Scale.setStatus(App::Property::ReadOnly, false);
}
else if (ScaleType.isValue("Automatic")) {
Scale.setStatus(App::Property::ReadOnly, true);
// apply a Scale
if (!checkFit(page)) {
double newScale = autoScale(page->getPageWidth(), page->getPageHeight());
if (std::abs(newScale - getScale()) > FLT_EPSILON) { //stops onChanged/execute loop
Scale.setValue(newScale);
Scale.purgeTouched();
}
}
else if (ScaleType.isValue("Custom")) {
// allow Scale changes
Scale.setStatus(App::Property::ReadOnly, false);
}
else if (ScaleType.isValue("Automatic")) {
Scale.setStatus(App::Property::ReadOnly, true);
// apply an automatic Scale
if (!checkFit(page)) {
double newScale = autoScale(page->getPageWidth(), page->getPageHeight());
if (std::abs(newScale - getScale()) > FLT_EPSILON) { //stops onChanged/execute loop
Scale.setValue(newScale);
Scale.purgeTouched();
}
}
}
}
DrawView::onChanged(prop);
}
@@ -196,19 +185,16 @@ App::DocumentObjectExecReturn *DrawViewDetail::execute()
{
// Base::Console().Message("DVD::execute() - %s\n", getNameInDocument());
if (!keepUpdated()) {
return App::DocumentObject::StdReturn;
return DrawView::execute();
}
App::DocumentObject* baseObj = BaseView.getValue();
if (!baseObj) {
Base::Console().Log("DVD::execute - No BaseView(s) linked. - %s\n",
getNameInDocument());
return DrawView::execute();
}
if (!baseObj->getTypeId().isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId())) {
Base::Console().Log("DVD::execute - %s - BaseView object is not a DrawViewPart object\n",
getNameInDocument());
//this can only happen via scripting?
return DrawView::execute();
}
@@ -224,8 +210,6 @@ App::DocumentObjectExecReturn *DrawViewDetail::execute()
}
if (shape.IsNull()) {
Base::Console().Log("DVD::execute - %s - Source shape is Null\n",
getNameInDocument());
return DrawView::execute();
}
@@ -241,18 +225,7 @@ App::DocumentObjectExecReturn *DrawViewDetail::execute()
detailExec(shape, dvp, dvs);
addShapes2d();
//second pass if required
if (ScaleType.isValue("Automatic") && !checkFit()) {
double newScale = autoScale();
Scale.setValue(newScale);
Scale.purgeTouched();
if (geometryObject) {
delete geometryObject;
geometryObject = nullptr;
detailExec(shape, dvp, dvs);
}
}
dvp->requestPaint(); //to refresh detail highlight!
dvp->requestPaint(); //to refresh detail highlight in base view
return DrawView::execute();
}
@@ -262,93 +235,91 @@ void DrawViewDetail::detailExec(TopoDS_Shape& shape,
DrawViewPart* dvp,
DrawViewSection* dvs)
{
if (waitingForResult()) {
if (waitingForHlr() ||
waitingForDetail()) {
// Base::Console().Message("DVD::detailExec - waiting for result\n");
return;
}
if (waitingForDetail()) {
return;
}
QObject::connect(&m_detailWatcher, SIGNAL(finished()), this, SLOT(onMakeDetailFinished()));
m_detailFuture = QtConcurrent::run(this, &DrawViewDetail::makeDetailShape, shape, dvp, dvs);
m_detailWatcher.setFuture(m_detailFuture);
waitingForDetail(true);
}
//this runs in a separate thread since it can sometimes take a long time
//make a common of the input shape and a cylinder (or prism depending on
//the matting style)
void DrawViewDetail::makeDetailShape(TopoDS_Shape& shape,
DrawViewPart* dvp,
DrawViewSection* dvs)
{
waitingForDetail(true);
showProgressMessage(getNameInDocument(), "is making detail shape");
// auto start = chrono::high_resolution_clock::now();
Base::Vector3d anchor = AnchorPoint.getValue(); //this is a 2D point (in unrotated coords)
Base::Vector3d dirDetail = dvp->Direction.getValue();
double radius = getFudgeRadius();
int solidCount = DrawUtil::countSubShapes(shape, TopAbs_SOLID);
int shellCount = DrawUtil::countSubShapes(shape, TopAbs_SHELL);
//make a copy of the input shape so we don't inadvertently change it
BRepBuilderAPI_Copy BuilderCopy(shape);
TopoDS_Shape myShape = BuilderCopy.Shape();
TopoDS_Shape copyShape = BuilderCopy.Shape();
m_saveShape = copyShape;
m_saveDvp = dvp;
m_saveDvs = dvs;
gp_Pnt gpCenter = TechDraw::findCentroid(myShape,
gp_Pnt gpCenter = TechDraw::findCentroid(copyShape,
dirDetail);
Base::Vector3d shapeCenter = Base::Vector3d(gpCenter.X(),gpCenter.Y(),gpCenter.Z());
m_saveCentroid = shapeCenter; //centroid of original shape
if (dvs) {
if (!dvs) {
//section cutShape should already be on origin
} else {
myShape = TechDraw::moveShape(myShape, //centre shape on origin
copyShape = TechDraw::moveShape(copyShape, //centre shape on origin
-shapeCenter);
}
shapeCenter = Base::Vector3d(0.0, 0.0, 0.0);
m_viewAxis = dvp->getProjectionCS(shapeCenter);
anchor = Base::Vector3d(anchor.x,anchor.y, 0.0); //anchor coord in projection CS
Base::Vector3d anchorOffset3d = DrawUtil::toR3(m_viewAxis, anchor); //actual anchor coords in R3
m_viewAxis = dvp->getProjectionCS(shapeCenter); //save the CS for later
Base::Vector3d anchor = AnchorPoint.getValue(); //this is a 2D point in unrotated base view coords
anchor = DrawUtil::toR3(m_viewAxis, anchor); //actual anchor coords in R3
Bnd_Box bbxSource;
bbxSource.SetGap(0.0);
BRepBndLib::AddOptimal(myShape, bbxSource);
BRepBndLib::AddOptimal(copyShape, bbxSource);
double diag = sqrt(bbxSource.SquareExtent());
Base::Vector3d toolPlaneOrigin = anchorOffset3d + dirDetail * diag * -1.0; //center tool about anchor
Base::Vector3d toolPlaneOrigin = anchor + dirDetail * diag * -1.0; //center tool about anchor
double extrudeLength = 2.0 * toolPlaneOrigin.Length();
gp_Pnt gpnt(toolPlaneOrigin.x,toolPlaneOrigin.y,toolPlaneOrigin.z);
gp_Dir gdir(dirDetail.x,dirDetail.y,dirDetail.z);
double hideToolRadius = radius * 1.0;
TopoDS_Face aProjFace;
TopoDS_Face extrusionFace;
Base::Vector3d extrudeVec = dirDetail * extrudeLength;
gp_Vec extrudeDir(extrudeVec.x,extrudeVec.y,extrudeVec.z);
gp_Vec extrudeDir(extrudeVec.x, extrudeVec.y, extrudeVec.z);
TopoDS_Shape tool;
if (Preferences::mattingStyle()) {
//square mat
gp_Pln gpln(gpnt,gdir);
BRepBuilderAPI_MakeFace mkFace(gpln, -hideToolRadius,hideToolRadius,-hideToolRadius,hideToolRadius);
aProjFace = mkFace.Face();
if(aProjFace.IsNull()) {
Base::Console().Warning("DVD::detailExec - %s - failed to create tool base face\n", getNameInDocument());
BRepBuilderAPI_MakeFace mkFace(gpln, -radius, radius, -radius, radius);
extrusionFace = mkFace.Face();
if(extrusionFace.IsNull()) {
Base::Console().Warning("DVD::makeDetailShape - %s - failed to create tool base face\n", getNameInDocument());
return;
}
tool = BRepPrimAPI_MakePrism(aProjFace, extrudeDir, false, true).Shape();
tool = BRepPrimAPI_MakePrism(extrusionFace, extrudeDir, false, true).Shape();
if(tool.IsNull()) {
Base::Console().Warning("DVD::detailExec - %s - failed to create tool (prism)\n", getNameInDocument());
Base::Console().Warning("DVD::makeDetailShape - %s - failed to create tool (prism)\n", getNameInDocument());
return;
}
} else {
//circular mat
gp_Ax2 cs(gpnt, gdir);
BRepPrimAPI_MakeCylinder mkTool(cs, hideToolRadius, extrudeLength);
BRepPrimAPI_MakeCylinder mkTool(cs, radius, extrudeLength);
tool = mkTool.Shape();
if(tool.IsNull()) {
Base::Console().Warning("DVD::detailExec - %s - failed to create tool (cylinder)\n", getNameInDocument());
@@ -356,29 +327,29 @@ void DrawViewDetail::makeDetailShape(TopoDS_Shape& shape,
}
}
//for each solid and shell in the input shape, make a common with the tool and
//add the result to a compound. This avoids issues with some geometry errors in the
//input shape.
BRep_Builder builder;
TopoDS_Compound pieces;
builder.MakeCompound(pieces);
if (solidCount > 0) {
TopExp_Explorer expl(myShape, TopAbs_SOLID);
TopExp_Explorer expl(copyShape, TopAbs_SOLID);
for (; expl.More(); expl.Next()) {
const TopoDS_Solid& s = TopoDS::Solid(expl.Current());
BRepAlgoAPI_Common mkCommon(s,tool);
if (!mkCommon.IsDone()) {
// Base::Console().Warning("DVD::execute - %s - detail cut operation failed (1)\n", getNameInDocument());
continue;
}
if (mkCommon.Shape().IsNull()) {
// Base::Console().Warning("DVD::execute - %s - detail cut operation failed (2)\n", getNameInDocument());
continue;
}
//this might be overkill for piecewise algo
//Did we get at least 1 solid?
TopExp_Explorer xp;
xp.Init(mkCommon.Shape(),TopAbs_SOLID);
if (!(xp.More() == Standard_True)) {
// Base::Console().Warning("DVD::execute - mkCommon.Shape is not a solid!\n");
if ((xp.More() != Standard_True)) {
continue;
}
builder.Add(pieces, mkCommon.Shape());
@@ -386,25 +357,22 @@ void DrawViewDetail::makeDetailShape(TopoDS_Shape& shape,
}
if (shellCount > 0) {
TopExp_Explorer expl(myShape, TopAbs_SHELL);
TopExp_Explorer expl(copyShape, TopAbs_SHELL);
for (; expl.More(); expl.Next()) {
const TopoDS_Shell& s = TopoDS::Shell(expl.Current());
BRepAlgoAPI_Common mkCommon(s,tool);
if (!mkCommon.IsDone()) {
// Base::Console().Warning("DVD::execute - %s - detail cut operation failed (1)\n", getNameInDocument());
continue;
}
if (mkCommon.Shape().IsNull()) {
// Base::Console().Warning("DVD::execute - %s - detail cut operation failed (2)\n", getNameInDocument());
continue;
}
//this might be overkill for piecewise algo
//Did we get at least 1 shell?
TopExp_Explorer xp;
xp.Init(mkCommon.Shape(),TopAbs_SHELL);
if (!(xp.More() == Standard_True)) {
// Base::Console().Warning("DVD::execute - mkCommon.Shape is not a shell!\n");
if ((xp.More() != Standard_True)) {
continue;
}
builder.Add(pieces, mkCommon.Shape());
@@ -413,17 +381,10 @@ void DrawViewDetail::makeDetailShape(TopoDS_Shape& shape,
if (debugDetail()) {
BRepTools::Write(tool, "DVDTool.brep"); //debug
BRepTools::Write(myShape, "DVDCopy.brep"); //debug
BRepTools::Write(copyShape, "DVDCopy.brep"); //debug
BRepTools::Write(pieces, "DVDCommon.brep"); //debug
}
//for debugging show compound instead of common
// BRep_Builder builder;
// TopoDS_Compound Comp;
// builder.MakeCompound(Comp);
// builder.Add(Comp, tool);
// builder.Add(Comp, myShape);
gp_Pnt inputCenter;
try {
//centroid of result
@@ -438,7 +399,7 @@ void DrawViewDetail::makeDetailShape(TopoDS_Shape& shape,
(shellCount > 0)) {
//align shape with detail anchor
TopoDS_Shape centeredShape = TechDraw::moveShape(pieces,
anchorOffset3d * -1.0);
anchor * -1.0);
m_scaledShape = TechDraw::scaleShape(centeredShape,
getScale());
if (debugDetail()) {
@@ -446,9 +407,9 @@ void DrawViewDetail::makeDetailShape(TopoDS_Shape& shape,
}
} else {
//no solids, no shells, do what you can with edges
TopoDS_Shape projectedEdges = projectEdgesOntoFace(myShape, aProjFace, gdir);
TopoDS_Shape projectedEdges = projectEdgesOntoFace(copyShape, extrusionFace, gdir);
TopoDS_Shape centeredShape = TechDraw::moveShape(projectedEdges,
anchorOffset3d * -1.0);
anchor * -1.0);
if (debugDetail()) {
BRepTools::Write(projectedEdges, "DVDProjectedEdges.brep"); //debug
BRepTools::Write(centeredShape, "DVDCenteredShape.brep"); //debug
@@ -472,10 +433,6 @@ void DrawViewDetail::makeDetailShape(TopoDS_Shape& shape,
return;
}
// auto end = chrono::high_resolution_clock::now();
// auto diff = end - start;
// double diffOut = chrono::duration <double, milli>(diff).count();
// Base::Console().Message("DVD::makeDetailShape - %s spent: %.3f millisecs making detail shape\n", getNameInDocument(), diffOut);
showProgressMessage(getNameInDocument(), "has finished making detail shape");
}
@@ -485,6 +442,19 @@ void DrawViewDetail::postHlrTasks(void)
geometryObject->pruneVertexGeom(Base::Vector3d(0.0,0.0,0.0),
Radius.getValue() * getScale()); //remove vertices beyond clipradius
DrawViewPart::postHlrTasks();
//second pass if required
if (ScaleType.isValue("Automatic") && !checkFit()) {
double newScale = autoScale();
Scale.setValue(newScale);
Scale.purgeTouched();
if (geometryObject) {
delete geometryObject;
geometryObject = nullptr;
detailExec(m_saveShape, m_saveDvp, m_saveDvs);
}
}
overrideKeepUpdated(false);
}
//continue processing after makeDetailShape thread is finished
@@ -497,6 +467,15 @@ void DrawViewDetail::onMakeDetailFinished(void)
geometryObject = buildGeometryObject(m_scaledShape, m_viewAxis);
}
bool DrawViewDetail::waitingForResult() const
{
if (DrawViewPart::waitingForResult() ||
waitingForDetail()) {
return true;
}
return false;
}
TopoDS_Shape DrawViewDetail::projectEdgesOntoFace(TopoDS_Shape &edgeShape,
TopoDS_Face &projFace,
gp_Dir& projDir)