(Crash) when creating projection views; needs addView() first

Discussions about the development of the TechDraw workbench
vocx
Posts: 618
Joined: Thu Oct 18, 2018 9:18 pm

(Crash) when creating projection views; needs addView() first

Postby vocx » Sun May 12, 2019 1:13 am

As per this thread, Generate 2d view images from 3d model?, I was thinking of a way of programmatically generating the 2D projections of a solid body, so I turned to TechDraw NewProjGroup.

In the wiki page there was this code

Code: Select all

group = FreeCAD.ActiveDocument.addObject('TechDraw::DrawProjGroup','pGroup')
group.Source = [fusion]
group.ScaleType = "Document"
anchorView = group.addProjection("Front")
group.Anchor = anchorView

leftView = group.addProjection("Left")
topView = group.addProjection("Top")
page.addView(group)
This creates a projection group, adds front, left, and top views, and finally adds the view to the page. That code cannot be tested immediately as it lacks the creation of the "fusion" and of the "page"; therefore I added more code to make a complete example.

However, there seems to be a crash when the addProjection() is called before addView() is called.

This code works, but behind the front view a padlock icon appears.

Code: Select all

FreeCAD.newDocument()
Cube = FreeCAD.ActiveDocument.addObject("Part::Box", "Box")

Page = FreeCAD.ActiveDocument.addObject('TechDraw::DrawPage', 'Page')
Template = FreeCAD.ActiveDocument.addObject('TechDraw::DrawSVGTemplate', 'Template')
Template.Template = '/usr/share/freecad/Mod/TechDraw/Templates/A4_LandscapeTD.svg'
Page.Template = Template
FreeCAD.ActiveDocument.recompute()

proj_group = FreeCAD.ActiveDocument.addObject('TechDraw::DrawProjGroup', 'ProjectionGroup')
proj_group.Source = [Cube]
Page.addView(proj_group)
FreeCAD.ActiveDocument.recompute()

front_view = proj_group.addProjection("Front")
left_view = proj_group.addProjection("Left")
top_view = proj_group.addProjection("Top")
FreeCAD.ActiveDocument.recompute()
If the addView() is placed at the end the entire program crashes. The first addProjection("Front") works, but when a second addProjection() is added, the program crashes. Namely, the call to addView() must be made before the second addProjection() is made, or the program will crash.

Code: Select all

FreeCAD.newDocument()
Cube = FreeCAD.ActiveDocument.addObject("Part::Box", "Box")

Page = FreeCAD.ActiveDocument.addObject('TechDraw::DrawPage', 'Page')
Template = FreeCAD.ActiveDocument.addObject('TechDraw::DrawSVGTemplate', 'Template')
Template.Template = '/usr/share/freecad/Mod/TechDraw/Templates/A4_LandscapeTD.svg'
Page.Template = Template
FreeCAD.ActiveDocument.recompute()

proj_group = FreeCAD.ActiveDocument.addObject('TechDraw::DrawProjGroup', 'ProjectionGroup')
proj_group.Source = [Cube]
FreeCAD.ActiveDocument.recompute()

front_view = proj_group.addProjection("Front")
left_view = proj_group.addProjection("Left")
top_view = proj_group.addProjection("Top")
Page.addView(proj_group)
FreeCAD.ActiveDocument.recompute()
The entire trace is this.

Code: Select all

