23
#include "PreCompiled.h"
30
#include <boost/regex.hpp>
33
#include <App/Application.h>
34
#include <Base/Console.h>
35
#include <Base/FileInfo.h>
37
#include "FeatureClip.h"
38
#include "FeaturePage.h"
39
#include "FeatureView.h"
42
using namespace Drawing;
49
PROPERTY_SOURCE(Drawing::FeaturePage, App::DocumentObjectGroup)
51
FeaturePage::FeaturePage(void)
54
static const char* group = "Drawing view";
56
ADD_PROPERTY_TYPE(PageResult,
60
"Resulting SVG document of that page");
61
ADD_PROPERTY_TYPE(Template, (""), group, App::Prop_None, "Template for the page");
62
ADD_PROPERTY_TYPE(EditableTexts,
66
"Substitution values for the editable strings in the template");
69
FeaturePage::~FeaturePage()
72
void FeaturePage::onBeforeChange(const App::Property* prop)
75
numChildren = Group.getSize();
78
App::DocumentObjectGroup::onBeforeChange(prop);
82
void FeaturePage::onChanged(const App::Property* prop)
84
if (prop == &PageResult) {
85
if (this->isRestoring()) {
88
Base::FileInfo fi(PageResult.getValue());
94
else if (prop == &EditableTexts) {
95
if (!this->isRestoring()) {
100
else if (prop == &Template) {
101
if (!this->isRestoring()) {
102
EditableTexts.setValues(getEditableTextsFromTemplate());
105
else if (prop == &Group) {
106
if (Group.getSize() != numChildren) {
107
numChildren = Group.getSize();
112
App::DocumentObjectGroup::onChanged(prop);
115
void FeaturePage::onDocumentRestored()
118
this->StatusBits.set(App::Restore);
120
Base::FileInfo templateInfo(Template.getValue());
121
if (!templateInfo.exists()) {
122
Base::FileInfo fi(Template.getValue());
123
if (fi.fileName().empty()) {
124
fi.setFile(PageResult.getValue());
127
App::Application::getResourceDir() + "Mod/Drawing/Templates/" + fi.fileName();
129
Base::FileInfo tempfi(App::Application::getUserAppDataDir() + "Templates/" + fi.fileName());
130
if (tempfi.exists()) {
131
path = tempfi.filePath();
133
Template.setValue(path);
136
this->StatusBits.reset(App::Restore);
139
App::DocumentObjectExecReturn* FeaturePage::execute(void)
141
std::string temp = Template.getValue();
143
return App::DocumentObject::StdReturn;
146
Base::FileInfo fi(temp);
147
if (!fi.isReadable()) {
149
fi.setFile(App::Application::getResourceDir() + "Mod/Drawing/Templates/" + fi.fileName());
151
if (!fi.isReadable()) {
152
Base::Console().Log("FeaturePage::execute() not able to open %s!\n",
153
Template.getValue());
154
std::string error = std::string("Cannot open file ") + Template.getValue();
155
return new App::DocumentObjectExecReturn(error);
159
if (std::string(PageResult.getValue()).empty()) {
160
PageResult.setValue(fi.filePath().c_str());
165
ifstream file(fi.filePath().c_str());
168
string tempName = PageResult.getExchangeTempFile();
170
string tempendl = "--endOfLine--";
172
while (getline(file, line)) {
174
if (line.find("<!-- DrawingContent -->") == string::npos) {
176
ofile << line << tempendl;
180
const std::vector<App::DocumentObject*>& Grp = Group.getValues();
181
for (std::vector<App::DocumentObject*>::const_iterator It = Grp.begin();
184
if ((*It)->isDerivedFrom<Drawing::FeatureView>()) {
185
Drawing::FeatureView* View = static_cast<Drawing::FeatureView*>(*It);
186
if (View->Visible.getValue()) {
187
ofile << View->ViewResult.getValue();
188
ofile << tempendl << tempendl << tempendl;
191
else if ((*It)->isDerivedFrom<Drawing::FeatureClip>()) {
192
Drawing::FeatureClip* Clip = static_cast<Drawing::FeatureClip*>(*It);
193
if (Clip->Visible.getValue()) {
194
ofile << Clip->ViewResult.getValue();
195
ofile << tempendl << tempendl << tempendl;
198
else if ((*It)->isDerivedFrom<App::DocumentObjectGroup>()) {
200
App::DocumentObjectGroup* SubGroup =
201
static_cast<App::DocumentObjectGroup*>(*It);
202
const std::vector<App::DocumentObject*>& SubGrp = SubGroup->Group.getValues();
203
for (std::vector<App::DocumentObject*>::const_iterator Grit = SubGrp.begin();
204
Grit != SubGrp.end();
206
if ((*Grit)->isDerivedFrom<Drawing::FeatureView>()) {
207
Drawing::FeatureView* SView = static_cast<Drawing::FeatureView*>(*Grit);
208
if (SView->Visible.getValue()) {
209
ofile << SView->ViewResult.getValue();
210
ofile << tempendl << tempendl << tempendl;
222
string outfragment(ofile.str());
223
const std::vector<std::string>& editText = EditableTexts.getValues();
224
if (!editText.empty()) {
225
boost::regex e1("<text.*?freecad:editable=\"(.*?)\".*?<tspan.*?>(.*?)</tspan>");
226
string::const_iterator begin, end;
227
begin = outfragment.begin();
228
end = outfragment.end();
229
boost::match_results<std::string::const_iterator> what;
230
std::size_t count = 0;
231
std::string newfragment;
232
newfragment.reserve(outfragment.size());
234
while (boost::regex_search(begin, end, what, e1)) {
235
if (count < editText.size()) {
237
boost::regex e2("(<text.*?freecad:editable=\"" + what[1].str()
238
+ "\".*?<tspan.*?)>(.*?)(</tspan>)");
239
std::back_insert_iterator<std::string> out(newfragment);
240
boost::regex_replace(out,
244
"$1>" + editText[count] + "$3");
247
begin = what[0].second;
251
newfragment.insert(newfragment.end(), begin, end);
252
outfragment = newfragment;
256
boost::regex e3("--endOfLine--");
258
outfragment = boost::regex_replace(outfragment, e3, fmt);
259
ofstream outfinal(tempName.c_str());
260
outfinal << outfragment;
263
PageResult.setValue(tempName.c_str());
265
return App::DocumentObject::StdReturn;
268
std::vector<std::string> FeaturePage::getEditableTextsFromTemplate(void) const
272
std::vector<string> eds;
274
std::string temp = Template.getValue();
276
Base::FileInfo tfi(temp);
277
if (!tfi.isReadable()) {
279
tfi.setFile(App::Application::getResourceDir() + "Mod/Drawing/Templates/"
282
if (!tfi.isReadable()) {
287
ifstream tfile(tfi.filePath().c_str());
288
while (getline(tfile, tline)) {
290
tfrag += "--endOfLine--";
293
boost::regex e("<text.*?freecad:editable=\"(.*?)\".*?<tspan.*?>(.*?)</tspan>");
294
string::const_iterator tbegin, tend;
295
tbegin = tfrag.begin();
297
boost::match_results<std::string::const_iterator> twhat;
298
while (boost::regex_search(tbegin, tend, twhat, e)) {
299
eds.push_back(twhat[2]);
300
tbegin = twhat[0].second;