diff --git a/src/Mod/TechDraw/App/CMakeLists.txt b/src/Mod/TechDraw/App/CMakeLists.txt index aa8fe00e31a5..f1787642f5fa 100644 --- a/src/Mod/TechDraw/App/CMakeLists.txt +++ b/src/Mod/TechDraw/App/CMakeLists.txt @@ -133,6 +133,8 @@ SET(TechDraw_SRCS AppTechDrawPy.cpp DrawUtil.cpp DrawUtil.h + ShapeExtractor.cpp + ShapeExtractor.h HatchLine.cpp HatchLine.h PreCompiled.cpp diff --git a/src/Mod/TechDraw/App/DrawViewPart.cpp b/src/Mod/TechDraw/App/DrawViewPart.cpp index bbb2cf235175..9cf8c3a68a81 100644 --- a/src/Mod/TechDraw/App/DrawViewPart.cpp +++ b/src/Mod/TechDraw/App/DrawViewPart.cpp @@ -28,47 +28,47 @@ #ifndef _PreComp_ # include -#include -#include +#include #include +#include +#include +#include #include +#include #include -#include +#include +#include +#include #include -#include #include -#include -# include -#include -#include -#include -#include -#include +#include +#include +#include #include -#include +#include +#include #include -#include #include #include +#include +#include #include -#include #include -#include +#include #include -#include +#include #include +#include #include +#include +#include +#include +#include #include #include -#include -#include #include #include - -#include -#include #include -#include #endif @@ -87,22 +87,24 @@ #include #include #include +#include -#include "DrawUtil.h" -#include "DrawViewSection.h" -#include "DrawProjectSplit.h" -#include "Geometry.h" -#include "GeometryObject.h" -#include "DrawViewPart.h" -#include "DrawHatch.h" +#include "Cosmetic.h" #include "DrawGeomHatch.h" -#include "DrawViewDimension.h" +#include "DrawHatch.h" +#include "DrawPage.h" +#include "DrawProjectSplit.h" +#include "DrawUtil.h" #include "DrawViewBalloon.h" #include "DrawViewDetail.h" -#include "DrawPage.h" +#include "DrawViewDimension.h" +#include "DrawViewPart.h" +#include "DrawViewSection.h" #include "EdgeWalker.h" +#include "Geometry.h" +#include "GeometryObject.h" #include "LineGroup.h" -#include "Cosmetic.h" +#include "ShapeExtractor.h" #include // generated from DrawViewPartPy.xml @@ -178,108 +180,28 @@ TopoDS_Shape DrawViewPart::getSourceShape(void) const getNameInDocument()); } } else { - std::vector sourceShapes; - for (auto& l:links) { - auto shape = Part::Feature::getShape(l); - if(!shape.IsNull()) - sourceShapes.push_back(shape); - else { - std::vector shapeList = getShapesFromObject(l); - sourceShapes.insert(sourceShapes.end(),shapeList.begin(),shapeList.end()); - } - } - - BRep_Builder builder; - TopoDS_Compound comp; - builder.MakeCompound(comp); - bool found = false; - for (auto& s:sourceShapes) { - if (s.IsNull()) { - continue; //has no shape - } - found = true; - BRepBuilderAPI_Copy BuilderCopy(s); - TopoDS_Shape shape = BuilderCopy.Shape(); - builder.Add(comp, shape); - } - //it appears that an empty compound is !IsNull(), so we need to check a different way - //if we added anything to the compound. - if (!found) { - Base::Console().Error("DVP::getSourceShapes - source shape is empty!\n"); - } else { - result = comp; - } - } - return result; -} - -std::vector DrawViewPart::getShapesFromObject(App::DocumentObject* docObj) const -{ - std::vector result; - App::GroupExtension* gex = dynamic_cast(docObj); - App::Property* gProp = docObj->getPropertyByName("Group"); - App::Property* sProp = docObj->getPropertyByName("Shape"); - if (docObj->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId())) { - Part::Feature* pf = static_cast(docObj); - Part::TopoShape ts = pf->Shape.getShape(); - ts.setPlacement(pf->globalPlacement()); - result.push_back(ts.getShape()); - } else if (gex != nullptr) { //is a group extension - std::vector objs = gex->Group.getValues(); - std::vector shapes; - for (auto& d: objs) { - shapes = getShapesFromObject(d); - if (!shapes.empty()) { - result.insert(result.end(),shapes.begin(),shapes.end()); - } - } - //the next 2 bits are mostly for Arch module objects - } else if (gProp != nullptr) { //has a Group property - App::PropertyLinkList* list = dynamic_cast(gProp); - if (list != nullptr) { - std::vector objs = list->getValues(); - std::vector shapes; - for (auto& d: objs) { - shapes = getShapesFromObject(d); - if (!shapes.empty()) { - result.insert(result.end(),shapes.begin(),shapes.end()); - } - } - } else { - Base::Console().Log("DVP::getShapesFromObject - Group is not a PropertyLinkList!\n"); - } - } else if (sProp != nullptr) { //has a Shape property - Part::PropertyPartShape* shape = dynamic_cast(sProp); - if (shape != nullptr) { - TopoDS_Shape occShape = shape->getValue(); - result.push_back(occShape); - } else { - Base::Console().Log("DVP::getShapesFromObject - Shape is not a PropertyPartShape!\n"); - } + result = ShapeExtractor::getShapes(links); } return result; } TopoDS_Shape DrawViewPart::getSourceShapeFused(void) const { - TopoDS_Shape baseShape = getSourceShape(); - if (!baseShape.IsNull()) { - TopoDS_Iterator it(baseShape); - TopoDS_Shape fusedShape = it.Value(); - it.Next(); - for (; it.More(); it.Next()) { - const TopoDS_Shape& aChild = it.Value(); - BRepAlgoAPI_Fuse mkFuse(fusedShape, aChild); - // Let's check if the fusion has been successful - if (!mkFuse.IsDone()) { - Base::Console().Error("DVP - Fusion failed - %s\n",getNameInDocument()); - return baseShape; - } - fusedShape = mkFuse.Shape(); + TopoDS_Shape result; + const std::vector& links = Source.getValues(); + if (links.empty()) { + bool isRestoring = getDocument()->testStatus(App::Document::Status::Restoring); + if (isRestoring) { + Base::Console().Warning("DVP::getSourceShape - No Sources (but document is restoring) - %s\n", + getNameInDocument()); + } else { + Base::Console().Error("Error: DVP::getSourceShape - No Source(s) linked. - %s\n", + getNameInDocument()); } - baseShape = fusedShape; + } else { + result = ShapeExtractor::getShapesFused(links); } - return baseShape; + return result; } App::DocumentObjectExecReturn *DrawViewPart::execute(void) @@ -303,7 +225,7 @@ App::DocumentObjectExecReturn *DrawViewPart::execute(void) return App::DocumentObject::StdReturn; } - TopoDS_Shape shape = getSourceShape(); //if shape is null, it is probably(?) obj creation time. + TopoDS_Shape shape = getSourceShape(); if (shape.IsNull()) { if (isRestoring) { Base::Console().Warning("DVP::execute - source shape is invalid - (but document is restoring) - %s\n", @@ -319,7 +241,7 @@ App::DocumentObjectExecReturn *DrawViewPart::execute(void) Base::Vector3d stdOrg(0.0,0.0,0.0); inputCenter = TechDraw::findCentroid(shape, - getViewAxis(stdOrg,Direction.getValue())); + getViewAxis(stdOrg,Direction.getValue())); shapeCentroid = Base::Vector3d(inputCenter.X(),inputCenter.Y(),inputCenter.Z()); TopoDS_Shape mirroredShape; @@ -347,11 +269,8 @@ App::DocumentObjectExecReturn *DrawViewPart::execute(void) } } - //add the cosmetic vertices to the geometry vertices list addCosmeticVertexesToGeom(); - //add the cosmetic Edges to geometry Edges list addCosmeticEdgesToGeom(); - //add centerlines to geometry edges list addCenterLinesToGeom(); auto end = std::chrono::high_resolution_clock::now(); diff --git a/src/Mod/TechDraw/App/DrawViewPart.h b/src/Mod/TechDraw/App/DrawViewPart.h index 9afaf4344ee9..f840d84e9595 100644 --- a/src/Mod/TechDraw/App/DrawViewPart.h +++ b/src/Mod/TechDraw/App/DrawViewPart.h @@ -161,9 +161,12 @@ class TechDrawExport DrawViewPart : public DrawView gp_Pln getProjPlane(void) const; virtual std::vector getWireForFace(int idx) const; + virtual TopoDS_Shape getSourceShape(void) const; - virtual std::vector getShapesFromObject(App::DocumentObject* docObj) const; +/* virtual std::vector getShapesFromObject(App::DocumentObject* docObj) const; */ virtual TopoDS_Shape getSourceShapeFused(void) const; +/* std::vector extractDrawableShapes(TopoDS_Shape shapeIn) const;*/ + bool isIso(void) const; virtual int addCosmeticVertex(Base::Vector3d pos); diff --git a/src/Mod/TechDraw/App/DrawViewSection.cpp b/src/Mod/TechDraw/App/DrawViewSection.cpp index 0ef635c28dbd..f55b56f52d4d 100644 --- a/src/Mod/TechDraw/App/DrawViewSection.cpp +++ b/src/Mod/TechDraw/App/DrawViewSection.cpp @@ -265,7 +265,6 @@ App::DocumentObjectExecReturn *DrawViewSection::execute(void) } TopoDS_Shape rawShape = mkCut.Shape(); - // BRepTools::Write(myShape, "DVSCopy.brep"); //debug // BRepTools::Write(prism, "DVSTool.brep"); //debug // BRepTools::Write(rawShape, "DVSResult.brep"); //debug @@ -281,7 +280,6 @@ App::DocumentObjectExecReturn *DrawViewSection::execute(void) m_cutShape = rawShape; m_cutShape = TechDraw::moveShape(m_cutShape, //centre on origin -SectionOrigin.getValue()); - gp_Pnt inputCenter; gp_Ax2 viewAxis; try { diff --git a/src/Mod/TechDraw/App/GeometryObject.cpp b/src/Mod/TechDraw/App/GeometryObject.cpp index 89ba5f79d92d..4e8a0272afa0 100644 --- a/src/Mod/TechDraw/App/GeometryObject.cpp +++ b/src/Mod/TechDraw/App/GeometryObject.cpp @@ -193,6 +193,7 @@ void GeometryObject::projectShape(const TopoDS_Shape& input, throw Base::RuntimeError("GeometryObject::projectShape - unknown error occurred while projecting shape"); // Standard_Failure::Raise("GeometryObject::projectShape - error occurred while projecting shape"); } + auto end = chrono::high_resolution_clock::now(); auto diff = end - start; double diffOut = chrono::duration (diff).count(); diff --git a/src/Mod/TechDraw/App/ShapeExtractor.cpp b/src/Mod/TechDraw/App/ShapeExtractor.cpp new file mode 100644 index 000000000000..d05cd393d9db --- /dev/null +++ b/src/Mod/TechDraw/App/ShapeExtractor.cpp @@ -0,0 +1,186 @@ +/*************************************************************************** + * Copyright (c) 2019 WandererFan * + * * + * 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 +#endif + +#include +#include +#include + + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "ShapeExtractor.h" + +using namespace TechDraw; + +TopoDS_Shape ShapeExtractor::getShapes(const std::vector links) +{ + TopoDS_Shape result; + std::vector sourceShapes; + + for (auto& l:links) { + auto shape = Part::Feature::getShape(l); //finds shape within DocObj?? + if(!shape.IsNull()) { +// BRepTools::Write(shape, "DVPgetShape.brep"); //debug + if (shape.ShapeType() > TopAbs_COMPSOLID) { + sourceShapes.push_back(shape); + } else { + std::vector drawable = extractDrawableShapes(shape); + if (!drawable.empty()) { + sourceShapes.insert(sourceShapes.end(),drawable.begin(),drawable.end()); + } + } + } else { + std::vector shapeList = getShapesFromObject(l); + sourceShapes.insert(sourceShapes.end(),shapeList.begin(),shapeList.end()); + } + } + + BRep_Builder builder; + TopoDS_Compound comp; + builder.MakeCompound(comp); + bool found = false; + for (auto& s:sourceShapes) { + if (s.IsNull()) { + continue; //has no shape + } + found = true; + BRepBuilderAPI_Copy BuilderCopy(s); + TopoDS_Shape shape = BuilderCopy.Shape(); + builder.Add(comp, shape); + } + //it appears that an empty compound is !IsNull(), so we need to check a different way + //if we added anything to the compound. + if (!found) { + Base::Console().Error("SE::getSourceShapes - source shape is empty!\n"); + } else { + result = comp; + } + return result; +} + +std::vector ShapeExtractor::getShapesFromObject(const App::DocumentObject* docObj) +{ + std::vector result; + + const App::GroupExtension* gex = dynamic_cast(docObj); + App::Property* gProp = docObj->getPropertyByName("Group"); + App::Property* sProp = docObj->getPropertyByName("Shape"); + if (docObj->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId())) { + const Part::Feature* pf = static_cast(docObj); + Part::TopoShape ts = pf->Shape.getShape(); + ts.setPlacement(pf->globalPlacement()); + result.push_back(ts.getShape()); + } else if (gex != nullptr) { //is a group extension + std::vector objs = gex->Group.getValues(); + std::vector shapes; + for (auto& d: objs) { + shapes = getShapesFromObject(d); + if (!shapes.empty()) { + result.insert(result.end(),shapes.begin(),shapes.end()); + } + } + //the next 2 bits are mostly for Arch module objects + } else if (gProp != nullptr) { //has a Group property + App::PropertyLinkList* list = dynamic_cast(gProp); + if (list != nullptr) { + std::vector objs = list->getValues(); + std::vector shapes; + for (auto& d: objs) { + shapes = getShapesFromObject(d); + if (!shapes.empty()) { + result.insert(result.end(),shapes.begin(),shapes.end()); + } + } + } else { + Base::Console().Log("SE::getShapesFromObject - Group is not a PropertyLinkList!\n"); + } + } else if (sProp != nullptr) { //has a Shape property + Part::PropertyPartShape* shape = dynamic_cast(sProp); + if (shape != nullptr) { + TopoDS_Shape occShape = shape->getValue(); + result.push_back(occShape); + } else { + Base::Console().Log("SE::getShapesFromObject - Shape is not a PropertyPartShape!\n"); + } + } + return result; +} + +TopoDS_Shape ShapeExtractor::getShapesFused(const std::vector links) +{ + TopoDS_Shape baseShape = getShapes(links); + if (!baseShape.IsNull()) { + TopoDS_Iterator it(baseShape); + TopoDS_Shape fusedShape = it.Value(); + it.Next(); + for (; it.More(); it.Next()) { + const TopoDS_Shape& aChild = it.Value(); + BRepAlgoAPI_Fuse mkFuse(fusedShape, aChild); + // Let's check if the fusion has been successful + if (!mkFuse.IsDone()) { + Base::Console().Error("SE - Fusion failed\n"); + return baseShape; + } + fusedShape = mkFuse.Shape(); + } + baseShape = fusedShape; + } + return baseShape; +} + +std::vector ShapeExtractor::extractDrawableShapes(const TopoDS_Shape shapeIn) +{ + //this pulls solids out of compound + //should it pull Shells, Faces, Wires, Edges, Vertexes too??? + std::vector result; + if ( (shapeIn.ShapeType() == TopAbs_COMPOUND) || + (shapeIn.ShapeType() == TopAbs_COMPSOLID) ) { //not sure about this one + TopExp_Explorer expSolid(shapeIn, TopAbs_SOLID); + for (int i = 1; expSolid.More(); expSolid.Next(), i++) { + TopoDS_Solid s = TopoDS::Solid(expSolid.Current()); + if (!s.IsNull()) { + result.push_back(s); + } + } + } + return result; +} + diff --git a/src/Mod/TechDraw/App/ShapeExtractor.h b/src/Mod/TechDraw/App/ShapeExtractor.h new file mode 100644 index 000000000000..769e5dc4e639 --- /dev/null +++ b/src/Mod/TechDraw/App/ShapeExtractor.h @@ -0,0 +1,59 @@ +/*************************************************************************** + * Copyright (c) 2019 WandererFan * + * * + * 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 * + * * + ***************************************************************************/ + +#ifndef _ShapeExtractor_h_ +#define _ShapeExtractor_h_ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace TechDraw +{ + +class TechDrawExport ShapeExtractor +{ +public: + static TopoDS_Shape getShapes(const std::vector links); + static std::vector getShapesFromObject(const App::DocumentObject* docObj); + static TopoDS_Shape getShapesFused(const std::vector links); + static std::vector extractDrawableShapes(const TopoDS_Shape shapeIn); + +protected: + +private: + +}; + +} //namespace TechDraw + +#endif // #ifndef _ShapeExtractor_h_