Program received signal SIGSEGV, Segmentation fault.
#0  /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20) [0x7f25a0798f20]
#1  0x7f25a2d21272 in App::Enumeration::isValid() const from /usr/lib/freecad-daily-python3/lib/libFreeCADApp.so+0x2
#2  0x7f25a2cd1ce2 in App::PropertyEnumeration::getValueAsString() const from /usr/lib/freecad-daily-python3/lib/libFreeCADApp.so+0x12
#3  0x7f25047a493b in TechDraw::DrawProjGroup::arrangeViewPointers(TechDraw::DrawProjGroupItem**) const from /usr/lib/freecad-daily-python3/lib/TechDraw.so+0x6b
#4  0x7f25047a5609 in TechDraw::DrawProjGroup::getXYPosition(char const*) from /usr/lib/freecad-daily-python3/lib/TechDraw.so+0x99
#5  0x7f25047aa5ff in TechDraw::DrawProjGroupItem::autoPosition() from /usr/lib/freecad-daily-python3/lib/TechDraw.so+0x9f
#6  0x7f25047aac30 in TechDraw::DrawProjGroupItem::execute() from /usr/lib/freecad-daily-python3/lib/TechDraw.so+0xe0
#7  0x7f25a2c15209 in App::DocumentObject::recompute() from /usr/lib/freecad-daily-python3/lib/libFreeCADApp.so+0x99
#8  0x7f25a2bcf004 in App::Document::_recomputeFeature(App::DocumentObject*) from /usr/lib/freecad-daily-python3/lib/libFreeCADApp.so+0xb4
#9  0x7f25a2bd6ac0 in App::Document::recomputeFeature(App::DocumentObject*) from /usr/lib/freecad-daily-python3/lib/libFreeCADApp.so+0x80
#10  0x7f25a2c15659 in App::DocumentObject::recomputeFeature() from /usr/lib/freecad-daily-python3/lib/libFreeCADApp.so+0x19
#11  0x7f25047a87df in TechDraw::DrawProjGroup::addProjection(char const*) from /usr/lib/freecad-daily-python3/lib/TechDraw.so+0x1cf
#12  0x7f25047f1977 in TechDraw::DrawProjGroupPy::addProjection(_object*) from /usr/lib/freecad-daily-python3/lib/TechDraw.so+0x47
#13  0x7f25047f1a67 in TechDraw::DrawProjGroupPy::staticCallback_addProjection(_object*, _object*) from /usr/lib/freecad-daily-python3/lib/TechDraw.so+0x37
#14  /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0(_PyCFunction_FastCallDict+0x1bb) [0x7f25a22919eb]
#15  /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0(+0x17be0c) [0x7f25a21f9e0c]
#16  /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0(_PyEval_EvalFrameDefault+0x4ec2) [0x7f25a2200092]
#17  /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0(+0x17b63f) [0x7f25a21f963f]
#18  /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0(PyEval_EvalCodeEx+0x3e) [0x7f25a21fa0fe]
#19  /usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0(PyEval_EvalCode+0x1b) [0x7f25a21fafdb]
#20  0x7f25a34e1a61 in Gui::InteractiveInterpreter::runCode(PyCodeObject*) const from /usr/lib/freecad-daily-python3/lib/libFreeCADGui.so+0x61
#21  0x7f25a34e1fdb in Gui::InteractiveInterpreter::runSource(char const*) const from /usr/lib/freecad-daily-python3/lib/libFreeCADGui.so+0x9b
#22  0x7f25a34e30dc in Gui::InteractiveInterpreter::push(char const*) from /usr/lib/freecad-daily-python3/lib/libFreeCADGui.so+0xbc
#23  0x7f25a34e38e9 in Gui::PythonConsole::runSource(QString const&) from /usr/lib/freecad-daily-python3/lib/libFreeCADGui.so+0xe9
#24  0x7f25a34e5c70 in Gui::PythonConsole::keyPressEvent(QKeyEvent*) from /usr/lib/freecad-daily-python3/lib/libFreeCADGui.so+0x640
#25  0x7f25a19ca877 in QWidget::event(QEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5+0xa27
#26  0x7f25a1a6b4de in QFrame::event(QEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5+0x1e
#27  0x7f25a1a74623 in QAbstractScrollArea::event(QEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5+0x383
#28  0x7f25a1b494f5 in QPlainTextEdit::event(QEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5+0xc5
#29  0x7f25a198b83c in QApplicationPrivate::notify_helper(QObject*, QEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5+0x9c
#30  0x7f25a1994642 in QApplication::notify(QObject*, QEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5+0x17e2
#31  0x7f25a338d668 in Gui::GUIApplication::notify(QObject*, QEvent*) from /usr/lib/freecad-daily-python3/lib/libFreeCADGui.so+0x88
#32  0x7f25a13769e8 in QCoreApplication::notifyInternal2(QObject*, QEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0x118
#33  /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5(+0x1b07c5) [0x7f25a19e77c5]
#34  0x7f25a198b83c in QApplicationPrivate::notify_helper(QObject*, QEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5+0x9c
#35  0x7f25a1993104 in QApplication::notify(QObject*, QEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5+0x2a4
#36  0x7f25a338d668 in Gui::GUIApplication::notify(QObject*, QEvent*) from /usr/lib/freecad-daily-python3/lib/libFreeCADGui.so+0x88
#37  0x7f25a13769e8 in QCoreApplication::notifyInternal2(QObject*, QEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0x118
#38  0x7f259d4a20f0 in QGuiApplicationPrivate::processKeyEvent(QWindowSystemInterfacePrivate::KeyEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5+0x180
#39  0x7f259d4a7035 in QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5+0x115
#40  0x7f259d47e2eb in QWindowSystemInterface::sendWindowSystemEvents(QFlags<QEventLoop::ProcessEventsFlag>) from /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5+0xab
#41  /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5(+0x9d1c0) [0x7f25910451c0]
#42  /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0(g_main_context_dispatch+0x2e7) [0x7f259b7fb417]
#43  /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0(+0x4c650) [0x7f259b7fb650]
#44  /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0(g_main_context_iteration+0x2c) [0x7f259b7fb6dc]
#45  0x7f25a13cf99f in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0x5f
#46  0x7f25a1374a1a in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0x13a
#47  0x7f25a137dac4 in QCoreApplication::exec() from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0x94
#48  0x7f25a331ca7f in Gui::Application::runApplication() from /usr/lib/freecad-daily-python3/lib/libFreeCADGui.so+0x148f
#49  freecad-daily(main+0x6a2) [0x559764e095f2]
#50  /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7) [0x7f25a077bb97]
#51  freecad-daily(_start+0x2a) [0x559764e0a2ba]
Program received signal SIGSEGV, Segmentation fault.
#0  /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20) [0x7f25a0798f20]
#0  /lib/x86_64-linux-gnu/libc.so.6(+0x3ef20) [0x7f25a0798f20]
#1  0x7f25a1372da1 in QAbstractEventDispatcherPrivate::allocateTimerId() from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0x41
#2  0x7f25a1373419 in QAbstractEventDispatcher::registerTimer(int, Qt::TimerType, QObject*) from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0x19
#3  0x7f25a13a717e in QObject::startTimer(int, Qt::TimerType) from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0x6e
#4  0x7f25a13b23ec in QTimer::start() from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0x3c
#5  0x7f259ebfd242 in QNetworkConfigurationManagerPrivate::startPolling() from /usr/lib/x86_64-linux-gnu/libQt5Network.so.5+0x172
#6  0x7f259ebfd84d in QNetworkConfigurationManagerPrivate::updateConfigurations() from /usr/lib/x86_64-linux-gnu/libQt5Network.so.5+0x5ed
#7  /usr/lib/x86_64-linux-gnu/libQt5Network.so.5(+0x13860d) [0x7f259ec7d60d]
#8  0x7f25a13a61d2 in QObject::event(QEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0xe2
#9  0x7f25a13769d1 in QCoreApplication::notifyInternal2(QObject*, QEvent*) from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0x101
#10  0x7f25a137915d in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0x1ed
#11  /usr/lib/x86_64-linux-gnu/libQt5Core.so.5(+0x2e4373) [0x7f25a13d0373]
#12  /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0(g_main_context_dispatch+0x2e7) [0x7f259b7fb417]
#13  /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0(+0x4c650) [0x7f259b7fb650]
#14  /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0(g_main_context_iteration+0x2c) [0x7f259b7fb6dc]
#15  0x7f25a13cf99f in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0x5f
#16  0x7f25a1374a1a in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5+0x13a
This is with the daily build

