[TD]simplify embedded hatch file handling

This commit is contained in:
wandererfan
2022-11-09 14:36:08 -05:00
committed by WandererFan
parent 1cf51ec1c3
commit a2976bcfbc
9 changed files with 131 additions and 212 deletions

View File

@@ -74,7 +74,7 @@ DrawGeomHatch::DrawGeomHatch()
{
static const char *vgroup = "GeomHatch";
ADD_PROPERTY_TYPE(Source, (nullptr), vgroup, (App::PropertyType)(App::Prop_None),
ADD_PROPERTY_TYPE(Source, (nullptr), vgroup, App::PropertyType::Prop_None,
"The View + Face to be crosshatched");
Source.setScope(App::LinkScope::Global);
ADD_PROPERTY_TYPE(FilePattern, (prefGeomHatchFile()), vgroup, App::Prop_None,
@@ -92,56 +92,34 @@ DrawGeomHatch::DrawGeomHatch()
std::string patFilter("pat files (*.pat *.PAT);;All files (*)");
FilePattern.setFilter(patFilter);
}
void DrawGeomHatch::onChanged(const App::Property* prop)
{
if (!isRestoring()) {
if (prop == &Source) {
DrawGeomHatch::execute();
}
App::Document* doc = getDocument();
if ((prop == &FilePattern) && doc) {
if (!FilePattern.isEmpty()) {
replacePatIncluded(FilePattern.getValue());
DrawGeomHatch::execute(); //remake the line sets
}
}
if ((prop == &NamePattern) && doc) {
DrawGeomHatch::execute(); //remake the line sets
}
} else {
if ((prop == &FilePattern) || //make sure right pattern gets loaded at start up
(prop == &NamePattern)) {
DrawGeomHatch::execute();
}
if (isRestoring()) {
App::DocumentObject::onChanged(prop);
return;
}
if (prop == &Source) {
//rebuild the linesets
makeLineSets();
}
if (prop == &FilePattern) {
replacePatIncluded(FilePattern.getValue());
makeLineSets();
}
if (prop == &NamePattern) {
makeLineSets();
}
App::DocumentObject::onChanged(prop);
}
short DrawGeomHatch::mustExecute() const
{
short result = 0;
if (!isRestoring()) {
result = (Source.isTouched() ||
FilePattern.isTouched() ||
NamePattern.isTouched() ||
ScalePattern.isTouched());
}
if (result) {
return result;
}
return App::DocumentObject::mustExecute();
}
App::DocumentObjectExecReturn *DrawGeomHatch::execute()
{
// Base::Console().Message("DGH::execute()\n");
makeLineSets();
//does execute even need to exist? Its all about the property value changes
DrawViewPart* parent = getSourceView();
if (parent) {
parent->requestPaint();
@@ -149,6 +127,47 @@ App::DocumentObjectExecReturn *DrawGeomHatch::execute()
return App::DocumentObject::StdReturn;
}
void DrawGeomHatch::onDocumentRestored()
{
//rebuild the linesets
makeLineSets();
App::DocumentObject::onDocumentRestored();
}
void DrawGeomHatch::replacePatIncluded(std::string newHatchFileName)
{
// Base::Console().Message("DGH::replaceFileIncluded(%s)\n", newHatchFileName.c_str());
if (newHatchFileName.empty()) {
return;
}
Base::FileInfo tfi(newHatchFileName);
if (tfi.isReadable()) {
PatIncluded.setValue(newHatchFileName.c_str());
} else {
throw Base::RuntimeError("Could not read the new PAT file");
}
}
void DrawGeomHatch::setupObject()
{
// Base::Console().Message("DGH::setupObject()\n");
replacePatIncluded(FilePattern.getValue());
}
void DrawGeomHatch::unsetupObject()
{
// Base::Console().Message("DGH::unsetupObject() - status: %lu removing: %d \n", getStatus(), isRemoving());
App::DocumentObject* source = Source.getValue();
DrawView* dv = dynamic_cast<DrawView*>(source);
if (dv) {
dv->requestPaint();
}
App::DocumentObject::unsetupObject();
}
//-----------------------------------------------------------------------------------
void DrawGeomHatch::makeLineSets()
{
@@ -212,6 +231,10 @@ std::vector<PATLineSpec> DrawGeomHatch::getDecodedSpecsFromFile(std::string file
std::vector<LineSet> DrawGeomHatch::getTrimmedLines(int i) //get the trimmed hatch lines for face i
{
if (m_lineSets.empty()) {
makeLineSets();
}
std::vector<LineSet> result;
DrawViewPart* source = getSourceView();
if (!source ||
@@ -467,6 +490,10 @@ std::vector<LineSet> DrawGeomHatch::getFaceOverlay(int fdx)
BRepBndLib::AddOptimal(face, bBox);
bBox.SetGap(0.0);
if (m_lineSets.empty()) {
makeLineSets();
}
for (auto& ls: m_lineSets) {
PATLineSpec hl = ls.getPATLineSpec();
std::vector<TopoDS_Edge> candidates = DrawGeomHatch::makeEdgeOverlay(hl, bBox, ScalePattern.getValue());
@@ -491,6 +518,7 @@ std::vector<LineSet> DrawGeomHatch::getFaceOverlay(int fdx)
/* static */
//! get TopoDS_Face(iface) from DVP
//! TODO: DVP can serve these up ready to use
TopoDS_Face DrawGeomHatch::extractFace(DrawViewPart* source, int iface )
{
TopoDS_Face result;
@@ -529,6 +557,8 @@ TopoDS_Face DrawGeomHatch::extractFace(DrawViewPart* source, int iface )
return result;
}
//--------------------------------------------------------------------------------------------------
PyObject *DrawGeomHatch::getPyObject()
{
if (PythonObject.is(Py::_None())) {
@@ -537,74 +567,6 @@ PyObject *DrawGeomHatch::getPyObject()
return Py::new_reference_to(PythonObject);
}
void DrawGeomHatch::replacePatIncluded(std::string newPatFile)
{
// Base::Console().Message("DGH::replacePatHatch(%s)\n", newPatFile.c_str());
if (PatIncluded.isEmpty()) {
setupPatIncluded();
} else {
std::string tempName = PatIncluded.getExchangeTempFile();
DrawUtil::copyFile(newPatFile, tempName);
PatIncluded.setValue(tempName.c_str());
}
}
void DrawGeomHatch::onDocumentRestored()
{
// Base::Console().Message("DGH::onDocumentRestored()\n");
if (PatIncluded.isEmpty()) {
if (!FilePattern.isEmpty()) {
std::string patFileName = FilePattern.getValue();
Base::FileInfo tfi(patFileName);
if (tfi.isReadable()) {
setupPatIncluded();
}
}
}
execute();
App::DocumentObject::onDocumentRestored();
}
void DrawGeomHatch::setupObject()
{
//by this point DGH should have a name and belong to a document
setupPatIncluded();
App::DocumentObject::setupObject();
}
void DrawGeomHatch::setupPatIncluded()
{
// Base::Console().Message("DGH::setupPatIncluded()\n");
App::Document* doc = getDocument();
std::string special = getNameInDocument();
special += "PatHatch.pat";
std::string dir = doc->TransientDir.getValue();
std::string patName = dir + special;
if (PatIncluded.isEmpty()) {
DrawUtil::copyFile(std::string(), patName);
PatIncluded.setValue(patName.c_str());
}
if (!FilePattern.isEmpty()) {
std::string exchName = PatIncluded.getExchangeTempFile();
DrawUtil::copyFile(FilePattern.getValue(), exchName);
PatIncluded.setValue(exchName.c_str(), special.c_str());
}
}
void DrawGeomHatch::unsetupObject()
{
// Base::Console().Message("DGH::unsetupObject() - status: %lu removing: %d \n", getStatus(), isRemoving());
App::DocumentObject* source = Source.getValue();
DrawView* dv = dynamic_cast<DrawView*>(source);
if (dv) {
dv->requestPaint();
}
App::DocumentObject::unsetupObject();
}
std::string DrawGeomHatch::prefGeomHatchFile()
{
return Preferences::patFile();

View File

@@ -62,14 +62,15 @@ public:
App::PropertyString NamePattern;
App::PropertyFloatConstraint ScalePattern;
virtual short mustExecute() const override;
virtual App::DocumentObjectExecReturn *execute(void) override;
virtual void onChanged(const App::Property* prop) override;
virtual const char* getViewProviderName(void) const override {
App::DocumentObjectExecReturn *execute(void) override;
void onChanged(const App::Property* prop) override;
const char* getViewProviderName(void) const override {
return "TechDrawGui::ViewProviderGeomHatch";
}
virtual PyObject *getPyObject(void) override;
virtual void unsetupObject(void) override;
PyObject *getPyObject(void) override;
void setupObject() override;
void unsetupObject(void) override;
void onDocumentRestored() override;
DrawViewPart* getSourceView(void) const;
@@ -96,19 +97,16 @@ public:
static std::vector<LineSet> makeLineSets(std::string fileSpec, std::string myPattern);
protected:
virtual void onDocumentRestored() override;
virtual void setupObject() override;
void setupPatIncluded(void);
void replacePatIncluded(std::string newPatFile);
void replacePatIncluded(std::string newHatchFileName);
void makeLineSets(void);
std::vector<PATLineSpec> getDecodedSpecsFromFile();
private:
std::vector<LineSet> m_lineSets;
std::string m_saveFile;
std::string m_saveName;
private:
static App::PropertyFloatConstraint::Constraints scaleRange;
};

View File

@@ -48,7 +48,7 @@ DrawHatch::DrawHatch(void)
{
static const char *vgroup = "Hatch";
ADD_PROPERTY_TYPE(Source, (nullptr), vgroup, (App::PropertyType)(App::Prop_None), "The View + Face to be hatched");
ADD_PROPERTY_TYPE(Source, (nullptr), vgroup, App::PropertyType::Prop_None, "The View + Face to be hatched");
Source.setScope(App::LinkScope::Global);
ADD_PROPERTY_TYPE(HatchPattern, (prefSvgHatch()), vgroup, App::Prop_None, "The hatch pattern file for this area");
ADD_PROPERTY_TYPE(SvgIncluded, (""), vgroup, App::Prop_None,
@@ -59,34 +59,17 @@ DrawHatch::DrawHatch(void)
void DrawHatch::onChanged(const App::Property* prop)
{
if (!isRestoring()) {
if (prop == &Source) {
DrawHatch::execute();
}
App::Document* doc = getDocument();
if ((prop == &HatchPattern) && doc) {
if (!HatchPattern.isEmpty()) {
replaceFileIncluded(HatchPattern.getValue());
}
}
if (isRestoring()) {
App::DocumentObject::onChanged(prop);
return;
}
if (prop == &HatchPattern) {
replaceFileIncluded(HatchPattern.getValue());
}
App::DocumentObject::onChanged(prop);
}
short DrawHatch::mustExecute() const
{
short result = 0;
if (!isRestoring()) {
result = (Source.isTouched() ||
HatchPattern.isTouched());
}
if (result) {
return result;
}
return App::DocumentObject::mustExecute();
}
App::DocumentObjectExecReturn *DrawHatch::execute(void)
{
DrawViewPart* parent = getSourceView();
@@ -183,66 +166,25 @@ bool DrawHatch::empty(void)
return sourceNames.empty();
}
void DrawHatch::replaceFileIncluded(std::string newSvgFile)
void DrawHatch::replaceFileIncluded(std::string newHatchFileName)
{
// Base::Console().Message("DH::replaceSvgHatch(%s)\n", newSvgFile.c_str());
if (SvgIncluded.isEmpty()) {
setupFileIncluded();
// Base::Console().Message("DH::replaceFileIncluded(%s)\n", newHatchFileName.c_str());
if (newHatchFileName.empty()) {
return;
}
Base::FileInfo tfi(newHatchFileName);
if (tfi.isReadable()) {
SvgIncluded.setValue(newHatchFileName.c_str());
} else {
std::string tempName = SvgIncluded.getExchangeTempFile();
DrawUtil::copyFile(newSvgFile, tempName);
SvgIncluded.setValue(tempName.c_str());
throw Base::RuntimeError("Could not read the new svg file");
}
}
void DrawHatch::onDocumentRestored()
{
//if this is a restore, we should be checking for SvgIncluded empty,
// if it is, set it up from hatchPattern,
// else, don't do anything
// Base::Console().Message("DH::onDocumentRestored()\n");
if (SvgIncluded.isEmpty()) {
if (!HatchPattern.isEmpty()) {
std::string svgFileName = HatchPattern.getValue();
Base::FileInfo tfi(svgFileName);
if (tfi.isReadable()) {
if (SvgIncluded.isEmpty()) {
setupFileIncluded();
}
}
}
}
App::DocumentObject::onDocumentRestored();
}
void DrawHatch::setupObject()
{
//by this point DH should have a name and belong to a document
setupFileIncluded();
App::DocumentObject::setupObject();
}
void DrawHatch::setupFileIncluded(void)
{
// Base::Console().Message("DH::setupFileIncluded()\n");
App::Document* doc = getDocument();
std::string special = getNameInDocument();
special += "Hatch.fill";
std::string dir = doc->TransientDir.getValue();
std::string svgName = dir + special;
if (SvgIncluded.isEmpty()) {
DrawUtil::copyFile(std::string(), svgName);
SvgIncluded.setValue(svgName.c_str());
}
if (!HatchPattern.isEmpty()) {
std::string exchName = SvgIncluded.getExchangeTempFile();
DrawUtil::copyFile(HatchPattern.getValue(), exchName);
SvgIncluded.setValue(exchName.c_str(), special.c_str());
}
// Base::Console().Message("DH::setupObject()\n");
replaceFileIncluded(HatchPattern.getValue());
}
void DrawHatch::unsetupObject(void)

View File

@@ -43,18 +43,18 @@ class TechDrawExport DrawHatch : public App::DocumentObject
public:
DrawHatch();
~DrawHatch() = default;
~DrawHatch() override = default;
App::PropertyLinkSub Source; // the dvp & face this hatch belongs to
App::PropertyFile HatchPattern;
App::PropertyFileIncluded SvgIncluded;
App::DocumentObjectExecReturn *execute() override;
short mustExecute() const override;
const char* getViewProviderName() const override {
return "TechDrawGui::ViewProviderHatch";
}
void setupObject() override;
void unsetupObject() override;
//return PyObject as DrawHatchPy
@@ -74,9 +74,6 @@ public:
protected:
void onChanged(const App::Property* prop) override;
void onDocumentRestored() override;
void setupObject() override;
void setupFileIncluded();
void replaceFileIncluded(std::string newSvgFile);
private:

View File

@@ -173,7 +173,7 @@ void TaskHatch::apply(bool forceUpdate)
void TaskHatch::createHatch()
{
// Base::Console().Message("TH::createHatch()\n");
Base::Console().Message("TH::createHatch()\n");
App::Document* doc = m_dvp->getDocument();
std::string FeatName = doc->getUniqueObjectName("Hatch");
std::stringstream featLabel;
@@ -208,7 +208,7 @@ void TaskHatch::createHatch()
void TaskHatch::updateHatch()
{
// Base::Console().Message("TH::updateHatch()\n");
Base::Console().Message("TH::updateHatch()\n");
std::string FeatName = m_hatch->getNameInDocument();
Command::openCommand(QT_TRANSLATE_NOOP("Command", "Update Hatch"));

View File

@@ -115,9 +115,17 @@ void ViewProviderGeomHatch::onChanged(const App::Property* p)
Gui::ViewProviderDocumentObject::onChanged(p);
}
//for feature properties - but each letter/digit in property editor triggers this!
//for feature properties
void ViewProviderGeomHatch::updateData(const App::Property* prop)
{
if ( prop == &(getViewObject()->FilePattern) ||
prop == &(getViewObject()->NamePattern) ) {
TechDraw::DrawViewPart* parent = getViewObject()->getSourceView();
if (parent) {
parent->requestPaint();
}
}
Gui::ViewProviderDocumentObject::updateData(prop);
}

View File

@@ -48,19 +48,19 @@ public:
/// constructor
ViewProviderGeomHatch();
/// destructor
virtual ~ViewProviderGeomHatch();
~ViewProviderGeomHatch() override;
App::PropertyFloat WeightPattern;
App::PropertyColor ColorPattern;
virtual void updateData(const App::Property*) override;
virtual void onChanged(const App::Property *prop) override;
virtual bool setEdit(int ModNum) override;
virtual bool doubleClicked(void) override;
virtual bool useNewSelectionModel(void) const override {return false;}
void updateData(const App::Property*) override;
void onChanged(const App::Property *prop) override;
bool setEdit(int ModNum) override;
bool doubleClicked(void) override;
bool useNewSelectionModel(void) const override {return false;}
void updateGraphic(void);
void getParameters(void);
virtual bool canDelete(App::DocumentObject* obj) const override;
bool canDelete(App::DocumentObject* obj) const override;
TechDraw::DrawGeomHatch* getViewObject() const;

View File

@@ -105,6 +105,17 @@ void ViewProviderHatch::onChanged(const App::Property* prop)
}
}
void ViewProviderHatch::updateData(const App::Property* prop)
{
if (prop == &(getViewObject()->HatchPattern)) {
TechDraw::DrawViewPart* parent = getViewObject()->getSourceView();
if (parent) {
parent->requestPaint();
}
}
Gui::ViewProviderDocumentObject::updateData(prop);
}
TechDraw::DrawHatch* ViewProviderHatch::getViewObject() const
{
return dynamic_cast<TechDraw::DrawHatch*>(pcObject);

View File

@@ -51,6 +51,7 @@ public:
bool useNewSelectionModel() const override {return false;}
void onChanged(const App::Property* prop) override;
void updateData(const App::Property*) override;
bool setEdit(int ModNum) override;
bool doubleClicked() override;
bool canDelete(App::DocumentObject* obj) const override;