/
DrawViewSection.cpp
779 lines (683 loc) · 28.1 KB
/
DrawViewSection.cpp
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/***************************************************************************
* Copyright (c) Jürgen Riegel (juergen.riegel@web.de) 2002 *
* Copyright (c) Luke Parry (l.parry@warwick.ac.uk) 2013 *
* Copyright (c) WandererFan (wandererfan@gmail.com) 2016 *
* *
* 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 <sstream>
#include <Bnd_Box.hxx>
#include <BRepBndLib.hxx>
//#include <BRepBuilderAPI_MakePolygon.hxx>
#include <BRepBuilderAPI_Copy.hxx>
#include <BRepAlgoAPI_Cut.hxx>
#include <BRepPrimAPI_MakePrism.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <BRep_Builder.hxx>
#include <BRepTools.hxx>
#include <gp_Ax2.hxx>
#include <gp_Ax3.hxx>
#include <gp_Pnt.hxx>
#include <gp_Pln.hxx>
#include <gp_Dir.hxx>
#include <Geom_Plane.hxx>
#include <HLRBRep_Algo.hxx>
#include <HLRAlgo_Projector.hxx>
#include <HLRBRep_HLRToShape.hxx>
#include <ShapeAnalysis.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Compound.hxx>
#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#endif
#include <chrono>
# include <QFile>
# include <QFileInfo>
#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/Interpreter.h>
#include <Base/Parameter.h>
#include <Mod/Part/App/PartFeature.h>
#include "Geometry.h"
#include "GeometryObject.h"
#include "Cosmetic.h"
#include "HatchLine.h"
#include "EdgeWalker.h"
#include "DrawUtil.h"
#include "DrawProjGroupItem.h"
#include "DrawProjectSplit.h"
#include "DrawGeomHatch.h"
#include "DrawViewSection.h"
using namespace TechDraw;
using namespace std;
const char* DrawViewSection::SectionDirEnums[]= {"Right",
"Left",
"Up",
"Down",
NULL};
//===========================================================================
// DrawViewSection
//===========================================================================
PROPERTY_SOURCE(TechDraw::DrawViewSection, TechDraw::DrawViewPart)
DrawViewSection::DrawViewSection()
{
static const char *sgroup = "Section";
static const char *fgroup = "Cut Surface Format";
ADD_PROPERTY_TYPE(SectionSymbol ,("A"),sgroup,App::Prop_None,"The identifier for this section");
ADD_PROPERTY_TYPE(BaseView ,(0),sgroup,App::Prop_None,"2D View source for this Section");
BaseView.setScope(App::LinkScope::Global);
ADD_PROPERTY_TYPE(SectionNormal ,(0,0,1.0) ,sgroup,App::Prop_None,"Section Plane normal direction"); //direction of extrusion of cutting prism
ADD_PROPERTY_TYPE(SectionOrigin ,(0,0,0) ,sgroup,App::Prop_None,"Section Plane Origin");
SectionDirection.setEnums(SectionDirEnums);
ADD_PROPERTY_TYPE(SectionDirection,((long)0),sgroup, App::Prop_None, "Direction in Base View for this Section");
ADD_PROPERTY_TYPE(FuseBeforeCut ,(false),sgroup,App::Prop_None,"Merge Source(s) into a single shape before cutting");
ADD_PROPERTY_TYPE(FileHatchPattern ,(""),fgroup,App::Prop_None,"The hatch pattern file for the cut surface");
ADD_PROPERTY_TYPE(NameGeomPattern ,(""),fgroup,App::Prop_None,"The pattern name for geometric hatching");
ADD_PROPERTY_TYPE(HatchScale,(1.0),fgroup,App::Prop_None,"Hatch pattern size adjustment");
getParameters();
std::string hatchFilter("Svg files (*.svg *.SVG);;PAT files (*.pat *.PAT);;All files (*)");
FileHatchPattern.setFilter(hatchFilter);
}
DrawViewSection::~DrawViewSection()
{
}
short DrawViewSection::mustExecute() const
{
short result = 0;
if (!isRestoring()) {
result = (Scale.isTouched() ||
Direction.isTouched() ||
BaseView.isTouched() ||
SectionNormal.isTouched() ||
SectionOrigin.isTouched() );
}
if (result) {
return result;
}
return TechDraw::DrawView::mustExecute();
}
void DrawViewSection::onChanged(const App::Property* prop)
{
if (!isRestoring()) {
if (prop == &SectionSymbol) {
std::string lblText = "Section " +
std::string(SectionSymbol.getValue()) +
" - " +
std::string(SectionSymbol.getValue());
Label.setValue(lblText);
}
if (prop == &SectionOrigin) {
App::DocumentObject* base = BaseView.getValue();
TechDraw::DrawView* dv = dynamic_cast<TechDraw::DrawView*>(base);
if (dv != nullptr) {
dv->requestPaint();
}
}
}
if (prop == &FileHatchPattern ||
prop == &NameGeomPattern ) {
std::string fileSpec = FileHatchPattern.getValue();
Base::FileInfo fi(fileSpec);
std::string ext = fi.extension();
if ( (ext == "pat") ||
(ext == "PAT") ) {
if ((!FileHatchPattern.isEmpty()) &&
(!NameGeomPattern.isEmpty())) {
std::vector<PATLineSpec> specs =
DrawGeomHatch::getDecodedSpecsFromFile(FileHatchPattern.getValue(),NameGeomPattern.getValue());
m_lineSets.clear();
for (auto& hl: specs) {
//hl.dump("hl from section");
LineSet ls;
ls.setPATLineSpec(hl);
m_lineSets.push_back(ls);
}
}
}
}
DrawView::onChanged(prop);
}
App::DocumentObjectExecReturn *DrawViewSection::execute(void)
{
// Base::Console().Message("DVS::execute() - %s \n", getNameInDocument());
if (!keepUpdated()) {
return App::DocumentObject::StdReturn;
}
App::DocumentObject* base = BaseView.getValue();
if (base == nullptr) {
return new App::DocumentObjectExecReturn("BaseView object not found");
}
if (!base->getTypeId().isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()))
return new App::DocumentObjectExecReturn("BaseView object is not a DrawViewPart object");
TopoDS_Shape baseShape;
if (FuseBeforeCut.getValue()) {
baseShape = static_cast<TechDraw::DrawViewPart*>(base)->getSourceShapeFused();
} else {
baseShape = static_cast<TechDraw::DrawViewPart*>(base)->getSourceShape();
}
if (baseShape.IsNull()) {
bool isRestoring = getDocument()->testStatus(App::Document::Status::Restoring);
if (isRestoring) {
Base::Console().Warning("DVS::execute - base shape is invalid - (but document is restoring) - %s\n",
getNameInDocument());
} else {
Base::Console().Error("Error: DVS::execute - base shape is Null. - %s\n",
getNameInDocument());
}
return new App::DocumentObjectExecReturn("BaseView Source object is Null");
}
//is SectionOrigin valid?
Bnd_Box centerBox;
BRepBndLib::Add(baseShape, centerBox);
centerBox.SetGap(0.0);
gp_Pln pln = getSectionPlane();
gp_Dir gpNormal = pln.Axis().Direction();
Base::Vector3d orgPnt = SectionOrigin.getValue();
if(!isReallyInBox(gp_Pnt(orgPnt.x,orgPnt.y,orgPnt.z), centerBox)) {
Base::Console().Warning("DVS: SectionOrigin doesn't intersect part in %s\n",getNameInDocument());
Base::Console().Warning("DVS: Using center of bounding box.\n");
double Xmin,Ymin,Zmin,Xmax,Ymax,Zmax;
centerBox.Get(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
orgPnt = Base::Vector3d((Xmax + Xmin)/2.0,
(Ymax + Ymin)/2.0,
(Zmax + Zmin)/2.0);
SectionOrigin.setValue(orgPnt);
}
// Make the extrusion face
double dMax = sqrt(centerBox.SquareExtent());
BRepBuilderAPI_MakeFace mkFace(pln, -dMax,dMax,-dMax,dMax);
TopoDS_Face aProjFace = mkFace.Face();
if(aProjFace.IsNull()) {
return new App::DocumentObjectExecReturn("DrawViewSection - Projected face is NULL");
}
gp_Vec extrudeDir = dMax * gp_Vec(gpNormal);
TopoDS_Shape prism = BRepPrimAPI_MakePrism(aProjFace, extrudeDir, false, true).Shape();
// We need to copy the shape to not modify the BRepstructure
BRepBuilderAPI_Copy BuilderCopy(baseShape);
TopoDS_Shape myShape = BuilderCopy.Shape();
BRepAlgoAPI_Cut mkCut(myShape, prism);
if (!mkCut.IsDone()) {
return new App::DocumentObjectExecReturn("Section cut has failed");
}
TopoDS_Shape rawShape = mkCut.Shape();
// BRepTools::Write(myShape, "DVSCopy.brep"); //debug
// BRepTools::Write(prism, "DVSTool.brep"); //debug
// BRepTools::Write(rawShape, "DVSResult.brep"); //debug
Bnd_Box testBox;
BRepBndLib::Add(rawShape, testBox);
testBox.SetGap(0.0);
if (testBox.IsVoid()) { //prism & input don't intersect. rawShape is garbage, don't bother.
Base::Console().Message("INFO - DVS::execute - prism & input don't intersect\n");
return DrawView::execute();
}
m_cutShape = rawShape;
m_cutShape = TechDraw::moveShape(m_cutShape, //centre on origin
-SectionOrigin.getValue());
gp_Pnt inputCenter;
gp_Ax2 viewAxis;
try {
inputCenter = TechDraw::findCentroid(rawShape,
Direction.getValue());
TopoDS_Shape mirroredShape = TechDraw::mirrorShape(rawShape,
inputCenter,
getScale());
viewAxis = getSectionCS(SectionDirection.getValueAsString());
if (!DrawUtil::fpCompare(Rotation.getValue(),0.0)) {
mirroredShape = TechDraw::rotateShape(mirroredShape,
viewAxis,
Rotation.getValue());
}
geometryObject = buildGeometryObject(mirroredShape,viewAxis);
#if MOD_TECHDRAW_HANDLE_FACES
extractFaces();
#endif //#if MOD_TECHDRAW_HANDLE_FACES
}
catch (Standard_Failure& e1) {
Base::Console().Log("LOG - DVS::execute - base shape failed for %s - %s **\n",getNameInDocument(),e1.GetMessageString());
return new App::DocumentObjectExecReturn(e1.GetMessageString());
}
try {
TopoDS_Compound sectionCompound = findSectionPlaneIntersections(rawShape);
TopoDS_Shape mirroredSection = TechDraw::mirrorShape(sectionCompound,
inputCenter,
getScale());
if (!DrawUtil::fpCompare(Rotation.getValue(),0.0)) {
mirroredSection = TechDraw::rotateShape(mirroredSection,
viewAxis,
Rotation.getValue());
}
sectionFaceWires.clear();
TopoDS_Compound newFaces;
BRep_Builder builder;
builder.MakeCompound(newFaces);
TopExp_Explorer expl(mirroredSection, TopAbs_FACE);
int idb = 0;
for (; expl.More(); expl.Next()) {
const TopoDS_Face& face = TopoDS::Face(expl.Current());
TopoDS_Face pFace = projectFace(face,
inputCenter,
Direction.getValue());
if (!pFace.IsNull()) {
builder.Add(newFaces,pFace);
sectionFaceWires.push_back(ShapeAnalysis::OuterWire(pFace));
}
idb++;
}
sectionFaces = newFaces;
}
catch (Standard_Failure& e2) {
Base::Console().Log("LOG - DVS::execute - failed building section faces for %s - %s **\n",getNameInDocument(),e2.GetMessageString());
return new App::DocumentObjectExecReturn(e2.GetMessageString());
}
addCosmeticVertexesToGeom();
addCosmeticEdgesToGeom();
addCenterLinesToGeom();
return DrawView::execute();
}
gp_Pln DrawViewSection::getSectionPlane() const
{
Base::Vector3d plnPnt = SectionOrigin.getValue();
Base::Vector3d plnNorm = SectionNormal.getValue();
gp_Ax2 viewAxis = getViewAxis(plnPnt,plnNorm,false);
gp_Ax3 viewAxis3(viewAxis);
return gp_Pln(viewAxis3);
}
//! tries to find the intersection of the section plane with the shape giving a collection of planar faces
TopoDS_Compound DrawViewSection::findSectionPlaneIntersections(const TopoDS_Shape& shape)
{
TopoDS_Compound result;
if(shape.IsNull()){
Base::Console().Warning("DrawViewSection::getSectionSurface - Sectional View shape is Empty\n");
return result;
}
gp_Pln plnSection = getSectionPlane();
BRep_Builder builder;
builder.MakeCompound(result);
TopExp_Explorer expFaces(shape, TopAbs_FACE);
int i;
int dbAdded = 0;
for (i = 1 ; expFaces.More(); expFaces.Next(), i++) {
const TopoDS_Face& face = TopoDS::Face(expFaces.Current());
BRepAdaptor_Surface adapt(face);
if (adapt.GetType() == GeomAbs_Plane){
gp_Pln plnFace = adapt.Plane();
if(plnSection.Contains(plnFace.Location(), Precision::Confusion()) &&
plnFace.Axis().IsParallel(plnSection.Axis(), Precision::Angular())) {
dbAdded++;
builder.Add(result, face);
}
}
}
return result;
}
//! get display geometry for Section faces
std::vector<TechDraw::Face*> DrawViewSection::getFaceGeometry()
{
std::vector<TechDraw::Face*> result;
TopoDS_Compound c = sectionFaces;
TopExp_Explorer faces(c, TopAbs_FACE);
for (; faces.More(); faces.Next()) {
TechDraw::Face* f = new TechDraw::Face();
const TopoDS_Face& face = TopoDS::Face(faces.Current());
TopExp_Explorer wires(face, TopAbs_WIRE);
for (; wires.More(); wires.Next()) {
TechDraw::Wire* w = new TechDraw::Wire();
const TopoDS_Wire& wire = TopoDS::Wire(wires.Current());
TopExp_Explorer edges(wire, TopAbs_EDGE);
for (; edges.More(); edges.Next()) {
const TopoDS_Edge& edge = TopoDS::Edge(edges.Current());
//dumpEdge("edge",edgeCount,edge);
TechDraw::BaseGeom* base = TechDraw::BaseGeom::baseFactory(edge);
w->geoms.push_back(base);
}
f->wires.push_back(w);
}
result.push_back(f);
}
return result;
}
//! project a single face using HLR - used for section faces
TopoDS_Face DrawViewSection::projectFace(const TopoDS_Shape &face,
gp_Pnt faceCenter,
const Base::Vector3d &direction)
{
(void) direction;
if(face.IsNull()) {
throw Base::ValueError("DrawViewSection::projectFace - input Face is NULL");
}
Base::Vector3d origin(faceCenter.X(),faceCenter.Y(),faceCenter.Z());
gp_Ax2 viewAxis = getSectionCS(SectionDirection.getValueAsString());
HLRBRep_Algo *brep_hlr = new HLRBRep_Algo();
brep_hlr->Add(face);
HLRAlgo_Projector projector( viewAxis );
brep_hlr->Projector(projector);
brep_hlr->Update();
brep_hlr->Hide();
HLRBRep_HLRToShape hlrToShape(brep_hlr);
TopoDS_Shape hardEdges = hlrToShape.VCompound();
// TopoDS_Shape outEdges = hlrToShape.OutLineVCompound();
std::vector<TopoDS_Edge> faceEdges;
TopExp_Explorer expl(hardEdges, TopAbs_EDGE);
int i;
for (i = 1 ; expl.More(); expl.Next(),i++) {
const TopoDS_Edge& edge = TopoDS::Edge(expl.Current());
if (edge.IsNull()) {
Base::Console().Log("INFO - DVS::projectFace - hard edge: %d is NULL\n",i);
continue;
}
faceEdges.push_back(edge);
}
//TODO: verify that outline edges aren't required
//if edge is both hard & outline, it will be duplicated? are hard edges enough?
// TopExp_Explorer expl2(outEdges, TopAbs_EDGE);
// for (i = 1 ; expl2.More(); expl2.Next(),i++) {
// const TopoDS_Edge& edge = TopoDS::Edge(expl2.Current());
// if (edge.IsNull()) {
// Base::Console().Log("INFO - GO::projectFace - outline edge: %d is NULL\n",i);
// continue;
// }
// bool addEdge = true;
// //is edge already in faceEdges? maybe need to use explorer for this for IsSame to work?
// for (auto& e:faceEdges) {
// if (e.IsPartner(edge)) {
// addEdge = false;
// Base::Console().Message("TRACE - DVS::projectFace - skipping an edge 1\n");
// }
// }
// expl.ReInit();
// for (; expl.More(); expl.Next()){
// const TopoDS_Edge& eHard = TopoDS::Edge(expl.Current());
// if (eHard.IsPartner(edge)) {
// addEdge = false;
// Base::Console().Message("TRACE - DVS::projectFace - skipping an edge 2\n");
// }
// }
// if (addEdge) {
// faceEdges.push_back(edge);
// }
// }
TopoDS_Face projectedFace;
if (faceEdges.empty()) {
Base::Console().Log("LOG - DVS::projectFace - no faceEdges\n");
return projectedFace;
}
//recreate the wires for this single face
EdgeWalker ew;
ew.loadEdges(faceEdges);
bool success = ew.perform();
if (success) {
std::vector<TopoDS_Wire> fw = ew.getResultNoDups();
if (!fw.empty()) {
std::vector<TopoDS_Wire> sortedWires = ew.sortStrip(fw, true);
if (sortedWires.empty()) {
return projectedFace;
}
BRepBuilderAPI_MakeFace mkFace(sortedWires.front(),true); //true => only want planes?
std::vector<TopoDS_Wire>::iterator itWire = ++sortedWires.begin(); //starting with second face
for (; itWire != sortedWires.end(); itWire++) {
mkFace.Add(*itWire);
}
projectedFace = mkFace.Face();
}
} else {
Base::Console().Warning("DVS::projectFace - input is not planar graph. No face detection\n");
}
return projectedFace;
}
//this should really be in BoundBox.h
//!check if point is in box or on boundary of box
//!compare to isInBox which doesn't allow on boundary
bool DrawViewSection::isReallyInBox (const Base::Vector3d v, const Base::BoundBox3d bb) const
{
if (v.x <= bb.MinX || v.x >= bb.MaxX)
return false;
if (v.y <= bb.MinY || v.y >= bb.MaxY)
return false;
if (v.z <= bb.MinZ || v.z >= bb.MaxZ)
return false;
return true;
}
bool DrawViewSection::isReallyInBox (const gp_Pnt p, const Bnd_Box& bb) const
{
return !bb.IsOut(p);
}
void DrawViewSection::setNormalFromBase(const std::string sectionName)
{
// Base::Console().Message("DVS::setNormalFromBase(%s)\n", sectionName.c_str());
Base::Vector3d normal = getSectionVector(sectionName);
Direction.setValue(normal);
SectionNormal.setValue(normal);
}
//! calculate the section Normal/Projection Direction given section name
//TODO: this should take base view rotation into account.
Base::Vector3d DrawViewSection::getSectionVector (const std::string sectionName)
{
// Base::Console().Message("DVS::getSectionVector(%s) - %s\n", sectionName.c_str(), getNameInDocument());
Base::Vector3d result;
Base::Vector3d stdX(1.0,0.0,0.0);
Base::Vector3d stdY(0.0,1.0,0.0);
Base::Vector3d stdZ(0.0,0.0,1.0);
Base::Vector3d view = getBaseDVP()->Direction.getValue();
view.Normalize();
Base::Vector3d left = view.Cross(stdZ);
left.Normalize();
Base::Vector3d up = view.Cross(left);
up.Normalize();
double dot = view.Dot(stdZ);
if (sectionName == "Up") {
result = up;
if (DrawUtil::fpCompare(dot,1.0)) { //view = stdZ
result = (-1.0 * stdY);
} else if (DrawUtil::fpCompare(dot,-1.0)) { //view = -stdZ
result = stdY;
}
} else if (sectionName == "Down") {
result = up * -1.0;
if (DrawUtil::fpCompare(dot,1.0)) { //view = stdZ
result = stdY;
} else if (DrawUtil::fpCompare(dot, -1.0)) { //view = -stdZ
result = (-1.0 * stdY);
}
} else if (sectionName == "Left") {
result = left * -1.0;
if (DrawUtil::fpCompare(fabs(dot),1.0)) { //view = +/- stdZ
result = stdX;
}
} else if (sectionName == "Right") {
result = left;
if (DrawUtil::fpCompare(fabs(dot),1.0)) {
result = -1.0 * stdX;
}
} else {
Base::Console().Log("Error - DVS::getSectionVector - bad sectionName: %s\n",sectionName.c_str());
result = stdZ;
}
return result;
}
//returns current section cs
gp_Ax2 DrawViewSection::getSectionCS(void)
{
std::string dirName = SectionDirection.getValueAsString();
return getSectionCS(dirName);
}
//! calculate the section Projection CS given section direction name
gp_Ax2 DrawViewSection::getSectionCS (const std::string dirName)
{
Base::Vector3d view = getBaseDVP()->Direction.getValue();
view.Normalize();
Base::Vector3d sectionOrg = SectionOrigin.getValue();
gp_Ax2 baseCS = getBaseDVP()->getViewAxis(sectionOrg,
view);
// cardinal: 0 - left, 1 - right, 2 - up, 3 - down
int cardinal = 0;
if (dirName == "Up") {
cardinal = 2;
} else if (dirName == "Down") {
cardinal = 3;
} else if (dirName == "Left") {
cardinal = 0;
} else if (dirName == "Right") {
cardinal = 1;
}
gp_Ax2 newCS = rotateCSCardinal(baseCS, cardinal);
return newCS;
}
//TODO: this should be able to handle arbitrary rotation of the CS
//TODO: this is useful beyond DVS. move to GO or DU or ???
// or at least static in DVS. doesn't depend on DVS object
gp_Ax2 DrawViewSection::rotateCSCardinal(gp_Ax2 oldCS, int cardinal)
{
// cardinal: 0 - left, 1 - right, 2 - up, 3 - down
// as in DVS::SectionDirection
gp_Pnt oldOrg = oldCS.Location();
gp_Dir oldMain = oldCS.Direction();
gp_Dir oldX = oldCS.XDirection();
gp_Ax1 rotAxis;
gp_Dir crossed;
gp_Ax2 newCS;
double angle;
//Note: cardinal refers to the the motion of the viewer's head. When the head turns to
// the left, the "Direction" moves to the right! When the viewer's head moves to look up,
// the "Direction" moves down!
switch (cardinal) {
case 0: //right
crossed = oldMain.Crossed(oldX);
rotAxis = gp_Ax1(oldOrg, crossed);
angle = 90.0 * M_PI / 180.0;
newCS = oldCS.Rotated(rotAxis, angle);
break;
case 1: //left
crossed = oldMain.Crossed(oldX);
rotAxis = gp_Ax1(oldOrg, crossed);
angle = -90.0 * M_PI / 180.0;
newCS = oldCS.Rotated(rotAxis, angle);
break;
case 2: // head looks up, so Direction(projection normal) points down
rotAxis = gp_Ax1(oldOrg, oldX);
angle = -90.0 * M_PI / 180.0;
newCS = oldCS.Rotated(rotAxis, angle);
break;
case 3: //down
rotAxis = gp_Ax1(oldOrg, oldX); //one of these should be -oldX?
angle = 90.0 * M_PI / 180.0;
newCS = oldCS.Rotated(rotAxis, angle);
break;
default:
newCS = oldCS;
break;
}
return newCS;
}
gp_Ax2 DrawViewSection::rotateCSArbitrary(gp_Ax2 oldCS,
Base::Vector3d axis,
double degAngle)
{
gp_Ax2 newCS;
gp_Pnt oldOrg = oldCS.Location();
gp_Dir gAxis(axis.x, axis.y, axis.z);
gp_Ax1 rotAxis = gp_Ax1(oldOrg, gAxis);
double radAngle = degAngle * M_PI / 180.0;
newCS = oldCS.Rotated(rotAxis, radAngle);
return newCS;
}
std::vector<LineSet> DrawViewSection::getDrawableLines(int i)
{
std::vector<LineSet> result;
result = DrawGeomHatch::getTrimmedLines(this,m_lineSets,i,HatchScale.getValue());
return result;
}
std::vector<TopoDS_Wire> DrawViewSection::getWireForFace(int idx) const
{
std::vector<TopoDS_Wire> result;
result.push_back(sectionFaceWires.at(idx));
return result;
}
void DrawViewSection::unsetupObject()
{
TechDraw::DrawViewPart* base = getBaseDVP();
if (base != nullptr) {
base->touch();
}
DrawViewPart::unsetupObject();
}
TechDraw::DrawViewPart* DrawViewSection::getBaseDVP()
{
TechDraw::DrawViewPart* baseDVP = nullptr;
App::DocumentObject* base = BaseView.getValue();
if (base != nullptr) {
if (base->getTypeId().isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId())) {
baseDVP = static_cast<TechDraw::DrawViewPart*>(base);
}
}
return baseDVP;
}
TechDraw::DrawProjGroupItem* DrawViewSection::getBaseDPGI()
{
TechDraw::DrawProjGroupItem* baseDPGI = nullptr;
App::DocumentObject* base = BaseView.getValue();
if (base != nullptr) {
if (base->getTypeId().isDerivedFrom(TechDraw::DrawProjGroupItem::getClassTypeId())) {
baseDPGI = static_cast<TechDraw::DrawProjGroupItem*>(base);
}
}
return baseDPGI;
}
void DrawViewSection::getParameters()
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw");
std::string defaultDir = App::Application::getResourceDir() + "Mod/Drawing/patterns/";
std::string defaultFileName = defaultDir + "simple.svg";
QString patternFileName = QString::fromStdString(hGrp->GetASCII("FileHatch",defaultFileName.c_str()));
if (patternFileName.isEmpty()) {
patternFileName = QString::fromStdString(defaultFileName);
}
QFileInfo tfi(patternFileName);
if (tfi.isReadable()) {
FileHatchPattern.setValue(patternFileName.toUtf8().constData());
}
std::string patternName = hGrp->GetASCII("PatternName","Diamond");
NameGeomPattern.setValue(patternName);
hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/General");
bool fuseFirst = hGrp->GetBool("SectionFuseFirst",true);
FuseBeforeCut.setValue(fuseFirst);
}
// Python Drawing feature ---------------------------------------------------------
namespace App {
/// @cond DOXERR
PROPERTY_SOURCE_TEMPLATE(TechDraw::DrawViewSectionPython, TechDraw::DrawViewSection)
template<> const char* TechDraw::DrawViewSectionPython::getViewProviderName(void) const {
return "TechDrawGui::ViewProviderDrawingView";
}
/// @endcond
// explicit template instantiation
template class TechDrawExport FeaturePythonT<TechDraw::DrawViewSection>;
}