Code: Select all

OS: Ubuntu 18.04.2 LTS (ubuntu:GNOME/ubuntu)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.
Build type: Release
Python version: 3.6.7
Qt version: 5.9.5
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedStates (en_US)
wandererfan
Posts: 2569
Joined: Tue Nov 06, 2012 5:42 pm

Re: (Crash) when creating projection views; needs addView() first

Postby wandererfan » Sun May 12, 2019 11:56 am

vocx wrote:
Sun May 12, 2019 1:13 am
As per this thread, Generate 2d view images from 3d model?, I was thinking of a way of programmatically generating the 2D projections of a solid body, so I turned to TechDraw NewProjGroup.
I'll replace the wiki example with code from "source-dir/src/Mod/TechDraw/TDTest/DProjGroupTest.py". That runs during every PR build so it should be pretty reliable.
This code works, but behind the front view a padlock icon appears.
The padlock is ok. In this case it indicates that the Front View is locked to the ProjectionGroup. If you were to unlock the Front View you could move it relative to the other views. The other Views would then try to position themselves relative to Front and chaos would ensue.

LockPosition is ReadOnly for Front. Maybe I should hide the lock icon and the property for Front Views.
wandererfan
Posts: 2569
Joined: Tue Nov 06, 2012 5:42 pm

