File size: 7,966 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 | /***************************************************************************
* Copyright (c) 2015 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 <iomanip>
# include <sstream>
#include <App/Application.h>
#include <App/Document.h>
#include <Base/Console.h>
#include <Base/FileInfo.h>
#include <Base/Parameter.h>
#include "DrawHatch.h"
#include "DrawHatchPy.h" // generated from DrawHatchPy.xml
#include "DrawUtil.h"
#include "DrawViewPart.h"
#include "Preferences.h"
using namespace TechDraw;
using DU = DrawUtil;
PROPERTY_SOURCE(TechDraw::DrawHatch, App::DocumentObject)
DrawHatch::DrawHatch(void)
{
static const char *vgroup = "Hatch";
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,
"Embedded SVG hatch file. System use only."); // n/a to end users
std::string svgFilter("SVG files (*.svg *.SVG);;Bitmap files(*.jpg *.jpeg *.png *.bmp);;All files (*)");
HatchPattern.setFilter(svgFilter);
}
void DrawHatch::onChanged(const App::Property* prop)
{
if (isRestoring()) {
App::DocumentObject::onChanged(prop);
return;
}
if (prop == &HatchPattern) {
replaceFileIncluded(HatchPattern.getValue());
}
App::DocumentObject::onChanged(prop);
}
App::DocumentObjectExecReturn *DrawHatch::execute(void)
{
DrawViewPart* parent = getSourceView();
if (parent) {
parent->requestPaint();
}
return App::DocumentObject::StdReturn;
}
DrawViewPart* DrawHatch::getSourceView(void) const
{
App::DocumentObject* obj = Source.getValue();
DrawViewPart* result = freecad_cast<DrawViewPart*>(obj);
return result;
}
PyObject *DrawHatch::getPyObject(void)
{
if (PythonObject.is(Py::_None())) {
// ref counter is set to 1
PythonObject = Py::Object(new DrawHatchPy(this), true);
}
return Py::new_reference_to(PythonObject);
}
bool DrawHatch::faceIsHatched(int i, std::vector<TechDraw::DrawHatch*> hatchObjs)
{
for (auto& h:hatchObjs) {
const std::vector<std::string> &sourceNames = h->Source.getSubValues();
for (auto& s : sourceNames) {
int fdx = TechDraw::DrawUtil::getIndexFromName(s);
if (fdx == i) {
return true; // Found something
}
}
}
return false; // Found nothing
}
//does this hatch affect face i
bool DrawHatch::affectsFace(int i)
{
const std::vector<std::string> &sourceNames = Source.getSubValues();
for (auto& s : sourceNames) {
int fdx = TechDraw::DrawUtil::getIndexFromName(s);
if (fdx == i) {
return true; // Found something
}
}
return false; // Found nothing
}
//remove a subElement(Face) from Source PropertyLinkSub
bool DrawHatch::removeSub(std::string toRemove)
{
// Base::Console().message("DH::removeSub(%s)\n", toRemove.c_str());
bool removed = false;
const std::vector<std::string> &sourceNames = Source.getSubValues();
std::vector<std::string> newList;
App::DocumentObject* sourceFeat = Source.getValue();
for (auto& s: sourceNames) {
if (s == toRemove) {
removed = true;
} else {
newList.push_back(s);
}
}
if (removed) {
Source.setValue(sourceFeat, newList);
}
return removed;
}
bool DrawHatch::removeSub(int i)
{
// Base::Console().message("DH::removeSub(%d)\n", i);
std::stringstream ss;
ss << "Face" << i;
return removeSub(ss.str());
}
bool DrawHatch::empty(void)
{
const std::vector<std::string> &sourceNames = Source.getSubValues();
return sourceNames.empty();
}
void DrawHatch::replaceFileIncluded(std::string newHatchFileName)
{
// 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 {
throw Base::RuntimeError("Could not read the new svg file");
}
}
void DrawHatch::setupObject()
{
// Base::Console().message("DH::setupObject()\n");
replaceFileIncluded(HatchPattern.getValue());
}
void DrawHatch::unsetupObject(void)
{
// Base::Console().message("DH::unsetupObject() - status: %lu removing: %d \n", getStatus(), isRemoving());
App::DocumentObject* source = Source.getValue();
DrawView* dv = freecad_cast<DrawView*>(source);
if (dv) {
dv->requestPaint();
}
App::DocumentObject::unsetupObject();
}
bool DrawHatch::isSvgHatch(void) const
{
Base::FileInfo fi(HatchPattern.getValue());
return fi.hasExtension("svg");
}
bool DrawHatch::isBitmapHatch(void) const
{
Base::FileInfo fi(HatchPattern.getValue());
return fi.hasExtension({"bmp", "png", "jpg", "jpeg"});
}
//! get a translated label string from the context (ex TaskActiveView), the base name (ex ActiveView) and
//! the unique name within the document (ex ActiveView001), and use it to update the Label property.
void DrawHatch::translateLabel(std::string context, std::string baseName, std::string uniqueName)
{
Label.setValue(DU::translateArbitrary(context, baseName, uniqueName));
}
//standard preference getters
std::string DrawHatch::prefSvgHatch(void)
{
return Preferences::svgFile();
}
Base::Color DrawHatch::prefSvgHatchColor(void)
{
Base::Color fcColor;
fcColor.setPackedValue(Preferences::getPreferenceGroup("Colors")->GetUnsigned("Hatch", 0x00FF0000));
return fcColor;
}
// Python Drawing feature ---------------------------------------------------------
namespace App {
/// @cond DOXERR
PROPERTY_SOURCE_TEMPLATE(TechDraw::DrawHatchPython, TechDraw::DrawHatch)
template<> const char* TechDraw::DrawHatchPython::getViewProviderName(void) const {
return "TechDrawGui::ViewProviderHatch";
}
/// @endcond
// explicit template instantiation
template class TechDrawExport FeaturePythonT<TechDraw::DrawHatch>;
}
|