Re: (Crash) when creating projection views; needs addView() first

Postby wandererfan » Sun May 12, 2019 12:06 pm

vocx wrote:
Sun May 12, 2019 1:13 am
As per this thread, Generate 2d view images from 3d model?
In that thread, the OP is looking for a "headless" solution. Most of TechDraw relies on FreeCADGui/QtGui and the output options when running in App only mode are limited.

There are some useful routines in TechDraw_API.
vocx
Posts: 618
Joined: Thu Oct 18, 2018 9:18 pm

Re: (Crash) when creating projection views; needs addView() first

Postby vocx » Sun May 12, 2019 4:02 pm

wandererfan wrote:
Sun May 12, 2019 12:06 pm
In that thread, the OP is looking for a "headless" solution. Most of TechDraw relies on FreeCADGui/QtGui and the output options when running in App only mode are limited.
Correct. As far as I can tell, all the functions from my code example work in a headless case, as they don't make use of the View properties (QtGui). The user must export with DXF instead of using SVG or PDF, though.
vocx
Posts: 618
Joined: Thu Oct 18, 2018 9:18 pm

Re: (Crash) when creating projection views; needs addView() first

Postby vocx » Sun May 12, 2019 4:11 pm

wandererfan wrote:
Sun May 12, 2019 11:56 am
I'll replace the wiki example with code from "source-dir/src/Mod/TechDraw/TDTest/DProjGroupTest.py". That runs during every PR build so it should be pretty reliable.
But what about the crash? Is it not better to handle this case better? Because what causes the crash seems to be the order of the operations; I don't know, trying to access some pointer that hasn't been created first? Or do you suggest just documenting this better in the scripting section: "first call addView() before adding another projection!"
LockPosition is ReadOnly for Front. Maybe I should hide the lock icon and the property for Front Views.
Yeah, it just looks ugly aesthetically speaking, but doesn't seem to cause any other issue. I just wonder, if the user doesn't have control of the lock property then it shouldn't be shown.
wandererfan
Posts: 2569
Joined: Tue Nov 06, 2012 5:42 pm

Re: (Crash) when creating projection views; needs addView() first

Postby wandererfan » Sun May 12, 2019 4:42 pm

vocx wrote:
Sun May 12, 2019 4:11 pm
But what about the crash? Is it not better to handle this case better? Because what causes the crash seems to be the order of the operations;
That's on the list too, but that takes longer than updating the wiki with a better code example.
vocx
Posts: 618
Joined: Thu Oct 18, 2018 9:18 pm

Re: (Crash) when creating projection views; needs addView() first

Postby vocx » Sun May 12, 2019 4:56 pm

wandererfan wrote:
Sun May 12, 2019 4:42 pm
That's on the list too, but that takes longer than updating the wiki with a better code example.
Okay, I just wanted to make sure the priority is the crash, not the code example. By the way, I will modify the code again, because as I said, in the documentation I want to have a fully workable, standalone example.
wandererfan
Posts: 2569
Joined: Tue Nov 06, 2012 5:42 pm

Re: (Crash) when creating projection views; needs addView() first

Postby wandererfan » Mon May 13, 2019 4:37 pm

Should be fixed by git commit 75d43757c4.
vocx
Posts: 618
Joined: Thu Oct 18, 2018 9:18 pm

Re: (Crash) when creating projection views; needs addView() first

Postby vocx » Wed May 15, 2019 5:21 pm

wandererfan wrote:
Mon May 13, 2019 4:37 pm
Should be fixed by git commit 75d43757c4.
Okay, the program no longer crashes.

I use this to test.

Code: Select all

FreeCAD.newDocument()
Cube = FreeCAD.ActiveDocument.addObject("Part::Box", "Box")

Page = FreeCAD.ActiveDocument.addObject('TechDraw::DrawPage', 'Page')
Template = FreeCAD.ActiveDocument.addObject('TechDraw::DrawSVGTemplate', 'Template')
Template.Template = '/usr/share/freecad/Mod/TechDraw/Templates/A4_LandscapeTD.svg'
Page.Template = Template
FreeCAD.ActiveDocument.recompute()

proj_group = FreeCAD.ActiveDocument.addObject('TechDraw::DrawProjGroup', 'ProjectionGroup')
proj_group.Source = [Cube]
FreeCAD.ActiveDocument.recompute()
The crash would happen after the second addProjection()

Code: Select all

front_view = proj_group.addProjection("Front")
left_view = proj_group.addProjection("Left")
top_view = proj_group.addProjection("Top")
Page.addView(proj_group)
FreeCAD.ActiveDocument.recompute()
This is what is shown now

Code: Select all

DPG:addProjection - ProjectionGroup - DPG is not on a page!
DPG:addProjection - ProjectionGroup - DPG is not on a page!
DPG:arrangeViewPointers - ProjectionGroup - DPG is not on a page!
DPG:arrangeViewPointers - using system default Projection Type
DPG: ProjectionGroup - can not find parent page. Using default Projection Type. (1)
DPG:addProjection - ProjectionGroup - DPG is not on a page!
DPG:arrangeViewPointers - ProjectionGroup - DPG is not on a page!
DPG:arrangeViewPointers - using system default Projection Type
DPG: ProjectionGroup - can not find parent page. Using default Projection Type. (1)
Is this a problem?

The projection group objects appear all in the same spot. They don't automatically distribute around the front view, even when AutoDistribute is true.
TechDraw_projections_on_same_spot.png
TechDraw_projections_on_same_spot.png (3.07 KiB) Viewed 73 times
In order to adjust the position, give any value to the X or Y coordinates of the views; then they will automatically arrange themselves.

Code: Select all

left_view.X = 1
top_view.X = 1
FreeCAD.ActiveDocument.recompute()
wandererfan
Posts: 2569
Joined: Tue Nov 06, 2012 5:42 pm

Re: (Crash) when creating projection views; needs addView() first

Postby wandererfan » Thu May 16, 2019 12:12 am

vocx wrote:
Wed May 15, 2019 5:21 pm

Code: Select all

front_view = proj_group.addProjection("Front")
left_view = proj_group.addProjection("Left")
top_view = proj_group.addProjection("Top")
Page.addView(proj_group)  #<<<<<<<<<<<<
FreeCAD.ActiveDocument.recompute()
You need to add the ProjectionGroup to the Page before adding the individual Views. That's where the messages like "DPG:addProjection - ProjectionGroup - DPG is not on a page!" come from.